swimtracker-firmware/firmware/lib/session/SpiffsStorage.h

90 lines
1.8 KiB
C
Raw Normal View History

#pragma once
#include "StreamingMsgPackEncoder.h"
2020-06-05 20:55:01 +02:00
#include "FilesystemAbstraction.h"
2020-06-05 20:55:01 +02:00
#ifdef USE_ESP32
struct WriterAdaptor
{
File * f;
void write(const char * ptr, size_t size) {
f->write(reinterpret_cast<const uint8_t *>(ptr), size);
}
};
class SpiffsStorageWriter {
public:
SpiffsStorageWriter(const String &fileName) :
f_(SPIFFS.open(fileName, "w")),
adaptor_{&f_},
encoder_(&adaptor_),
fileName_(fileName)
{
bool success = f_;
Serial.println(success);
}
~SpiffsStorageWriter() {
f_.close();
Serial.println(fileName_);
Serial.println(SPIFFS.exists(fileName_));
}
StreamingMsgPackEncoder<WriterAdaptor> &encoder() { return encoder_; }
private:
File f_;
WriterAdaptor adaptor_;
StreamingMsgPackEncoder<WriterAdaptor> encoder_;
String fileName_;
};
#else
class SpiffsStorageWriter {
public:
SpiffsStorageWriter(const String &fileName) :
f_(SPIFFS.open(fileName, "w")),
2019-09-10 22:05:28 +02:00
encoder_(&f_),
fileName_(fileName)
{
bool success = f_;
Serial.println(success);
}
~SpiffsStorageWriter() {
f_.close();
Serial.println(fileName_);
Serial.println(SPIFFS.exists(fileName_));
}
StreamingMsgPackEncoder<File> &encoder() { return encoder_; }
private:
File f_;
StreamingMsgPackEncoder<File> encoder_;
2019-09-10 22:05:28 +02:00
String fileName_;
};
2020-06-05 20:55:01 +02:00
#endif
class SpiffsStorageReader
{
public:
2019-08-22 21:33:36 +02:00
SpiffsStorageReader(const String &fileName) :
2020-05-16 12:33:53 +02:00
f_(SPIFFS.open(fileName, "r"))
{}
2019-09-10 22:05:28 +02:00
~SpiffsStorageReader() {
f_.close();
}
uint32_t readBytes(char *buffer, size_t length) {
return f_.readBytes(buffer, length);
}
bool seek(uint32_t pos) {
return f_.seek(pos);
}
private:
File f_;
};