Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
FileReader.cpp
Go to the documentation of this file.
1
2#include "io/FileReader.h"
3
4namespace mnd
5{
6
8{
9 return std::filesystem::exists(m_filePath);
10}
11
13{
14 if (!Exists()) {
15 LOG_ERROR("Could not open file: %s", m_filePath.c_str());
16 return "";
17 }
18
19 std::ifstream file(m_filePath);
20 std::stringstream stream;
21
22 stream << file.rdbuf();
23
24 file.close();
25 return stream.str();
26}
27
28std::vector<std::string> FileReader::ReadLines()
29{
30 if (!Exists()) {
31 LOG_ERROR("Could not open file: %s", m_filePath.c_str());
32 return {};
33 }
34
35 std::ifstream file(m_filePath);
36 std::string line;
37 std::vector<std::string> buf_lines;
38
39 while (std::getline(file, line)) {
40 buf_lines.push_back(line);
41 }
42
43 return buf_lines;
44}
45
46std::vector<u64> FileReader::ReadToBytes()
47{
48 if (!Exists()) {
49 LOG_ERROR("Could not open file: %s", m_filePath.c_str());
50 return {};
51 }
52
53 std::ifstream file(m_filePath, std::ios::binary);
54 file.seekg(0, std::ios::end);
55 u64 size = file.tellg();
56 file.seekg(0, std::ios::beg);
57
58 std::vector<u64> bytes(size);
59
60 file.read(reinterpret_cast<char *>(bytes.data()), size);
61
62 return bytes;
63}
64
66{
67 if (!Exists()) {
68 LOG_ERROR("Could not open file: %s", m_filePath.c_str());
69 return {};
70 }
71
72 std::ifstream file(m_filePath, std::ios::binary);
73 file.seekg(0, std::ios::end);
74 u64 size = file.tellg();
75
76 return size;
77}
78
80{
81 if (!Exists()) {
82 LOG_ERROR("Could not open file: %s", m_filePath.c_str());
83 return {};
84 }
85
86 return m_filePath;
87}
88
89} // namespace mnd
Thin wrapper around std::ifstream for reading a single file.
#define LOG_ERROR(fmt,...)
Definition Log.h:81
std::vector< u64 > ReadToBytes()
Read the file as raw bytes.
std::string GetPath()
Return the path this reader was constructed with.
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