Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 Alexandre Bourget |
| 11 | * |
| 12 | * Plugin to transform a Rockbox produced m3u playlist into something |
| 13 | * understandable by the picky original iRiver firmware. |
| 14 | * |
| 15 | * Based on sort.c by the Rockbox team. |
| 16 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License |
| 19 | * as published by the Free Software Foundation; either version 2 |
| 20 | * of the License, or (at your option) any later version. |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 21 | * |
| 22 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 23 | * KIND, either express or implied. |
| 24 | * |
| 25 | ****************************************************************************/ |
| 26 | #include "plugin.h" |
| 27 | |
Jens Arnold | a36b1d4 | 2006-01-15 18:20:18 +0000 | [diff] [blame] | 28 | PLUGIN_HEADER |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 29 | |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 30 | static const struct plugin_api* rb; |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 31 | |
Michael Sevakis | 26d242a | 2007-04-21 18:38:25 +0000 | [diff] [blame] | 32 | ssize_t buf_size; |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 33 | static char *filename; |
| 34 | static int readsize; |
| 35 | static char *stringbuffer; |
| 36 | static char crlf[2] = "\r\n"; |
| 37 | |
| 38 | int read_buffer(int offset) |
| 39 | { |
| 40 | int fd; |
| 41 | |
| 42 | fd = rb->open(filename, O_RDONLY); |
| 43 | if(fd < 0) |
| 44 | return 10 * fd - 1; |
| 45 | |
| 46 | /* Fill the buffer from the file */ |
| 47 | rb->lseek(fd, offset, SEEK_SET); |
| 48 | readsize = rb->read(fd, stringbuffer, buf_size); |
| 49 | rb->close(fd); |
| 50 | |
| 51 | if(readsize < 0) |
| 52 | return readsize * 10 - 2; |
| 53 | |
| 54 | if(readsize == buf_size) |
| 55 | return buf_size; /* File too big */ |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int write_file(void) |
| 61 | { |
| 62 | char tmpfilename[MAX_PATH+1]; |
| 63 | int fd; |
| 64 | int rc; |
| 65 | char *buf_ptr; |
| 66 | char *str_begin; |
| 67 | |
| 68 | /* Create a temporary file */ |
| 69 | |
| 70 | rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename); |
| 71 | |
Jens Arnold | 67eb154 | 2007-02-01 23:08:15 +0000 | [diff] [blame] | 72 | fd = rb->creat(tmpfilename); |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 73 | if(fd < 0) |
| 74 | return 10 * fd - 1; |
| 75 | |
| 76 | /* Let's make sure it always writes CR/LF and not only LF */ |
| 77 | buf_ptr = stringbuffer; |
| 78 | str_begin = stringbuffer; |
| 79 | do { |
| 80 | /* Transform slashes into backslashes */ |
| 81 | if(*buf_ptr == '/') |
| 82 | *buf_ptr = '\\'; |
| 83 | |
| 84 | if((*buf_ptr == '\r') || (*buf_ptr == '\n')) { |
| 85 | /* We have no complete string ? It's only a leading \n or \r ? */ |
| 86 | if (!str_begin) |
| 87 | continue; |
| 88 | |
| 89 | /* Terminate string */ |
| 90 | *buf_ptr = 0; |
| 91 | |
| 92 | /* Write our new string */ |
| 93 | rc = rb->write(fd, str_begin, rb->strlen(str_begin)); |
| 94 | if(rc < 0) { |
| 95 | rb->close(fd); |
| 96 | return 10 * rc - 2; |
| 97 | } |
| 98 | /* Write CR/LF */ |
| 99 | rc = rb->write(fd, crlf, 2); |
| 100 | if(rc < 0) { |
| 101 | rb->close(fd); |
| 102 | return 10 * rc - 3; |
| 103 | } |
| 104 | |
| 105 | /* Reset until we get a new line */ |
| 106 | str_begin = NULL; |
| 107 | |
| 108 | } |
| 109 | else { |
| 110 | /* We start a new line here */ |
| 111 | if (!str_begin) |
| 112 | str_begin = buf_ptr; |
| 113 | } |
| 114 | |
Linus Nielsen Feltzing | 3841348 | 2005-05-27 06:54:16 +0000 | [diff] [blame] | 115 | /* Next char, until ... */ |
| 116 | } while(buf_ptr++ < stringbuffer + readsize); |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 117 | |
| 118 | rb->close(fd); |
| 119 | |
| 120 | /* Remove the original file */ |
| 121 | rc = rb->remove(filename); |
| 122 | if(rc < 0) { |
| 123 | return 10 * rc - 4; |
| 124 | } |
| 125 | |
| 126 | /* Replace the old file with the new */ |
| 127 | rc = rb->rename(tmpfilename, filename); |
| 128 | if(rc < 0) { |
| 129 | return 10 * rc - 5; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 135 | enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter) |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 136 | { |
| 137 | char *buf; |
| 138 | int rc; |
Kevin Ferrare | ac01778 | 2007-07-25 09:38:55 +0000 | [diff] [blame] | 139 | int i; |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 140 | filename = (char *)parameter; |
| 141 | |
| 142 | rb = api; |
| 143 | |
Michael Sevakis | 8676dc2 | 2007-04-21 19:07:15 +0000 | [diff] [blame] | 144 | buf = rb->plugin_get_audio_buffer((size_t *)&buf_size); /* start munching memory */ |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 145 | |
| 146 | stringbuffer = buf; |
| 147 | |
Kevin Ferrare | ac01778 | 2007-07-25 09:38:55 +0000 | [diff] [blame] | 148 | FOR_NB_SCREENS(i) |
| 149 | rb->screens[i]->clear_display(); |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 150 | rb->splash(0, "Converting..."); |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 151 | |
| 152 | rc = read_buffer(0); |
Kevin Ferrare | ac01778 | 2007-07-25 09:38:55 +0000 | [diff] [blame] | 153 | FOR_NB_SCREENS(i) |
| 154 | rb->screens[i]->clear_display(); |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 155 | if(rc == 0) { |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 156 | rb->splash(0, "Writing..."); |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 157 | rc = write_file(); |
| 158 | |
Kevin Ferrare | ac01778 | 2007-07-25 09:38:55 +0000 | [diff] [blame] | 159 | FOR_NB_SCREENS(i) |
| 160 | rb->screens[i]->clear_display(); |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 161 | if(rc < 0) { |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 162 | rb->splash(HZ, "Can't write file: %d", rc); |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 163 | } else { |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 164 | rb->splash(HZ, "Done"); |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 165 | } |
| 166 | } else { |
| 167 | if(rc < 0) { |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 168 | rb->splash(HZ, "Can't read file: %d", rc); |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 169 | } else { |
Jens Arnold | 4d6374c | 2007-03-16 21:56:08 +0000 | [diff] [blame] | 170 | rb->splash(HZ, "The file is too big"); |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
Kevin Ferrare | ac01778 | 2007-07-25 09:38:55 +0000 | [diff] [blame] | 173 | |
Linus Nielsen Feltzing | 2fa2bf6 | 2005-04-07 12:17:14 +0000 | [diff] [blame] | 174 | return PLUGIN_OK; |
| 175 | } |