2 #include "AudioConfig.h"
9 # define NOARD_OVR override
17 enum FileMode { FILE_READ=
'r', FILE_WRITE=
'w', FILE_APPEND=
'a'};
18 enum SeekMode { SeekSet = 0, SeekCur = 1, SeekEnd = 2 };
28 File(
const char* fn) {
32 open(file.name(), FILE_READ);
36 open(file.name(), FILE_READ);
40 void open(
const char* name, FileMode mode){
44 stream.open(name, stream.binary | stream.in);
48 stream.open(name, stream.binary | stream.trunc | stream.out);
52 stream.open(name, stream.binary | stream.out);
67 virtual int print(
const char* str) NOARD_OVR {
72 virtual int println(
const char* str=
"") NOARD_OVR {
73 stream << str <<
"\n";
77 virtual int print(
int number) NOARD_OVR {
79 int len = snprintf(buffer,80,
"%d", number);
84 virtual int println(
int number){
86 int len = snprintf(buffer,80,
"%d\n", number);
91 virtual void flush()
override {
95 virtual void write(uint8_t* str,
int len) {
96 stream.write((
const char*)str, len);
99 virtual size_t write(int32_t value){
104 virtual size_t write(uint8_t value)
override{
109 virtual int available()
override{
110 int result = size()-position();
114 virtual int read()
override{
118 virtual size_t readBytes(uint8_t* data,
size_t len)
override {
119 stream.read((
char*)data, len);
120 return stream?len : stream.gcount();
123 virtual int peek()
override {
124 return stream.peek();
127 bool seek(uint32_t pos, SeekMode mode){
131 stream.seekg(pos, std::ios_base::beg);
134 stream.seekg(pos, std::ios_base::cur);
137 stream.seekg(pos, std::ios_base::end);
143 stream.seekp(pos, std::ios_base::beg);
146 stream.seekp(pos, std::ios_base::cur);
149 stream.seekp(pos, std::ios_base::end);
153 return stream.fail()==
false;
156 bool seek(uint32_t pos){
157 return seek(pos, SeekSet);
161 return stream.tellp();
164 size_t size()
const {
165 std::streampos fsize = 0;
166 std::ifstream file( file_path, std::ios::binary );
168 fsize = file.tellg();
169 file.seekg( 0, std::ios::end );
170 fsize = file.tellg() - fsize;
180 const char* name()
const {
185 return stream.is_open();
191 const char* file_path=
nullptr;
201 File open(
const char* path, FileMode mode = FILE_READ){
203 file.open(path, mode);
206 File open(
const std::string &path, FileMode mode = FILE_READ){
207 const char* path_str = path.c_str();
208 return this->open(path_str, mode);
210 bool exists(
const char* path){
212 return (stat (path, &buffer) == 0);
214 bool exists(
const std::string& path){
215 return exists(path.c_str());
217 bool remove(
const char* path) {
218 return ::remove(path)==0;
220 bool remove(
const std::string& path){
221 return remove(path.c_str());
223 bool rename(
const char* pathFrom,
const char* pathTo) {
224 return ::rename(pathFrom, pathTo) == 0;
226 bool rename(
const std::string& pathFrom,
const std::string& pathTo){
227 return rename(pathFrom.c_str(), pathTo.c_str());
229 bool mkdir(
const char *path) {
230 return ::mkdir(path, 0777)==0;
232 bool mkdir(
const std::string &path){
233 return mkdir(path.c_str());
235 bool rmdir(
const char *path){
236 return ::rmdir(path)==0;
238 bool rmdir(
const std::string &path){
239 return rmdir(path.c_str());