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