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 ****************************************************************************/
31 static char *filename
;
33 static char *stringbuffer
;
34 static char crlf
[2] = "\r\n";
36 int read_buffer(int offset
)
40 fd
= rb
->open(filename
, O_RDONLY
);
44 /* Fill the buffer from the file */
45 rb
->lseek(fd
, offset
, SEEK_SET
);
46 readsize
= rb
->read(fd
, stringbuffer
, buf_size
);
50 return readsize
* 10 - 2;
52 if(readsize
== buf_size
)
53 return buf_size
; /* File too big */
58 static int write_file(void)
60 char tmpfilename
[MAX_PATH
+1];
66 /* Create a temporary file */
68 rb
->snprintf(tmpfilename
, MAX_PATH
+1, "%s.tmp", filename
);
70 fd
= rb
->creat(tmpfilename
);
74 /* Let's make sure it always writes CR/LF and not only LF */
75 buf_ptr
= stringbuffer
;
76 str_begin
= stringbuffer
;
78 /* Transform slashes into backslashes */
82 if((*buf_ptr
== '\r') || (*buf_ptr
== '\n')) {
83 /* We have no complete string ? It's only a leading \n or \r ? */
87 /* Terminate string */
90 /* Write our new string */
91 rc
= rb
->write(fd
, str_begin
, rb
->strlen(str_begin
));
97 rc
= rb
->write(fd
, crlf
, 2);
103 /* Reset until we get a new line */
108 /* We start a new line here */
113 /* Next char, until ... */
114 } while(buf_ptr
++ < stringbuffer
+ readsize
);
118 /* Remove the original file */
119 rc
= rb
->remove(filename
);
124 /* Replace the old file with the new */
125 rc
= rb
->rename(tmpfilename
, filename
);
133 enum plugin_status
plugin_start(UNUSED_ATTR
const void* parameter
)
138 if(!parameter
) return PLUGIN_ERROR
;
139 filename
= (char *)parameter
;
141 buf
= rb
->plugin_get_audio_buffer((size_t *)&buf_size
); /* start munching memory */
146 rb
->screens
[i
]->clear_display();
147 rb
->splash(0, "Converting...");
151 rb
->screens
[i
]->clear_display();
153 rb
->splash(0, "Writing...");
157 rb
->screens
[i
]->clear_display();
159 rb
->splashf(HZ
, "Can't write file: %d", rc
);
161 rb
->splash(HZ
, "Done");
165 rb
->splashf(HZ
, "Can't read file: %d", rc
);
167 rb
->splash(HZ
, "The file is too big");