-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathReaderInterface.cpp
More file actions
45 lines (38 loc) · 1.24 KB
/
ReaderInterface.cpp
File metadata and controls
45 lines (38 loc) · 1.24 KB
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
#include "ReaderInterface.hpp"
#include <cstddef>
#include <span>
#include <string>
#include <ystdlib/error_handling/Result.hpp>
#include "ErrorCode.hpp"
using ystdlib::error_handling::Result;
namespace ystdlib::io_interface {
auto ReaderInterface::read_char() -> ystdlib::error_handling::Result<char> {
char c{};
YSTDLIB_ASSERT_NO_ERROR(read({&c, 1}, 1, true));
return c;
}
auto ReaderInterface::read_string(size_t num_bytes)
-> ystdlib::error_handling::Result<std::string> {
std::string str(num_bytes, 0);
YSTDLIB_ASSERT_NO_ERROR(read({str.data(), num_bytes}, 1, true));
return str;
}
auto ReaderInterface::read_to_delimiter(std::span<char> output_buffer, char delim, bool keep_delim)
-> ystdlib::error_handling::Result<size_t> {
size_t num_bytes_read{0};
while (num_bytes_read < output_buffer.size()) {
auto const c{YSTDLIB_TRY(read_char())};
if (delim == c) {
break;
}
output_buffer[num_bytes_read++] = c;
}
if (num_bytes_read == output_buffer.size()) {
return ErrorCode{ErrorCodeEnum::BufferFull};
}
if (keep_delim) {
output_buffer[num_bytes_read++] = delim;
}
return num_bytes_read;
}
} // namespace ystdlib::io_interface