Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * |
| 3 | * __________ __ ___. |
| 4 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 5 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 6 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 7 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 8 | * \/ \/ \/ \/ \/ |
| 9 | * $Id$ |
| 10 | * |
| 11 | * Copyright (C) 2003 Stefan Meyer |
| 12 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 17 | * |
| 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 Arnold | a36b1d4 | 2006-01-15 18:20:18 +0000 | [diff] [blame] | 25 | PLUGIN_HEADER |
| 26 | |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 27 | static const struct plugin_api* rb; |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 28 | |
| 29 | #define BUFFER_SIZE 16384 |
| 30 | |
| 31 | static int fd; |
| 32 | static int fdw; |
| 33 | |
| 34 | static int file_size; |
| 35 | static int results = 0; |
| 36 | |
| 37 | static char buffer[BUFFER_SIZE+1]; |
| 38 | static char search_string[60] ; |
| 39 | |
| 40 | static int buffer_pos; /* Position of the buffer in the file */ |
| 41 | |
| 42 | static int line_end; /* Index of the end of line */ |
| 43 | |
| 44 | char resultfile[MAX_PATH]; |
| 45 | char path[MAX_PATH]; |
| 46 | |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 47 | static 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 Feltzing | a6880a4 | 2004-07-26 23:28:37 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 56 | static void fill_buffer(int pos){ |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 57 | int numread; |
| 58 | int i; |
| 59 | int found = false ; |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 60 | const char crlf = '\n'; |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 61 | |
| 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 Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 74 | switch(buffer[i]) { |
| 75 | case '\r': |
| 76 | buffer[i] = ' '; |
| 77 | break; |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 78 | case '\n': |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 79 | buffer[i] = 0; |
| 80 | buffer_pos = pos + i +1 ; |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 81 | |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 82 | if (found){ |
| 83 | /* write to playlist */ |
| 84 | rb->write(fdw, &buffer[line_end], |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 85 | rb->strlen( &buffer[line_end] )); |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 86 | rb->write(fdw, &crlf, 1); |
| 87 | |
| 88 | found = false ; |
| 89 | results++ ; |
| 90 | } |
| 91 | line_end = i +1 ; |
| 92 | |
| 93 | break; |
| 94 | |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 95 | default: |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 96 | if (!found && tolower(buffer[i]) == tolower(search_string[0])) |
Linus Nielsen Feltzing | a6880a4 | 2004-07-26 23:28:37 +0000 | [diff] [blame] | 97 | found = strpcasecmp(&search_string[0],&buffer[i]) ; |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 98 | break; |
| 99 | } |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 100 | } |
Linus Nielsen Feltzing | a6880a4 | 2004-07-26 23:28:37 +0000 | [diff] [blame] | 101 | DEBUGF("\n-------------------\n"); |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 104 | static void search_buffer(void){ |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 105 | buffer_pos = 0; |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 106 | |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 107 | fill_buffer(0); |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 108 | while ((buffer_pos+1) < file_size) |
| 109 | fill_buffer(buffer_pos); |
| 110 | } |
| 111 | |
Kevin Ferrare | 7a828b1 | 2007-08-07 14:09:12 +0000 | [diff] [blame] | 112 | static void clear_display(void){ |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 113 | int i; |
| 114 | FOR_NB_SCREENS(i){ |
| 115 | rb->screens[i]->clear_display(); |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 119 | static bool search_init(const char* file){ |
Linus Nielsen Feltzing | a6880a4 | 2004-07-26 23:28:37 +0000 | [diff] [blame] | 120 | rb->memset(search_string, 0, sizeof(search_string)); |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 121 | |
| 122 | if (!rb->kbd_input(search_string,sizeof search_string)){ |
| 123 | clear_display(); |
| 124 | rb->splash(0, "Searching..."); |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 125 | fd = rb->open(file, O_RDONLY); |
| 126 | if (fd==-1) |
| 127 | return false; |
| 128 | |
Jens Arnold | 67eb154 | 2007-02-01 23:08:15 +0000 | [diff] [blame] | 129 | fdw = rb->creat(resultfile); |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 130 | |
| 131 | if (fdw < 0) { |
| 132 | #ifdef HAVE_LCD_BITMAP |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 133 | rb->splash(HZ, "Failed to create result file!"); |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 134 | #else |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 135 | rb->splash(HZ, "File creation failed"); |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 136 | #endif |
| 137 | return false; |
| 138 | } |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 139 | |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 140 | file_size = rb->lseek(fd, 0, SEEK_END); |
| 141 | |
| 142 | return true; |
| 143 | } |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 144 | |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 145 | return false ; |
| 146 | } |
| 147 | |
| 148 | /* this is the plugin entry point */ |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 149 | enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter) |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 150 | { |
| 151 | int ok; |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 152 | const char *filename = parameter; |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 153 | char *p; |
| 154 | |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 155 | rb = api; |
| 156 | |
Jens Arnold | f68362a | 2007-03-17 09:54:28 +0000 | [diff] [blame] | 157 | DEBUGF("%s - %s\n", (char *)parameter, &filename[rb->strlen(filename)-4]); |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 158 | /* Check the extension. We only allow .m3u files. */ |
Jonathan Gordon | 48d6bfc | 2007-01-10 08:27:42 +0000 | [diff] [blame] | 159 | if(rb->strcasecmp(&filename[rb->strlen(filename)-4], ".m3u") && |
| 160 | rb->strcasecmp(&filename[rb->strlen(filename)-5], ".m3u8")) { |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 161 | rb->splash(HZ, "Not a .m3u or .m3u8 file"); |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 162 | return PLUGIN_ERROR; |
| 163 | } |
| 164 | |
| 165 | rb->strcpy(path, filename); |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 166 | |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 167 | p = rb->strrchr(path, '/'); |
| 168 | if(p) |
| 169 | *p = 0; |
| 170 | |
Jens Arnold | 79c8a8c | 2007-03-17 09:02:53 +0000 | [diff] [blame] | 171 | rb->snprintf(resultfile, MAX_PATH, "%s/search_result.m3u", path); |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 172 | ok = search_init(parameter); |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 173 | if (!ok) |
| 174 | return PLUGIN_ERROR; |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 175 | search_buffer(); |
| 176 | |
Kevin Ferrare | 9dddf3b | 2007-08-07 12:46:26 +0000 | [diff] [blame] | 177 | clear_display(); |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 178 | rb->splash(HZ, "Done"); |
Linus Nielsen Feltzing | 6d84d3a | 2004-07-13 14:25:19 +0000 | [diff] [blame] | 179 | 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 | } |