2019-08-17 23:26:17 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-09-08 18:00:09 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2019-08-17 23:26:17 +02:00
|
|
|
|
2019-08-18 19:41:19 +02:00
|
|
|
#include <vector>
|
2019-08-17 23:26:17 +02:00
|
|
|
|
2019-08-18 19:41:19 +02:00
|
|
|
|
|
|
|
class VectorAdaptor {
|
|
|
|
public:
|
|
|
|
VectorAdaptor(std::vector<uint8_t> * v) : v_(v) {}
|
|
|
|
|
|
|
|
void write(const char *data, uint32_t size) {
|
|
|
|
v_->insert(v_->end(), data, data + size );
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<uint8_t> * v_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FilePtrAdaptor {
|
2019-08-17 23:26:17 +02:00
|
|
|
public:
|
2019-08-18 19:41:19 +02:00
|
|
|
FilePtrAdaptor(const String &fileName) {
|
2019-08-17 23:26:17 +02:00
|
|
|
static const String baseDirectory(".");
|
|
|
|
auto fullFileName = baseDirectory + fileName;
|
|
|
|
fptr = fopen(fullFileName.c_str(), "wb");
|
2019-09-08 18:00:09 +02:00
|
|
|
if (fptr == NULL) {
|
|
|
|
printf("fopen of %s failed, errno = %d - %s\n", fullFileName.c_str(), errno, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-08-17 23:26:17 +02:00
|
|
|
}
|
|
|
|
|
2019-08-18 19:41:19 +02:00
|
|
|
~FilePtrAdaptor() {
|
2019-08-17 23:26:17 +02:00
|
|
|
fclose(fptr);
|
|
|
|
}
|
2019-08-18 19:41:19 +02:00
|
|
|
FilePtrAdaptor(const FilePtrAdaptor &) = delete;
|
|
|
|
void operator=(const FilePtrAdaptor &) = delete;
|
2019-08-17 23:26:17 +02:00
|
|
|
|
|
|
|
void write(const char *data, uint32_t size) {
|
|
|
|
fwrite(data, size, 1, fptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FILE * fptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class MockStorageWriter {
|
|
|
|
public:
|
|
|
|
MockStorageWriter(const String &fileName) {
|
2019-08-18 19:41:19 +02:00
|
|
|
adaptor_ = new FilePtrAdaptor(fileName);
|
|
|
|
encoder_ = new StreamingMsgPackEncoder <FilePtrAdaptor>(adaptor_);
|
2019-08-17 23:26:17 +02:00
|
|
|
}
|
|
|
|
~MockStorageWriter(){
|
|
|
|
delete adaptor_;
|
|
|
|
delete encoder_;
|
|
|
|
}
|
|
|
|
MockStorageWriter(const MockStorageWriter &) = delete;
|
|
|
|
|
2019-08-18 19:41:19 +02:00
|
|
|
StreamingMsgPackEncoder<FilePtrAdaptor> &encoder() { return *encoder_; }
|
2019-08-17 23:26:17 +02:00
|
|
|
|
|
|
|
private:
|
2019-08-18 19:41:19 +02:00
|
|
|
FilePtrAdaptor * adaptor_;
|
|
|
|
StreamingMsgPackEncoder <FilePtrAdaptor> * encoder_;
|
2019-08-17 23:26:17 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|