Standardize File handling with FsHelpers (#110)
## Summary * Standardize File handling with FsHelpers * Better central place to manage to logic of if files exist/open for reading/writing
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#pragma once
|
||||
#include <FS.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace serialization {
|
||||
@@ -7,21 +9,44 @@ static void writePod(std::ostream& os, const T& value) {
|
||||
os.write(reinterpret_cast<const char*>(&value), sizeof(T));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void writePod(File& file, const T& value) {
|
||||
file.write(reinterpret_cast<const uint8_t*>(&value), sizeof(T));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void readPod(std::istream& is, T& value) {
|
||||
is.read(reinterpret_cast<char*>(&value), sizeof(T));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void readPod(File& file, T& value) {
|
||||
file.read(reinterpret_cast<uint8_t*>(&value), sizeof(T));
|
||||
}
|
||||
|
||||
static void writeString(std::ostream& os, const std::string& s) {
|
||||
const uint32_t len = s.size();
|
||||
writePod(os, len);
|
||||
os.write(s.data(), len);
|
||||
}
|
||||
|
||||
static void writeString(File& file, const std::string& s) {
|
||||
const uint32_t len = s.size();
|
||||
writePod(file, len);
|
||||
file.write(reinterpret_cast<const uint8_t*>(s.data()), len);
|
||||
}
|
||||
|
||||
static void readString(std::istream& is, std::string& s) {
|
||||
uint32_t len;
|
||||
readPod(is, len);
|
||||
s.resize(len);
|
||||
is.read(&s[0], len);
|
||||
}
|
||||
|
||||
static void readString(File& file, std::string& s) {
|
||||
uint32_t len;
|
||||
readPod(file, len);
|
||||
s.resize(len);
|
||||
file.read(reinterpret_cast<uint8_t*>(&s[0]), len);
|
||||
}
|
||||
} // namespace serialization
|
||||
|
||||
Reference in New Issue
Block a user