Fix red in bootloaders
[maemo-rb.git] / apps / plugins / iriverify.c
blob35a96a05c49bc27b6da9ef9acf056a9143640bff
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 * 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 ****************************************************************************/
26 #include "plugin.h"
28 PLUGIN_HEADER
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(const void* parameter)
135 char *buf;
136 int rc;
137 int i;
138 filename = (char *)parameter;
140 buf = rb->plugin_get_audio_buffer((size_t *)&buf_size); /* start munching memory */
142 stringbuffer = buf;
144 FOR_NB_SCREENS(i)
145 rb->screens[i]->clear_display();
146 rb->splash(0, "Converting...");
148 rc = read_buffer(0);
149 FOR_NB_SCREENS(i)
150 rb->screens[i]->clear_display();
151 if(rc == 0) {
152 rb->splash(0, "Writing...");
153 rc = write_file();
155 FOR_NB_SCREENS(i)
156 rb->screens[i]->clear_display();
157 if(rc < 0) {
158 rb->splashf(HZ, "Can't write file: %d", rc);
159 } else {
160 rb->splash(HZ, "Done");
162 } else {
163 if(rc < 0) {
164 rb->splashf(HZ, "Can't read file: %d", rc);
165 } else {
166 rb->splash(HZ, "The file is too big");
170 return PLUGIN_OK;