Make open() posix compliant api-wise. A few calls (those with O_CREAT) need the addit...
[kugel-rb.git] / apps / plugins / search.c
blobba16b8821b0e975aa3a2de3e89735c045840011b
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 #define BUFFER_SIZE 16384
29 static int fd;
30 static int fdw;
32 static int file_size, bomsize;
33 static int results = 0;
35 static char buffer[BUFFER_SIZE+1];
36 static char search_string[60] ;
38 static int buffer_pos; /* Position of the buffer in the file */
40 static int line_end; /* Index of the end of line */
42 char resultfile[MAX_PATH];
43 char path[MAX_PATH];
45 static int strpcasecmp(const char *s1, const char *s2){
46 while (*s1 != '\0' && tolower(*s1) == tolower(*s2)) {
47 s1++;
48 s2++;
51 return (*s1 == '\0');
54 static void fill_buffer(int pos){
55 int numread;
56 int i;
57 int found = false ;
58 const char crlf = '\n';
60 rb->lseek(fd, pos+bomsize, SEEK_SET);
61 numread = rb->read(fd, buffer, MIN(BUFFER_SIZE, file_size-pos));
63 buffer[numread] = 0;
64 line_end = 0;
66 for(i=0;i<numread;i++) {
67 switch(buffer[i]) {
68 case '\r':
69 buffer[i] = ' ';
70 break;
71 case '\n':
72 buffer[i] = 0;
73 buffer_pos = pos + i +1 ;
75 if (found){
76 /* write to playlist */
77 rb->write(fdw, &buffer[line_end],
78 rb->strlen( &buffer[line_end] ));
79 rb->write(fdw, &crlf, 1);
81 found = false ;
82 results++ ;
84 line_end = i +1 ;
86 break;
88 default:
89 if (!found && tolower(buffer[i]) == tolower(search_string[0]))
90 found = strpcasecmp(&search_string[0],&buffer[i]) ;
91 break;
94 DEBUGF("\n-------------------\n");
97 static void search_buffer(void){
98 buffer_pos = 0;
100 fill_buffer(0);
101 while ((buffer_pos+1) < file_size)
102 fill_buffer(buffer_pos);
105 static void clear_display(void){
106 int i;
107 FOR_NB_SCREENS(i){
108 rb->screens[i]->clear_display();
112 static bool search_init(const char* file){
113 rb->memset(search_string, 0, sizeof(search_string));
115 if (!rb->kbd_input(search_string,sizeof search_string)){
116 clear_display();
117 rb->splash(0, "Searching...");
118 fd = rb->open_utf8(file, O_RDONLY);
119 if (fd < 0)
120 return false;
122 bomsize = rb->lseek(fd, 0, SEEK_CUR);
123 if (bomsize)
124 fdw = rb->open_utf8(resultfile, O_WRONLY|O_CREAT|O_TRUNC);
125 else
126 fdw = rb->open(resultfile, O_WRONLY|O_CREAT|O_TRUNC, 0666);
128 if (fdw < 0) {
129 #ifdef HAVE_LCD_BITMAP
130 rb->splash(HZ, "Failed to create result file!");
131 #else
132 rb->splash(HZ, "File creation failed");
133 #endif
134 rb->close(fd);
135 return false;
138 file_size = rb->lseek(fd, 0, SEEK_END) - bomsize;
140 return true;
143 return false ;
146 /* this is the plugin entry point */
147 enum plugin_status plugin_start(const void* parameter)
149 int ok;
150 const char *filename = parameter;
151 char *p;
152 if(!parameter) return PLUGIN_ERROR;
154 DEBUGF("%s - %s\n", (char *)parameter, &filename[rb->strlen(filename)-4]);
155 /* Check the extension. We only allow .m3u files. */
156 if (!(p = rb->strrchr(filename, '.')) ||
157 (rb->strcasecmp(p, ".m3u") && rb->strcasecmp(p, ".m3u8")))
159 rb->splash(HZ, "Not a .m3u or .m3u8 file");
160 return PLUGIN_ERROR;
163 rb->strcpy(path, filename);
165 p = rb->strrchr(path, '/');
166 if(p)
167 *p = 0;
169 rb->snprintf(resultfile, MAX_PATH, "%s/search_result.m3u", path);
170 ok = search_init(parameter);
171 if (!ok)
172 return PLUGIN_ERROR;
173 search_buffer();
175 clear_display();
176 rb->splash(HZ, "Done");
177 rb->close(fdw);
178 rb->close(fd);
179 rb->reload_directory();
181 return PLUGIN_OK;