16#include <system_error>
32 while (!text.empty() &&
33 (text.front() ==
' ' || text.front() ==
'\t' || text.front() ==
'\n' || text.front() ==
'\r'))
35 text.remove_prefix(1);
37 while (!text.empty() &&
38 (text.back() ==
' ' || text.back() ==
'\t' || text.back() ==
'\n' || text.back() ==
'\r'))
40 text.remove_suffix(1);
44 const auto [ptr, ec] = std::from_chars(text.data(), text.data() + text.size(), value);
45 if (ec != std::errc())
63 if (openResult.isError())
68 std::array<std::byte, 64> buffer{};
69 auto readResult = fs.
read(openResult.value(), std::span<std::byte>(buffer.data(), buffer.size()));
70 (void)fs.
close(openResult.value());
72 if (readResult.isError())
76 if (readResult.value() == 0)
81 const std::string_view text{
reinterpret_cast<const char *
>(buffer.data()), readResult.value()};
97 if (openResult.isError())
102 const std::string text = std::to_string(value);
103 std::vector<std::byte> bytes(text.size());
104 for (std::size_t i = 0; i < text.size(); ++i)
106 bytes[i] =
static_cast<std::byte
>(text[i]);
109 auto writeResult = fs.
write(openResult.value(), std::span<const std::byte>(bytes.data(), bytes.size()));
110 (void)fs.
close(openResult.value());
112 if (writeResult.isError())
Abstract file-system interface.
virtual Result< std::size_t > read(FileDescriptor fd, std::span< std::byte > buffer)=0
Reads bytes from an open descriptor.
virtual Result< void > close(FileDescriptor fd)=0
Closes an open descriptor.
virtual Result< FileDescriptor > open(const std::string &path, OpenMode mode)=0
Opens a file path with the requested mode.
virtual Result< std::size_t > write(FileDescriptor fd, std::span< const std::byte > data)=0
Writes bytes to an open descriptor.
A result type that holds either a success value of type T or an ErrorCode.
static Result error(ErrorCode code)
Constructs a failed Result with the given error code.
static Result ok(T value)
Constructs a successful Result containing the given value.
Error codes and Result<T> type for fallible operations.
File descriptor types and descriptor-table abstraction.
IFileSystem interface for file-system operations.
Result< RegisterValue > parseRegisterValue(std::string_view text) noexcept
Parses a decimal integer string into a RegisterValue.
Result< RegisterValue > readFileValue(IFileSystem &fs, const std::string &path)
Reads a RegisterValue stored as decimal text from a file.
Result< void > writeFileValue(IFileSystem &fs, const std::string &path, RegisterValue value)
Writes a RegisterValue as decimal text to a file, truncating first.
std::int32_t RegisterValue
Value stored in a CPU register.
Common type aliases, sentinel constants, and forward declarations used throughout the Contur 2 kernel...