Kun Yi | 913ba97 | 2019-01-16 11:08:08 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "sys_file.hpp" |
| 4 | |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | #include <cstring> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | using std::size_t; |
| 12 | using std::uint16_t; |
| 13 | using std::uint32_t; |
| 14 | using std::uint8_t; |
| 15 | |
| 16 | using namespace std::string_literals; |
| 17 | |
| 18 | namespace binstore |
| 19 | { |
| 20 | |
| 21 | /* An in-memory file implementation to test read/write operations, which works |
| 22 | * around the problem that Docker image cannot create file in tmpdir. */ |
| 23 | class FakeSysFile : public SysFile |
| 24 | { |
| 25 | public: |
| 26 | size_t readToBuf(size_t pos, size_t count, char* buf) const override |
| 27 | { |
| 28 | auto result = readAsStr(pos, count); |
| 29 | std::copy(result.begin(), result.end(), buf); |
| 30 | return result.size(); |
| 31 | } |
| 32 | |
| 33 | std::string readAsStr(size_t pos, size_t count) const override |
| 34 | { |
| 35 | if (pos >= data_.size()) |
| 36 | { |
| 37 | return ""; |
| 38 | } |
| 39 | |
| 40 | return data_.substr(pos, count); |
| 41 | } |
| 42 | |
| 43 | std::string readRemainingAsStr(size_t pos) const override |
| 44 | { |
| 45 | return readAsStr(pos, data_.size()); |
| 46 | } |
| 47 | |
| 48 | void writeStr(const std::string& data, size_t pos) override |
| 49 | { |
| 50 | if (pos >= data.size()) |
| 51 | { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | data_.resize(pos); |
| 56 | data_.insert(data_.begin() + pos, data.begin(), data.end()); |
| 57 | } |
| 58 | |
| 59 | protected: |
| 60 | std::string data_ = ""s; |
| 61 | }; |
| 62 | |
| 63 | } // namespace binstore |