-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathReaderInterface.cpp
61 lines (53 loc) · 1.78 KB
/
ReaderInterface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "ReaderInterface.hpp"
#include <cstddef>
#include <span>
#include <string>
#include <ystdlib/error_handling/Result.hpp>
#include "ErrorCode.hpp"
namespace ystdlib::io_interface {
auto ReaderInterface::read(size_t num_bytes) -> Result<std::string> {
std::string str(num_bytes, 0);
auto const num_bytes_read{YSTDLIB_ERROR_HANDLING_TRYX(read({str.data(), num_bytes}))};
str.resize(num_bytes_read);
return str;
}
auto ReaderInterface::read_exact_length(std::span<char> buf) -> Result<void> {
auto const num_bytes_read{YSTDLIB_ERROR_HANDLING_TRYX(read(buf))};
if (0 == num_bytes_read) {
return ErrorCode{ErrorCodeEnum::EndOfStream};
}
if (num_bytes_read < buf.size()) {
return ErrorCode{ErrorCodeEnum::Truncated};
}
return ystdlib::error_handling::success();
}
auto ReaderInterface::read_exact_length(size_t num_bytes) -> Result<std::string> {
std::string str(num_bytes, 0);
YSTDLIB_ERROR_HANDLING_TRYV(read_exact_length({str.data(), num_bytes}));
return str;
}
auto ReaderInterface::read_to_delimiter(char delim, bool keep_delimiter) -> Result<std::string> {
char c{0};
std::string str;
// The first read needs to succeed
c = YSTDLIB_ERROR_HANDLING_TRYX(read_numeric_value<char>());
while (delim != c) {
str += c;
auto const result{read_numeric_value<char>()};
if (result.has_error()) {
auto const error{result.error()};
if (ErrorCode{ErrorCodeEnum::EndOfStream} == error
|| ErrorCode{ErrorCodeEnum::EndOfFile} == error)
{
return str;
}
return error;
}
c = result.value();
}
if (keep_delimiter) {
str += delim;
}
return str;
}
} // namespace ystdlib::io_interface