A couple more tweaks
[Rockbox.git] / apps / plugins / iriverify.c
blobb578cf7e0c42737f6fe9b01ba6217489d93da860
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 * All files in this archive are subject to the GNU General Public License.
18 * See the file COPYING in the source tree root for full license agreement.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include "plugin.h"
26 PLUGIN_HEADER
28 static struct plugin_api* rb;
30 ssize_t buf_size;
31 static char *filename;
32 static int readsize;
33 static char *stringbuffer;
34 static char crlf[2] = "\r\n";
36 int read_buffer(int offset)
38 int fd;
40 fd = rb->open(filename, O_RDONLY);
41 if(fd < 0)
42 return 10 * fd - 1;
44 /* Fill the buffer from the file */
45 rb->lseek(fd, offset, SEEK_SET);
46 readsize = rb->read(fd, stringbuffer, buf_size);
47 rb->close(fd);
49 if(readsize < 0)
50 return readsize * 10 - 2;
52 if(readsize == buf_size)
53 return buf_size; /* File too big */
55 return 0;
58 static int write_file(void)
60 char tmpfilename[MAX_PATH+1];
61 int fd;
62 int rc;
63 char *buf_ptr;
64 char *str_begin;
66 /* Create a temporary file */
68 rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename);
70 fd = rb->creat(tmpfilename);
71 if(fd < 0)
72 return 10 * fd - 1;
74 /* Let's make sure it always writes CR/LF and not only LF */
75 buf_ptr = stringbuffer;
76 str_begin = stringbuffer;
77 do {
78 /* Transform slashes into backslashes */
79 if(*buf_ptr == '/')
80 *buf_ptr = '\\';
82 if((*buf_ptr == '\r') || (*buf_ptr == '\n')) {
83 /* We have no complete string ? It's only a leading \n or \r ? */
84 if (!str_begin)
85 continue;
87 /* Terminate string */
88 *buf_ptr = 0;
90 /* Write our new string */
91 rc = rb->write(fd, str_begin, rb->strlen(str_begin));
92 if(rc < 0) {
93 rb->close(fd);
94 return 10 * rc - 2;
96 /* Write CR/LF */
97 rc = rb->write(fd, crlf, 2);
98 if(rc < 0) {
99 rb->close(fd);
100 return 10 * rc - 3;
103 /* Reset until we get a new line */
104 str_begin = NULL;
107 else {
108 /* We start a new line here */
109 if (!str_begin)
110 str_begin = buf_ptr;
113 /* Next char, until ... */
114 } while(buf_ptr++ < stringbuffer + readsize);
116 rb->close(fd);
118 /* Remove the original file */
119 rc = rb->remove(filename);
120 if(rc < 0) {
121 return 10 * rc - 4;
124 /* Replace the old file with the new */
125 rc = rb->rename(tmpfilename, filename);
126 if(rc < 0) {
127 return 10 * rc - 5;
130 return 0;
133 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
135 char *buf;
136 int rc;
137 int i;
138 filename = (char *)parameter;
140 rb = api;
142 buf = rb->plugin_get_audio_buffer((size_t *)&buf_size); /* start munching memory */
144 stringbuffer = buf;
146 FOR_NB_SCREENS(i)
147 rb->screens[i]->clear_display();
148 rb->splash(0, "Converting...");
150 rc = read_buffer(0);
151 FOR_NB_SCREENS(i)
152 rb->screens[i]->clear_display();
153 if(rc == 0) {
154 rb->splash(0, "Writing...");
155 rc = write_file();
157 FOR_NB_SCREENS(i)
158 rb->screens[i]->clear_display();
159 if(rc < 0) {
160 rb->splash(HZ, "Can't write file: %d", rc);
161 } else {
162 rb->splash(HZ, "Done");
164 } else {
165 if(rc < 0) {
166 rb->splash(HZ, "Can't read file: %d", rc);
167 } else {
168 rb->splash(HZ, "The file is too big");
172 return PLUGIN_OK;