-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnsiFile.h
420 lines (369 loc) · 7.15 KB
/
AnsiFile.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//////////////////////////////////////
//
// AnsiFile.h
//
// Ilkka Prusi
//
#ifndef _ANSIFILE_H_
#define _ANSIFILE_H_
#include <string>
#include <exception>
// exception with GCC does no have suitable constructor so replace it
class BaseException
{
public:
std::string m_message;
public:
BaseException(const char *szMessage)
: m_message(szMessage)
{}
const char* what() { return m_message.c_str(); }
};
// exception-classes for error cases
class IOException : public BaseException
{
public:
IOException(const char *szMessage)
: BaseException(szMessage)
{
}
};
class ArcException : public BaseException
{
protected:
std::string m_szData;
public:
ArcException(const char *szMessage, const std::string &szData)
: BaseException(szMessage)
, m_szData(szData)
{
}
ArcException(const char *szMessage, const size_t nData)
: BaseException(szMessage)
, m_szData()
{
// TODO:
//m_szData = std::lexical_cast<std::string>(nData);
//m_szData = ltoa(nData;
}
std::string GetData()
{
return m_szData;
}
};
class CReadBuffer
{
private:
enum tSizes
{
INITIAL_READ_BUFFER_SIZE = 16384,
MAX_READ_BUFFER_SIZE = 1024*1024
};
unsigned char *m_pReadBuffer;
size_t m_nReadBufferSize;
//size_t m_nMaxBufferSize; // limit of growing buffer..
//bool m_bKeepExisting; // keep existing data if growing buffer..
//bool m_bPageAlign; // page-size aligned allocations
inline void CreateBuffer(const size_t nMinSize)
{
// allocate new
m_pReadBuffer = new unsigned char[nMinSize];
m_nReadBufferSize = nMinSize;
::memset(m_pReadBuffer, 0, m_nReadBufferSize);
}
protected:
// helpers for users:
// user-defined position in buffer
// (read/write position)
//
size_t m_nCurrentPos;
public:
CReadBuffer(void)
: m_pReadBuffer(nullptr)
, m_nReadBufferSize(0)
//, m_nMaxBufferSize(MAX_READ_BUFFER_SIZE)
, m_nCurrentPos(0)
{
CreateBuffer(INITIAL_READ_BUFFER_SIZE);
}
CReadBuffer(const size_t nMinsize)
: m_pReadBuffer(nullptr)
, m_nReadBufferSize(0)
//, m_nMaxBufferSize(MAX_READ_BUFFER_SIZE)
, m_nCurrentPos(0)
{
CreateBuffer(nMinsize);
}
~CReadBuffer(void)
{
if (m_pReadBuffer != nullptr)
{
delete m_pReadBuffer;
}
}
// allocate or grow if necessary
void PrepareBuffer(const size_t nMinSize, bool bKeepData = true)
{
if (m_pReadBuffer == nullptr
|| m_nReadBufferSize == 0)
{
// must create new
CreateBuffer(nMinSize);
return;
}
// growing, do we need this..?
if (m_pReadBuffer != nullptr
&& m_nReadBufferSize < nMinSize)
{
size_t nNewSize = nMinSize;
/*
if (nNewSize > m_nMaxBufferSize)
{
nNewSize = m_nMaxBufferSize;
}
*/
unsigned char *pNewBuf = new unsigned char[nNewSize];
if (bKeepData == true)
{
memcpy(pNewBuf, m_pReadBuffer, m_nReadBufferSize); // keep existing data
}
delete m_pReadBuffer; // destroy old smaller
// keep new buffer
m_pReadBuffer = pNewBuf;
m_nReadBufferSize = nNewSize;
}
if (bKeepData == false)
{
// otherwise just clear existing (keep existing)
::memset(m_pReadBuffer, 0, m_nReadBufferSize);
m_nCurrentPos = 0;
}
}
// note: don't make it const:
// allow modify to read into it..
//
unsigned char *GetBegin()
{
return m_pReadBuffer;
}
// reduce repeated code -> count end ptr
unsigned char *GetEnd()
{
return m_pReadBuffer + m_nReadBufferSize;
}
// reduce repeated code -> count to given offset from start
unsigned char *GetAt(const size_t nOffset)
{
#ifdef _DEBUG
// debug-case, access beyond buffer
if (m_nReadBufferSize <= nOffset)
{
return nullptr;
}
#endif
return m_pReadBuffer + nOffset;
}
// current allocation
//
size_t GetSize() const
{
return m_nReadBufferSize;
}
// user-defined position in buffer
// (read/write position)
size_t GetCurrentPos() const
{
return m_nCurrentPos;
}
void SetCurrentPos(const size_t nCurrentPos)
{
m_nCurrentPos = nCurrentPos;
}
// helpers for read/write single character:
// use instead of direct-IO
unsigned char GetNextByte()
{
unsigned char *pBuf = GetAt(m_nCurrentPos);
m_nCurrentPos++;
return (*pBuf);
}
void SetNextByte(const unsigned char ucValue)
{
unsigned char *pBuf = GetAt(m_nCurrentPos);
(*pBuf) = ucValue;
m_nCurrentPos++;
}
// copy given, start at current
void Append(unsigned char *pData, size_t nSize)
{
#ifdef _DEBUG
if ((m_nCurrentPos + nSize) > m_nReadBufferSize)
{
// exception, access beyond allocated buffer
}
#endif
unsigned char *pBuf = GetAt(m_nCurrentPos);
memcpy(pBuf, pData, nSize);
m_nCurrentPos += nSize;
}
};
// ANSI-C style file-API helper
class CAnsiFile
{
protected:
FILE *m_pFile;
size_t m_nFileSize;
bool m_bIsWritable;
size_t GetSizeOfFile()
{
if (m_pFile == NULL)
{
return 0;
}
// should be at start (0)
long lCurrent = ftell(m_pFile);
// locate end
if (fseek(m_pFile, 0L, SEEK_END) != 0)
{
return 0;
}
size_t nSize = ftell(m_pFile);
// return to start
fseek(m_pFile, lCurrent, SEEK_SET);
return nSize;
}
public:
CAnsiFile(void)
: m_pFile(NULL)
, m_nFileSize(0)
, m_bIsWritable(false)
{}
CAnsiFile(const std::string &szFile, bool bWritable = false)
: m_pFile(NULL)
, m_nFileSize(0)
, m_bIsWritable(bWritable)
{
Open(szFile, bWritable);
}
~CAnsiFile(void)
{
Close();
}
operator FILE *() const
{
return m_pFile;
}
bool Open(const std::string &szFile, bool bWritable = false)
{
Close(); // close previous (if any)
if (bWritable == false)
{
m_pFile = fopen(szFile.c_str(), "rb");
m_nFileSize = GetSizeOfFile();
}
else
{
// size zero if new file..
m_pFile = fopen(szFile.c_str(), "wb");
}
m_bIsWritable = bWritable;
return IsOk();
}
void Close()
{
if (m_pFile != NULL)
{
fclose(m_pFile);
m_pFile = NULL;
}
}
size_t GetSize()
{
// get size as it was in start
if (m_bIsWritable == false)
{
return m_nFileSize;
}
// determine current size
return GetSizeOfFile();
}
bool IsOk()
{
if (m_pFile == NULL)
{
return false;
}
if (ferror(m_pFile) != 0)
{
return false;
}
return true;
}
bool Flush()
{
if (fflush(m_pFile) != 0)
{
return false;
}
return true;
}
bool Write(const void *pBuffer, const size_t nBytes)
{
size_t nWritten = fwrite(pBuffer, 1, nBytes, m_pFile);
if (IsOk() == false
|| nWritten < nBytes)
{
return false;
}
return Flush();
}
bool Read(void *pBuffer, const size_t nBytes)
{
size_t nRead = fread(pBuffer, 1, nBytes, m_pFile);
if (IsOk() == false
|| nRead < nBytes)
{
return false;
}
return true;
}
bool Read(CReadBuffer &Buffer)
{
return Read(Buffer.GetBegin(), Buffer.GetSize());
}
bool Tell(long &lPos)
{
lPos = ftell(m_pFile);
if (lPos < 0)
{
return false;
}
return true;
}
bool Seek(const size_t nBytes, const int iOrigin)
{
if (fseek(m_pFile, nBytes, iOrigin) != 0)
{
return false;
}
return true;
}
/*
bool SeekOff(const long lOffset, const int iOrigin)
{
if (fseeko(m_pFile, lOffset, iOrigin) != 0)
{
return false;
}
return true;
}
*/
};
class CPathHelper
{
public:
static bool MakePath(const std::string &szPath);
static bool MakePathToFile(const std::string &szOutFile);
};
#endif // ifndef _ANSIFILE_H_