A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 106 lines 2.9 kB view raw
1/* zenutils - Utilities for working with creative firmwares. 2 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19#include "file.h" 20#include <fstream> 21 22 23bool shared::read_file(const std::string& filename, bytes& buffer, 24 std::streampos offset, std::streamsize count) 25{ 26 std::ifstream ifs; 27 ifs.open(filename.c_str(), std::ios::binary); 28 if (!ifs) 29 { 30 return false; 31 } 32 33 std::ifstream::pos_type startpos = offset; 34 ifs.seekg(offset, std::ios::beg); 35 if (count == -1) 36 ifs.seekg(0, std::ios::end); 37 else 38 ifs.seekg(count, std::ios::cur); 39 std::ifstream::pos_type endpos = ifs.tellg(); 40 41 buffer.resize(endpos-startpos); 42 ifs.seekg(offset, std::ios::beg); 43 44 ifs.read((char*)&buffer[0], endpos-startpos); 45 46 ifs.close(); 47 return ifs.good(); 48} 49 50 51bool shared::write_file(const std::string& filename, bytes& buffer, 52 bool truncate, std::streampos offset, 53 std::streamsize count) 54{ 55 std::ios::openmode mode = std::ios::in|std::ios::out|std::ios::binary; 56 if (truncate) 57 mode |= std::ios::trunc; 58 59 std::fstream ofs; 60 ofs.open(filename.c_str(), mode); 61 if (!ofs) 62 { 63 return false; 64 } 65 66 if (count == -1) 67 count = buffer.size(); 68 else if (count > buffer.size()) 69 return false; 70 71 ofs.seekg(offset, std::ios::beg); 72 73 ofs.write((char*)&buffer[0], count); 74 75 ofs.close(); 76 return ofs.good(); 77} 78 79bool shared::file_exists(const std::string& filename) 80{ 81 std::ifstream ifs; 82 ifs.open(filename.c_str(), std::ios::in); 83 if (ifs.is_open()) 84 { 85 ifs.close(); 86 return true; 87 } 88 return false; 89} 90 91bool shared::copy_file(const std::string& srcname, const std::string& dstname) 92{ 93 bytes buffer; 94 if (!read_file(srcname, buffer)) 95 return false; 96 return write_file(dstname, buffer, true); 97} 98 99bool shared::backup_file(const std::string& filename, bool force) 100{ 101 std::string backupname = filename + ".bak"; 102 if (!force) 103 if (file_exists(backupname)) 104 return true; 105 return copy_file(filename, backupname); 106}