blob: a45f881a5540dc2b74b799075f22e444cc3c25ee [file] [log] [blame]
Kun Yi9125e632019-01-09 13:52:06 -08001#pragma once
2
Kun Yi9125e632019-01-09 13:52:06 -08003#include <fcntl.h>
4#include <unistd.h>
5
6#include <string>
7
8namespace binstore
9{
10
11/**
12 * @brief Represents a file that supports read/write semantics
13 * TODO: leverage stdplus's support for smart file descriptors when it's ready.
14 */
15class SysFile
16{
17 public:
18 virtual ~SysFile() = default;
19
20 /**
21 * @brief Reads content at pos to char* buffer
22 * @param pos The byte pos into the file to read from
23 * @param count How many bytes to read
24 * @param buf Output data
25 * @returns The size of data read
26 * @throws std::system_error if read operation cannot be completed
27 */
28 virtual size_t readToBuf(size_t pos, size_t count, char* buf) const = 0;
29
30 /**
31 * @brief Reads content at pos
32 * @param pos The byte pos into the file to read from
33 * @param count How many bytes to read
34 * @returns The data read in a vector, whose size might be smaller than
Kun Yic83d2fa2019-04-03 18:40:19 -070035 * count if there is not enough to read. Might be empty if the
36 * count specified is too large to even fit in a std::string.
Kun Yi9125e632019-01-09 13:52:06 -080037 * @throws std::system_error if read operation cannot be completed
Kun Yic83d2fa2019-04-03 18:40:19 -070038 * std::bad_alloc if cannot construct string with 'count' size
Kun Yi9125e632019-01-09 13:52:06 -080039 */
40 virtual std::string readAsStr(size_t pos, size_t count) const = 0;
41
42 /**
43 * @brief Reads all the content in file after pos
44 * @param pos The byte pos to read from
45 * @returns The data read in a vector, whose size might be smaller than
46 * count if there is not enough to read.
47 * @throws std::system_error if read operation cannot be completed
48 */
49 virtual std::string readRemainingAsStr(size_t pos) const = 0;
50
51 /**
52 * @brief Writes all of data into file at pos
53 * @param pos The byte pos to write
54 * @returns void
55 * @throws std::system_error if write operation cannot be completed or
56 * not all of the bytes can be written
57 */
58 virtual void writeStr(const std::string& data, size_t pos) = 0;
59};
60
Kun Yi9125e632019-01-09 13:52:06 -080061} // namespace binstore