1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 /****************************************************************************
24 * We allocate the MP3 buffer for storing the text to be sorted, and then
25 * search the buffer for newlines and store an array of character pointers
28 * The pointer array grows from the top of the buffer and downwards:
31 * | pointers[2] |--------|
32 * | pointers[1] |------| |
33 * | pointers[0] |----| | |
34 * |-------------| | | |
37 * | free space | | | |
40 * |-------------| | | |
42 * | line 3\0 |<---| | |
43 * | line 2\0 |<-----| |
44 * | line 1\0 |<-------|
47 * The advantage of this method is that we optimize the buffer usage.
49 * The disadvantage is that the string pointers will be loaded in reverse
50 * order. We therefore sort the strings in reverse order as well, so we
51 * don't have to sort an already sorted buffer.
52 ****************************************************************************/
54 /***************************************************************************
55 * TODO: Implement a merge sort for files larger than the buffer
56 ****************************************************************************/
60 static struct plugin_api
* rb
;
63 static char *filename
;
64 static int num_entries
;
65 static char **pointers
;
66 static char *stringbuffer
;
67 static char crlf
[2] = "\r\n";
69 /* Compare function for sorting backwards */
70 static int compare(const void* p1
, const void* p2
)
72 char *s1
= *(char **)p1
;
73 char *s2
= *(char **)p2
;
75 return rb
->strcasecmp(s2
, s1
);
78 static void sort_buffer(void)
80 rb
->qsort(pointers
, num_entries
, sizeof(char *), compare
);
83 int read_buffer(int offset
)
90 fd
= rb
->open(filename
, O_RDONLY
);
94 /* Fill the buffer from the file */
95 rb
->lseek(fd
, offset
, SEEK_SET
);
96 readsize
= rb
->read(fd
, stringbuffer
, buf_size
);
100 return readsize
* 10 - 2;
102 /* Temporary fix until we can do merged sorting */
103 if(readsize
== buf_size
)
104 return buf_size
; /* File too big */
106 buf_ptr
= stringbuffer
;
111 while(*buf_ptr
!= '\n' && buf_ptr
< (char *)pointers
) {
112 /* Terminate the string with CR... */
121 return tmp_ptr
- stringbuffer
; /* Buffer is full, return
122 the point to resume at */
129 } while(buf_ptr
< stringbuffer
+ readsize
);
134 static int write_file(void)
136 char tmpfilename
[MAX_PATH
+1];
141 /* Create a temporary file */
142 rb
->snprintf(tmpfilename
, MAX_PATH
+1, "%s.tmp", filename
);
143 fd
= rb
->creat(tmpfilename
);
147 /* Write the sorted strings, with appended CR/LF, to the temp file,
149 for(i
= num_entries
-1;i
>= 0;i
--) {
150 rc
= rb
->write(fd
, pointers
[i
], rb
->strlen(pointers
[i
]));
156 rc
= rb
->write(fd
, crlf
, 2);
165 /* Remove the original file */
166 rc
= rb
->remove(filename
);
171 /* Replace the old file with the new */
172 rc
= rb
->rename(tmpfilename
, filename
);
179 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
184 filename
= (char *)parameter
;
188 buf
= rb
->plugin_get_audio_buffer((size_t *)&buf_size
); /* start munching memory */
191 pointers
= (char **)(buf
+ buf_size
- sizeof(int));
193 rb
->lcd_clear_display();
194 rb
->splash(0, "Loading...");
198 rb
->lcd_clear_display();
199 rb
->splash(0, "Sorting...");
202 rb
->lcd_clear_display();
203 rb
->splash(0, "Writing...");
207 rb
->lcd_clear_display();
208 rb
->splash(HZ
, "Can't write file: %d", rc
);
210 rb
->lcd_clear_display();
211 rb
->splash(HZ
, "Done");
215 rb
->lcd_clear_display();
216 rb
->splash(HZ
, "Can't read file: %d", rc
);
218 rb
->lcd_clear_display();
219 rb
->splash(HZ
, "The file is too big");