Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * I/O utility functions.
5 *
6 * General-purpose file and path utilities used across the I/O subsystem:
7 * path resolution, JSON file reading.
8 */
9
10#include <filesystem>
11#include <fstream>
12#include <stdexcept>
13#include <string>
14
15#include <nlohmann/json.hpp>
16
17namespace q3 {
18namespace detail {
19
20/**
21 * Resolve a path relative to a given directory.
22 * Returns the path unchanged if it is already absolute.
23 */
24inline std::string resolve_path(const std::string& base_directory, const std::string& path) {
25 std::filesystem::path path_object(path);
26 if (path_object.is_absolute())
27 return path;
28 return (std::filesystem::path(base_directory) / path_object).string();
29}
30
31/**
32 * Read a JSON file and return the parsed object.
33 */
34inline nlohmann::json read_json_file(const std::string& path) {
35 std::ifstream file_stream(path);
36 if (!file_stream.is_open())
37 throw std::runtime_error("Cannot open JSON file: " + path);
38 return nlohmann::json::parse(file_stream);
39}
40
41} // namespace detail
42} // namespace q3
nlohmann::json read_json_file(const std::string &path)
Definition utils.hpp:34
std::string resolve_path(const std::string &base_directory, const std::string &path)
Definition utils.hpp:24