diff options
author | Linus Nielsen Feltzing <linus@haxx.se> | 2004-07-13 14:25:19 +0000 |
---|---|---|
committer | Linus Nielsen Feltzing <linus@haxx.se> | 2004-07-13 14:25:19 +0000 |
commit | 6d84d3a691f51a6fe58a90207b5cab36f765d9b0 (patch) | |
tree | 1af2e6625ca00cc9b5780f0f1855fc5e98ec7eb3 | |
parent | a96a130f5ac3753e4e882f2e2a4b8c41f221e704 (diff) | |
download | rockbox-6d84d3a691f51a6fe58a90207b5cab36f765d9b0.tar.gz rockbox-6d84d3a691f51a6fe58a90207b5cab36f765d9b0.zip |
New plugin, search.rock, by Stefan Meyer (Patch #723682)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@4874 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | apps/plugins/search.c | 180 | ||||
-rw-r--r-- | apps/plugins/viewers.config | 1 |
2 files changed, 181 insertions, 0 deletions
diff --git a/apps/plugins/search.c b/apps/plugins/search.c new file mode 100644 index 0000000000..611a48e0bc --- /dev/null +++ b/apps/plugins/search.c | |||
@@ -0,0 +1,180 @@ | |||
1 | /*************************************************************************** | ||
2 | * | ||
3 | * __________ __ ___. | ||
4 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
5 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
6 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
7 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
8 | * \/ \/ \/ \/ \/ | ||
9 | * $Id$ | ||
10 | * | ||
11 | * Copyright (C) 2003 Stefan Meyer | ||
12 | * | ||
13 | * All files in this archive are subject to the GNU General Public License. | ||
14 | * See the file COPYING in the source tree root for full license agreement. | ||
15 | * | ||
16 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
17 | * KIND, either express or implied. | ||
18 | * | ||
19 | ****************************************************************************/ | ||
20 | #include "plugin.h" | ||
21 | #include "ctype.h" | ||
22 | |||
23 | static struct plugin_api* rb; | ||
24 | |||
25 | #define BUFFER_SIZE 16384 | ||
26 | |||
27 | static int fd; | ||
28 | static int fdw; | ||
29 | |||
30 | static int file_size; | ||
31 | static int results = 0; | ||
32 | |||
33 | static char buffer[BUFFER_SIZE+1]; | ||
34 | static char search_string[60] ; | ||
35 | |||
36 | static int buffer_pos; /* Position of the buffer in the file */ | ||
37 | |||
38 | static int line_end; /* Index of the end of line */ | ||
39 | |||
40 | char resultfile[MAX_PATH]; | ||
41 | char path[MAX_PATH]; | ||
42 | |||
43 | static int strpcasecmp(const char *s1, const char *s2) | ||
44 | { | ||
45 | while (*s1 != '\0' && tolower(*s1) == tolower(*s2)) { | ||
46 | s1++; | ||
47 | s2++; | ||
48 | } | ||
49 | |||
50 | return (*s1 == '\0') ; | ||
51 | } | ||
52 | |||
53 | static void fill_buffer(int pos) | ||
54 | { | ||
55 | int numread; | ||
56 | int i; | ||
57 | int found = false ; | ||
58 | char crlf[2] = "\n\0"; | ||
59 | |||
60 | if (pos>=file_size-BUFFER_SIZE) | ||
61 | pos = file_size-BUFFER_SIZE; | ||
62 | if (pos<0) | ||
63 | pos = 0; | ||
64 | |||
65 | rb->lseek(fd, pos, SEEK_SET); | ||
66 | numread = rb->read(fd, buffer, BUFFER_SIZE); | ||
67 | |||
68 | buffer[numread] = 0; | ||
69 | line_end = 0; | ||
70 | |||
71 | for(i=0;i<numread;i++) { | ||
72 | switch(buffer[i]) { | ||
73 | case '\r': | ||
74 | buffer[i] = ' '; | ||
75 | break; | ||
76 | case '\n': | ||
77 | buffer[i] = 0; | ||
78 | buffer_pos = pos + i +1 ; | ||
79 | |||
80 | if (found) | ||
81 | { | ||
82 | /* write to playlist */ | ||
83 | rb->write(fdw, &buffer[line_end], | ||
84 | rb->strlen( &buffer[line_end] )); | ||
85 | rb->write(fdw, &crlf,1); | ||
86 | |||
87 | found = false ; | ||
88 | results++ ; | ||
89 | } | ||
90 | line_end = i +1 ; | ||
91 | |||
92 | break; | ||
93 | |||
94 | default: | ||
95 | if (!found && tolower(buffer[i]) == tolower(search_string[0])) | ||
96 | { | ||
97 | found = strpcasecmp(&search_string[0],&buffer[i]) ; | ||
98 | } | ||
99 | break; | ||
100 | } | ||
101 | } | ||
102 | } | ||
103 | |||
104 | static void search_buffer(void) | ||
105 | { | ||
106 | buffer_pos = 0; | ||
107 | |||
108 | fill_buffer(0); | ||
109 | while ((buffer_pos+1) < file_size) { | ||
110 | fill_buffer(buffer_pos); | ||
111 | } | ||
112 | } | ||
113 | |||
114 | static bool search_init(char* file) | ||
115 | { | ||
116 | if (!rb->kbd_input(search_string,sizeof search_string)) | ||
117 | { | ||
118 | rb->lcd_clear_display(); | ||
119 | rb->splash(0, true, "Searching..."); | ||
120 | fd = rb->open(file, O_RDONLY); | ||
121 | if (fd==-1) | ||
122 | return false; | ||
123 | |||
124 | fdw = rb->creat(resultfile,0); | ||
125 | |||
126 | if (fdw < 0) { | ||
127 | #ifdef HAVE_LCD_BITMAP | ||
128 | rb->splash(HZ, true, "Failed to create result file!"); | ||
129 | #else | ||
130 | rb->splash(HZ, true, "File creation failed"); | ||
131 | #endif | ||
132 | return false; | ||
133 | } | ||
134 | |||
135 | file_size = rb->lseek(fd, 0, SEEK_END); | ||
136 | |||
137 | return true; | ||
138 | } | ||
139 | |||
140 | return false ; | ||
141 | } | ||
142 | |||
143 | /* this is the plugin entry point */ | ||
144 | enum plugin_status plugin_start(struct plugin_api* api, void* parameter) | ||
145 | { | ||
146 | int ok; | ||
147 | char *filename = parameter; | ||
148 | char *p; | ||
149 | |||
150 | TEST_PLUGIN_API(api); | ||
151 | |||
152 | rb = api; | ||
153 | |||
154 | /* Check the extension. We only allow .m3u files. */ | ||
155 | if(!strpcasecmp(&filename[rb->strlen(filename)-4], ".m3u")) { | ||
156 | rb->splash(HZ, true, "Not a .m3u file"); | ||
157 | return PLUGIN_ERROR; | ||
158 | } | ||
159 | |||
160 | rb->strcpy(path, filename); | ||
161 | |||
162 | p = rb->strrchr(path, '/'); | ||
163 | if(p) | ||
164 | *p = 0; | ||
165 | |||
166 | rb->snprintf(resultfile, MAX_PATH, "%s/search_result.m3u", path, p+1); | ||
167 | ok = search_init(parameter); | ||
168 | if (!ok) { | ||
169 | return PLUGIN_ERROR; | ||
170 | } | ||
171 | search_buffer(); | ||
172 | |||
173 | rb->lcd_clear_display(); | ||
174 | rb->splash(HZ, true, "Done"); | ||
175 | rb->close(fdw); | ||
176 | rb->close(fd); | ||
177 | |||
178 | /* We fake a USB connection to force a reload of the file browser */ | ||
179 | return PLUGIN_USB_CONNECTED; | ||
180 | } | ||
diff --git a/apps/plugins/viewers.config b/apps/plugins/viewers.config index 2acbe17af3..2b1d52766b 100644 --- a/apps/plugins/viewers.config +++ b/apps/plugins/viewers.config | |||
@@ -4,3 +4,4 @@ jpg,jpeg.rock,18 24 3C 3C 24 18 | |||
4 | ucl,rockbox_flash.rock,2A 7F 41 41 7F 2A | 4 | ucl,rockbox_flash.rock,2A 7F 41 41 7F 2A |
5 | rvf,video.rock,5D 7F 5D 7F 5D 7F | 5 | rvf,video.rock,5D 7F 5D 7F 5D 7F |
6 | mp3,vbrfix.rock,10 08 58 38 04 02 | 6 | mp3,vbrfix.rock,10 08 58 38 04 02 |
7 | m3u,search.rock,00 00 00 00 00 00 | ||