forked from len0rd/rockbox
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
This commit is contained in:
parent
275d960b04
commit
0cbf210d76
4 changed files with 46 additions and 32 deletions
|
@ -675,6 +675,7 @@ static const struct plugin_api rockbox_api = {
|
||||||
pcmbuf_beep,
|
pcmbuf_beep,
|
||||||
#endif
|
#endif
|
||||||
crc_32,
|
crc_32,
|
||||||
|
open_utf8,
|
||||||
};
|
};
|
||||||
|
|
||||||
int plugin_load(const char* plugin, const void* parameter)
|
int plugin_load(const char* plugin, const void* parameter)
|
||||||
|
|
|
@ -135,7 +135,7 @@ void* plugin_get_buffer(size_t *buffer_size);
|
||||||
#define PLUGIN_MAGIC 0x526F634B /* RocK */
|
#define PLUGIN_MAGIC 0x526F634B /* RocK */
|
||||||
|
|
||||||
/* increase this every time the api struct changes */
|
/* increase this every time the api struct changes */
|
||||||
#define PLUGIN_API_VERSION 176
|
#define PLUGIN_API_VERSION 177
|
||||||
|
|
||||||
/* update this to latest version if a change to the api struct breaks
|
/* update this to latest version if a change to the api struct breaks
|
||||||
backwards compatibility (and please take the opportunity to sort in any
|
backwards compatibility (and please take the opportunity to sort in any
|
||||||
|
@ -329,24 +329,27 @@ struct plugin_api {
|
||||||
unsigned (*lcd_remote_get_foreground)(void);
|
unsigned (*lcd_remote_get_foreground)(void);
|
||||||
void (*lcd_remote_set_background)(unsigned background);
|
void (*lcd_remote_set_background)(unsigned background);
|
||||||
unsigned (*lcd_remote_get_background)(void);
|
unsigned (*lcd_remote_get_background)(void);
|
||||||
void (*lcd_remote_bitmap_part)(const fb_remote_data *src, int src_x, int src_y,
|
void (*lcd_remote_bitmap_part)(const fb_remote_data *src,
|
||||||
int stride, int x, int y, int width, int height);
|
int src_x, int src_y, int stride,
|
||||||
void (*lcd_remote_bitmap)(const fb_remote_data *src, int x, int y, int width,
|
int x, int y, int width, int height);
|
||||||
int height);
|
void (*lcd_remote_bitmap)(const fb_remote_data *src, int x, int y,
|
||||||
|
int width, int height);
|
||||||
#endif
|
#endif
|
||||||
void (*viewport_set_defaults)(struct viewport *vp, enum screen_type screen);
|
void (*viewport_set_defaults)(struct viewport *vp,
|
||||||
|
const enum screen_type screen);
|
||||||
/* list */
|
/* list */
|
||||||
void (*gui_synclist_init)(struct gui_synclist * lists,
|
void (*gui_synclist_init)(struct gui_synclist * lists,
|
||||||
list_get_name callback_get_item_name, void * data,
|
list_get_name callback_get_item_name, void * data,
|
||||||
bool scroll_all,int selected_size,
|
bool scroll_all,int selected_size,
|
||||||
struct viewport parent[NB_SCREENS]);
|
struct viewport parent[NB_SCREENS]);
|
||||||
void (*gui_synclist_set_nb_items)(struct gui_synclist * lists, int nb_items);
|
void (*gui_synclist_set_nb_items)(struct gui_synclist * lists, int nb_items);
|
||||||
void (*gui_synclist_set_icon_callback)(struct gui_synclist * lists, list_get_icon icon_callback);
|
void (*gui_synclist_set_icon_callback)(struct gui_synclist * lists,
|
||||||
|
list_get_icon icon_callback);
|
||||||
int (*gui_synclist_get_nb_items)(struct gui_synclist * lists);
|
int (*gui_synclist_get_nb_items)(struct gui_synclist * lists);
|
||||||
int (*gui_synclist_get_sel_pos)(struct gui_synclist * lists);
|
int (*gui_synclist_get_sel_pos)(struct gui_synclist * lists);
|
||||||
void (*gui_synclist_draw)(struct gui_synclist * lists);
|
void (*gui_synclist_draw)(struct gui_synclist * lists);
|
||||||
void (*gui_synclist_select_item)(struct gui_synclist * lists,
|
void (*gui_synclist_select_item)(struct gui_synclist * lists,
|
||||||
int item_number);
|
int item_number);
|
||||||
void (*gui_synclist_add_item)(struct gui_synclist * lists);
|
void (*gui_synclist_add_item)(struct gui_synclist * lists);
|
||||||
void (*gui_synclist_del_item)(struct gui_synclist * lists);
|
void (*gui_synclist_del_item)(struct gui_synclist * lists);
|
||||||
void (*gui_synclist_limit_scroll)(struct gui_synclist * lists, bool scroll);
|
void (*gui_synclist_limit_scroll)(struct gui_synclist * lists, bool scroll);
|
||||||
|
@ -358,7 +361,7 @@ struct plugin_api {
|
||||||
const struct text_message * yes_message,
|
const struct text_message * yes_message,
|
||||||
const struct text_message * no_message);
|
const struct text_message * no_message);
|
||||||
void (*simplelist_info_init)(struct simplelist_info *info, char* title,
|
void (*simplelist_info_init)(struct simplelist_info *info, char* title,
|
||||||
int count, void* data);
|
int count, void* data);
|
||||||
bool (*simplelist_show_list)(struct simplelist_info *info);
|
bool (*simplelist_show_list)(struct simplelist_info *info);
|
||||||
|
|
||||||
/* button */
|
/* button */
|
||||||
|
@ -431,7 +434,8 @@ struct plugin_api {
|
||||||
void (*yield)(void);
|
void (*yield)(void);
|
||||||
volatile long* current_tick;
|
volatile long* current_tick;
|
||||||
long (*default_event_handler)(long event);
|
long (*default_event_handler)(long event);
|
||||||
long (*default_event_handler_ex)(long event, void (*callback)(void *), void *parameter);
|
long (*default_event_handler_ex)(long event,
|
||||||
|
void (*callback)(void *), void *parameter);
|
||||||
unsigned int (*create_thread)(void (*function)(void), void* stack,
|
unsigned int (*create_thread)(void (*function)(void), void* stack,
|
||||||
size_t stack_size, unsigned flags,
|
size_t stack_size, unsigned flags,
|
||||||
const char *name
|
const char *name
|
||||||
|
@ -554,7 +558,8 @@ struct plugin_api {
|
||||||
const char * (*sound_unit)(int setting);
|
const char * (*sound_unit)(int setting);
|
||||||
int (*sound_val2phys)(int setting, int value);
|
int (*sound_val2phys)(int setting, int value);
|
||||||
#ifndef SIMULATOR
|
#ifndef SIMULATOR
|
||||||
void (*mp3_play_data)(const unsigned char* start, int size, void (*get_more)(unsigned char** start, size_t* size));
|
void (*mp3_play_data)(const unsigned char* start, int size,
|
||||||
|
void (*get_more)(unsigned char** start, size_t* size));
|
||||||
void (*mp3_play_pause)(bool play);
|
void (*mp3_play_pause)(bool play);
|
||||||
void (*mp3_play_stop)(void);
|
void (*mp3_play_stop)(void);
|
||||||
bool (*mp3_is_playing)(void);
|
bool (*mp3_is_playing)(void);
|
||||||
|
@ -849,6 +854,7 @@ struct plugin_api {
|
||||||
int amplitude);
|
int amplitude);
|
||||||
#endif
|
#endif
|
||||||
unsigned (*crc_32)(const void *src, unsigned len, unsigned crc32);
|
unsigned (*crc_32)(const void *src, unsigned len, unsigned crc32);
|
||||||
|
int (*open_utf8)(const char* pathname, int flags);
|
||||||
};
|
};
|
||||||
|
|
||||||
/* plugin header */
|
/* plugin header */
|
||||||
|
|
|
@ -29,7 +29,7 @@ PLUGIN_HEADER
|
||||||
static int fd;
|
static int fd;
|
||||||
static int fdw;
|
static int fdw;
|
||||||
|
|
||||||
static int file_size;
|
static int file_size, bomsize;
|
||||||
static int results = 0;
|
static int results = 0;
|
||||||
|
|
||||||
static char buffer[BUFFER_SIZE+1];
|
static char buffer[BUFFER_SIZE+1];
|
||||||
|
@ -57,13 +57,8 @@ static void fill_buffer(int pos){
|
||||||
int found = false ;
|
int found = false ;
|
||||||
const char crlf = '\n';
|
const char crlf = '\n';
|
||||||
|
|
||||||
if (pos>=file_size-BUFFER_SIZE)
|
rb->lseek(fd, pos+bomsize, SEEK_SET);
|
||||||
pos = file_size-BUFFER_SIZE;
|
numread = rb->read(fd, buffer, MIN(BUFFER_SIZE, file_size-pos));
|
||||||
if (pos<0)
|
|
||||||
pos = 0;
|
|
||||||
|
|
||||||
rb->lseek(fd, pos, SEEK_SET);
|
|
||||||
numread = rb->read(fd, buffer, BUFFER_SIZE);
|
|
||||||
|
|
||||||
buffer[numread] = 0;
|
buffer[numread] = 0;
|
||||||
line_end = 0;
|
line_end = 0;
|
||||||
|
@ -120,11 +115,15 @@ static bool search_init(const char* file){
|
||||||
if (!rb->kbd_input(search_string,sizeof search_string)){
|
if (!rb->kbd_input(search_string,sizeof search_string)){
|
||||||
clear_display();
|
clear_display();
|
||||||
rb->splash(0, "Searching...");
|
rb->splash(0, "Searching...");
|
||||||
fd = rb->open(file, O_RDONLY);
|
fd = rb->open_utf8(file, O_RDONLY);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
fdw = rb->creat(resultfile);
|
bomsize = rb->lseek(fd, 0, SEEK_CUR);
|
||||||
|
if (bomsize)
|
||||||
|
fdw = rb->open_utf8(resultfile, O_WRONLY|O_CREAT|O_TRUNC);
|
||||||
|
else
|
||||||
|
fdw = rb->open(resultfile, O_WRONLY|O_CREAT|O_TRUNC);
|
||||||
|
|
||||||
if (fdw < 0) {
|
if (fdw < 0) {
|
||||||
#ifdef HAVE_LCD_BITMAP
|
#ifdef HAVE_LCD_BITMAP
|
||||||
|
@ -136,7 +135,7 @@ static bool search_init(const char* file){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_size = rb->lseek(fd, 0, SEEK_END);
|
file_size = rb->lseek(fd, 0, SEEK_END) - bomsize;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -177,7 +176,7 @@ enum plugin_status plugin_start(const void* parameter)
|
||||||
rb->splash(HZ, "Done");
|
rb->splash(HZ, "Done");
|
||||||
rb->close(fdw);
|
rb->close(fdw);
|
||||||
rb->close(fd);
|
rb->close(fd);
|
||||||
|
rb->reload_directory();
|
||||||
|
|
||||||
/* We fake a USB connection to force a reload of the file browser */
|
return PLUGIN_OK;
|
||||||
return PLUGIN_USB_CONNECTED;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,6 +65,7 @@ static int num_entries;
|
||||||
static char **pointers;
|
static char **pointers;
|
||||||
static char *stringbuffer;
|
static char *stringbuffer;
|
||||||
static char crlf[2] = "\r\n";
|
static char crlf[2] = "\r\n";
|
||||||
|
static int bomsize;
|
||||||
|
|
||||||
/* Compare function for sorting backwards */
|
/* Compare function for sorting backwards */
|
||||||
static int compare(const void* p1, const void* p2)
|
static int compare(const void* p1, const void* p2)
|
||||||
|
@ -86,11 +87,14 @@ int read_buffer(int offset)
|
||||||
char *buf_ptr;
|
char *buf_ptr;
|
||||||
char *tmp_ptr;
|
char *tmp_ptr;
|
||||||
int readsize;
|
int readsize;
|
||||||
|
|
||||||
fd = rb->open(filename, O_RDONLY);
|
fd = rb->open_utf8(filename, O_RDONLY);
|
||||||
if(fd < 0)
|
if(fd < 0)
|
||||||
return 10 * fd - 1;
|
return 10 * fd - 1;
|
||||||
|
|
||||||
|
bomsize = rb->lseek(fd, 0, SEEK_CUR);
|
||||||
|
offset += bomsize;
|
||||||
|
|
||||||
/* Fill the buffer from the file */
|
/* Fill the buffer from the file */
|
||||||
rb->lseek(fd, offset, SEEK_SET);
|
rb->lseek(fd, offset, SEEK_SET);
|
||||||
readsize = rb->read(fd, stringbuffer, buf_size);
|
readsize = rb->read(fd, stringbuffer, buf_size);
|
||||||
|
@ -127,7 +131,7 @@ int read_buffer(int offset)
|
||||||
num_entries++;
|
num_entries++;
|
||||||
buf_ptr++;
|
buf_ptr++;
|
||||||
} while(buf_ptr < stringbuffer + readsize);
|
} while(buf_ptr < stringbuffer + readsize);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,7 +144,11 @@ static int write_file(void)
|
||||||
|
|
||||||
/* Create a temporary file */
|
/* Create a temporary file */
|
||||||
rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename);
|
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)
|
if(fd < 0)
|
||||||
return 10 * fd - 1;
|
return 10 * fd - 1;
|
||||||
|
|
||||||
|
@ -191,16 +199,16 @@ enum plugin_status plugin_start(const void* parameter)
|
||||||
|
|
||||||
rb->lcd_clear_display();
|
rb->lcd_clear_display();
|
||||||
rb->splash(0, "Loading...");
|
rb->splash(0, "Loading...");
|
||||||
|
|
||||||
rc = read_buffer(0);
|
rc = read_buffer(0);
|
||||||
if(rc == 0) {
|
if(rc == 0) {
|
||||||
rb->lcd_clear_display();
|
rb->lcd_clear_display();
|
||||||
rb->splash(0, "Sorting...");
|
rb->splash(0, "Sorting...");
|
||||||
sort_buffer();
|
sort_buffer();
|
||||||
|
|
||||||
rb->lcd_clear_display();
|
rb->lcd_clear_display();
|
||||||
rb->splash(0, "Writing...");
|
rb->splash(0, "Writing...");
|
||||||
|
|
||||||
rc = write_file();
|
rc = write_file();
|
||||||
if(rc < 0) {
|
if(rc < 0) {
|
||||||
rb->lcd_clear_display();
|
rb->lcd_clear_display();
|
||||||
|
@ -218,6 +226,6 @@ enum plugin_status plugin_start(const void* parameter)
|
||||||
rb->splash(HZ, "The file is too big");
|
rb->splash(HZ, "The file is too big");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return PLUGIN_OK;
|
return PLUGIN_OK;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue