arduino-emulator
Loading...
Searching...
No Matches
DirectoryFS.h
1/*
2 DirectoryFS.h
3 Copyright (c) 2026 Phil Schatzmann. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14*/
15#pragma once
16#include <stdlib.h>
17#include <string.h>
18
19#include <cstdio>
20#include <string>
21
22#include "SD.h"
23
29class DirectoryFS {
30 public:
31 DirectoryFS(const char* envName, const char* defaultRoot)
32 : env_name(envName), root(defaultRoot), default_root(defaultRoot) {}
33
35 bool beginRoot() {
36 const char* env = getenv(env_name);
37 root = env != nullptr && env[0] != 0 ? env : default_root;
38 struct stat info;
39 return stat(root.c_str(), &info) == 0 && (info.st_mode & S_IFDIR);
40 }
41
42 void end() {}
43
45 void setRoot(const char* dir) { root = dir; }
46 const char* getRoot() { return root.c_str(); }
47
49 File open(const char* path, const char* mode = "r", bool create = false) {
50 File file;
51 file.open(fullPath(path).c_str(), toFlags(mode));
52 return file;
53 }
54
55 bool exists(const char* path) {
56 struct stat info;
57 return stat(fullPath(path).c_str(), &info) == 0;
58 }
59
60 bool remove(const char* path) { return std::remove(fullPath(path).c_str()) == 0; }
61
62 bool rename(const char* from, const char* to) {
63 return std::rename(fullPath(from).c_str(), fullPath(to).c_str()) == 0;
64 }
65
66 bool mkdir(const char* path) {
67#if defined(_WIN32)
68 return ::_mkdir(fullPath(path).c_str()) == 0;
69#else
70 return ::mkdir(fullPath(path).c_str(), 0777) == 0;
71#endif
72 }
73
74 bool rmdir(const char* path) { return ::rmdir(fullPath(path).c_str()) == 0; }
75
76 bool format() { return false; }
77
78 size_t totalBytes() { return SD.totalBytes(); }
79 size_t usedBytes() { return SD.usedBytes(); }
80
81 protected:
82 const char* env_name;
83 std::string root;
84 const char* default_root;
85
86 std::string fullPath(const char* path) {
87 std::string result = root;
88 if (path == nullptr) return result;
89 if (path[0] != '/') result += "/";
90 result += path;
91 return result;
92 }
93
94 static int toFlags(const char* mode) {
95 if (mode == nullptr || strcmp(mode, "r") == 0 || strcmp(mode, "rb") == 0) return O_RDONLY;
96 if (mode[0] == 'r') return ios::in | ios::out; // r+
97 if (mode[0] == 'w') return mode[1] == '+' ? ios::in | ios::out | ios::trunc : ios::out | ios::trunc;
98 if (mode[0] == 'a') return mode[1] == '+' ? ios::in | ios::out | ios::app : ios::out | ios::app;
99 return O_RDONLY;
100 }
101};
102
void setRoot(const char *dir)
Changes the root directory.
Definition DirectoryFS.h:45
File open(const char *path, const char *mode="r", bool create=false)
Opens a file: mode "r", "w", "a", "r+", "w+" or "a+".
Definition DirectoryFS.h:49
bool beginRoot()
Determines the root directory: the environment variable or the default.
Definition DirectoryFS.h:35
C++ std based emulatoion ofr File.
Definition SD.h:100