Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / apps / plugins / search.c
blobd1c7f041daeeb7d732e2e057d3feebc0b37b85e8
1 /***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
11 * Copyright (C) 2003 Stefan Meyer
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "plugin.h"
23 #include "ctype.h"
25 PLUGIN_HEADER
27 static const struct plugin_api* rb;
29 #define BUFFER_SIZE 16384
31 static int fd;
32 static int fdw;
34 static int file_size;
35 static int results = 0;
37 static char buffer[BUFFER_SIZE+1];
38 static char search_string[60] ;
40 static int buffer_pos; /* Position of the buffer in the file */
42 static int line_end; /* Index of the end of line */
44 char resultfile[MAX_PATH];
45 char path[MAX_PATH];
47 static int strpcasecmp(const char *s1, const char *s2){
48 while (*s1 != '\0' && tolower(*s1) == tolower(*s2)) {
49 s1++;
50 s2++;
53 return (*s1 == '\0');
56 static void fill_buffer(int pos){
57 int numread;
58 int i;
59 int found = false ;
60 const char crlf = '\n';
62 if (pos>=file_size-BUFFER_SIZE)
63 pos = file_size-BUFFER_SIZE;
64 if (pos<0)
65 pos = 0;
67 rb->lseek(fd, pos, SEEK_SET);
68 numread = rb->read(fd, buffer, BUFFER_SIZE);
70 buffer[numread] = 0;
71 line_end = 0;
73 for(i=0;i<numread;i++) {
74 switch(buffer[i]) {
75 case '\r':
76 buffer[i] = ' ';
77 break;
78 case '\n':
79 buffer[i] = 0;
80 buffer_pos = pos + i +1 ;
82 if (found){
83 /* write to playlist */
84 rb->write(fdw, &buffer[line_end],
85 rb->strlen( &buffer[line_end] ));
86 rb->write(fdw, &crlf, 1);
88 found = false ;
89 results++ ;
91 line_end = i +1 ;
93 break;
95 default:
96 if (!found && tolower(buffer[i]) == tolower(search_string[0]))
97 found = strpcasecmp(&search_string[0],&buffer[i]) ;
98 break;
101 DEBUGF("\n-------------------\n");
104 static void search_buffer(void){
105 buffer_pos = 0;
107 fill_buffer(0);
108 while ((buffer_pos+1) < file_size)
109 fill_buffer(buffer_pos);
112 static void clear_display(void){
113 int i;
114 FOR_NB_SCREENS(i){
115 rb->screens[i]->clear_display();
119 static bool search_init(const char* file){
120 rb->memset(search_string, 0, sizeof(search_string));
122 if (!rb->kbd_input(search_string,sizeof search_string)){
123 clear_display();
124 rb->splash(0, "Searching...");
125 fd = rb->open(file, O_RDONLY);
126 if (fd==-1)
127 return false;
129 fdw = rb->creat(resultfile);
131 if (fdw < 0) {
132 #ifdef HAVE_LCD_BITMAP
133 rb->splash(HZ, "Failed to create result file!");
134 #else
135 rb->splash(HZ, "File creation failed");
136 #endif
137 return false;
140 file_size = rb->lseek(fd, 0, SEEK_END);
142 return true;
145 return false ;
148 /* this is the plugin entry point */
149 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
151 int ok;
152 const char *filename = parameter;
153 char *p;
155 rb = api;
157 DEBUGF("%s - %s\n", (char *)parameter, &filename[rb->strlen(filename)-4]);
158 /* Check the extension. We only allow .m3u files. */
159 if(rb->strcasecmp(&filename[rb->strlen(filename)-4], ".m3u") &&
160 rb->strcasecmp(&filename[rb->strlen(filename)-5], ".m3u8")) {
161 rb->splash(HZ, "Not a .m3u or .m3u8 file");
162 return PLUGIN_ERROR;
165 rb->strcpy(path, filename);
167 p = rb->strrchr(path, '/');
168 if(p)
169 *p = 0;
171 rb->snprintf(resultfile, MAX_PATH, "%s/search_result.m3u", path);
172 ok = search_init(parameter);
173 if (!ok)
174 return PLUGIN_ERROR;
175 search_buffer();
177 clear_display();
178 rb->splash(HZ, "Done");
179 rb->close(fdw);
180 rb->close(fd);
182 /* We fake a USB connection to force a reload of the file browser */
183 return PLUGIN_USB_CONNECTED;