summaryrefslogtreecommitdiffstats
path: root/apps/plugins/sort.c
diff options
context:
space:
mode:
authorTeruaki Kawashima <teru@rockbox.org>2009-12-20 12:59:25 +0000
committerTeruaki Kawashima <teru@rockbox.org>2009-12-20 12:59:25 +0000
commit0cbf210d76dbc8f01a2da944e76f05fdb050003b (patch)
tree217bde3bcd1fe6538c80c24e7e7927e753881b9c /apps/plugins/sort.c
parent275d960b04ea74f45e97331408644c4ec4be6d8f (diff)
downloadrockbox-0cbf210d76dbc8f01a2da944e76f05fdb050003b.tar.gz
rockbox-0cbf210d76dbc8f01a2da944e76f05fdb050003b.tar.bz2
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/sort.c')
-rw-r--r--apps/plugins/sort.c24
1 files changed, 16 insertions, 8 deletions
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;
static char **pointers;
static char *stringbuffer;
static char crlf[2] = "\r\n";
+static int bomsize;
/* Compare function for sorting backwards */
static int compare(const void* p1, const void* p2)
@@ -86,11 +87,14 @@ int read_buffer(int offset)
char *buf_ptr;
char *tmp_ptr;
int readsize;
-
- fd = rb->open(filename, O_RDONLY);
+
+ fd = rb->open_utf8(filename, O_RDONLY);
if(fd < 0)
return 10 * fd - 1;
+ bomsize = rb->lseek(fd, 0, SEEK_CUR);
+ offset += bomsize;
+
/* Fill the buffer from the file */
rb->lseek(fd, offset, SEEK_SET);
readsize = rb->read(fd, stringbuffer, buf_size);
@@ -127,7 +131,7 @@ int read_buffer(int offset)
num_entries++;
buf_ptr++;
} while(buf_ptr < stringbuffer + readsize);
-
+
return 0;
}
@@ -140,7 +144,11 @@ static int write_file(void)
/* Create a temporary file */
rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename);
- fd = rb->creat(tmpfilename);
+ if (bomsize)
+ fd = rb->open_utf8(tmpfilename, O_WRONLY|O_CREAT|O_TRUNC);
+ else
+ fd = rb->open(tmpfilename, O_WRONLY|O_CREAT|O_TRUNC);
+
if(fd < 0)
return 10 * fd - 1;
@@ -191,16 +199,16 @@ enum plugin_status plugin_start(const void* parameter)
rb->lcd_clear_display();
rb->splash(0, "Loading...");
-
+
rc = read_buffer(0);
if(rc == 0) {
rb->lcd_clear_display();
rb->splash(0, "Sorting...");
sort_buffer();
-
+
rb->lcd_clear_display();
rb->splash(0, "Writing...");
-
+
rc = write_file();
if(rc < 0) {
rb->lcd_clear_display();
@@ -218,6 +226,6 @@ enum plugin_status plugin_start(const void* parameter)
rb->splash(HZ, "The file is too big");
}
}
-
+
return PLUGIN_OK;
}