-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathu_file_map.h
283 lines (226 loc) · 6.58 KB
/
u_file_map.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
#ifndef FILE_MAP_H
#define FILE_MAP_H
#define _POSIX_MAPPED_FILE
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
class bad_file { };
template<class T>
class file_map
{
protected:
int m_fdFile;
T* m_DataMap;
BYTE* m_FileMap;
DWORD m_dwFileSize;
DWORD m_iCurrentElem;
DWORD m_dwSizeOfHeader;
bool m_bEof;
bool m_bHand;
file_map<T>& operator=(const file_map<T>&);
file_map(const file_map<T>&);
public:
file_map(DWORD dwSizeOfHeader = 0): m_fdFile(-1),
m_dwFileSize(0),
m_iCurrentElem(0),
m_dwSizeOfHeader(dwSizeOfHeader),
m_bEof(false)
{
}
file_map(char* szFileName, DWORD dwSizeOfHeader = 0, DWORD dwFileSize = 0):
m_fdFile(-1),
m_dwFileSize(dwFileSize),
m_iCurrentElem(0),
m_dwSizeOfHeader(dwSizeOfHeader),
m_bEof(false)
{
if(!this->create(szFileName, dwFileSize))
throw new bad_file();
}
bool create(char* szFileName, DWORD dwFileSize = 0)
{
m_fdFile = open(szFileName, O_RDWR);
ASSERT(m_fdFile != -1);
if (m_fdFile != -1)
{
struct stat stFile;
fstat(m_fdFile, &stFile);
if (dwFileSize == 0)
{
m_dwFileSize = stFile.st_size;
m_FileMap = (BYTE*)mmap(0, m_dwFileSize, PROT_READ | PROT_WRITE,
MAP_SHARED, m_fdFile, 0);
m_bHand = false;
}
else
{
m_dwFileSize = dwFileSize;
m_FileMap = (BYTE*)malloc(m_dwFileSize);
ASSERT(m_FileMap != NULL);
if (m_FileMap == NULL)
m_FileMap = (BYTE*)MAP_FAILED;
/*m_FileMap = (BYTE*)mmap(0, m_dwFileSize, PROT_READ | PROT_WRITE,
MAP_ANON, -1, 0);*/
read(m_fdFile, m_FileMap, stFile.st_size);
m_bHand = true;
}
m_DataMap = (T*)(m_FileMap + m_dwSizeOfHeader);
ASSERT(m_FileMap != MAP_FAILED);
if (m_FileMap == MAP_FAILED)
{
throw new bad_alloc();
}
else
{
m_iCurrentElem = 0;
return true;
}
}
return false;
}
inline T read_next()
{
return(m_DataMap[m_iCurrentElem++]);
}
inline T operator[](size_t i)
{
return (m_DataMap[i]);
}
inline DWORD get_size()
{
struct stat stFile;
fstat(m_fdFile, &stFile);
return(stFile.st_size);
}
inline bool eof()
{
return (m_dwFileSize <= ((m_iCurrentElem + 1) * sizeof(T) + m_dwSizeOfHeader));
}
inline BYTE* get_header()
{
return (m_FileMap);
}
~file_map()
{
if (m_bHand)
{
lseek(m_fdFile, 0, SEEK_SET);
write(m_fdFile, m_FileMap, m_dwFileSize);
//free(m_FileMap);
}
else
{
munmap(m_FileMap, m_dwFileSize);
}
close(m_fdFile);
}
};
template<>
class file_map<bit>
{
private:
int m_fdFile;
BYTE* m_DataMap;
BYTE* m_FileMap;
DWORD m_dwFileSize;
DWORD m_iCurrentByte;
DWORD m_nBitsInCurrentByte;
BYTE m_CurrentByte;
DWORD m_dwSizeOfHeader;
bool m_bEof;
file_map<bit>& operator=(const file_map<bit>&);
file_map(const file_map<bit>&);
public:
class bad_file { };
file_map(DWORD dwSizeOfHeader = 8): m_fdFile(-1),
m_dwFileSize(0),
m_iCurrentByte(0),
m_nBitsInCurrentByte(0),
m_CurrentByte(0),
m_dwSizeOfHeader(dwSizeOfHeader),
m_bEof(false)
{
}
file_map(char* szFileName, DWORD dwSizeOfHeader = 8, DWORD dwFileSize = 0):
m_fdFile(-1),
m_dwFileSize(dwFileSize),
m_iCurrentByte(0),
m_nBitsInCurrentByte(0),
m_CurrentByte(0),
m_dwSizeOfHeader(dwSizeOfHeader),
m_bEof(false)
{
if(!this->create(szFileName, dwFileSize))
throw new bad_file();
}
bool create(char* szFileName, DWORD dwFileSize = 0)
{
m_fdFile = open(szFileName, O_RDWR);
ASSERT(m_fdFile != -1);
if (m_fdFile != -1)
{
if (dwFileSize == 0)
{
struct stat stFile;
fstat(m_fdFile, &stFile);
m_dwFileSize = stFile.st_size;
}
else
{
m_dwFileSize = dwFileSize;
}
m_FileMap = (BYTE*)mmap(0, m_dwFileSize, PROT_READ | PROT_WRITE,
MAP_PRIVATE, m_fdFile, 0);
m_DataMap = m_FileMap + m_dwSizeOfHeader;
ASSERT(m_FileMap != MAP_FAILED);
if (m_FileMap != MAP_FAILED)
{
m_iCurrentByte = 0;
m_nBitsInCurrentByte = 0;
m_CurrentByte = 0;
return true;
}
}
return false;
}
inline bit read_next()
{
if (m_nBitsInCurrentByte == 0)
{
m_CurrentByte = m_DataMap[m_iCurrentByte++];
m_nBitsInCurrentByte = 8;
}
bit retval = (MSB(m_CurrentByte) ? 1 : 0);
m_CurrentByte <<= 1;
--m_nBitsInCurrentByte;
return (retval);
}
inline bit operator[](size_t i)
{
BYTE TargetByte = m_DataMap[i / 8];
bit TargetBit = ( ( TargetByte & (1 << ((i % 8) - 1)) ) == 0 ? 0 : 1);
return TargetBit;
}
inline DWORD get_size()
{
struct stat stFile;
fstat(m_fdFile, &stFile);
return(stFile.st_size);
}
inline BYTE* get_header()
{
return (m_FileMap);
}
inline bool eof()
{
return (m_dwFileSize == (m_iCurrentByte + 1 + m_dwSizeOfHeader));
}
~file_map()
{
munmap(m_FileMap, m_dwFileSize);
close(m_fdFile);
}
};
#endif //FILE_MAP_H