galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
FileUtils.cpp
Go to the documentation of this file.
1
7
8#include <fstream>
9
10#include <nlohmann/json.hpp>
11#include <SDL3/SDL_misc.h>
12
14
15#include "FileUtils.hpp"
16
17namespace galaxy
18{
19 namespace fileutils
20 {
21 std::optional<std::string> extension(const std::string& filepath) noexcept
22 {
23 const auto path = std::filesystem::path(filepath);
24 if (path.has_extension())
25 {
26 return std::make_optional(path.extension().string());
27 }
28
29 return std::nullopt;
30 }
31
32 std::expected<std::string, FileError> read(const std::string& filepath)
33 {
34 const auto path = std::filesystem::path(filepath);
35 if (!std::filesystem::exists(path))
36 {
37 return std::unexpected(FileError("read", "File does not exist.", path));
38 }
39
40 if (!std::filesystem::is_regular_file(path))
41 {
42 return std::unexpected(FileError("read", "Path is an irregular file or a directory", path));
43 }
44
45 std::ifstream input {path, std::ifstream::in};
46 if (!input.good())
47 {
48 return std::unexpected(FileError("read", "Permission denied.", path));
49 }
50
51 std::string buffer(std::filesystem::file_size(path), '\0');
52 input.read(buffer.data(), buffer.size());
53
54 if (buffer.empty())
55 {
56 return std::unexpected(FileError("read", "Failed to read file.", path));
57 }
58
59 return buffer;
60 }
61
62 std::optional<FileError> write(const std::string& filepath, const std::string& data)
63 {
64 const auto path = std::filesystem::path(filepath);
65 if (std::filesystem::exists(path))
66 {
67 if (std::filesystem::is_directory(path))
68 {
69 return std::make_optional(FileError("write", "Tried to write to a directory not a file.", path));
70 }
71 }
72
73 std::ofstream ofs {path, std::ofstream::out | std::ofstream::trunc};
74 if (!ofs.good())
75 {
76 return std::make_optional(FileError("write", "Permission denied.", path));
77 }
78
79 ofs << data;
80
81 if (ofs.fail())
82 {
83 return std::make_optional(FileError("write", "Failed to write into file.", path));
84 }
85
86 return std::nullopt;
87 }
88
89 std::optional<nlohmann::json> read_json(const std::string& filepath)
90 {
91 const auto read = fileutils::read(filepath);
92 if (read)
93 {
94 return std::make_optional(nlohmann::json::parse(read.value()));
95 }
96 else
97 {
98 read.error().log();
99 return std::nullopt;
100 }
101 }
102
103 bool write_json(const std::string& filepath, const nlohmann::json& json)
104 {
105 const auto write = fileutils::write(filepath, json.dump(4));
106 if (write)
107 {
108 write.value().log();
109 return false;
110 }
111
112 return true;
113 }
114
115 void open_url(const std::string& url) noexcept
116 {
117 if (!SDL_OpenURL(url.c_str()))
118 {
119 GALAXY_LOG(GALAXY_ERROR, "Failed to open url: {0}.", SDL_GetError());
120 }
121 }
122 } // namespace fileutils
123} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
#define GALAXY_ERROR
Definition Log.hpp:24
Stores information about a File I/O error.
Definition FileError.hpp:20
std::optional< FileError > write(const std::string &filepath, const std::string &data)
Writes a non-binary file to disk.
Definition FileUtils.cpp:62
void open_url(const std::string &url) noexcept
Open URL in default webbrowser.
std::optional< std::string > extension(const std::string &filepath) noexcept
Get a file or path's extension.
Definition FileUtils.cpp:21
bool write_json(const std::string &filepath, const nlohmann::json &json)
Write json to disk.
std::expected< std::string, FileError > read(const std::string &filepath)
Read a non-binary file on disk.
Definition FileUtils.cpp:32
std::optional< nlohmann::json > read_json(const std::string &filepath)
Read a json file from disk.
Definition FileUtils.cpp:89
Animated.cpp galaxy.
Definition Animated.cpp:16