arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
VFS.h
1#pragma once
2#include "AudioTools/CoreAudio/AudioBasic/Str.h"
3#include "VFSFile.h"
4
5namespace audio_tools {
6
7/***
8 * @brief Abstract Base class which represents an ESP32 Virtual File System.
9 * After initializing the VFS the regular c file operations are supported.
10 * @brief Abstract Base class which represents an ESP32 Virtual File System.
11 * After initializing the VFS the regular c file operations are supported.
12 * @ingroup player
13 * @author Phil Schatzmann
14 * @copyright GPLv3
15 */
16class VFS {
17 public:
19 virtual bool begin() {return true;}
21 virtual void end() {}
23 virtual void setMountPoint(const char* mp) = 0;
24
25 VFSFile open(const char* file, FileMode mode = VFS_FILE_READ) {
26 VFSFile vfs_file;
27 vfs_file.open(expand(file), mode);
28 return vfs_file;
29 }
30 VFSFile open(const std::string& path, FileMode mode = VFS_FILE_READ) {
31 const char* path_str = path.c_str();
32 const char* path_str_exanded = expand(path_str);
33 LOGI("open: %s", path_str_exanded);
34 return this->open(path_str_exanded, mode);
35 }
36 bool exists(const char* path) {
37 struct stat buffer;
38 return (stat(expand(path), &buffer) == 0);
39 }
40 bool exists(const std::string& path) { return exists(path.c_str()); }
41 bool remove(const char* path) { return ::remove(expand(path)) == 0; }
42 bool remove(const std::string& path) { return remove(path.c_str()); }
43 bool rename(const char* pathFrom, const char* pathTo) {
44 return ::rename(expand(pathFrom), expand(pathTo)) == 0;
45 }
46 bool rename(const std::string& pathFrom, const std::string& pathTo) {
47 return rename(pathFrom.c_str(), pathTo.c_str());
48 }
49 bool mkdir(const char* path) { return ::mkdir(expand(path), 0777) == 0; }
50 bool mkdir(const std::string& path) { return mkdir(path.c_str()); }
51 bool rmdir(const char* path) { return ::rmdir(expand(path)) == 0; }
52 bool rmdir(const std::string& path) { return rmdir(path.c_str()); }
53
55 const char* mountPoint() { return mount_point; }
56
57 protected:
58 const char* mount_point = "/";
59
60 Str tmp;
61
63 const char* expand(const char* file) {
64 assert(mount_point != nullptr);
65 assert(file != nullptr);
66 tmp = mount_point;
67 if (!StrView(file).startsWith("/") && !StrView(mount_point).endsWith("/")) {
68 tmp.add("/");
69 }
70 tmp.add(file);
71 return tmp.c_str();
72 }
73};
74
75} // namespace audio_tools
Str which keeps the data on the heap. We grow the allocated memory only if the copy source is not fit...
Definition Str.h:24
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:28
virtual const char * c_str()
provides the string value as const char*
Definition StrView.h:379
virtual void add(int value)
adds a int value
Definition StrView.h:126
Arduino File support using std::fstream.
Definition VFSFile.h:33
Definition VFS.h:16
virtual bool begin()
mount the file systems
Definition VFS.h:19
const char * expand(const char *file)
expands the file name with the mount point
Definition VFS.h:63
virtual void setMountPoint(const char *mp)=0
provide the mount point (root directory for the file system)
virtual void end()
unmount the file system
Definition VFS.h:21
const char * mountPoint()
provides the actual mount point
Definition VFS.h:55
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10