swimtracker-firmware/session/MockStorage.h

68 lines
1.4 KiB
C++

#pragma once
#include <stdio.h>
#include <stdlib.h>
class Adaptor {
public:
Adaptor(const String &fileName) {
static const String baseDirectory(".");
auto fullFileName = baseDirectory + fileName;
fptr = fopen(fullFileName.c_str(), "wb");
}
~Adaptor() {
fclose(fptr);
}
void write(const char *data, uint32_t size) {
fwrite(data, size, 1, fptr);
}
private:
FILE * fptr;
};
class MockStorageWriter {
public:
MockStorageWriter(const String &fileName) {
adaptor_ = new Adaptor(fileName);
encoder_ = new StreamingMsgPackEncoder <Adaptor>(adaptor_);
}
~MockStorageWriter(){
delete adaptor_;
delete encoder_;
}
MockStorageWriter(const MockStorageWriter &) = delete;
StreamingMsgPackEncoder<Adaptor> &encoder() { return *encoder_; }
private:
Adaptor * adaptor_;
StreamingMsgPackEncoder <Adaptor> * encoder_;
};
class MockStorageReader {
public:
MockStorageReader(const String &fileName)
{
static const String baseDirectory(".");
auto fullFileName = baseDirectory + fileName;
fptr = fopen(fullFileName.c_str(), "rb");
}
uint32_t readBytes(char *buffer, size_t length) {
return fread(buffer, length, 1, fptr);
}
bool seek(uint32_t pos) {
auto ret = fseek(fptr, pos, SEEK_SET);
return ret == 0;
}
private:
FILE * fptr;
};