1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Alexandre Bourget
12 * Plugin to transform a Rockbox produced m3u playlist into something
13 * understandable by the picky original iRiver firmware.
15 * Based on sort.c by the Rockbox team.
17 * 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.
22 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23 * KIND, either express or implied.
25 ****************************************************************************/
30 static const struct plugin_api
* rb
;
33 static char *filename
;
35 static char *stringbuffer
;
36 static char crlf
[2] = "\r\n";
38 int read_buffer(int offset
)
42 fd
= rb
->open(filename
, O_RDONLY
);
46 /* Fill the buffer from the file */
47 rb
->lseek(fd
, offset
, SEEK_SET
);
48 readsize
= rb
->read(fd
, stringbuffer
, buf_size
);
52 return readsize
* 10 - 2;
54 if(readsize
== buf_size
)
55 return buf_size
; /* File too big */
60 static int write_file(void)
62 char tmpfilename
[MAX_PATH
+1];
68 /* Create a temporary file */
70 rb
->snprintf(tmpfilename
, MAX_PATH
+1, "%s.tmp", filename
);
72 fd
= rb
->creat(tmpfilename
);
76 /* Let's make sure it always writes CR/LF and not only LF */
77 buf_ptr
= stringbuffer
;
78 str_begin
= stringbuffer
;
80 /* Transform slashes into backslashes */
84 if((*buf_ptr
== '\r') || (*buf_ptr
== '\n')) {
85 /* We have no complete string ? It's only a leading \n or \r ? */
89 /* Terminate string */
92 /* Write our new string */
93 rc
= rb
->write(fd
, str_begin
, rb
->strlen(str_begin
));
99 rc
= rb
->write(fd
, crlf
, 2);
105 /* Reset until we get a new line */
110 /* We start a new line here */
115 /* Next char, until ... */
116 } while(buf_ptr
++ < stringbuffer
+ readsize
);
120 /* Remove the original file */
121 rc
= rb
->remove(filename
);
126 /* Replace the old file with the new */
127 rc
= rb
->rename(tmpfilename
, filename
);
135 enum plugin_status
plugin_start(const struct plugin_api
* api
, const void* parameter
)
140 filename
= (char *)parameter
;
144 buf
= rb
->plugin_get_audio_buffer((size_t *)&buf_size
); /* start munching memory */
149 rb
->screens
[i
]->clear_display();
150 rb
->splash(0, "Converting...");
154 rb
->screens
[i
]->clear_display();
156 rb
->splash(0, "Writing...");
160 rb
->screens
[i
]->clear_display();
162 rb
->splashf(HZ
, "Can't write file: %d", rc
);
164 rb
->splash(HZ
, "Done");
168 rb
->splashf(HZ
, "Can't read file: %d", rc
);
170 rb
->splash(HZ
, "The file is too big");