Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 Linus Nielsen Feltzing |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include "plugin.h" |
| 22 | |
| 23 | /**************************************************************************** |
| 24 | * Buffer handling: |
| 25 | * |
| 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. |
| 29 | * |
| 30 | * The pointer array grows from the top of the buffer and downwards: |
| 31 | * |
| 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 | * |-------------| |
| 48 | * |
| 49 | * The advantage of this method is that we optimize the buffer usage. |
| 50 | * |
| 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 | ****************************************************************************/ |
| 55 | |
| 56 | /*************************************************************************** |
| 57 | * TODO: Implement a merge sort for files larger than the buffer |
| 58 | ****************************************************************************/ |
| 59 | |
Jens Arnold | a36b1d4 | 2006-01-15 18:20:18 +0000 | [diff] [blame] | 60 | PLUGIN_HEADER |
| 61 | |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 62 | static const struct plugin_api* rb; |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 63 | |
Michael Sevakis | 26d242a | 2007-04-21 18:38:25 +0000 | [diff] [blame] | 64 | ssize_t buf_size; |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 65 | static char *filename; |
| 66 | static int num_entries; |
| 67 | static char **pointers; |
| 68 | static char *stringbuffer; |
| 69 | static char crlf[2] = "\r\n"; |
| 70 | |
| 71 | /* Compare function for sorting backwards */ |
| 72 | static int compare(const void* p1, const void* p2) |
| 73 | { |
| 74 | char *s1 = *(char **)p1; |
| 75 | char *s2 = *(char **)p2; |
| 76 | |
Linus Nielsen Feltzing | 0112fc8 | 2004-07-23 15:13:18 +0000 | [diff] [blame] | 77 | return rb->strcasecmp(s2, s1); |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static void sort_buffer(void) |
| 81 | { |
| 82 | rb->qsort(pointers, num_entries, sizeof(char *), compare); |
| 83 | } |
| 84 | |
| 85 | int read_buffer(int offset) |
| 86 | { |
| 87 | int fd; |
| 88 | char *buf_ptr; |
| 89 | char *tmp_ptr; |
| 90 | int readsize; |
| 91 | |
| 92 | fd = rb->open(filename, O_RDONLY); |
| 93 | if(fd < 0) |
| 94 | return 10 * fd - 1; |
| 95 | |
| 96 | /* Fill the buffer from the file */ |
| 97 | rb->lseek(fd, offset, SEEK_SET); |
| 98 | readsize = rb->read(fd, stringbuffer, buf_size); |
| 99 | rb->close(fd); |
| 100 | |
| 101 | if(readsize < 0) |
| 102 | return readsize * 10 - 2; |
| 103 | |
| 104 | /* Temporary fix until we can do merged sorting */ |
| 105 | if(readsize == buf_size) |
| 106 | return buf_size; /* File too big */ |
| 107 | |
| 108 | buf_ptr = stringbuffer; |
| 109 | num_entries = 0; |
| 110 | |
| 111 | do { |
| 112 | tmp_ptr = buf_ptr; |
| 113 | while(*buf_ptr != '\n' && buf_ptr < (char *)pointers) { |
| 114 | /* Terminate the string with CR... */ |
| 115 | if(*buf_ptr == '\r') |
| 116 | *buf_ptr = 0; |
| 117 | buf_ptr++; |
| 118 | } |
| 119 | /* ...and with LF */ |
| 120 | if(*buf_ptr == '\n') |
| 121 | *buf_ptr = 0; |
| 122 | else { |
| 123 | return tmp_ptr - stringbuffer; /* Buffer is full, return |
| 124 | the point to resume at */ |
| 125 | } |
| 126 | |
| 127 | pointers--; |
| 128 | *pointers = tmp_ptr; |
| 129 | num_entries++; |
| 130 | buf_ptr++; |
| 131 | } while(buf_ptr < stringbuffer + readsize); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int write_file(void) |
| 137 | { |
| 138 | char tmpfilename[MAX_PATH+1]; |
| 139 | int fd; |
| 140 | int i; |
| 141 | int rc; |
| 142 | |
| 143 | /* Create a temporary file */ |
| 144 | rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename); |
Jens Arnold | 67eb154 | 2007-02-01 23:08:15 +0000 | [diff] [blame] | 145 | fd = rb->creat(tmpfilename); |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 146 | if(fd < 0) |
| 147 | return 10 * fd - 1; |
| 148 | |
| 149 | /* Write the sorted strings, with appended CR/LF, to the temp file, |
| 150 | in reverse order */ |
| 151 | for(i = num_entries-1;i >= 0;i--) { |
| 152 | rc = rb->write(fd, pointers[i], rb->strlen(pointers[i])); |
| 153 | if(rc < 0) { |
| 154 | rb->close(fd); |
| 155 | return 10 * rc - 2; |
| 156 | } |
| 157 | |
| 158 | rc = rb->write(fd, crlf, 2); |
| 159 | if(rc < 0) { |
| 160 | rb->close(fd); |
| 161 | return 10 * rc - 3; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | rb->close(fd); |
| 166 | |
| 167 | /* Remove the original file */ |
| 168 | rc = rb->remove(filename); |
| 169 | if(rc < 0) { |
| 170 | return 10 * rc - 4; |
| 171 | } |
| 172 | |
| 173 | /* Replace the old file with the new */ |
| 174 | rc = rb->rename(tmpfilename, filename); |
| 175 | if(rc < 0) { |
| 176 | return 10 * rc - 5; |
| 177 | } |
| 178 | return 0; |
| 179 | } |
| 180 | |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 181 | enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter) |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 182 | { |
| 183 | char *buf; |
| 184 | int rc; |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 185 | |
| 186 | filename = (char *)parameter; |
| 187 | |
| 188 | rb = api; |
| 189 | |
Michael Sevakis | 8676dc2 | 2007-04-21 19:07:15 +0000 | [diff] [blame] | 190 | buf = rb->plugin_get_audio_buffer((size_t *)&buf_size); /* start munching memory */ |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 191 | |
| 192 | stringbuffer = buf; |
| 193 | pointers = (char **)(buf + buf_size - sizeof(int)); |
| 194 | |
Linus Nielsen Feltzing | 9bcd2a9 | 2004-11-17 12:13:33 +0000 | [diff] [blame] | 195 | rb->lcd_clear_display(); |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 196 | rb->splash(0, "Loading..."); |
Linus Nielsen Feltzing | 9bcd2a9 | 2004-11-17 12:13:33 +0000 | [diff] [blame] | 197 | |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 198 | rc = read_buffer(0); |
| 199 | if(rc == 0) { |
Linus Nielsen Feltzing | 9bcd2a9 | 2004-11-17 12:13:33 +0000 | [diff] [blame] | 200 | rb->lcd_clear_display(); |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 201 | rb->splash(0, "Sorting..."); |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 202 | sort_buffer(); |
Linus Nielsen Feltzing | 9bcd2a9 | 2004-11-17 12:13:33 +0000 | [diff] [blame] | 203 | |
| 204 | rb->lcd_clear_display(); |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 205 | rb->splash(0, "Writing..."); |
Linus Nielsen Feltzing | 9bcd2a9 | 2004-11-17 12:13:33 +0000 | [diff] [blame] | 206 | |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 207 | rc = write_file(); |
| 208 | if(rc < 0) { |
Linus Nielsen Feltzing | 9bcd2a9 | 2004-11-17 12:13:33 +0000 | [diff] [blame] | 209 | rb->lcd_clear_display(); |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 210 | rb->splash(HZ, "Can't write file: %d", rc); |
Linus Nielsen Feltzing | 9bcd2a9 | 2004-11-17 12:13:33 +0000 | [diff] [blame] | 211 | } else { |
| 212 | rb->lcd_clear_display(); |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 213 | rb->splash(HZ, "Done"); |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 214 | } |
| 215 | } else { |
Linus Nielsen Feltzing | 9bcd2a9 | 2004-11-17 12:13:33 +0000 | [diff] [blame] | 216 | if(rc < 0) { |
| 217 | rb->lcd_clear_display(); |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 218 | rb->splash(HZ, "Can't read file: %d", rc); |
Linus Nielsen Feltzing | 9bcd2a9 | 2004-11-17 12:13:33 +0000 | [diff] [blame] | 219 | } else { |
| 220 | rb->lcd_clear_display(); |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 221 | rb->splash(HZ, "The file is too big"); |
Linus Nielsen Feltzing | 9bcd2a9 | 2004-11-17 12:13:33 +0000 | [diff] [blame] | 222 | } |
Linus Nielsen Feltzing | a17df38 | 2004-07-23 14:54:04 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return PLUGIN_OK; |
| 226 | } |