1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 /****************************************************************************
26 * We allocate the MP3 buffer for storing the text to be sorted, and then
27 * search the buffer for newlines and store an array of character pointers
30 * The pointer array grows from the top of the buffer and downwards:
33 * | pointers[2] |--------|
34 * | pointers[1] |------| |
35 * | pointers[0] |----| | |
36 * |-------------| | | |
39 * | free space | | | |
42 * |-------------| | | |
44 * | line 3\0 |<---| | |
45 * | line 2\0 |<-----| |
46 * | line 1\0 |<-------|
49 * The advantage of this method is that we optimize the buffer usage.
51 * The disadvantage is that the string pointers will be loaded in reverse
52 * order. We therefore sort the strings in reverse order as well, so we
53 * don't have to sort an already sorted buffer.
54 ****************************************************************************/
56 /***************************************************************************
57 * TODO: Implement a merge sort for files larger than the buffer
58 ****************************************************************************/
62 static const struct plugin_api
* rb
;
65 static char *filename
;
66 static int num_entries
;
67 static char **pointers
;
68 static char *stringbuffer
;
69 static char crlf
[2] = "\r\n";
71 /* Compare function for sorting backwards */
72 static int compare(const void* p1
, const void* p2
)
74 char *s1
= *(char **)p1
;
75 char *s2
= *(char **)p2
;
77 return rb
->strcasecmp(s2
, s1
);
80 static void sort_buffer(void)
82 rb
->qsort(pointers
, num_entries
, sizeof(char *), compare
);
85 int read_buffer(int offset
)
92 fd
= rb
->open(filename
, O_RDONLY
);
96 /* Fill the buffer from the file */
97 rb
->lseek(fd
, offset
, SEEK_SET
);
98 readsize
= rb
->read(fd
, stringbuffer
, buf_size
);
102 return readsize
* 10 - 2;
104 /* Temporary fix until we can do merged sorting */
105 if(readsize
== buf_size
)
106 return buf_size
; /* File too big */
108 buf_ptr
= stringbuffer
;
113 while(*buf_ptr
!= '\n' && buf_ptr
< (char *)pointers
) {
114 /* Terminate the string with CR... */
123 return tmp_ptr
- stringbuffer
; /* Buffer is full, return
124 the point to resume at */
131 } while(buf_ptr
< stringbuffer
+ readsize
);
136 static int write_file(void)
138 char tmpfilename
[MAX_PATH
+1];
143 /* Create a temporary file */
144 rb
->snprintf(tmpfilename
, MAX_PATH
+1, "%s.tmp", filename
);
145 fd
= rb
->creat(tmpfilename
);
149 /* Write the sorted strings, with appended CR/LF, to the temp file,
151 for(i
= num_entries
-1;i
>= 0;i
--) {
152 rc
= rb
->write(fd
, pointers
[i
], rb
->strlen(pointers
[i
]));
158 rc
= rb
->write(fd
, crlf
, 2);
167 /* Remove the original file */
168 rc
= rb
->remove(filename
);
173 /* Replace the old file with the new */
174 rc
= rb
->rename(tmpfilename
, filename
);
181 enum plugin_status
plugin_start(const struct plugin_api
* api
, const void* parameter
)
186 filename
= (char *)parameter
;
190 buf
= rb
->plugin_get_audio_buffer((size_t *)&buf_size
); /* start munching memory */
193 pointers
= (char **)(buf
+ buf_size
- sizeof(int));
195 rb
->lcd_clear_display();
196 rb
->splash(0, "Loading...");
200 rb
->lcd_clear_display();
201 rb
->splash(0, "Sorting...");
204 rb
->lcd_clear_display();
205 rb
->splash(0, "Writing...");
209 rb
->lcd_clear_display();
210 rb
->splashf(HZ
, "Can't write file: %d", rc
);
212 rb
->lcd_clear_display();
213 rb
->splash(HZ
, "Done");
217 rb
->lcd_clear_display();
218 rb
->splashf(HZ
, "Can't read file: %d", rc
);
220 rb
->lcd_clear_display();
221 rb
->splash(HZ
, "The file is too big");