CoriEngine
Loading...
Searching...
No Matches
FileManager.cpp
Go to the documentation of this file.
1#include "FileManager.hpp"
2
3namespace Cori {
4 namespace FileSystem {
5 std::string FileManager::ReadTextFile(const std::filesystem::path& filepath) {
6 std::ifstream file(filepath, std::ios::in | std::ios::binary);
7 if (!file) {
8 CORI_CORE_ERROR("FileManager: Could not open file: '{0}'", filepath.string());
9 return "";
10 }
11
12 // Try to get file size for pre-allocation (optimization)
13 file.seekg(0, std::ios::end);
14 std::streampos file_size_pos = file.tellg();
15 file.seekg(0, std::ios::beg); // Seek back to the beginning
16
17 // Check for errors after seeking and telling
18 if (file_size_pos == static_cast<std::streampos>(-1) || !file.good()) {
19 CORI_CORE_ERROR("FileManager: Error determining file size or seeking in file: '{0}'", filepath.string());
20 return "";
21 }
22
23 auto file_size = static_cast<std::size_t>(file_size_pos);
24
25 std::string content;
26 if (file_size > 0) {
27 content.reserve(file_size);
28 content.assign(std::istreambuf_iterator<char>(file),
29 std::istreambuf_iterator<char>());
30 }
31 else {
32 content.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
33 }
34
35 if (file.bad() || (file.fail() && !file.eof())) {
36 CORI_CORE_ERROR("FileManager: Error while reading file content: '{0}'", filepath.string());
37 return "";
38 }
39
40 return content;
41 }
42 }
43}
#define CORI_CORE_ERROR(...)
Definition Logger.hpp:1035
static std::string ReadTextFile(const std::filesystem::path &filepath)
Reads any file as a string.
Everything connected to interacting with files and filesystem is in this namespace.
Global engine namespace.