Contur 2
Educational OS kernel simulator
Loading...
Searching...
No Matches
fs_utils.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <array>
11#include <charconv>
12#include <cstddef>
13#include <span>
14#include <string>
15#include <string_view>
16#include <system_error>
17#include <vector>
18
19#include "contur/core/error.h"
20#include "contur/core/types.h"
21
24
25namespace contur {
26
30 [[nodiscard]] inline Result<RegisterValue> parseRegisterValue(std::string_view text) noexcept
31 {
32 while (!text.empty() &&
33 (text.front() == ' ' || text.front() == '\t' || text.front() == '\n' || text.front() == '\r'))
34 {
35 text.remove_prefix(1);
36 }
37 while (!text.empty() &&
38 (text.back() == ' ' || text.back() == '\t' || text.back() == '\n' || text.back() == '\r'))
39 {
40 text.remove_suffix(1);
41 }
42
43 RegisterValue value = 0;
44 const auto [ptr, ec] = std::from_chars(text.data(), text.data() + text.size(), value);
45 if (ec != std::errc())
46 {
48 }
49 return Result<RegisterValue>::ok(value);
50 }
51
60 [[nodiscard]] inline Result<RegisterValue> readFileValue(IFileSystem &fs, const std::string &path)
61 {
62 auto openResult = fs.open(path, OpenMode::Read);
63 if (openResult.isError())
64 {
65 return Result<RegisterValue>::error(openResult.errorCode());
66 }
67
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());
71
72 if (readResult.isError())
73 {
74 return Result<RegisterValue>::error(readResult.errorCode());
75 }
76 if (readResult.value() == 0)
77 {
79 }
80
81 const std::string_view text{reinterpret_cast<const char *>(buffer.data()), readResult.value()};
82 return parseRegisterValue(text);
83 }
84
94 [[nodiscard]] inline Result<void> writeFileValue(IFileSystem &fs, const std::string &path, RegisterValue value)
95 {
96 auto openResult = fs.open(path, OpenMode::Create | OpenMode::Write | OpenMode::Truncate);
97 if (openResult.isError())
98 {
99 return Result<void>::error(openResult.errorCode());
100 }
101
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)
105 {
106 bytes[i] = static_cast<std::byte>(text[i]);
107 }
108
109 auto writeResult = fs.write(openResult.value(), std::span<const std::byte>(bytes.data(), bytes.size()));
110 (void)fs.close(openResult.value());
111
112 if (writeResult.isError())
113 {
114 return Result<void>::error(writeResult.errorCode());
115 }
116 return Result<void>::ok();
117 }
118
119} // namespace contur
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.
Definition error.h:104
static Result error(ErrorCode code)
Constructs a failed Result with the given error code.
Definition error.h:113
static Result ok(T value)
Constructs a successful Result containing the given value.
Definition error.h:107
Error codes and Result<T> type for fallible operations.
File descriptor types and descriptor-table abstraction.
IFileSystem interface for file-system operations.
Definition block.h:15
Result< RegisterValue > parseRegisterValue(std::string_view text) noexcept
Parses a decimal integer string into a RegisterValue.
Definition fs_utils.h:30
Result< RegisterValue > readFileValue(IFileSystem &fs, const std::string &path)
Reads a RegisterValue stored as decimal text from a file.
Definition fs_utils.h:60
Result< void > writeFileValue(IFileSystem &fs, const std::string &path, RegisterValue value)
Writes a RegisterValue as decimal text to a file, truncating first.
Definition fs_utils.h:94
std::int32_t RegisterValue
Value stored in a CPU register.
Definition types.h:21
Common type aliases, sentinel constants, and forward declarations used throughout the Contur 2 kernel...