-
Notifications
You must be signed in to change notification settings - Fork 0
/
GMemFile.cpp
224 lines (187 loc) · 3.5 KB
/
GMemFile.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// GMemFile by fastman92
#include "GMemFile.h"
#include <string>
// Constructor
GMemFile::GMemFile()
{
this -> Ptr = NULL;
this -> RealSize = 0;
this -> SizeLimit = 0;
this -> Pos = NULL;
}
// Opens file from memory.
void GMemFile::Open(void *Ptr, tPosition RealSize, tPosition SizeLimit)
{
this -> Ptr = (char*)Ptr;
this -> RealSize = RealSize;
this -> SizeLimit = SizeLimit;
this -> Pos = NULL;
}
// Closes a file
void GMemFile::Close()
{
}
// Reads NULL terminated string
void GMemFile::ReadNULLterminatedString(char* str)
{
while (GMemFile::Read(str, 1))
{
if (!*str)
break;
str++;
}
}
// Reads Bully aligned string
void GMemFile::Read4byteAlignedNULLterminatedString(char* str)
{
char data[4];
while (this->Read(data, 4))
{
if (data[3])
*(__int32*)str = *(__int32*)data;
else
{
for (int i = 0; i < 4; i++)
{
str[i] = data[i];
if (!str[i])
break;
}
break;
}
str += 4;
}
}
// Reads to memory
GMemFile::tPosition GMemFile::Read(void* Ptr, size_t Size)
{
if(this -> Ptr && this -> Pos < this -> RealSize)
{
tPosition SizeToRead = Size;
if((this -> Pos + Size) > this -> RealSize)
SizeToRead = this -> RealSize - this -> Pos;
memcpy(Ptr, this -> Ptr + this -> Pos, SizeToRead);
this -> Pos += SizeToRead;
return SizeToRead;
}
else
return false;
}
// Reads one ANSI character
char GMemFile::ReadC()
{
if(Ptr)
return this -> Ptr[this -> Pos++];
else
return -1;
}
/*
// Reads one Unicode character
wchar_t GMemFile::ReadWideChar()
{
if(Ptr)
return this -> Ptr[this -> Pos];
else
return -1;
}
*/
// Writes to memory file
void GMemFile::Write(const void* Ptr, size_t Size)
{
tPosition SizeToCopy = this -> Pos + Size > this -> SizeLimit ? this -> SizeLimit - this -> Pos : Size;
memcpy(this -> Ptr + this -> Pos, Ptr, SizeToCopy);
this -> Pos += SizeToCopy;
if(this -> Pos > this -> RealSize)
this -> RealSize = this -> Pos;
}
// Writes string to memory file
void GMemFile::WriteString(const char* str)
{
this->Write(str, strlen(str) + 1);
}
// Writes 4 byte aligned NULL terminated string
bool GMemFile::Writes4byteAlignedNULLterminatedString(const char* str)
{
while (true)
{
char data[4] = { 0 };
for (int i = 0; i < 4; i++)
{
if (str[i])
data[i] = str[i];
else
break;
}
this->Write(data, sizeof(data));
if (!data[3])
break;
str += 4;
}
return true;
}
// Seek to certain position
bool GMemFile::Seek(size_t offset, int origin)
{
bool result = false;
if(origin == SEEK_SET)
{
if(offset >= 0 && offset <= this -> RealSize)
{
this -> Pos = offset;
result = true;
}
}
else if(origin == SEEK_CUR)
{
if(Pos + offset >= 0 && Pos + offset <= this -> RealSize)
{
this -> Pos += offset;
result = true;
}
}
else if(origin == SEEK_END)
{
if(Pos + RealSize + offset >= 0 && Pos + RealSize + offset <= RealSize)
{
this -> Pos += RealSize + offset;
result = true;
}
}
return result;
}
// Check End-of-File indicator
bool GMemFile::isEOF()
{
return this -> Pos >= this -> RealSize;
}
// Returns current position
GMemFile::tPosition GMemFile::Tell()
{
return this -> Pos;
}
// Gets pointer to file
void* GMemFile::GetPtr()
{
return this -> Ptr;
}
// Gets Ptr + current position
void* GMemFile::GetCurPtr()
{
return this -> Ptr + Pos;
}
// Gets filesize
size_t GMemFile::GetSize()
{
return this -> RealSize;
}
// Sets filesize
bool GMemFile::SetSize(tPosition filesize)
{
if (filesize <= this->SizeLimit)
{
this->RealSize = filesize;
return true;
}
else
return false;
}