persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
IOManager.cpp
Go to the documentation of this file.
1#include "IOManager.hpp"
2#include "core/logger.hpp"
3
4extern std::string fatBasePath;
9
10// TODO: update to not use malloc/free
11void* IOManager::loadToRAM(const std::string& filePath, u32* outSize)
12{
13 // open file
14 std::string path = basePath + filePath;
15 FILE* file = fopen(filePath.c_str(), "rb");
16 if (!file)
17 {
18 if (outSize)
19 {
20 *outSize = 0;
21 }
22 return NULL;
23 }
24
25 // get file size
26 fseek(file, 0, SEEK_END);
27 u32 size = ftell(file);
28 // return to beginning of file
29 rewind(file);
30
31 if (size == 0)
32 {
33 fclose(file);
34 if (outSize)
35 {
36 *outSize = 0;
37 }
38 return NULL;
39 }
40
41 // allocate buffer
42 void* buffer = malloc(size);
43 if (buffer)
44 {
45 fread(buffer, 1, size, file);
46 }
47 else
48 {
49 logMessage(LogSeverity::ERROR, "malloc failed for: " + path);
50 }
51
52 fclose(file);
53
54 if (outSize)
55 {
56 *outSize = size;
57 }
58
59 return buffer;
60}
61
62// TODO: update to not use malloc/free
63void IOManager::unloadFromRAM(void* buffer)
64{
65 // free memory
66 if (buffer)
67 {
68 free(buffer);
69 }
70}
71
72std::string IOManager::getAssetFilePath(const std::string& path, const char* suffix)
73{
74 std::string directPath = basePath + path + suffix;
75
76 FILE* file = fopen(directPath.c_str(), "rb");
77 if (file)
78 {
79 fclose(file);
80 return directPath;
81 }
82
83 size_t end = path.find_last_not_of('/');
84 if (end == std::string::npos)
85 {
86 return directPath;
87 }
88
89 size_t slash = path.find_last_of('/', end);
90 std::string leaf =
91 path.substr(slash == std::string::npos ? 0 : slash + 1, end - (slash == std::string::npos ? 0 : slash + 1) + 1);
92 return path.substr(0, end + 1) + "/" + leaf + suffix;
93}
94
95// TODO: update to not use malloc/free
96void* IOManager::openFile(const std::string& filePath)
97{
98 std::string path = basePath + filePath;
99 FILE* file = fopen(path.c_str(), "rb");
100 if (!file)
101 {
102 return nullptr;
103 }
104
105 //get file size
106 fseek(file, 0, SEEK_END);
107 u32 size = ftell(file);
108 rewind(file);
109
110 if (size == 0)
111 {
112 fclose(file);
113 return nullptr;
114 }
115
116 void* buffer = malloc(size);
117 if (buffer)
118 {
119 fread(buffer, 1, size, file);
120 }
121 else
122 {
123 logMessage(LogSeverity::ERROR, "malloc failed for: " + path);
124 }
125 fclose(file);
126
127 return buffer;
128}
129
130// TODO: update to not use malloc/free
131void* IOManager::openFile(const std::string& filePath, u32& size)
132{
133 std::string path = basePath + filePath;
134 FILE* file = fopen(path.c_str(), "rb");
135 if (!file)
136 {
137 return nullptr;
138 }
139
140 fseek(file, 0, SEEK_END);
141 size = ftell(file);
142 rewind(file);
143
144 if (size == 0)
145 {
146 fclose(file);
147 return nullptr;
148 }
149
150 void* buffer = malloc(size);
151 if (buffer)
152 {
153 fread(buffer, 1, size, file);
154 }
155 else
156 {
157 logMessage(LogSeverity::ERROR, "malloc failed for: " + path);
158 }
159 fclose(file);
160
161 return buffer;
162}
163
164FileBuffer IOManager::openFileBuffer(const std::string& filePath)
165{
166 u32 size = 0;
167 return FileBuffer(openFile(filePath, size), size);
168}
Manager for IO functions.
Owns a heap-allocated file buffer and its length.
Definition IOManager.hpp:23
void * loadToRAM(const std::string &filePath, u32 *outSize)
Loads an entire file into a buffer.
Definition IOManager.cpp:11
std::string basePath
FileBuffer openFileBuffer(const std::string &path)
void unloadFromRAM(void *buffer)
Releases a buffer previously returned by loadToRAM().
Definition IOManager.cpp:63
void * openFile(const std::string &path)
Open a file and return a pointer to its contents.
Definition IOManager.cpp:96
std::string getAssetFilePath(const std::string &path, const char *suffix)
Resolves the on-disk path for an asset, allowing it to be stored either as a flat file or grouped in ...
Definition IOManager.cpp:72
void init() override
Definition IOManager.cpp:5
std::string fatBasePath
Definition main.cpp:38
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Definition logger.cpp:110
Util to handle error logging.