-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASCIIFile.cpp
More file actions
302 lines (226 loc) · 4.42 KB
/
Copy pathASCIIFile.cpp
File metadata and controls
302 lines (226 loc) · 4.42 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
//#include "stdafx.h" // for precompiled headers (has to be in first place)
#include "global.h"
#include <stdlib.h>
#include <malloc.h> // malloc,free
#include <stdio.h> // FILE
#include <fcntl.h> // filesize
#include <string.h> // strlen
#include <assert.h> // assert
#ifdef _WIN32
#include <io.h> // filesize
#define NOMINMAX
#include <windows.h> // GetFileSize()
#else
#include <sys/stat.h>
#include <unistd.h>
#endif
#include "ASCIIFile.h"
#pragma warning( disable : 4996 )
/// test0
/// {} = This is a test {key2}
/// = And it goes further {key3}y as expected
/// {key3} = trick
/// {key2} = (Another {key3,separator})
/// {key3} = AAA
/// {key3} = BBB
/// {separator} = ;
// ++fbuf; /* Skip overlay byte */
// {} =" Application: {AppName}"
// =" date(mm/dd/yy): {__Date__}"
// =" time(hh:mm:ss): {__Time__}"
// =" line count: {__CodeLines__}"
// =" code file count: {__CodeFiles__}"
// {AppName} = CppScanner
size_t IO_GetFileSize(const char* Name)
{
assert(Name);
int handle;
handle = open(Name, O_RDONLY);
if (handle == -1)
return 0;
#ifdef _WIN32
size_t size = _filelengthi64(handle);
#else
struct stat st;
fstat(handle, &st);
off_t size = st.st_size;
#endif
close(handle);
return size;
}
/*
// @param must not be 0
size_t IO_GetFileSize(const wchar_t* filePath)
{
assert(filePath);
#ifdef _WIN32
HANDLE handle = CreateFile(filePath,
GENERIC_READ,
0, // exclusive access
NULL,
OPEN_EXISTING,
0,
NULL);
if (handle == INVALID_HANDLE_VALUE)
return 0;
DWORD lowFileSize = 0;
DWORD highFileSize = 0;
lowFileSize = GetFileSize(handle, &highFileSize);
CloseHandle(handle);
return (((size_t)highFileSize)<<32) | (size_t)lowFileSize;
#else
assert(0); // todo
return 0;
#endif
}
*/
CASCIIFile::CASCIIFile()
{
m_Size = 0;
m_Data = 0;
}
// destructor
CASCIIFile::~CASCIIFile()
{
ReleaseData();
}
// copy constructor
CASCIIFile::CASCIIFile(CASCIIFile& a)
{
char* mem = (char*)malloc(a.m_Size);
if (mem)
{
m_Data = mem;
m_Size = a.m_Size;
}
else throw "low memory";
}
// assignment operator
const CASCIIFile CASCIIFile::operator=(const CASCIIFile& a)
{
char* mem = (char*)malloc(a.m_Size);
if (mem)
{
ReleaseData();
m_Data = mem;
m_Size = a.m_Size;
}
else throw "low memory";
return *this;
}
void CASCIIFile::ReleaseData()
{
free((void*)m_Data);
m_Data = 0;
m_Size = 0;
}
bool CASCIIFile::IO_GetAvailability(const char* pathname)
{
int handle;
handle = open(pathname, O_RDONLY);
if (handle == -1)
return false;
#ifdef _WIN32
close(handle);
#else
assert(0); // todo
#endif
return true;
}
/*
bool CASCIIFile::IO_SaveASCIIFile(const wchar_t* pathname)
{
if (m_Data == 0)
return false;
FILE* file;
#ifdef _WIN32
if ((file = _wfopen(pathname, L"wb")) != NULL)
{
fwrite(m_Data, m_Size - 1, 1, file);
fclose(file);
return true;
}
#else
assert(0); // todo
#endif
return false;
}
*/
bool CASCIIFile::IO_SaveASCIIFile(const char* pathname)
{
if (m_Data == 0)
return false;
FILE* file;
if ((file = fopen(pathname, "wb")) != NULL)
{
fwrite(m_Data, m_Size - 1, 1, file);
fclose(file);
return true;
}
return false;
}
bool CASCIIFile::IO_LoadASCIIFile(const char* pathname)
{
if (*pathname == 0)
return false;
size_t size = IO_GetFileSize(pathname);
char* ret = 0;
ret = (char*)malloc(size + 1);
if (ret == 0)
return false; // no memory
FILE* file;
if ((file = fopen(pathname, "rb")) != NULL)
{
fread(ret, size, 1, file);
ret[size] = 0; // 0 terminate
m_Size = size + 1;
m_Data = ret;
fclose(file);
return true;
}
free(ret);
return false;
}
/*
bool CASCIIFile::IO_LoadASCIIFile(const wchar_t* pathname)
{
if (*pathname == 0)
return false;
size_t size = IO_GetFileSize(pathname);
char* ret = 0;
ret = (char*)malloc(size + 1);
if (ret == 0)
return false; // no memory
FILE* file;
#ifdef _WIN32
if ((file = _wfopen(pathname, L"rb")) != NULL)
{
fread(ret, size, 1, file);
ret[size] = 0; // 0 terminate
m_Size = size + 1;
m_Data = ret;
fclose(file);
return true;
}
#else
assert(0); // todo
#endif
free(ret);
return false;
}
*/
size_t CASCIIFile::GetDataSize() const
{
if (m_Data)return m_Size - 1;
else return 0;
}
const char* CASCIIFile::GetDataPtr()
{
return m_Data;
}
void CASCIIFile::CoverThisData(const char* ptr, const size_t size)
{
ReleaseData();
m_Data = ptr;
m_Size = size + 1;
}