blob: c88ddc7b09494a6421a99c220736310532606918 [file] [log] [blame]
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 Alexandre Bourget
11 *
12 * Plugin to transform a Rockbox produced m3u playlist into something
13 * understandable by the picky original iRiver firmware.
14 *
15 * Based on sort.c by the Rockbox team.
16 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000017 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version 2
20 * of the License, or (at your option) any later version.
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +000021 *
22 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23 * KIND, either express or implied.
24 *
25 ****************************************************************************/
26#include "plugin.h"
27
Jens Arnolda36b1d42006-01-15 18:20:18 +000028PLUGIN_HEADER
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +000029
Steve Bavin65265772008-05-13 09:57:56 +000030static const struct plugin_api* rb;
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +000031
Michael Sevakis26d242a2007-04-21 18:38:25 +000032ssize_t buf_size;
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +000033static char *filename;
34static int readsize;
35static char *stringbuffer;
36static char crlf[2] = "\r\n";
37
38int read_buffer(int offset)
39{
40 int fd;
41
42 fd = rb->open(filename, O_RDONLY);
43 if(fd < 0)
44 return 10 * fd - 1;
45
46 /* Fill the buffer from the file */
47 rb->lseek(fd, offset, SEEK_SET);
48 readsize = rb->read(fd, stringbuffer, buf_size);
49 rb->close(fd);
50
51 if(readsize < 0)
52 return readsize * 10 - 2;
53
54 if(readsize == buf_size)
55 return buf_size; /* File too big */
56
57 return 0;
58}
59
60static int write_file(void)
61{
62 char tmpfilename[MAX_PATH+1];
63 int fd;
64 int rc;
65 char *buf_ptr;
66 char *str_begin;
67
68 /* Create a temporary file */
69
70 rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename);
71
Jens Arnold67eb1542007-02-01 23:08:15 +000072 fd = rb->creat(tmpfilename);
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +000073 if(fd < 0)
74 return 10 * fd - 1;
75
76 /* Let's make sure it always writes CR/LF and not only LF */
77 buf_ptr = stringbuffer;
78 str_begin = stringbuffer;
79 do {
80 /* Transform slashes into backslashes */
81 if(*buf_ptr == '/')
82 *buf_ptr = '\\';
83
84 if((*buf_ptr == '\r') || (*buf_ptr == '\n')) {
85 /* We have no complete string ? It's only a leading \n or \r ? */
86 if (!str_begin)
87 continue;
88
89 /* Terminate string */
90 *buf_ptr = 0;
91
92 /* Write our new string */
93 rc = rb->write(fd, str_begin, rb->strlen(str_begin));
94 if(rc < 0) {
95 rb->close(fd);
96 return 10 * rc - 2;
97 }
98 /* Write CR/LF */
99 rc = rb->write(fd, crlf, 2);
100 if(rc < 0) {
101 rb->close(fd);
102 return 10 * rc - 3;
103 }
104
105 /* Reset until we get a new line */
106 str_begin = NULL;
107
108 }
109 else {
110 /* We start a new line here */
111 if (!str_begin)
112 str_begin = buf_ptr;
113 }
114
Linus Nielsen Feltzing38413482005-05-27 06:54:16 +0000115 /* Next char, until ... */
116 } while(buf_ptr++ < stringbuffer + readsize);
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000117
118 rb->close(fd);
119
120 /* Remove the original file */
121 rc = rb->remove(filename);
122 if(rc < 0) {
123 return 10 * rc - 4;
124 }
125
126 /* Replace the old file with the new */
127 rc = rb->rename(tmpfilename, filename);
128 if(rc < 0) {
129 return 10 * rc - 5;
130 }
131
132 return 0;
133}
134
Steve Bavin65265772008-05-13 09:57:56 +0000135enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000136{
137 char *buf;
138 int rc;
Kevin Ferrareac017782007-07-25 09:38:55 +0000139 int i;
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000140 filename = (char *)parameter;
141
142 rb = api;
143
Michael Sevakis8676dc22007-04-21 19:07:15 +0000144 buf = rb->plugin_get_audio_buffer((size_t *)&buf_size); /* start munching memory */
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000145
146 stringbuffer = buf;
147
Kevin Ferrareac017782007-07-25 09:38:55 +0000148 FOR_NB_SCREENS(i)
149 rb->screens[i]->clear_display();
Jens Arnold4d6374c2007-03-16 21:56:08 +0000150 rb->splash(0, "Converting...");
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000151
152 rc = read_buffer(0);
Kevin Ferrareac017782007-07-25 09:38:55 +0000153 FOR_NB_SCREENS(i)
154 rb->screens[i]->clear_display();
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000155 if(rc == 0) {
Jens Arnold4d6374c2007-03-16 21:56:08 +0000156 rb->splash(0, "Writing...");
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000157 rc = write_file();
158
Kevin Ferrareac017782007-07-25 09:38:55 +0000159 FOR_NB_SCREENS(i)
160 rb->screens[i]->clear_display();
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000161 if(rc < 0) {
Jens Arnold4d6374c2007-03-16 21:56:08 +0000162 rb->splash(HZ, "Can't write file: %d", rc);
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000163 } else {
Jens Arnold4d6374c2007-03-16 21:56:08 +0000164 rb->splash(HZ, "Done");
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000165 }
166 } else {
167 if(rc < 0) {
Jens Arnold4d6374c2007-03-16 21:56:08 +0000168 rb->splash(HZ, "Can't read file: %d", rc);
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000169 } else {
Jens Arnold4d6374c2007-03-16 21:56:08 +0000170 rb->splash(HZ, "The file is too big");
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000171 }
172 }
Kevin Ferrareac017782007-07-25 09:38:55 +0000173
Linus Nielsen Feltzing2fa2bf62005-04-07 12:17:14 +0000174 return PLUGIN_OK;
175}