cgv
Loading...
Searching...
No Matches
big_binary_file.cxx
1#include <cgv/utils/big_binary_file.h>
2#include <iostream>
3
4#ifdef _MSC_VER
5#include <windows.h>
6HANDLE& F(void*& file) { return (HANDLE&) file; }
7const HANDLE& F(const void*& file) { return (const HANDLE&) file; }
8std::wstring str2wstr(const std::string& s)
9{
10 std::wstring ws;
11 ws.resize(s.size());
12 int n = MultiByteToWideChar(CP_ACP,0,s.c_str(),(int)s.size(),&ws[0],(int)ws.size());
13 ws.resize(n);
14 return ws;
15}
16#else
17FILE*& F(void*& file) { return (FILE*&) file; }
18const FILE*& F(const void*& file) { return (const FILE*&) file; }
19std::string str2wstr(const std::string& s)
20{
21 std::cerr << "str2wstr(const std::string& s) not implemented" << std::endl;
22 return s;
23}
24#endif
25
26
27namespace cgv {
28 namespace utils{
29
30 #ifdef _MSC_VER
31
32 big_binary_file::big_binary_file(const std::string &filename)
33 {
34 this->filename = filename;
35 fileopened=false;
36 };
37
38 bool big_binary_file::open( MODE m, const std::string& file_name)
39 {
40 if(fileopened)
41 close();
42
43 if (!file_name.empty())
44 filename = file_name;
45#ifdef UNICODE
46 std::wstring wfile_name = str2wstr(file_name);
47 const wchar_t* name = wfile_name.c_str();
48#else
49 const char* name = file_name.c_str();
50#endif
51
52 access_mode = m;
53 if(m == READ) {
54 //open for read
55 F(file) = CreateFile(name,
56 GENERIC_READ,
57 FILE_SHARE_READ,
58 NULL,
59 OPEN_EXISTING,
60 FILE_ATTRIBUTE_NORMAL,
61 NULL);
62
63 }
64 else if (m == WRITE) {
65 //open for write
66 F(file) = CreateFile(name,
67 GENERIC_WRITE,
68 0,
69 NULL,
70 CREATE_ALWAYS,
71 FILE_ATTRIBUTE_NORMAL,
72 NULL);
73 }
74 else {
75 //open for write
76 F(file) = CreateFile(name,
77 GENERIC_READ|GENERIC_WRITE,
78 0,
79 NULL,
80 OPEN_ALWAYS,
81 FILE_ATTRIBUTE_NORMAL,
82 NULL);
83 }
84 if (F(file) != INVALID_HANDLE_VALUE) {
85 fileopened=true;
86 return true;
87 }
88 return false;
89 }
90
91
93 {
94 if(fileopened)
95 CloseHandle(F(file));
96 fileopened=false;
97
98 }
99
101 {
102 return fileopened;
103 }
104
105
106 long long big_binary_file::size()
107 {
108 if(fileopened)
109 {
110 LARGE_INTEGER fs;
111
112 if(GetFileSizeEx(F(file),(PLARGE_INTEGER) &fs))
113 return fs.QuadPart;
114 else
115 return -1;
116 }
117 else
118 {
119 open(READ);
120
121 LARGE_INTEGER fs;
122 long long value;
123
124 if(GetFileSizeEx(F(file),(PLARGE_INTEGER) &fs))
125 value = fs.QuadPart;
126 else
127 value = -1;
128
129 close();
130 return value;
131 }
132 }
133
134 bool big_binary_file::read(unsigned char *targetbuffer,unsigned long num, unsigned long *numread)
135 {
136 if(fileopened && access_mode == READ)
137 {
138 if(numread == NULL)
139 {
140 unsigned long _numread;
141 ReadFile(F(file),targetbuffer,num,&_numread,NULL);
142 return num == _numread;
143 }
144 else
145 {
146 ReadFile(F(file),targetbuffer,num,numread,NULL);
147 return num == *numread;
148 }
149 }
150 return false;
151 }
152
153 bool big_binary_file::write(const unsigned char *sourcebuffer,unsigned long num, unsigned long *numwrote)
154 {
155 if(fileopened && access_mode == WRITE)
156 {
157 if(numwrote == NULL)
158 {
159 unsigned long _numwrote;
160 WriteFile(F(file),sourcebuffer, num, &_numwrote, NULL);
161 return _numwrote == num;
162 }
163 else
164 {
165 if(!WriteFile(F(file),sourcebuffer, num, numwrote, NULL))
166
167 {
168 std::cerr << "write error"<<std::endl;
169
170 }
171
172 return *numwrote == num;
173 }
174 }
175 return false;
176 }
177
178 bool big_binary_file::seek(long long index)
179 {
180 if(fileopened)
181 {
182 LARGE_INTEGER l;
183 l.QuadPart=(index);
184
185 if(SetFilePointerEx(F(file),l,NULL,FILE_BEGIN))
186 return true;
187 else
188 return false;
189 }
190 return false;
191 }
192
193 long long big_binary_file::position()
194 {
195 if(fileopened)
196 {
197 LARGE_INTEGER l1,l2;
198 l1.QuadPart=0;
199 if(SetFilePointerEx(F(file),l1,&l2,FILE_CURRENT))
200 return l2.QuadPart;
201 else
202 return -1;
203
204 }
205 else
206 return false;
207
208 }
209
210
212 {
213 if(fileopened)
214 CloseHandle(F(file));
215 }
216
217
218 #else
219
220/*
221 // Linux-Implementation
222
223 big_binary_file::big_binary_file(const std::string &filename)
224 {
225 this->filename = filename;
226 fileopened=false;
227 };
228
229 bool big_binary_file::open(MODE m, const std::string& file_name)
230 {
231 if(fileopened)
232 close();
233
234 if (!file_name.empty())
235 filename = file_name;
236
237 access_mode = m;
238 if(m == READ)
239 {
240 //open for read
241 F(file) = fopen64(filename.c_str(), "rb");
242 } if (m == WRITE) else
243 {
244 //open for write
245 F(file) = fopen64(filename.c_str(), "wb");
246 }
247 else {
248 //open for write
249 F(file) = fopen64(filename.c_str(), "w+b");
250 }
251 if (file) {
252 fileopened=true;
253 return true;
254 }
255 return false;
256 }
257
258 void big_binary_file::close()
259 {
260 if(fileopened)
261 fclose(F(file));
262
263 fileopened=false;
264 }
265
266 bool big_binary_file::is_open()
267 {
268 return fileopened;
269 }
270
271 long long big_binary_file::size()
272 {
273 if(fileopened)
274 {
275 off64_t size;
276
277 if(!fseeko64(F(file), 0, SEEK_END))
278 {
279 size = ftello64(F(file));
280 fseeko64(F(file), 0, SEEK_SET);
281 return size;
282 }
283 else
284 {
285 fseeko64(F(file), 0, SEEK_SET);
286 return -1;
287 }
288
289 }
290 else
291 {
292 open(READ);
293
294 off64_t size;
295 long long value;
296
297 if(!fseeko64(F(file), 0, SEEK_END))
298 {
299 size = ftello64(F(file));
300 fseeko64(F(file), 0, SEEK_SET);
301 value = size;
302 }
303 else
304 {
305 fseeko64(F(file), 0, SEEK_SET);
306 value = -1;
307 }
308
309 close();
310
311 return value;
312 }
313 }
314
315 bool big_binary_file::read(unsigned char *targetbuffer,unsigned long num, unsigned long *numread)
316 {
317 if(fileopened && access_mode == READ)
318 {
319 if(numread == NULL)
320 {
321 unsigned long _numread;
322 _numread = fread(targetbuffer, 1, num, F(file));
323 return num == _numread;
324 }
325 else
326 {
327 *numread = fread(targetbuffer, 1, num, F(file));
328 return num == *numread;
329 }
330 }
331 return false;
332 }
333
334 bool big_binary_file::write(unsigned char *sourcebuffer,unsigned long num, unsigned long *numwrote)
335 {
336 if(fileopened && access_mode == WRITE)
337 {
338 if(numwrote == NULL)
339 {
340 unsigned long _numwrote;
341 _numwrote = fwrite(sourcebuffer, 1, num, F(file));
342 return _numwrote == num;
343 }
344 else
345 {
346 if(!fwrite(sourcebuffer, 1, num, F(file)))
347 std::cerr << "write error"<<std::endl;
348
349 return *numwrote == num;
350 }
351 }
352 return false;
353 }
354
355 bool big_binary_file::seek(long long index)
356 {
357 if(fileopened)
358 {
359 off64_t pos;
360 pos = index;
361
362 if(!fseeko64(F(file), pos, SEEK_SET))
363 return true;
364 else
365 return false;
366 }
367
368 return false;
369 }
370
371 long long big_binary_file::position()
372 {
373 if(fileopened)
374 return ftello64(F(file));
375 else
376 return false;
377 }
378
379 big_binary_file::~big_binary_file()
380 {
381 if(fileopened)
382 fclose(F(file));
383 }
384*/
385 #endif
386
387 }
388}
void close()
close the file
big_binary_file(const std::string &filename="")
assosiates the new instance to the file filename
long long position()
return the position of the file pointer in bytes
bool open(MODE m, const std::string &file_name="")
open a file in read or write mode
long long size()
return the size of the file in bytes
bool is_open()
return true if the file is opened
bool read(unsigned char *targetbuffer, unsigned long num, unsigned long *numread=NULL)
read num bytes from the file into the targetbuffer
virtual ~big_binary_file()
close the file (if it is still opened)
bool write(const unsigned char *sourcebuffer, unsigned long num, unsigned long *numwrote=NULL)
write num bytes to the file from the sourcebuffer (file must be opened with write access first)
std::wstring str2wstr(const std::string &s)
convert a 8-bit string to a 16-bit string
Definition convert.cxx:11
the cgv namespace
Definition print.h:11