blob: d1c7f041daeeb7d732e2e057d3feebc0b37b85e8 [file] [log] [blame]
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +00001/***************************************************************************
2 *
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
10 *
11 * Copyright (C) 2003 Stefan Meyer
12 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000013 * 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.
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +000017 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22#include "plugin.h"
23#include "ctype.h"
24
Jens Arnolda36b1d42006-01-15 18:20:18 +000025PLUGIN_HEADER
26
Steve Bavin65265772008-05-13 09:57:56 +000027static const struct plugin_api* rb;
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +000028
29#define BUFFER_SIZE 16384
30
31static int fd;
32static int fdw;
33
34static int file_size;
35static int results = 0;
36
37static char buffer[BUFFER_SIZE+1];
38static char search_string[60] ;
39
40static int buffer_pos; /* Position of the buffer in the file */
41
42static int line_end; /* Index of the end of line */
43
44char resultfile[MAX_PATH];
45char path[MAX_PATH];
46
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +000047static int strpcasecmp(const char *s1, const char *s2){
48 while (*s1 != '\0' && tolower(*s1) == tolower(*s2)) {
49 s1++;
50 s2++;
51 }
52
53 return (*s1 == '\0');
Linus Nielsen Feltzinga6880a42004-07-26 23:28:37 +000054}
55
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +000056static void fill_buffer(int pos){
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +000057 int numread;
58 int i;
59 int found = false ;
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +000060 const char crlf = '\n';
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +000061
62 if (pos>=file_size-BUFFER_SIZE)
63 pos = file_size-BUFFER_SIZE;
64 if (pos<0)
65 pos = 0;
66
67 rb->lseek(fd, pos, SEEK_SET);
68 numread = rb->read(fd, buffer, BUFFER_SIZE);
69
70 buffer[numread] = 0;
71 line_end = 0;
72
73 for(i=0;i<numread;i++) {
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +000074 switch(buffer[i]) {
75 case '\r':
76 buffer[i] = ' ';
77 break;
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +000078 case '\n':
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +000079 buffer[i] = 0;
80 buffer_pos = pos + i +1 ;
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +000081
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +000082 if (found){
83 /* write to playlist */
84 rb->write(fdw, &buffer[line_end],
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +000085 rb->strlen( &buffer[line_end] ));
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +000086 rb->write(fdw, &crlf, 1);
87
88 found = false ;
89 results++ ;
90 }
91 line_end = i +1 ;
92
93 break;
94
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +000095 default:
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +000096 if (!found && tolower(buffer[i]) == tolower(search_string[0]))
Linus Nielsen Feltzinga6880a42004-07-26 23:28:37 +000097 found = strpcasecmp(&search_string[0],&buffer[i]) ;
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +000098 break;
99 }
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000100 }
Linus Nielsen Feltzinga6880a42004-07-26 23:28:37 +0000101 DEBUGF("\n-------------------\n");
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000102}
103
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +0000104static void search_buffer(void){
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000105 buffer_pos = 0;
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +0000106
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000107 fill_buffer(0);
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +0000108 while ((buffer_pos+1) < file_size)
109 fill_buffer(buffer_pos);
110}
111
Kevin Ferrare7a828b12007-08-07 14:09:12 +0000112static void clear_display(void){
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +0000113 int i;
114 FOR_NB_SCREENS(i){
115 rb->screens[i]->clear_display();
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000116 }
117}
118
Steve Bavin65265772008-05-13 09:57:56 +0000119static bool search_init(const char* file){
Linus Nielsen Feltzinga6880a42004-07-26 23:28:37 +0000120 rb->memset(search_string, 0, sizeof(search_string));
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +0000121
122 if (!rb->kbd_input(search_string,sizeof search_string)){
123 clear_display();
124 rb->splash(0, "Searching...");
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000125 fd = rb->open(file, O_RDONLY);
126 if (fd==-1)
127 return false;
128
Jens Arnold67eb1542007-02-01 23:08:15 +0000129 fdw = rb->creat(resultfile);
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000130
131 if (fdw < 0) {
132#ifdef HAVE_LCD_BITMAP
Jens Arnold4d6374c2007-03-16 21:56:08 +0000133 rb->splash(HZ, "Failed to create result file!");
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000134#else
Jens Arnold4d6374c2007-03-16 21:56:08 +0000135 rb->splash(HZ, "File creation failed");
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000136#endif
137 return false;
138 }
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +0000139
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000140 file_size = rb->lseek(fd, 0, SEEK_END);
141
142 return true;
143 }
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +0000144
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000145 return false ;
146}
147
148/* this is the plugin entry point */
Steve Bavin65265772008-05-13 09:57:56 +0000149enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000150{
151 int ok;
Steve Bavin65265772008-05-13 09:57:56 +0000152 const char *filename = parameter;
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000153 char *p;
154
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000155 rb = api;
156
Jens Arnoldf68362a2007-03-17 09:54:28 +0000157 DEBUGF("%s - %s\n", (char *)parameter, &filename[rb->strlen(filename)-4]);
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000158 /* Check the extension. We only allow .m3u files. */
Jonathan Gordon48d6bfc2007-01-10 08:27:42 +0000159 if(rb->strcasecmp(&filename[rb->strlen(filename)-4], ".m3u") &&
160 rb->strcasecmp(&filename[rb->strlen(filename)-5], ".m3u8")) {
Jens Arnold4d6374c2007-03-16 21:56:08 +0000161 rb->splash(HZ, "Not a .m3u or .m3u8 file");
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000162 return PLUGIN_ERROR;
163 }
164
165 rb->strcpy(path, filename);
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +0000166
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000167 p = rb->strrchr(path, '/');
168 if(p)
169 *p = 0;
170
Jens Arnold79c8a8c2007-03-17 09:02:53 +0000171 rb->snprintf(resultfile, MAX_PATH, "%s/search_result.m3u", path);
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000172 ok = search_init(parameter);
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +0000173 if (!ok)
174 return PLUGIN_ERROR;
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000175 search_buffer();
176
Kevin Ferrare9dddf3b2007-08-07 12:46:26 +0000177 clear_display();
Jens Arnold4d6374c2007-03-16 21:56:08 +0000178 rb->splash(HZ, "Done");
Linus Nielsen Feltzing6d84d3a2004-07-13 14:25:19 +0000179 rb->close(fdw);
180 rb->close(fd);
181
182 /* We fake a USB connection to force a reload of the file browser */
183 return PLUGIN_USB_CONNECTED;
184}