text editor: optimize a bit and try to reduce size.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22449 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Teruaki Kawashima 2009-08-21 14:56:23 +00:00
parent 32ad9a5966
commit f6999b8f2a

View file

@ -24,13 +24,12 @@
#if PLUGIN_BUFFER_SIZE > 0x45000 #if PLUGIN_BUFFER_SIZE > 0x45000
#define MAX_CHARS 0x40000 /* 256 kiB */ #define MAX_CHARS 0x40000 /* 256 kiB */
#else #else
#define MAX_CHARS 0x5FE0 /* a bit less than 24 kiB */ #define MAX_CHARS 0x6000 /* 24 kiB */
#endif #endif
#define MAX_LINE_LEN 2048 #define MAX_LINE_LEN 2048
PLUGIN_HEADER PLUGIN_HEADER
static char buffer[MAX_CHARS]; static char buffer[MAX_CHARS];
static char eol[3];
static int char_count = 0; static int char_count = 0;
static int line_count = 0; static int line_count = 0;
static int last_action_line = 0; static int last_action_line = 0;
@ -126,38 +125,36 @@ static const char* list_get_name_cb(int selected_item, void* data,
{ {
(void)data; (void)data;
char *b = &buffer[do_action(ACTION_GET,0,selected_item)]; char *b = &buffer[do_action(ACTION_GET,0,selected_item)];
if (rb->strlen(b) >= buf_len) /* strlcpy(dst, src, siz) returns strlen(src) */
if (rb->strlcpy(buf, b, buf_len) >= buf_len)
{ {
char t = b[buf_len-10]; rb->strcpy(&buf[buf_len-10], " ...");
b[buf_len-10] = '\0';
rb->snprintf(buf , buf_len, "%s ...", b);
b[buf_len-10] = t;
} }
else rb->strlcpy(buf, b, buf_len);
return buf; return buf;
} }
char filename[MAX_PATH]; char filename[MAX_PATH];
char eol[3];
bool newfile; bool newfile;
int get_eol_string(char* fn) void get_eol_string(char* fn)
{ {
int fd, result; int fd;
char t; char t;
/* assume LF first */
rb->strcpy(eol,"\n");
if (!fn || !fn[0]) if (!fn || !fn[0])
return 0; return;
fd = rb->open(fn,O_RDONLY); fd = rb->open(fn,O_RDONLY);
if (fd<0) if (fd<0)
return 0; return;
eol[0] = '\0'; while (1)
result = 1;
while (!eol[0])
{ {
if (!rb->read(fd,&t,1)) if (!rb->read(fd,&t,1) || t == '\n')
{ {
rb->strcpy(eol,"\n"); break;
result = 0;
} }
if (t == '\r') if (t == '\r')
{ {
@ -165,14 +162,11 @@ int get_eol_string(char* fn)
rb->strcpy(eol,"\r\n"); rb->strcpy(eol,"\r\n");
else else
rb->strcpy(eol,"\r"); rb->strcpy(eol,"\r");
} break;
else if (t == '\n')
{
rb->strcpy(eol,"\n");
} }
} }
rb->close(fd); rb->close(fd);
return result; return;
} }
bool save_changes(int overwrite) bool save_changes(int overwrite)
@ -226,6 +220,7 @@ void setup_lists(struct gui_synclist *lists, int sel)
rb->gui_synclist_select_item(lists, sel); rb->gui_synclist_select_item(lists, sel);
rb->gui_synclist_draw(lists); rb->gui_synclist_draw(lists);
} }
enum { enum {
MENU_RET_SAVE = -1, MENU_RET_SAVE = -1,
MENU_RET_NO_UPDATE, MENU_RET_NO_UPDATE,
@ -237,8 +232,8 @@ int do_item_menu(int cur_sel, char* copy_buffer)
MENUITEM_STRINGLIST(menu, "Line Options", NULL, MENUITEM_STRINGLIST(menu, "Line Options", NULL,
"Cut/Delete", "Copy", "Cut/Delete", "Copy",
"Insert Above", "Insert Below", "Insert Above", "Insert Below",
"Concat To Above", "Save", "Concat To Above",
"Show Playback Menu",); "Save", "Playback Control");
switch (rb->do_menu(&menu, NULL, NULL, false)) switch (rb->do_menu(&menu, NULL, NULL, false))
{ {
@ -352,10 +347,7 @@ enum plugin_status plugin_start(const void* parameter)
char *c = NULL; char *c = NULL;
#endif #endif
rb->strcpy(filename,(char*)parameter); rb->strcpy(filename,(char*)parameter);
if (!get_eol_string(filename)) get_eol_string(filename);
{
rb->strcpy(eol,"\n");
}
fd = rb->open(filename,O_RDONLY); fd = rb->open(filename,O_RDONLY);
if (fd<0) if (fd<0)
{ {
@ -368,7 +360,7 @@ enum plugin_status plugin_start(const void* parameter)
edit_colors_file = true; edit_colors_file = true;
#endif #endif
/* read in the file */ /* read in the file */
while (rb->read_line(fd,temp_line,MAX_LINE_LEN)) while (rb->read_line(fd,temp_line,MAX_LINE_LEN) > 0)
{ {
if (!do_action(ACTION_INSERT,temp_line,line_count)) if (!do_action(ACTION_INSERT,temp_line,line_count))
{ {
@ -386,6 +378,7 @@ enum plugin_status plugin_start(const void* parameter)
rb->strcpy(eol,"\n"); rb->strcpy(eol,"\n");
newfile = true; newfile = true;
} }
#ifdef HAVE_ADJUSTABLE_CPU_FREQ #ifdef HAVE_ADJUSTABLE_CPU_FREQ
rb->cpu_boost(0); rb->cpu_boost(0);
#endif #endif
@ -416,7 +409,7 @@ enum plugin_status plugin_start(const void* parameter)
if (line_count) if (line_count)
{ {
MENUITEM_STRINGLIST(menu, "Edit What?", NULL, MENUITEM_STRINGLIST(menu, "Edit What?", NULL,
"Extension", "Colour",); "Extension", "Colour");
rb->strcpy(extension, name); rb->strcpy(extension, name);
if (value) if (value)
hex_to_rgb(value, &color); hex_to_rgb(value, &color);
@ -488,7 +481,7 @@ enum plugin_status plugin_start(const void* parameter)
{ {
MENUITEM_STRINGLIST(menu, "Do What?", NULL, MENUITEM_STRINGLIST(menu, "Do What?", NULL,
"Return", "Return",
"Show Playback Menu", "Save Changes", "Playback Control", "Save Changes",
"Save As...", "Save and Exit", "Save As...", "Save and Exit",
"Ignore Changes and Exit"); "Ignore Changes and Exit");
switch (rb->do_menu(&menu, NULL, NULL, false)) switch (rb->do_menu(&menu, NULL, NULL, false))
@ -506,7 +499,6 @@ enum plugin_status plugin_start(const void* parameter)
if(save_changes(0)) if(save_changes(0))
changed = 0; changed = 0;
break; break;
case 4: case 4:
if(save_changes(1)) if(save_changes(1))
exit=1; exit=1;