-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileLoader.h
188 lines (140 loc) · 3.87 KB
/
FileLoader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#pragma once
#ifndef FILELOADER_H
#define FILELOADER_H
#include <Windows.h>
#include <iostream>
#include <fstream>
class FileLoader
{
private:
const char * FilePath;
FILE * FilePointer;
char * FileDataBuffer;
UINT FileLength = 0;
UINT BytesReadFromBuffer = 0;
void setLoaderFilePath(const char * FileToRead)
{
FilePath = FileToRead;
}
BOOL getFileHandle(void)
{
errno_t FileOpen = fopen_s(&FilePointer, FilePath, "rb");
if (FileOpen != 0)
{
std::cout << "Failed to open file";
return FALSE;
}
return TRUE;
}
BOOL getFileLength(void)
{
if (!FilePointer)
return FALSE;
if (fseek(FilePointer, 0, SEEK_END) != 0)
{
std::cout << "Failed to set cursor to end of file." << std::endl;
return FALSE;
}
FileLength = ftell(FilePointer);
if (FileLength == -1L)
{
std::cout << "Failed to get length of file (ftell)" << std::endl;
return FALSE;
}
rewind(FilePointer);
return TRUE;
}
void AllocateBufferMemory(void)
{
if (!FilePointer)
return;
FileDataBuffer = new char[FileLength + 1];
}
BOOL loadToMemory(void)
{
if (!FilePointer)
return FALSE;
UINT ReadSize = fread(FileDataBuffer, sizeof(char), FileLength, FilePointer);
if (ReadSize != FileLength)
{
std::cout << "loadToMemory Failed. ReadSize != FileLength";
return FALSE;
}
return TRUE;
}
public:
~FileLoader(void)
{
unloadFile ();
}
FileLoader(void)
{
}
BOOL createLoader(const char * FileToRead)
{
setLoaderFilePath(FileToRead);
if (!getFileHandle() || !getFileLength())
return FALSE;
AllocateBufferMemory();
FileDataBuffer[FileLength] = '\0';
return TRUE;
}
BOOL loadFile(void)
{
if (!loadToMemory())
return FALSE;
std::cout << "Length: " << FileLength << std::endl;
return TRUE;
}
// This function does not unload IFP data, to unload IFP call unloadIFP function
void unloadFile(void)
{
if (FileDataBuffer != nullptr)
{
delete[] FileDataBuffer;
fclose(FilePointer);
FileDataBuffer = nullptr;
}
}
void PrintReadOffset(void)
{
std::cout << "Bytes read from buffer: " << BytesReadFromBuffer << std::endl;
}
template < class T >
void readBuffer ( T * Destination )
{
const UINT ReadOffset = BytesReadFromBuffer;
BytesReadFromBuffer += sizeof ( T );
*Destination = *reinterpret_cast < T * > (FileDataBuffer + ReadOffset);
}
void readBytes(void * Destination, const UINT BytesToRead)
{
const UINT ReadOffset = BytesReadFromBuffer;
BytesReadFromBuffer += BytesToRead;
memcpy(Destination, FileDataBuffer + ReadOffset, BytesToRead);
}
std::string readString(UINT StringSizeInBytes)
{
std::string String;
String.resize(StringSizeInBytes);
for (UINT i = 0; i < StringSizeInBytes; i++)
{
const UINT ReadOffset = BytesReadFromBuffer;
String[i] = FileDataBuffer[ReadOffset];
BytesReadFromBuffer++;
}
return String;
}
void readCString (char * Destination, const UINT BytesToRead)
{
const UINT ReadOffset = BytesReadFromBuffer;
BytesReadFromBuffer += BytesToRead;
memcpy(Destination, FileDataBuffer + ReadOffset, BytesToRead);
*(Destination + (BytesToRead - 1)) = '\0';
}
void skipBytes(UINT TotalBytesToSkip)
{
BytesReadFromBuffer += TotalBytesToSkip;
}
};
#endif