mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-05 07:07:38 +03:00
65 lines
1.4 KiB
C++
65 lines
1.4 KiB
C++
#pragma once
|
|
#include <SdFat.h>
|
|
|
|
#include <iostream>
|
|
|
|
namespace serialization {
|
|
template <typename T>
|
|
static void writePod(std::ostream& os, const T& value) {
|
|
os.write(reinterpret_cast<const char*>(&value), sizeof(T));
|
|
}
|
|
|
|
template <typename T>
|
|
static void writePod(FsFile& 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(FsFile& 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(FsFile& 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);
|
|
if (len > 4096) {
|
|
s = "";
|
|
return;
|
|
}
|
|
s.resize(len);
|
|
if (len > 0) {
|
|
is.read(&s[0], len);
|
|
}
|
|
}
|
|
|
|
static void readString(FsFile& file, std::string& s) {
|
|
uint32_t len;
|
|
readPod(file, len);
|
|
if (len > 4096) {
|
|
s = "";
|
|
return;
|
|
}
|
|
s.resize(len);
|
|
if (len > 0) {
|
|
file.read(&s[0], len);
|
|
}
|
|
}
|
|
} // namespace serialization
|