-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathV2File.cpp
207 lines (136 loc) · 3.17 KB
/
V2File.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
#include "V2Base.h"
#include "V2File.h"
#if LINUX || MACOS
#include <fcntl.h>
#endif
using namespace V2;
FileMgr *V2::TheFileMgr = NULL;
const String FileException::GetTypeString(void) const
{
return "FileException";
}
const String FileException::GetErrorString(void) const
{
static const String error[] =
{
"kFileOkay",
"kFileOpenFailed",
"kFileIOFailed"
};
return error[fileResult];
}
const String File::FileName(const String &name)
{
String fileName = name;
#if WIN
for (char *c = fileName;; ++c)
{
long k = *c;
if (k == 0) break;
if (k == '/') *c = '\\';
}
#endif
return fileName;
}
File::File(const String name, FileOpenMode mode)
{
#if WIN
DWORD access = (mode == kFileReadOnly) ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE;
DWORD creation = (mode == kFileCreate) ? CREATE_ALWAYS : OPEN_EXISTING;
fileHandle = CreateFileA(FileName(name), access, FILE_SHARE_READ, NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
if (fileHandle == INVALID_HANDLE_VALUE)
throw FileException(kFileOpenFailed);
#elif LINUX || MACOS
int flags = (mode == kFileReadOnly) ? O_RDONLY : O_RDWR;
if (mode == kFileCreate)
flags |= O_CREAT | O_TRUNC;
fileDesc = open(FileName(name), flags, 0644);
if (fileDesc == -1)
throw FileException(kFileOpenFailed);
#endif
filePosition = 0;
fileComplete = true;
fileResult = kFileOkay;
}
File::~File()
{
#if WIN
CloseHandle(fileHandle);
#elif LINUX || MACOS
close(fileDesc);
#endif
}
FileResult File::Read(void *buffer, long size)
{
#if WIN
DWORD actual;
SetFilePointer(fileHandle, filePosition, NULL, FILE_BEGIN);
if (ReadFile(fileHandle, buffer, size, &actual, NULL) && (actual == size))
{
filePosition += size;
return (kFileOkay);
}
return (kFileIOFailed);
#elif LINUX || MACOS
lseek(fileDesc, filePosition, SEEK_SET);
if (read(fileDesc, buffer, size) == size)
{
filePosition += size;
return (kFileOkay);
}
return (kFileIOFailed);
#endif
}
FileResult File::Write(const void *buffer, long size)
{
#if WIN
DWORD actual;
SetFilePointer(fileHandle, filePosition, NULL, FILE_BEGIN);
if (WriteFile(fileHandle, buffer, size, &actual, NULL) && (actual == size))
{
filePosition += size;
return (kFileOkay);
}
return (kFileIOFailed);
#elif LINUX || MACOS
lseek(fileDesc, filePosition, SEEK_SET);
if (write(fileDesc, buffer, size) == size)
{
filePosition += size;
return (kFileOkay);
}
return (kFileIOFailed);
#endif
}
unsigned long File::GetSize() const
{
#if WIN
return (GetFileSize(fileHandle, NULL));
#elif LINUX || MACOS
struct stat buf;
fstat(fileDesc, &buf);
return (buf.st_size);
#endif
}
unsigned long File::SetPosition(long position, FilePositioningMode mode)
{
switch (mode)
{
case kFileBegin:
filePosition = position;
break;
case kFileCurrent:
filePosition += position;
break;
case kFileEnd:
filePosition = GetSize() - position;
break;
}
return (filePosition);
}
FileMgr::~FileMgr()
{
}
FileMgr::FileMgr() : Singleton<FileMgr>(TheFileMgr)
{
}