diff options
author | Teruaki Kawashima <teru@rockbox.org> | 2009-12-20 12:59:25 +0000 |
---|---|---|
committer | Teruaki Kawashima <teru@rockbox.org> | 2009-12-20 12:59:25 +0000 |
commit | 0cbf210d76dbc8f01a2da944e76f05fdb050003b (patch) | |
tree | 217bde3bcd1fe6538c80c24e7e7927e753881b9c /apps/plugins | |
parent | 275d960b04ea74f45e97331408644c4ec4be6d8f (diff) | |
download | rockbox-0cbf210d76dbc8f01a2da944e76f05fdb050003b.tar.gz rockbox-0cbf210d76dbc8f01a2da944e76f05fdb050003b.zip |
plugin: search,sort: Handle UTF-8 BOM at the start of file.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24081 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins')
-rw-r--r-- | apps/plugins/search.c | 25 | ||||
-rw-r--r-- | apps/plugins/sort.c | 24 |
2 files changed, 28 insertions, 21 deletions
diff --git a/apps/plugins/search.c b/apps/plugins/search.c index d732c0282b..4f60c82d08 100644 --- a/apps/plugins/search.c +++ b/apps/plugins/search.c | |||
@@ -29,7 +29,7 @@ PLUGIN_HEADER | |||
29 | static int fd; | 29 | static int fd; |
30 | static int fdw; | 30 | static int fdw; |
31 | 31 | ||
32 | static int file_size; | 32 | static int file_size, bomsize; |
33 | static int results = 0; | 33 | static int results = 0; |
34 | 34 | ||
35 | static char buffer[BUFFER_SIZE+1]; | 35 | static char buffer[BUFFER_SIZE+1]; |
@@ -57,13 +57,8 @@ static void fill_buffer(int pos){ | |||
57 | int found = false ; | 57 | int found = false ; |
58 | const char crlf = '\n'; | 58 | const char crlf = '\n'; |
59 | 59 | ||
60 | if (pos>=file_size-BUFFER_SIZE) | 60 | rb->lseek(fd, pos+bomsize, SEEK_SET); |
61 | pos = file_size-BUFFER_SIZE; | 61 | numread = rb->read(fd, buffer, MIN(BUFFER_SIZE, file_size-pos)); |
62 | if (pos<0) | ||
63 | pos = 0; | ||
64 | |||
65 | rb->lseek(fd, pos, SEEK_SET); | ||
66 | numread = rb->read(fd, buffer, BUFFER_SIZE); | ||
67 | 62 | ||
68 | buffer[numread] = 0; | 63 | buffer[numread] = 0; |
69 | line_end = 0; | 64 | line_end = 0; |
@@ -120,11 +115,15 @@ static bool search_init(const char* file){ | |||
120 | if (!rb->kbd_input(search_string,sizeof search_string)){ | 115 | if (!rb->kbd_input(search_string,sizeof search_string)){ |
121 | clear_display(); | 116 | clear_display(); |
122 | rb->splash(0, "Searching..."); | 117 | rb->splash(0, "Searching..."); |
123 | fd = rb->open(file, O_RDONLY); | 118 | fd = rb->open_utf8(file, O_RDONLY); |
124 | if (fd < 0) | 119 | if (fd < 0) |
125 | return false; | 120 | return false; |
126 | 121 | ||
127 | fdw = rb->creat(resultfile); | 122 | bomsize = rb->lseek(fd, 0, SEEK_CUR); |
123 | if (bomsize) | ||
124 | fdw = rb->open_utf8(resultfile, O_WRONLY|O_CREAT|O_TRUNC); | ||
125 | else | ||
126 | fdw = rb->open(resultfile, O_WRONLY|O_CREAT|O_TRUNC); | ||
128 | 127 | ||
129 | if (fdw < 0) { | 128 | if (fdw < 0) { |
130 | #ifdef HAVE_LCD_BITMAP | 129 | #ifdef HAVE_LCD_BITMAP |
@@ -136,7 +135,7 @@ static bool search_init(const char* file){ | |||
136 | return false; | 135 | return false; |
137 | } | 136 | } |
138 | 137 | ||
139 | file_size = rb->lseek(fd, 0, SEEK_END); | 138 | file_size = rb->lseek(fd, 0, SEEK_END) - bomsize; |
140 | 139 | ||
141 | return true; | 140 | return true; |
142 | } | 141 | } |
@@ -177,7 +176,7 @@ enum plugin_status plugin_start(const void* parameter) | |||
177 | rb->splash(HZ, "Done"); | 176 | rb->splash(HZ, "Done"); |
178 | rb->close(fdw); | 177 | rb->close(fdw); |
179 | rb->close(fd); | 178 | rb->close(fd); |
179 | rb->reload_directory(); | ||
180 | 180 | ||
181 | /* We fake a USB connection to force a reload of the file browser */ | 181 | return PLUGIN_OK; |
182 | return PLUGIN_USB_CONNECTED; | ||
183 | } | 182 | } |
diff --git a/apps/plugins/sort.c b/apps/plugins/sort.c index 1877831815..2ae788ebbd 100644 --- a/apps/plugins/sort.c +++ b/apps/plugins/sort.c | |||
@@ -65,6 +65,7 @@ static int num_entries; | |||
65 | static char **pointers; | 65 | static char **pointers; |
66 | static char *stringbuffer; | 66 | static char *stringbuffer; |
67 | static char crlf[2] = "\r\n"; | 67 | static char crlf[2] = "\r\n"; |
68 | static int bomsize; | ||
68 | 69 | ||
69 | /* Compare function for sorting backwards */ | 70 | /* Compare function for sorting backwards */ |
70 | static int compare(const void* p1, const void* p2) | 71 | static int compare(const void* p1, const void* p2) |
@@ -86,11 +87,14 @@ int read_buffer(int offset) | |||
86 | char *buf_ptr; | 87 | char *buf_ptr; |
87 | char *tmp_ptr; | 88 | char *tmp_ptr; |
88 | int readsize; | 89 | int readsize; |
89 | 90 | ||
90 | fd = rb->open(filename, O_RDONLY); | 91 | fd = rb->open_utf8(filename, O_RDONLY); |
91 | if(fd < 0) | 92 | if(fd < 0) |
92 | return 10 * fd - 1; | 93 | return 10 * fd - 1; |
93 | 94 | ||
95 | bomsize = rb->lseek(fd, 0, SEEK_CUR); | ||
96 | offset += bomsize; | ||
97 | |||
94 | /* Fill the buffer from the file */ | 98 | /* Fill the buffer from the file */ |
95 | rb->lseek(fd, offset, SEEK_SET); | 99 | rb->lseek(fd, offset, SEEK_SET); |
96 | readsize = rb->read(fd, stringbuffer, buf_size); | 100 | readsize = rb->read(fd, stringbuffer, buf_size); |
@@ -127,7 +131,7 @@ int read_buffer(int offset) | |||
127 | num_entries++; | 131 | num_entries++; |
128 | buf_ptr++; | 132 | buf_ptr++; |
129 | } while(buf_ptr < stringbuffer + readsize); | 133 | } while(buf_ptr < stringbuffer + readsize); |
130 | 134 | ||
131 | return 0; | 135 | return 0; |
132 | } | 136 | } |
133 | 137 | ||
@@ -140,7 +144,11 @@ static int write_file(void) | |||
140 | 144 | ||
141 | /* Create a temporary file */ | 145 | /* Create a temporary file */ |
142 | rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename); | 146 | rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename); |
143 | fd = rb->creat(tmpfilename); | 147 | if (bomsize) |
148 | fd = rb->open_utf8(tmpfilename, O_WRONLY|O_CREAT|O_TRUNC); | ||
149 | else | ||
150 | fd = rb->open(tmpfilename, O_WRONLY|O_CREAT|O_TRUNC); | ||
151 | |||
144 | if(fd < 0) | 152 | if(fd < 0) |
145 | return 10 * fd - 1; | 153 | return 10 * fd - 1; |
146 | 154 | ||
@@ -191,16 +199,16 @@ enum plugin_status plugin_start(const void* parameter) | |||
191 | 199 | ||
192 | rb->lcd_clear_display(); | 200 | rb->lcd_clear_display(); |
193 | rb->splash(0, "Loading..."); | 201 | rb->splash(0, "Loading..."); |
194 | 202 | ||
195 | rc = read_buffer(0); | 203 | rc = read_buffer(0); |
196 | if(rc == 0) { | 204 | if(rc == 0) { |
197 | rb->lcd_clear_display(); | 205 | rb->lcd_clear_display(); |
198 | rb->splash(0, "Sorting..."); | 206 | rb->splash(0, "Sorting..."); |
199 | sort_buffer(); | 207 | sort_buffer(); |
200 | 208 | ||
201 | rb->lcd_clear_display(); | 209 | rb->lcd_clear_display(); |
202 | rb->splash(0, "Writing..."); | 210 | rb->splash(0, "Writing..."); |
203 | 211 | ||
204 | rc = write_file(); | 212 | rc = write_file(); |
205 | if(rc < 0) { | 213 | if(rc < 0) { |
206 | rb->lcd_clear_display(); | 214 | rb->lcd_clear_display(); |
@@ -218,6 +226,6 @@ enum plugin_status plugin_start(const void* parameter) | |||
218 | rb->splash(HZ, "The file is too big"); | 226 | rb->splash(HZ, "The file is too big"); |
219 | } | 227 | } |
220 | } | 228 | } |
221 | 229 | ||
222 | return PLUGIN_OK; | 230 | return PLUGIN_OK; |
223 | } | 231 | } |