A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 171 lines 4.3 kB view raw
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 * 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. 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 28 29 30ssize_t buf_size; 31static char *filename; 32static int readsize; 33static char *stringbuffer; 34static char crlf[2] __NONSTRING = "\r\n"; 35 36int read_buffer(int offset) 37{ 38 int fd; 39 40 fd = rb->open(filename, O_RDONLY); 41 if(fd < 0) 42 return 10 * fd - 1; 43 44 /* Fill the buffer from the file */ 45 rb->lseek(fd, offset, SEEK_SET); 46 readsize = rb->read(fd, stringbuffer, buf_size); 47 rb->close(fd); 48 49 if(readsize < 0) 50 return readsize * 10 - 2; 51 52 if(readsize == buf_size) 53 return buf_size; /* File too big */ 54 55 return 0; 56} 57 58static int write_file(void) 59{ 60 char tmpfilename[MAX_PATH+1]; 61 int fd; 62 int rc; 63 char *buf_ptr; 64 char *str_begin; 65 66 /* Create a temporary file */ 67 68 rb->snprintf(tmpfilename, MAX_PATH+1, "%s.tmp", filename); 69 70 fd = rb->creat(tmpfilename, 0666); 71 if(fd < 0) 72 return 10 * fd - 1; 73 74 /* Let's make sure it always writes CR/LF and not only LF */ 75 buf_ptr = stringbuffer; 76 str_begin = stringbuffer; 77 do { 78 /* Transform slashes into backslashes */ 79 if(*buf_ptr == '/') 80 *buf_ptr = '\\'; 81 82 if((*buf_ptr == '\r') || (*buf_ptr == '\n')) { 83 /* We have no complete string ? It's only a leading \n or \r ? */ 84 if (!str_begin) 85 continue; 86 87 /* Terminate string */ 88 *buf_ptr = 0; 89 90 /* Write our new string */ 91 rc = rb->write(fd, str_begin, rb->strlen(str_begin)); 92 if(rc < 0) { 93 rb->close(fd); 94 return 10 * rc - 2; 95 } 96 /* Write CR/LF */ 97 rc = rb->write(fd, crlf, 2); 98 if(rc < 0) { 99 rb->close(fd); 100 return 10 * rc - 3; 101 } 102 103 /* Reset until we get a new line */ 104 str_begin = NULL; 105 106 } 107 else { 108 /* We start a new line here */ 109 if (!str_begin) 110 str_begin = buf_ptr; 111 } 112 113 /* Next char, until ... */ 114 } while(buf_ptr++ < stringbuffer + readsize); 115 116 rb->close(fd); 117 118 /* Remove the original file */ 119 rc = rb->remove(filename); 120 if(rc < 0) { 121 return 10 * rc - 4; 122 } 123 124 /* Replace the old file with the new */ 125 rc = rb->rename(tmpfilename, filename); 126 if(rc < 0) { 127 return 10 * rc - 5; 128 } 129 130 return 0; 131} 132 133enum plugin_status plugin_start(const void* parameter) 134{ 135 char *buf; 136 int rc; 137 if(!parameter) return PLUGIN_ERROR; 138 filename = (char *)parameter; 139 140 buf = rb->plugin_get_audio_buffer((size_t *)&buf_size); /* start munching memory */ 141 142 stringbuffer = buf; 143 144 FOR_NB_SCREENS(i) 145 rb->screens[i]->clear_display(); 146 rb->splash(0, "Converting..."); 147 148 rc = read_buffer(0); 149 FOR_NB_SCREENS(i) 150 rb->screens[i]->clear_display(); 151 if(rc == 0) { 152 rb->splash(0, "Writing..."); 153 rc = write_file(); 154 155 FOR_NB_SCREENS(i) 156 rb->screens[i]->clear_display(); 157 if(rc < 0) { 158 rb->splashf(HZ, "Can't write file: %d", rc); 159 } else { 160 rb->splash(HZ, "Done"); 161 } 162 } else { 163 if(rc < 0) { 164 rb->splashf(HZ, "Can't read file: %d", rc); 165 } else { 166 rb->splash(HZ, "The file is too big"); 167 } 168 } 169 170 return PLUGIN_OK; 171}