diff options
author | Teruaki Kawashima <teru@rockbox.org> | 2009-08-21 14:56:23 +0000 |
---|---|---|
committer | Teruaki Kawashima <teru@rockbox.org> | 2009-08-21 14:56:23 +0000 |
commit | f6999b8f2a7ffb01d9fa13a802f99b7458c2648a (patch) | |
tree | eaba1f620ff843e8b78e7a351f982c37bd7adb77 /apps/plugins/text_editor.c | |
parent | 32ad9a596645d08be8e91c39fbe53629bec35d69 (diff) | |
download | rockbox-f6999b8f2a7ffb01d9fa13a802f99b7458c2648a.tar.gz rockbox-f6999b8f2a7ffb01d9fa13a802f99b7458c2648a.zip |
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
Diffstat (limited to 'apps/plugins/text_editor.c')
-rw-r--r-- | apps/plugins/text_editor.c | 74 |
1 files changed, 33 insertions, 41 deletions
diff --git a/apps/plugins/text_editor.c b/apps/plugins/text_editor.c index dc792046fa..a55165e63e 100644 --- a/apps/plugins/text_editor.c +++ b/apps/plugins/text_editor.c | |||
@@ -22,15 +22,14 @@ | |||
22 | #include "lib/playback_control.h" | 22 | #include "lib/playback_control.h" |
23 | 23 | ||
24 | #if PLUGIN_BUFFER_SIZE > 0x45000 | 24 | #if PLUGIN_BUFFER_SIZE > 0x45000 |
25 | #define MAX_CHARS 0x40000 /* 256 kiB */ | 25 | #define MAX_CHARS 0x40000 /* 256 kiB */ |
26 | #else | 26 | #else |
27 | #define MAX_CHARS 0x5FE0 /* a bit less than 24 kiB */ | 27 | #define MAX_CHARS 0x6000 /* 24 kiB */ |
28 | #endif | 28 | #endif |
29 | #define MAX_LINE_LEN 2048 | 29 | #define MAX_LINE_LEN 2048 |
30 | PLUGIN_HEADER | 30 | PLUGIN_HEADER |
31 | 31 | ||
32 | static char buffer[MAX_CHARS]; | 32 | static char buffer[MAX_CHARS]; |
33 | static char eol[3]; | ||
34 | static int char_count = 0; | 33 | static int char_count = 0; |
35 | static int line_count = 0; | 34 | static int line_count = 0; |
36 | static int last_action_line = 0; | 35 | static int last_action_line = 0; |
@@ -126,38 +125,36 @@ static const char* list_get_name_cb(int selected_item, void* data, | |||
126 | { | 125 | { |
127 | (void)data; | 126 | (void)data; |
128 | char *b = &buffer[do_action(ACTION_GET,0,selected_item)]; | 127 | char *b = &buffer[do_action(ACTION_GET,0,selected_item)]; |
129 | if (rb->strlen(b) >= buf_len) | 128 | /* strlcpy(dst, src, siz) returns strlen(src) */ |
129 | if (rb->strlcpy(buf, b, buf_len) >= buf_len) | ||
130 | { | 130 | { |
131 | char t = b[buf_len-10]; | 131 | rb->strcpy(&buf[buf_len-10], " ..."); |
132 | b[buf_len-10] = '\0'; | ||
133 | rb->snprintf(buf , buf_len, "%s ...", b); | ||
134 | b[buf_len-10] = t; | ||
135 | } | 132 | } |
136 | else rb->strlcpy(buf, b, buf_len); | ||
137 | return buf; | 133 | return buf; |
138 | } | 134 | } |
139 | 135 | ||
140 | char filename[MAX_PATH]; | 136 | char filename[MAX_PATH]; |
137 | char eol[3]; | ||
141 | bool newfile; | 138 | bool newfile; |
142 | int get_eol_string(char* fn) | 139 | void get_eol_string(char* fn) |
143 | { | 140 | { |
144 | int fd, result; | 141 | int fd; |
145 | char t; | 142 | char t; |
146 | 143 | ||
144 | /* assume LF first */ | ||
145 | rb->strcpy(eol,"\n"); | ||
146 | |||
147 | if (!fn || !fn[0]) | 147 | if (!fn || !fn[0]) |
148 | return 0; | 148 | return; |
149 | fd = rb->open(fn,O_RDONLY); | 149 | fd = rb->open(fn,O_RDONLY); |
150 | if (fd<0) | 150 | if (fd<0) |
151 | return 0; | 151 | return; |
152 | 152 | ||
153 | eol[0] = '\0'; | 153 | while (1) |
154 | result = 1; | ||
155 | while (!eol[0]) | ||
156 | { | 154 | { |
157 | if (!rb->read(fd,&t,1)) | 155 | if (!rb->read(fd,&t,1) || t == '\n') |
158 | { | 156 | { |
159 | rb->strcpy(eol,"\n"); | 157 | break; |
160 | result = 0; | ||
161 | } | 158 | } |
162 | if (t == '\r') | 159 | if (t == '\r') |
163 | { | 160 | { |
@@ -165,14 +162,11 @@ int get_eol_string(char* fn) | |||
165 | rb->strcpy(eol,"\r\n"); | 162 | rb->strcpy(eol,"\r\n"); |
166 | else | 163 | else |
167 | rb->strcpy(eol,"\r"); | 164 | rb->strcpy(eol,"\r"); |
168 | } | 165 | break; |
169 | else if (t == '\n') | ||
170 | { | ||
171 | rb->strcpy(eol,"\n"); | ||
172 | } | 166 | } |
173 | } | 167 | } |
174 | rb->close(fd); | 168 | rb->close(fd); |
175 | return result; | 169 | return; |
176 | } | 170 | } |
177 | 171 | ||
178 | bool save_changes(int overwrite) | 172 | bool save_changes(int overwrite) |
@@ -226,6 +220,7 @@ void setup_lists(struct gui_synclist *lists, int sel) | |||
226 | rb->gui_synclist_select_item(lists, sel); | 220 | rb->gui_synclist_select_item(lists, sel); |
227 | rb->gui_synclist_draw(lists); | 221 | rb->gui_synclist_draw(lists); |
228 | } | 222 | } |
223 | |||
229 | enum { | 224 | enum { |
230 | MENU_RET_SAVE = -1, | 225 | MENU_RET_SAVE = -1, |
231 | MENU_RET_NO_UPDATE, | 226 | MENU_RET_NO_UPDATE, |
@@ -234,11 +229,11 @@ enum { | |||
234 | int do_item_menu(int cur_sel, char* copy_buffer) | 229 | int do_item_menu(int cur_sel, char* copy_buffer) |
235 | { | 230 | { |
236 | int ret = 0; | 231 | int ret = 0; |
237 | MENUITEM_STRINGLIST(menu, "Line Options", NULL, | 232 | MENUITEM_STRINGLIST(menu, "Line Options", NULL, |
238 | "Cut/Delete", "Copy", | 233 | "Cut/Delete", "Copy", |
239 | "Insert Above", "Insert Below", | 234 | "Insert Above", "Insert Below", |
240 | "Concat To Above", "Save", | 235 | "Concat To Above", |
241 | "Show Playback Menu",); | 236 | "Save", "Playback Control"); |
242 | 237 | ||
243 | switch (rb->do_menu(&menu, NULL, NULL, false)) | 238 | switch (rb->do_menu(&menu, NULL, NULL, false)) |
244 | { | 239 | { |
@@ -352,10 +347,7 @@ enum plugin_status plugin_start(const void* parameter) | |||
352 | char *c = NULL; | 347 | char *c = NULL; |
353 | #endif | 348 | #endif |
354 | rb->strcpy(filename,(char*)parameter); | 349 | rb->strcpy(filename,(char*)parameter); |
355 | if (!get_eol_string(filename)) | 350 | get_eol_string(filename); |
356 | { | ||
357 | rb->strcpy(eol,"\n"); | ||
358 | } | ||
359 | fd = rb->open(filename,O_RDONLY); | 351 | fd = rb->open(filename,O_RDONLY); |
360 | if (fd<0) | 352 | if (fd<0) |
361 | { | 353 | { |
@@ -368,7 +360,7 @@ enum plugin_status plugin_start(const void* parameter) | |||
368 | edit_colors_file = true; | 360 | edit_colors_file = true; |
369 | #endif | 361 | #endif |
370 | /* read in the file */ | 362 | /* read in the file */ |
371 | while (rb->read_line(fd,temp_line,MAX_LINE_LEN)) | 363 | while (rb->read_line(fd,temp_line,MAX_LINE_LEN) > 0) |
372 | { | 364 | { |
373 | if (!do_action(ACTION_INSERT,temp_line,line_count)) | 365 | if (!do_action(ACTION_INSERT,temp_line,line_count)) |
374 | { | 366 | { |
@@ -386,6 +378,7 @@ enum plugin_status plugin_start(const void* parameter) | |||
386 | rb->strcpy(eol,"\n"); | 378 | rb->strcpy(eol,"\n"); |
387 | newfile = true; | 379 | newfile = true; |
388 | } | 380 | } |
381 | |||
389 | #ifdef HAVE_ADJUSTABLE_CPU_FREQ | 382 | #ifdef HAVE_ADJUSTABLE_CPU_FREQ |
390 | rb->cpu_boost(0); | 383 | rb->cpu_boost(0); |
391 | #endif | 384 | #endif |
@@ -415,14 +408,14 @@ enum plugin_status plugin_start(const void* parameter) | |||
415 | rb->settings_parseline(temp_line, &name, &value); | 408 | rb->settings_parseline(temp_line, &name, &value); |
416 | if (line_count) | 409 | if (line_count) |
417 | { | 410 | { |
418 | MENUITEM_STRINGLIST(menu, "Edit What?", NULL, | 411 | MENUITEM_STRINGLIST(menu, "Edit What?", NULL, |
419 | "Extension", "Colour",); | 412 | "Extension", "Colour"); |
420 | rb->strcpy(extension, name); | 413 | rb->strcpy(extension, name); |
421 | if (value) | 414 | if (value) |
422 | hex_to_rgb(value, &color); | 415 | hex_to_rgb(value, &color); |
423 | else | 416 | else |
424 | color = 0; | 417 | color = 0; |
425 | 418 | ||
426 | switch (rb->do_menu(&menu, NULL, NULL, false)) | 419 | switch (rb->do_menu(&menu, NULL, NULL, false)) |
427 | { | 420 | { |
428 | case 0: | 421 | case 0: |
@@ -437,7 +430,7 @@ enum plugin_status plugin_start(const void* parameter) | |||
437 | /* Should never happen but makes compiler happy */ | 430 | /* Should never happen but makes compiler happy */ |
438 | temp_changed = false; | 431 | temp_changed = false; |
439 | } | 432 | } |
440 | 433 | ||
441 | if (temp_changed) | 434 | if (temp_changed) |
442 | { | 435 | { |
443 | rb->snprintf(temp_line, MAX_LINE_LEN, "%s: %02X%02X%02X", | 436 | rb->snprintf(temp_line, MAX_LINE_LEN, "%s: %02X%02X%02X", |
@@ -486,9 +479,9 @@ enum plugin_status plugin_start(const void* parameter) | |||
486 | case ACTION_STD_CANCEL: | 479 | case ACTION_STD_CANCEL: |
487 | if (changed) | 480 | if (changed) |
488 | { | 481 | { |
489 | MENUITEM_STRINGLIST(menu, "Do What?", NULL, | 482 | MENUITEM_STRINGLIST(menu, "Do What?", NULL, |
490 | "Return", | 483 | "Return", |
491 | "Show Playback Menu", "Save Changes", | 484 | "Playback Control", "Save Changes", |
492 | "Save As...", "Save and Exit", | 485 | "Save As...", "Save and Exit", |
493 | "Ignore Changes and Exit"); | 486 | "Ignore Changes and Exit"); |
494 | switch (rb->do_menu(&menu, NULL, NULL, false)) | 487 | switch (rb->do_menu(&menu, NULL, NULL, false)) |
@@ -506,7 +499,6 @@ enum plugin_status plugin_start(const void* parameter) | |||
506 | if(save_changes(0)) | 499 | if(save_changes(0)) |
507 | changed = 0; | 500 | changed = 0; |
508 | break; | 501 | break; |
509 | |||
510 | case 4: | 502 | case 4: |
511 | if(save_changes(1)) | 503 | if(save_changes(1)) |
512 | exit=1; | 504 | exit=1; |