arduino-audio-tools
File.h
1 #pragma once
2 #include "AudioConfig.h"
3 #include <iostream>
4 #include <fstream>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 
8 #ifdef IS_NOARDUINO
9 # define NOARD_OVR override
10 #else
11 # define NOARD_OVR
12 #endif
13 
14 
15 namespace audio_tools {
16 
17 enum FileMode { FILE_READ='r', FILE_WRITE='w', FILE_APPEND='a'};
18 enum SeekMode { SeekSet = 0, SeekCur = 1, SeekEnd = 2 };
19 
25 class File : public Stream {
26  public:
27  File() = default;
28  File(const char* fn) {
29  open(fn, FILE_READ);
30  }
31  File(const File &file){
32  open(file.name(), FILE_READ);
33  }
34 
35  File& operator =(File file){
36  open(file.name(), FILE_READ);
37  return *this;
38  }
39 
40  void open(const char* name, FileMode mode){
41  file_path = name;
42  switch(mode){
43  case FILE_READ:
44  stream.open(name, stream.binary | stream.in);
45  is_read = true;
46  break;
47  case FILE_WRITE:
48  stream.open(name, stream.binary | stream.trunc | stream.out);
49  is_read = false;
50  break;
51  case FILE_APPEND:
52  stream.open(name, stream.binary | stream.out);
53  is_read = false;
54  break;
55  }
56  }
57 
58  virtual bool begin(){
59  // move to beginning
60  return seek(0);
61  }
62 
63  virtual void end() {
64  stream.close();
65  }
66 
67  virtual int print(const char* str) NOARD_OVR {
68  stream << str;
69  return strlen(str);
70  }
71 
72  virtual int println(const char* str="") NOARD_OVR {
73  stream << str << "\n";
74  return strlen(str)+1;
75  }
76 
77  virtual int print(int number) NOARD_OVR {
78  char buffer[80];
79  int len = snprintf(buffer,80, "%d", number);
80  print(buffer);
81  return len;
82  }
83 
84  virtual int println(int number){
85  char buffer[80];
86  int len = snprintf(buffer,80, "%d\n", number);
87  print(buffer);
88  return len;
89  }
90 
91  virtual void flush() override {
92  stream.flush();
93  }
94 
95  virtual void write(uint8_t* str, int len) {
96  stream.write((const char*)str, len);
97  }
98 
99  virtual size_t write(int32_t value){
100  stream.put(value);
101  return 1;
102  }
103 
104  virtual size_t write(uint8_t value)override{
105  stream.put(value);
106  return 1;
107  }
108 
109  virtual int available() override{
110  int result = size()-position();
111  return result;
112  };
113 
114  virtual int read() override{
115  return stream.get();
116  }
117 
118  virtual size_t readBytes(uint8_t* data, size_t len) override {
119  stream.read((char*)data, len);
120  return stream?len : stream.gcount();
121  }
122 
123  virtual int peek() override {
124  return stream.peek();
125  }
126 
127  bool seek(uint32_t pos, SeekMode mode){
128  if (is_read){
129  switch(mode){
130  case SeekSet:
131  stream.seekg(pos, std::ios_base::beg);
132  break;
133  case SeekCur:
134  stream.seekg(pos, std::ios_base::cur);
135  break;
136  case SeekEnd:
137  stream.seekg(pos, std::ios_base::end);
138  break;
139  }
140  } else {
141  switch(mode){
142  case SeekSet:
143  stream.seekp(pos, std::ios_base::beg);
144  break;
145  case SeekCur:
146  stream.seekp(pos, std::ios_base::cur);
147  break;
148  case SeekEnd:
149  stream.seekp(pos, std::ios_base::end);
150  break;
151  }
152  }
153  return stream.fail()==false;
154  }
155 
156  bool seek(uint32_t pos){
157  return seek(pos, SeekSet);
158  }
159 
160  size_t position() {
161  return stream.tellp();
162  }
163 
164  size_t size() const {
165  std::streampos fsize = 0;
166  std::ifstream file( file_path, std::ios::binary );
167 
168  fsize = file.tellg();
169  file.seekg( 0, std::ios::end );
170  fsize = file.tellg() - fsize;
171  file.close();
172 
173  return fsize;
174  }
175 
176  void close() {
177  stream.close();
178  }
179 
180  const char* name() const {
181  return file_path;
182  }
183 
184  operator bool() {
185  return stream.is_open();
186  }
187 
188  protected:
189  std::fstream stream;
190  bool is_read=true;
191  const char* file_path=nullptr;
192 };
193 
199 class FS {
200 public:
201  File open(const char* path, FileMode mode = FILE_READ){
202  File file;
203  file.open(path, mode);
204  return file;
205  }
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);
209  }
210  bool exists(const char* path){
211  struct stat buffer;
212  return (stat (path, &buffer) == 0);
213  }
214  bool exists(const std::string& path){
215  return exists(path.c_str());
216  }
217  bool remove(const char* path) {
218  return ::remove(path)==0;
219  }
220  bool remove(const std::string& path){
221  return remove(path.c_str());
222  }
223  bool rename(const char* pathFrom, const char* pathTo) {
224  return ::rename(pathFrom, pathTo) == 0;
225  }
226  bool rename(const std::string& pathFrom, const std::string& pathTo){
227  return rename(pathFrom.c_str(), pathTo.c_str());
228  }
229  bool mkdir(const char *path) {
230  return ::mkdir(path, 0777)==0;
231  }
232  bool mkdir(const std::string &path){
233  return mkdir(path.c_str());
234  }
235  bool rmdir(const char *path){
236  return ::rmdir(path)==0;
237  }
238  bool rmdir(const std::string &path){
239  return rmdir(path.c_str());
240  }
241 };
242 
243 static FS SD;
244 static FS SDFAT;
245 
246 }
Eumlate FS using C++ or Posix functions.
Definition: File.h:199
Arduino File support using std::fstream.
Definition: File.h:25
Definition: NoArduino.h:125
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823