SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
File.cpp
Go to the documentation of this file.
1#include <Core/File.hpp>
2#include <iostream>
3#include <sstream>
4
5#ifdef _WIN32
6#include <windows.h>
7#else
8#include <sys/stat.h>
9#include <unistd.h>
10#endif
11
12namespace Sleak {
13
14 File::File() : m_mode("") {}
15
17 Close();
18 }
19
20 bool File::Open(const std::string& filename, const std::string& mode) {
21 Close(); // Close any previously opened file
22
23 // Set the file stream mode based on the input mode string
24 std::ios_base::openmode streamMode = std::ios_base::in; // Default to input mode
25 if (mode.find('w') != std::string::npos) {
26 streamMode = std::ios_base::out;
27 }
28 if (mode.find('b') != std::string::npos) {
29 streamMode |= std::ios_base::binary;
30 }
31
32 // Open the file stream
33 m_fileStream.open(filename, streamMode);
34 if (!m_fileStream.is_open()) {
35 std::cerr << "Failed to open file: " << filename << std::endl;
36 return false;
37 }
38
39 m_mode = mode;
40 return true;
41 }
42
43 void File::Close() {
44 if (m_fileStream.is_open()) {
45 m_fileStream.close();
46 }
47 }
48
49 std::string File::ReadAllText() {
50 if (!m_fileStream.is_open()) {
51 std::cerr << "File is not open for reading." << std::endl;
52 return "";
53 }
54
55 std::stringstream buffer;
56 buffer << m_fileStream.rdbuf();
57 return buffer.str();
58 }
59
60 std::vector<unsigned char> File::ReadAllBytes() {
61 if (!m_fileStream.is_open()) {
62 std::cerr << "File is not open for reading." << std::endl;
63 return {};
64 }
65
66 // Move the file pointer to the end to determine the file size
67 m_fileStream.seekg(0, std::ios::end);
68 std::streampos fileSize = m_fileStream.tellg();
69 m_fileStream.seekg(0, std::ios::beg);
70
71 // Read the file into a vector
72 std::vector<unsigned char> data(fileSize);
73 m_fileStream.read(reinterpret_cast<char*>(data.data()), fileSize);
74 return data;
75 }
76
77 bool File::WriteAllText(const std::string& content) {
78 if (!m_fileStream.is_open()) {
79 std::cerr << "File is not open for writing." << std::endl;
80 return false;
81 }
82
83 m_fileStream << content;
84 return m_fileStream.good();
85 }
86
87 bool File::WriteAllBytes(const std::vector<unsigned char>& data) {
88 if (!m_fileStream.is_open()) {
89 std::cerr << "File is not open for writing." << std::endl;
90 return false;
91 }
92
93 m_fileStream.write(reinterpret_cast<const char*>(data.data()), data.size());
94 return m_fileStream.good();
95 }
96
97 bool File::Exists(const std::string& filename) {
98 return ExistsImpl(filename);
99 }
100
101 // Platform-specific implementation for file existence check
102 bool File::ExistsImpl(const std::string& filename) {
103#ifdef _WIN32
104 DWORD attribs = GetFileAttributesA(filename.c_str());
105 return (attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY));
106#else
107 struct stat buffer;
108 return (stat(filename.c_str(), &buffer) == 0);
109#endif
110 }
111
112} // namespace Sleak
static bool Exists(const std::string &filename)
Definition File.cpp:97
std::vector< unsigned char > ReadAllBytes()
Definition File.cpp:60
bool WriteAllText(const std::string &content)
Definition File.cpp:77
bool Open(const std::string &filename, const std::string &mode)
Definition File.cpp:20
void Close()
Definition File.cpp:43
std::string ReadAllText()
Definition File.cpp:49
bool WriteAllBytes(const std::vector< unsigned char > &data)
Definition File.cpp:87
Root namespace for everything the engine exposes.
Definition Camera.hpp:10