Correct battery conditional and disk access icon
[kugel-rb.git] / apps / plugins / sort.c
blob18778318158ef7731d6d0ccb43297f965d2789a4
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "plugin.h"
23 /****************************************************************************
24 * Buffer handling:
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
28 * to the strings.
30 * The pointer array grows from the top of the buffer and downwards:
32 * |-------------|
33 * | pointers[2] |--------|
34 * | pointers[1] |------| |
35 * | pointers[0] |----| | |
36 * |-------------| | | |
37 * | | | | |
38 * | | | | |
39 * | free space | | | |
40 * | | | | |
41 * | | | | |
42 * |-------------| | | |
43 * | | | | |
44 * | line 3\0 |<---| | |
45 * | line 2\0 |<-----| |
46 * | line 1\0 |<-------|
47 * |-------------|
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 ****************************************************************************/
60 PLUGIN_HEADER
62 ssize_t buf_size;
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)
85 int fd;
86 char *buf_ptr;
87 char *tmp_ptr;
88 int readsize;
90 fd = rb->open(filename, O_RDONLY);
91 if(fd < 0)
92 return 10 * fd - 1;
94 /* Fill the buffer from the file */
95 rb->lseek(fd, offset, SEEK_SET);
96 readsize = rb->read(fd, stringbuffer, buf_size);
97 rb->close(fd);
99 if(readsize < 0)
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;
107 num_entries = 0;
109 do {
110 tmp_ptr = buf_ptr;
111 while(*buf_ptr != '\n' && buf_ptr < (char *)pointers) {
112 /* Terminate the string with CR... */
113 if(*buf_ptr == '\r')
114 *buf_ptr = 0;
115 buf_ptr++;
117 /* ...and with LF */
118 if(*buf_ptr == '\n')
119 *buf_ptr = 0;
120 else {
121 return tmp_ptr - stringbuffer; /* Buffer is full, return
122 the point to resume at */
125 pointers--;
126 *pointers = tmp_ptr;
127 num_entries++;
128 buf_ptr++;
129 } while(buf_ptr < stringbuffer + readsize);
131 return 0;
134 static int write_file(void)
136 char tmpfilename[MAX_PATH+1];
137 int fd;
138 int i;
139 int rc;
141 /* Create a temporary file */
142 rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename);
143 fd = rb->creat(tmpfilename);
144 if(fd < 0)
145 return 10 * fd - 1;
147 /* Write the sorted strings, with appended CR/LF, to the temp file,
148 in reverse order */
149 for(i = num_entries-1;i >= 0;i--) {
150 rc = rb->write(fd, pointers[i], rb->strlen(pointers[i]));
151 if(rc < 0) {
152 rb->close(fd);
153 return 10 * rc - 2;
156 rc = rb->write(fd, crlf, 2);
157 if(rc < 0) {
158 rb->close(fd);
159 return 10 * rc - 3;
163 rb->close(fd);
165 /* Remove the original file */
166 rc = rb->remove(filename);
167 if(rc < 0) {
168 return 10 * rc - 4;
171 /* Replace the old file with the new */
172 rc = rb->rename(tmpfilename, filename);
173 if(rc < 0) {
174 return 10 * rc - 5;
176 return 0;
179 enum plugin_status plugin_start(const void* parameter)
181 char *buf;
182 int rc;
183 if(!parameter) return PLUGIN_ERROR;
185 filename = (char *)parameter;
187 buf = rb->plugin_get_audio_buffer((size_t *)&buf_size); /* start munching memory */
189 stringbuffer = buf;
190 pointers = (char **)(buf + buf_size - sizeof(int));
192 rb->lcd_clear_display();
193 rb->splash(0, "Loading...");
195 rc = read_buffer(0);
196 if(rc == 0) {
197 rb->lcd_clear_display();
198 rb->splash(0, "Sorting...");
199 sort_buffer();
201 rb->lcd_clear_display();
202 rb->splash(0, "Writing...");
204 rc = write_file();
205 if(rc < 0) {
206 rb->lcd_clear_display();
207 rb->splashf(HZ, "Can't write file: %d", rc);
208 } else {
209 rb->lcd_clear_display();
210 rb->splash(HZ, "Done");
212 } else {
213 if(rc < 0) {
214 rb->lcd_clear_display();
215 rb->splashf(HZ, "Can't read file: %d", rc);
216 } else {
217 rb->lcd_clear_display();
218 rb->splash(HZ, "The file is too big");
222 return PLUGIN_OK;