diff options
Diffstat (limited to 'apps/plugins/iriverify.c')
-rw-r--r-- | apps/plugins/iriverify.c | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/apps/plugins/iriverify.c b/apps/plugins/iriverify.c new file mode 100644 index 0000000000..9e7cdfb16a --- /dev/null +++ b/apps/plugins/iriverify.c | |||
@@ -0,0 +1,176 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2005 Alexandre Bourget | ||
11 | * | ||
12 | * Plugin to transform a Rockbox produced m3u playlist into something | ||
13 | * understandable by the picky original iRiver firmware. | ||
14 | * | ||
15 | * Based on sort.c by the Rockbox team. | ||
16 | * | ||
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. | ||
19 | * | ||
20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
21 | * KIND, either express or implied. | ||
22 | * | ||
23 | ****************************************************************************/ | ||
24 | #include "plugin.h" | ||
25 | |||
26 | |||
27 | static struct plugin_api* rb; | ||
28 | |||
29 | int buf_size; | ||
30 | static char *filename; | ||
31 | static int readsize; | ||
32 | static char *stringbuffer; | ||
33 | static char crlf[2] = "\r\n"; | ||
34 | |||
35 | int read_buffer(int offset) | ||
36 | { | ||
37 | int fd; | ||
38 | |||
39 | fd = rb->open(filename, O_RDONLY); | ||
40 | if(fd < 0) | ||
41 | return 10 * fd - 1; | ||
42 | |||
43 | /* Fill the buffer from the file */ | ||
44 | rb->lseek(fd, offset, SEEK_SET); | ||
45 | readsize = rb->read(fd, stringbuffer, buf_size); | ||
46 | rb->close(fd); | ||
47 | |||
48 | if(readsize < 0) | ||
49 | return readsize * 10 - 2; | ||
50 | |||
51 | if(readsize == buf_size) | ||
52 | return buf_size; /* File too big */ | ||
53 | |||
54 | return 0; | ||
55 | } | ||
56 | |||
57 | static int write_file(void) | ||
58 | { | ||
59 | char tmpfilename[MAX_PATH+1]; | ||
60 | int fd; | ||
61 | int rc; | ||
62 | char *buf_ptr; | ||
63 | char *str_begin; | ||
64 | |||
65 | /* Create a temporary file */ | ||
66 | |||
67 | rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename); | ||
68 | |||
69 | fd = rb->creat(tmpfilename, 0); | ||
70 | if(fd < 0) | ||
71 | return 10 * fd - 1; | ||
72 | |||
73 | /* Let's make sure it always writes CR/LF and not only LF */ | ||
74 | buf_ptr = stringbuffer; | ||
75 | str_begin = stringbuffer; | ||
76 | do { | ||
77 | /* Transform slashes into backslashes */ | ||
78 | if(*buf_ptr == '/') | ||
79 | *buf_ptr = '\\'; | ||
80 | |||
81 | if((*buf_ptr == '\r') || (*buf_ptr == '\n')) { | ||
82 | /* We have no complete string ? It's only a leading \n or \r ? */ | ||
83 | if (!str_begin) | ||
84 | continue; | ||
85 | |||
86 | /* Terminate string */ | ||
87 | *buf_ptr = 0; | ||
88 | |||
89 | /* Write our new string */ | ||
90 | rc = rb->write(fd, str_begin, rb->strlen(str_begin)); | ||
91 | if(rc < 0) { | ||
92 | rb->close(fd); | ||
93 | return 10 * rc - 2; | ||
94 | } | ||
95 | /* Write CR/LF */ | ||
96 | rc = rb->write(fd, crlf, 2); | ||
97 | if(rc < 0) { | ||
98 | rb->close(fd); | ||
99 | return 10 * rc - 3; | ||
100 | } | ||
101 | |||
102 | /* Reset until we get a new line */ | ||
103 | str_begin = NULL; | ||
104 | |||
105 | } | ||
106 | else { | ||
107 | /* We start a new line here */ | ||
108 | if (!str_begin) | ||
109 | str_begin = buf_ptr; | ||
110 | } | ||
111 | |||
112 | /* Ok, skip a char */ | ||
113 | buf_ptr++; | ||
114 | |||
115 | /* until ... */ | ||
116 | } while(buf_ptr < stringbuffer + readsize); | ||
117 | |||
118 | rb->close(fd); | ||
119 | |||
120 | /* Remove the original file */ | ||
121 | rc = rb->remove(filename); | ||
122 | if(rc < 0) { | ||
123 | return 10 * rc - 4; | ||
124 | } | ||
125 | |||
126 | /* Replace the old file with the new */ | ||
127 | rc = rb->rename(tmpfilename, filename); | ||
128 | if(rc < 0) { | ||
129 | return 10 * rc - 5; | ||
130 | } | ||
131 | |||
132 | return 0; | ||
133 | } | ||
134 | |||
135 | enum plugin_status plugin_start(struct plugin_api* api, void* parameter) | ||
136 | { | ||
137 | char *buf; | ||
138 | int rc; | ||
139 | TEST_PLUGIN_API(api); | ||
140 | |||
141 | filename = (char *)parameter; | ||
142 | |||
143 | rb = api; | ||
144 | |||
145 | buf = rb->plugin_get_audio_buffer(&buf_size); /* start munching memory */ | ||
146 | |||
147 | stringbuffer = buf; | ||
148 | |||
149 | rb->lcd_clear_display(); | ||
150 | rb->splash(0, true, "Converting..."); | ||
151 | |||
152 | rc = read_buffer(0); | ||
153 | if(rc == 0) { | ||
154 | rb->lcd_clear_display(); | ||
155 | rb->splash(0, true, "Writing..."); | ||
156 | rc = write_file(); | ||
157 | |||
158 | if(rc < 0) { | ||
159 | rb->lcd_clear_display(); | ||
160 | rb->splash(HZ, true, "Can't write file: %d", rc); | ||
161 | } else { | ||
162 | rb->lcd_clear_display(); | ||
163 | rb->splash(HZ, true, "Done"); | ||
164 | } | ||
165 | } else { | ||
166 | if(rc < 0) { | ||
167 | rb->lcd_clear_display(); | ||
168 | rb->splash(HZ, true, "Can't read file: %d", rc); | ||
169 | } else { | ||
170 | rb->lcd_clear_display(); | ||
171 | rb->splash(HZ, true, "The file is too big"); | ||
172 | } | ||
173 | } | ||
174 | |||
175 | return PLUGIN_OK; | ||
176 | } | ||