-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS_Stream.cpp
More file actions
322 lines (297 loc) · 6.24 KB
/
DS_Stream.cpp
File metadata and controls
322 lines (297 loc) · 6.24 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "CommonTypeDefines.h"
#include "streams/DS_FileStream.h"
#include "fileUtils/_fileExists.h"
#include <algorithm>
using std::min;
namespace DataStructures
{
//------------------------------------------------------------------- Stream ------------------------------------------------------------------------
#if __WORDSIZE == 64
template <>
size_t Stream::Write(const unsigned int &buffer)
{
if (mForce32bitCompatable)
{
__u32 val = buffer;
return this->Write(&val, sizeof(__u32));
}
else
return this->Write(&buffer, sizeof(unsigned int));
}
template <>
size_t Stream::Write(const int &buffer)
{
if (mForce32bitCompatable)
{
__s32 val = buffer;
return this->Write(&val, sizeof(__s32));
}
else
return this->Write(&buffer, sizeof(int));
}
template <>
size_t Stream::Write(const unsigned long &buffer)
{
if (mForce32bitCompatable)
{
__u32 val = buffer;
return this->Write(&val, sizeof(__u32));
}
else
return this->Write(&buffer, sizeof(unsigned long));
}
template <>
size_t Stream::Write(const long &buffer)
{
if (mForce32bitCompatable)
{
__s32 val = buffer;
return this->Write(&val, sizeof(__s32));
}
else
return this->Write(&buffer, sizeof(long));
}
template <>
bool Stream::Read(unsigned int &dst)
{
if (mForce32bitCompatable)
{
__u32 t;
bool res = this->Read(&t, sizeof(__u32)) == sizeof(__u32);
dst = t;
return res;
}
else
return this->Read((void *)&dst, sizeof(unsigned int)) == sizeof(unsigned int);
}
template <>
bool Stream::Read(int &dst)
{
if (mForce32bitCompatable)
{
__s32 t;
bool res = this->Read(&t, sizeof(__s32)) == sizeof(__s32);
dst = t;
return res;
}
else
return this->Read((void *)&dst, sizeof(int)) == sizeof(int);
}
template <>
bool Stream::Read(unsigned long &dst)
{
if (mForce32bitCompatable)
{
__u32 t;
bool res = this->Read(&t, sizeof(__u32)) == sizeof(__u32);
dst = t;
return res;
}
else
return this->Read((void *)&dst, sizeof(unsigned long)) == sizeof(unsigned long);
}
template <>
bool Stream::Read(long &dst)
{
if (mForce32bitCompatable)
{
__s32 t;
bool res = this->Read(&t, sizeof(__s32)) == sizeof(__s32);
dst = t;
return res;
}
else
return this->Read((void *)&dst, sizeof(long)) == sizeof(long);
}
#endif
size_t Stream::WriteString(const char *buffer, size_t maxsize)
{
if (!CanWrite())
return 0;
__u16 size = min(strlen(buffer), maxsize + sizeof(__u16));
size_t actSize = this->Write(&size, sizeof(size));
actSize += this->Write(buffer, size);
return actSize;
}
size_t Stream::ReadString(char *dst, size_t maxsize)
{
if (Eof() || !dst || !CanRead())
return 0;
__u16 size;
size_t readIn = this->Read(&size, sizeof(size));
readIn += this->Read(dst, min((size_t)size, maxsize));
dst[size] = 0;
return readIn;
}
size_t Stream::CopyFrom(Stream *stream, size_t count)
{
const size_t _max_buffer_size = 0xf0000;
size_t size = count;
if (count == 0)
{
stream->Seek(0, SEEK_SET);
size = stream->Length();
}
size_t result = size;
size_t bufferSize;
if (size > _max_buffer_size)
bufferSize = _max_buffer_size;
else
bufferSize = size;
char *buffer = (char *)malloc(bufferSize);
size_t n;
while (size != 0)
{
if (size > bufferSize)
n = bufferSize;
else
n = size;
stream->Read(buffer, n);
Write(buffer, n);
size -= n;
}
free(buffer);
return result;
}
bool Stream::SaveToStream(Stream *stream)
{
if (!stream)
return false;
__s64 oldPos = Position();
__s64 oldPos1 = stream->Position();
bool res = stream->CopyFrom(this, 0) == Length();
Seek(oldPos, SEEK_SET);
stream->Seek(oldPos1, SEEK_SET);
return res;
}
bool Stream::LoadFromStream(Stream *stream)
{
if (!stream)
return false;
__s64 oldPos = Position();
__s64 oldPos1 = stream->Position();
size_t size = stream->Length();
SetLength(size);
Seek(0, SEEK_SET);
bool res = CopyFrom(stream, 0) == size;
Seek(oldPos, SEEK_SET);
stream->Seek(oldPos1, SEEK_SET);
return res;
}
bool Stream::SaveToFile(const char *fileName)
{
FileStream *stream = new FileStream(fileName, "wb+");
bool result = SaveToStream(stream);
stream->Close();
delete stream;
return result;
}
bool Stream::LoadFromFile(const char *fileName)
{
if (!_fileExists(fileName))
return false;
FileStream *stream = new FileStream(fileName, "rb");
bool result = LoadFromStream(stream);
stream->Close();
delete stream;
return result;
}
__s64 Stream::Length()
{
__s64 oldPos = Seek(0, SEEK_CUR);
__s64 length = Seek(0, SEEK_END);
Seek(oldPos, SEEK_SET);
return length;
}
__s64 Stream::Position()
{
return Seek(0, SEEK_CUR);
}
char *Stream::ReadLine(char *str, int num)
{
if (num <= 0)
return NULL;
char c = 0;
size_t maxCharsToRead = num - 1;
for (size_t i = 0; i < maxCharsToRead; ++i)
{
size_t result = Read(&c, 1);
if (result != 1)
{
str[i] = '\0';
break;
}
if (c == '\n')
{
str[i] = c;
str[i + 1] = '\0';
break;
}
else if(c == '\r')
{
str[i] = c;
// next may be '\n'
size_t pos = Position();
char nextChar = 0;
if (Read(&nextChar, 1) != 1)
{
// no more characters
str[i + 1] = '\0';
break;
}
if (nextChar == '\n')
{
if (i == maxCharsToRead - 1)
{
str[i + 1] = '\0';
break;
}
else
{
str[i + 1] = nextChar;
str[i + 2] = '\0';
break;
}
}
else
{
Seek(pos, SEEK_SET);
str[i + 1] = '\0';
break;
}
}
str[i] = c;
}
return str; // what if first read failed?
}
bool Stream::Eof()
{
return Position() >= Length();
}
bool Stream::Rewind()
{
if (!CanSeek())
return 0;
else
return Seek(0, SEEK_SET) == 0;
}
std::string Stream::GetAsString()
{
char *buffer;
__s64 fileLen = Length();
__s64 oldPosition = Position();
buffer = (char *)malloc(fileLen + 1);
Seek(0, SEEK_SET);
Read(buffer, fileLen);
Seek(oldPosition, SEEK_SET);
buffer[fileLen] = 0;
std::string result(buffer);
free(buffer);
return result;
}
__u32 Stream::GetMemoryCost()
{
//this value is not very accurate because of class HexString's cache using
return static_cast<__u32>(sizeof(*this) + strlen(mFileName));
}
}