A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 172 lines 4.1 kB view raw
1/*************************************************************************** 2 * __________ __ ___. 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7 * \/ \/ \/ \/ \/ 8 * $Id$ 9 * 10 * Copyright (C) 2014 Franklin Wei, Benjamin Brown 11 * Copyright (C) 2004 Gregory Montoir 12 * 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. 17 * 18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 * KIND, either express or implied. 20 * 21 ***************************************************************************/ 22 23#include "plugin.h" 24#include "file.h" 25 26void file_create(struct File* f, bool gzipped) { 27 f->gzipped = gzipped; 28 f->fd = -1; 29 f->ioErr = false; 30} 31 32bool file_open(struct File* f, const char *filename, const char *directory, const char *mode) { 33 char buf[512]; 34 rb->snprintf(buf, 512, "%s/%s", directory, filename); 35 char *p = buf + rb->strlen(directory) + 1; 36 string_lower(p); 37 38 int flags = 0; 39 for(int i = 0; mode[i]; ++i) 40 { 41 switch(mode[i]) 42 { 43 case 'w': 44 flags |= O_WRONLY | O_CREAT | O_TRUNC; 45 break; 46 case 'r': 47 flags |= O_RDONLY; 48 break; 49 default: 50 break; 51 } 52 } 53 f->fd = -1; 54 debug(DBG_FILE, "trying %s first", buf); 55 f->fd = rb->open(buf, flags, 0666); 56 if (f->fd < 0) { // let's try uppercase 57 string_upper(p); 58 debug(DBG_FILE, "now trying %s uppercase", buf); 59 f->fd = rb->open(buf, flags, 0666); 60 } 61 if(f->fd > 0) 62 return true; 63 else 64 return false; 65} 66 67void file_close(struct File* f) { 68 if(f->gzipped) 69 { 70 } 71 else 72 { 73 rb->close(f->fd); 74 } 75} 76 77bool file_ioErr(struct File* f) { 78 return f->ioErr; 79} 80 81void file_seek(struct File* f, int32_t off) { 82 if(f->gzipped) 83 { 84 } 85 else 86 { 87 rb->lseek(f->fd, off, SEEK_SET); 88 } 89} 90int file_read(struct File* f, void *ptr, uint32_t size) { 91 if(f->gzipped) 92 { 93 return -1; 94 } 95 else 96 { 97 unsigned int rc = rb->read(f->fd, ptr, size); 98 if(rc != size) 99 f->ioErr = true; 100 return rc; 101 } 102} 103uint8_t file_readByte(struct File* f) { 104 uint8_t b; 105 if(f->gzipped) 106 { 107 b = 0xff; 108 } 109 else 110 { 111 if(rb->read(f->fd, &b, 1) != 1) 112 { 113 f->ioErr = true; 114 debug(DBG_FILE, "file read failed"); 115 } 116 } 117 return b; 118} 119 120uint16_t file_readUint16BE(struct File* f) { 121 uint8_t hi = file_readByte(f); 122 uint8_t lo = file_readByte(f); 123 return (hi << 8) | lo; 124} 125 126uint32_t file_readUint32BE(struct File* f) { 127 uint16_t hi = file_readUint16BE(f); 128 uint16_t lo = file_readUint16BE(f); 129 return (hi << 16) | lo; 130} 131 132int file_write(struct File* f, void *ptr, uint32_t size) { 133 if(f->gzipped) 134 { 135 return 0; 136 } 137 else 138 { 139 return rb->write(f->fd, ptr, size); 140 } 141} 142 143void file_writeByte(struct File* f, uint8_t b) { 144 file_write(f, &b, 1); 145} 146 147void file_writeUint16BE(struct File* f, uint16_t n) { 148 file_writeByte(f, n >> 8); 149 file_writeByte(f, n & 0xFF); 150} 151 152void file_writeUint32BE(struct File* f, uint32_t n) { 153 file_writeUint16BE(f, n >> 16); 154 file_writeUint16BE(f, n & 0xFFFF); 155} 156 157void file_remove(const char* filename, const char* directory) 158{ 159 char buf[512]; 160 rb->snprintf(buf, 512, "%s/%s", directory, filename); 161 char *p = buf + rb->strlen(directory) + 1; 162 string_lower(p); 163 if(rb->file_exists(buf)) 164 { 165 rb->remove(buf); 166 } 167 else 168 { 169 string_upper(p); 170 rb->remove(buf); 171 } 172}