FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
env.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <string>
12#include <filesystem>
13
14#include "../std/expected.hpp"
15#include "../macro.hpp"
16#include "reserved.hpp"
17
28namespace ns_reserved::ns_env
29{
30
31namespace
32{
33
34namespace fs = std::filesystem;
35
36}
37
45inline Value<void> write(fs::path const& path_file_binary, std::string_view const& json)
46{
47 uint64_t space_available = ns_reserved::FIM_RESERVED_OFFSET_ENVIRONMENT_END - ns_reserved::FIM_RESERVED_OFFSET_ENVIRONMENT_BEGIN;
48 uint64_t space_required = json.size();
49 return_if(space_available <= space_required, Error("E::Not enough space to fit json data"));
50 Pop(ns_reserved::write(path_file_binary
51 , FIM_RESERVED_OFFSET_ENVIRONMENT_BEGIN
52 , FIM_RESERVED_OFFSET_ENVIRONMENT_END
53 , json.data()
54 , json.size()
55 ), "E::Failed to write data to {}", path_file_binary);
56 return {};
57}
58
65inline Value<std::string> read(fs::path const& path_file_binary)
66{
67 uint64_t offset_begin = ns_reserved::FIM_RESERVED_OFFSET_ENVIRONMENT_BEGIN;
68 uint64_t size = ns_reserved::FIM_RESERVED_OFFSET_ENVIRONMENT_END - offset_begin;
69 auto buffer = std::make_unique<char[]>(size);
70 Pop(ns_reserved::read(path_file_binary, offset_begin, buffer.get(), size)
71 , "E::Failed to read binary data from {}", path_file_binary
72 );
73 return buffer.get();
74}
75
76} // namespace ns_reserved::ns_env
77
78/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
Enhanced error handling framework built on std::expected.
Simplified macros for common control flow patterns with optional logging.
Environment variable storage in reserved space.
Value< void > write(fs::path const &path_file_binary, std::string_view const &json)
Writes the environment json string to the target binary.
Definition env.hpp:45
Value< std::string > read(fs::path const &path_file_binary)
Reads the environment json string from the target binary.
Definition env.hpp:65
Value< void > write(fs::path const &path_file_binary, uint64_t offset_begin, uint64_t offset_end, const char *data, uint64_t length) noexcept
Writes data to a file in binary format.
Definition reserved.hpp:142
Value< std::streamsize > read(fs::path const &path_file_binary, uint64_t offset, char *data, uint64_t length) noexcept
Reads data from a file in binary format.
Definition reserved.hpp:172
Manages reserved space.
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44