Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
FileReader.h
Go to the documentation of this file.
1/**
2 * @file FileReader.h
3 * @ingroup mnd_io
4 * @brief Thin wrapper around std::ifstream for reading a single file.
5 *
6 * Construct with an absolute or relative path, check Exists(), then call
7 * whichever read method fits the caller's needs. FileSystem::LoadFile() and
8 * FileSystem::LoadAssetFileText() are higher-level alternatives that handle
9 * asset-path resolution automatically.
10 *
11 * @see FileSystem
12 */
13
14#pragma once
15
16#include <filesystem>
17#include <fstream>
18#include <sstream>
19#include <string>
20#include <vector>
21
22#include "Log.h"
23#include "Types.h"
24
25namespace mnd
26{
27
28/**
29 * @brief Read helper bound to a single file path.
30 *
31 * All read methods open, read, and close the file on each call — there is no
32 * persistent file handle. Check Exists() before reading to avoid error logs.
33 */
35{
36private:
37 std::string m_filePath; ///< Absolute or relative path to the target file.
38
39public:
40 /**
41 * @brief Bind the reader to the given file path.
42 * @param path Absolute or relative path to the file.
43 */
44 explicit FileReader(const std::string &path)
45 : m_filePath(path)
46 {
47 }
48
49 /**
50 * @brief Check whether the file exists on disk.
51 * @return True if the path resolves to a regular file.
52 */
53 bool Exists();
54
55 /**
56 * @brief Read the file line-by-line.
57 * @return Vector of lines, each without a trailing newline.
58 */
59 std::vector<std::string> ReadLines();
60
61 /**
62 * @brief Read the entire file into a single string.
63 * @return File contents as a UTF-8 string.
64 */
65 std::string ReadToString();
66
67 /**
68 * @brief Read the file as raw bytes.
69 * @return Each byte of the file represented as a u64 value.
70 */
71 std::vector<u64> ReadToBytes();
72
73 /**
74 * @brief Return the file size in bytes.
75 * @return Number of bytes, or 0 if the file cannot be opened.
76 */
77 u64 GetSize();
78
79 /**
80 * @brief Return the path this reader was constructed with.
81 * @return The path string passed to the constructor.
82 */
83 std::string GetPath();
84};
85
86} // namespace mnd
Console logging macros for all engine and game code.
Engine-wide primitive type aliases and GLM math imports.
Read helper bound to a single file path.
Definition FileReader.h:35
std::vector< u64 > ReadToBytes()
Read the file as raw bytes.
std::string GetPath()
Return the path this reader was constructed with.
FileReader(const std::string &path)
Bind the reader to the given file path.
Definition FileReader.h:44
std::string ReadToString()
Read the entire file into a single string.
u64 GetSize()
Return the file size in bytes.
std::vector< std::string > ReadLines()
Read the file line-by-line.
bool Exists()
Check whether the file exists on disk.
Definition FileReader.cpp:7
uint64_t u64
Unsigned 64-bit integer.
Definition Types.h:33