arduino-audio-tools
Loading...
Searching...
No Matches
VFS.h
1#pragma once
2#include "AudioTools/CoreAudio/AudioBasic/Str.h"
3#include "VFSFile.h"
4
5namespace audio_tools {
6
7/***
8 * @brief Base class which uses c++ file functions. It is also used as base
9 * class for an ESP32 Virtual File System. After initializing the VFS the
10 * regular c file operations are supported.
11 * @ingroup player
12 * @author Phil Schatzmann
13 * @copyright GPLv3
14 */
15class VFS {
16 public:
18 virtual bool begin() { return true; }
20 virtual void end() {}
22 virtual void setMountPoint(const char* mp) { mount_point = mp; }
23
24 VFSFile open(const char* file, FileMode mode = VFS_FILE_READ) {
25 VFSFile vfs_file;
26 vfs_file.open(expand(file), mode);
27 return vfs_file;
28 }
29 VFSFile open(const std::string& path, FileMode mode = VFS_FILE_READ) {
30 const char* path_str = path.c_str();
31 const char* path_str_exanded = expand(path_str);
32 LOGI("open: %s", path_str_exanded);
33 return this->open(path_str_exanded, mode);
34 }
35 bool exists(const char* path) {
36 struct stat buffer;
37 return (stat(expand(path), &buffer) == 0);
38 }
39 bool exists(const std::string& path) { return exists(path.c_str()); }
40 bool remove(const char* path) { return ::remove(expand(path)) == 0; }
41 bool remove(const std::string& path) { return remove(path.c_str()); }
42 bool rename(const char* pathFrom, const char* pathTo) {
43 return ::rename(expand(pathFrom), expand(pathTo)) == 0;
44 }
45 bool rename(const std::string& pathFrom, const std::string& pathTo) {
46 return rename(pathFrom.c_str(), pathTo.c_str());
47 }
48 bool mkdir(const char* path) { return ::mkdir(expand(path), 0777) == 0; }
49 bool mkdir(const std::string& path) { return mkdir(path.c_str()); }
50 bool rmdir(const char* path) { return ::rmdir(expand(path)) == 0; }
51 bool rmdir(const std::string& path) { return rmdir(path.c_str()); }
52
54 const char* mountPoint() { return mount_point; }
55
56 protected:
57 const char* mount_point = "/";
58
59 Str tmp;
60
62 const char* expand(const char* file) {
63 assert(mount_point != nullptr);
64 assert(file != nullptr);
65 tmp = mount_point;
66 if (!StrView(file).startsWith("/") && !StrView(mount_point).endsWith("/")) {
67 tmp.add("/");
68 }
69 tmp.add(file);
70 return tmp.c_str();
71 }
72};
73
74} // 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:15
virtual void setMountPoint(const char *mp)
provide the mount point (root directory for the file system)
Definition VFS.h:22
virtual bool begin()
mount the file systems
Definition VFS.h:18
const char * expand(const char *file)
expands the file name with the mount point
Definition VFS.h:62
virtual void end()
unmount the file system
Definition VFS.h:20
const char * mountPoint()
provides the actual mount point
Definition VFS.h:54
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10