swimtracker-firmware/firmware/lib/basic/FilesystemAbstraction.h

243 lines
4.6 KiB
C
Raw Normal View History

2020-06-05 20:55:01 +02:00
#pragma once
2020-06-07 10:59:16 +02:00
#include "Dtypes.h"
2020-06-05 20:55:01 +02:00
#ifdef PLATFORM_ESP32
#include "SPIFFS.h"
namespace portablefs
{
using File = ::File;
2023-08-28 15:06:29 +02:00
using string = String;
template<typename T>
String to_string(const T & value) {
return String(value);
}
2020-06-05 20:55:01 +02:00
class Dir
{
public:
Dir() {}
Dir(const String &path)
: root_(SPIFFS.open(path))
{
//next();
}
bool next()
{
file_ = root_.openNextFile();
return file_;
}
bool isFile()
{
return !file_.isDirectory();
}
bool isDirectory()
{
return file_.isDirectory();
}
String fileName() const
{
return file_.name();
}
size_t fileSize() const
{
return file_.size();
}
private:
File root_;
File file_;
};
inline Dir openDir(const String &path)
{
return Dir(path);
}
inline File open(const char *name, const char *mode)
{
return SPIFFS.open(name, mode);
}
inline bool exists(const char *name)
{
return SPIFFS.exists(name);
}
inline bool remove(const char *name)
{
return SPIFFS.remove(name);
}
inline bool mkdir(const char *name)
{
return SPIFFS.mkdir(name);
}
inline size_t usedBytes()
{
return SPIFFS.usedBytes();
}
inline size_t totalBytes()
{
return SPIFFS.totalBytes();
}
2020-06-05 20:55:01 +02:00
} // namespace portablefs
#endif
2023-08-28 15:06:29 +02:00
2020-06-05 20:55:01 +02:00
#ifdef PLATFORM_NATIVE
#include <string>
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
2023-08-28 15:06:29 +02:00
enum SeekMode {
SeekSet = 0,
SeekCur = 1,
SeekEnd = 2
};
2020-06-05 20:55:01 +02:00
namespace portablefs
{
const std::string basePath = "./base";
2023-08-28 15:06:29 +02:00
using string = std::string;
2020-06-05 20:55:01 +02:00
2023-08-28 15:06:29 +02:00
using std::to_string;
2020-06-05 20:55:01 +02:00
class Dir
{
public:
Dir() {}
Dir(const String &path)
2023-08-28 15:06:29 +02:00
: it_(fs::directory_iterator(path)),
end_(fs::directory_iterator()),
2020-06-05 20:55:01 +02:00
firstIncremented_(false)
{
}
bool next()
{
if (!firstIncremented_)
firstIncremented_ = true;
else
++it_;
return it_ != end_;
}
bool isFile()
{
2023-08-28 15:06:29 +02:00
return it_->is_regular_file();
2020-06-05 20:55:01 +02:00
}
bool isDirectory()
{
2023-08-28 15:06:29 +02:00
return it_->is_directory();
2020-06-05 20:55:01 +02:00
}
String fileName() const
{
2023-08-28 15:06:29 +02:00
return it_->path().filename().string();
2020-06-05 20:55:01 +02:00
}
size_t fileSize() const
{
2023-08-28 15:06:29 +02:00
return it_->file_size();
2020-06-05 20:55:01 +02:00
}
private:
fs::directory_iterator it_;
fs::directory_iterator end_;
bool firstIncremented_;
};
inline Dir openDir(const String &path)
{
return Dir(path);
}
2023-08-28 15:06:29 +02:00
class File
{
public:
File(std::FILE * file) : file_(file) {}
bool seek(uint32_t pos, SeekMode mode)
{
if(mode == SeekSet)
return std::fseek(file_, pos, SEEK_SET) == 0;
else if (mode == SeekCur)
return std::fseek(file_, pos, SEEK_CUR) == 0;
else
return std::fseek(file_, pos, SEEK_END) == 0;
}
bool seek(uint32_t pos) { return std::fseek(file_, pos, SEEK_SET); }
size_t write(const uint8_t *buf, size_t size) {
return std::fwrite(buf, size, 1UL, file_);
}
size_t size() {
auto current_position = ftell(file_);
seek(0, SeekEnd);
auto end = ftell(file_);
seek(0, SeekSet);
auto begin = ftell(file_);
seek(current_position, SeekSet);
return end - begin;
}
private:
std::FILE * file_;
};
2020-06-05 20:55:01 +02:00
inline File open(const char *name, const char *mode)
{
2023-08-28 15:06:29 +02:00
std::string modeStr;
if(modeStr.find("b") == std::string::npos)
modeStr += "b";
return File(std::fopen(name, modeStr.c_str()));
2020-06-05 20:55:01 +02:00
}
inline bool exists(const char *name)
{
2023-08-28 15:06:29 +02:00
return fs::exists(fs::path(name));
2020-06-05 20:55:01 +02:00
}
inline bool remove(const char *name)
{
2023-08-28 15:06:29 +02:00
return fs::remove(fs::path(name));
2020-06-05 20:55:01 +02:00
}
inline bool mkdir(const char *name)
{
2023-08-28 15:06:29 +02:00
return fs::create_directory(fs::path(name));
2020-06-05 20:55:01 +02:00
}
2023-08-28 15:06:29 +02:00
//inline size_t totalBytes() {
// return 1024 * 1024 * 4; // some dummy value
//}
//inline size_t usedBytes() {
// // TODO: makes more sense to report total file size of all files in folder
// return 1024; // some dummy value
//}
2020-06-05 20:55:01 +02:00
} // namespace portablefs
#endif