Reverting parts of r19760 that was mistakenly committed.
[kugel-rb.git] / apps / plugins / iriverify.c
blob7e77ae3e69c2b6924621295e894dd52ffe917860
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 static const struct plugin_api* rb;
32 ssize_t buf_size;
33 static char *filename;
34 static int readsize;
35 static char *stringbuffer;
36 static char crlf[2] = "\r\n";
38 int read_buffer(int offset)
40 int fd;
42 fd = rb->open(filename, O_RDONLY);
43 if(fd < 0)
44 return 10 * fd - 1;
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);
51 if(readsize < 0)
52 return readsize * 10 - 2;
54 if(readsize == buf_size)
55 return buf_size; /* File too big */
57 return 0;
60 static int write_file(void)
62 char tmpfilename[MAX_PATH+1];
63 int fd;
64 int rc;
65 char *buf_ptr;
66 char *str_begin;
68 /* Create a temporary file */
70 rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename);
72 fd = rb->creat(tmpfilename);
73 if(fd < 0)
74 return 10 * fd - 1;
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 = '\\';
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;
89 /* Terminate string */
90 *buf_ptr = 0;
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;
98 /* Write CR/LF */
99 rc = rb->write(fd, crlf, 2);
100 if(rc < 0) {
101 rb->close(fd);
102 return 10 * rc - 3;
105 /* Reset until we get a new line */
106 str_begin = NULL;
109 else {
110 /* We start a new line here */
111 if (!str_begin)
112 str_begin = buf_ptr;
115 /* Next char, until ... */
116 } while(buf_ptr++ < stringbuffer + readsize);
118 rb->close(fd);
120 /* Remove the original file */
121 rc = rb->remove(filename);
122 if(rc < 0) {
123 return 10 * rc - 4;
126 /* Replace the old file with the new */
127 rc = rb->rename(tmpfilename, filename);
128 if(rc < 0) {
129 return 10 * rc - 5;
132 return 0;
135 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
137 char *buf;
138 int rc;
139 int i;
140 filename = (char *)parameter;
142 rb = api;
144 buf = rb->plugin_get_audio_buffer((size_t *)&buf_size); /* start munching memory */
146 stringbuffer = buf;
148 FOR_NB_SCREENS(i)
149 rb->screens[i]->clear_display();
150 rb->splash(0, "Converting...");
152 rc = read_buffer(0);
153 FOR_NB_SCREENS(i)
154 rb->screens[i]->clear_display();
155 if(rc == 0) {
156 rb->splash(0, "Writing...");
157 rc = write_file();
159 FOR_NB_SCREENS(i)
160 rb->screens[i]->clear_display();
161 if(rc < 0) {
162 rb->splashf(HZ, "Can't write file: %d", rc);
163 } else {
164 rb->splash(HZ, "Done");
166 } else {
167 if(rc < 0) {
168 rb->splashf(HZ, "Can't read file: %d", rc);
169 } else {
170 rb->splash(HZ, "The file is too big");
174 return PLUGIN_OK;