Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
filesystem.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <expected>
6#include <filesystem>
7#include <fstream>
8#include <ios>
9#include <string>
10#include <string_view>
11
12namespace helios::utils {
13
14enum class FileError : uint8_t { kCouldNotOpen, kReadError };
15
16/**
17 * @brief Converts FileError to a human-readable string.
18 * @param error The FileError to convert
19 * @return A string view representing the error
20 */
21[[nodiscard]] constexpr std::string_view FileErrorToString(
22 FileError error) noexcept {
23 switch (error) {
25 return "Could not open file";
27 return "Could not read file";
28 default:
29 return "Unknown file error";
30 }
31}
32
33/**
34 * @brief Reads the entire contents of a file into a string.
35 * @param filepath The path to the file
36 * @return An expected containing the file contents or a FileError
37 */
38[[nodiscard]] inline auto ReadFileToString(std::string_view filepath)
39 -> std::expected<std::string, FileError> {
40 if (filepath.empty()) {
41 return std::unexpected(FileError::kCouldNotOpen);
42 }
43
44 std::ifstream in(filepath.data(), std::ios::in | std::ios::binary);
45 if (!in) {
46 return std::unexpected(FileError::kCouldNotOpen);
47 }
48
49 std::string result;
50 in.seekg(0, std::ios::end);
51 const auto pos = in.tellg();
52 if (pos == std::ifstream::pos_type(-1)) {
53 return std::unexpected(FileError::kReadError);
54 }
55
56 const auto size = static_cast<size_t>(pos);
57 result.resize(size);
58 in.seekg(0, std::ios::beg);
59 in.read(result.data(), static_cast<std::streamsize>(result.size()));
60 if (!in) {
61 return std::unexpected(FileError::kReadError);
62 }
63 in.close();
64
65 return result;
66}
67
68/**
69 * @brief Reads the entire contents of a file into a string.
70 * @param filepath The path to the file
71 * @return An expected containing the file contents or a FileError
72 */
73[[nodiscard]] inline auto ReadFileToString(
74 const std::filesystem::path& filepath)
75 -> std::expected<std::string, FileError> {
76 if (filepath.empty()) {
77 return std::unexpected(FileError::kCouldNotOpen);
78 }
79
80 std::ifstream in(filepath, std::ios::in | std::ios::binary);
81 if (!in) {
82 return std::unexpected(FileError::kCouldNotOpen);
83 }
84
85 std::string result;
86 in.seekg(0, std::ios::end);
87 const auto pos = in.tellg();
88 if (pos == std::ifstream::pos_type(-1)) {
89 return std::unexpected(FileError::kReadError);
90 }
91
92 const auto size = static_cast<size_t>(pos);
93 result.resize(size);
94 in.seekg(0, std::ios::beg);
95 in.read(result.data(), static_cast<std::streamsize>(result.size()));
96 if (!in) {
97 return std::unexpected(FileError::kReadError);
98 }
99 in.close();
100
101 return result;
102}
103
104/**
105 * @brief Extracts the file name from a given path.
106 * @param path The full file path
107 * @return The file name
108 */
109[[nodiscard]] constexpr std::string_view GetFileName(std::string_view path) {
110 const size_t last_slash = path.find_last_of("/\\");
111 return (last_slash != std::string_view::npos) ? path.substr(last_slash + 1)
112 : path;
113}
114
115/**
116 * @brief Extracts the file extension from a given path.
117 * @param path The full file path
118 * @return The file extension, including the dot (e.g., ".txt"), or an empty
119 * string if none exists
120 */
121[[nodiscard]] static constexpr std::string_view GetFileExtension(
122 std::string_view path) {
123 const size_t last_dot = path.find_last_of('.');
124 return (last_dot != std::string_view::npos) ? path.substr(last_dot) : "";
125}
126
127} // namespace helios::utils
auto ReadFileToString(std::string_view filepath) -> std::expected< std::string, FileError >
Reads the entire contents of a file into a string.
static constexpr std::string_view GetFileExtension(std::string_view path)
Extracts the file extension from a given path.
constexpr std::string_view GetFileName(std::string_view path)
Extracts the file name from a given path.
constexpr std::string_view FileErrorToString(FileError error) noexcept
Converts FileError to a human-readable string.