1#include <cgv/utils/big_binary_file.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)
12 int n = MultiByteToWideChar(CP_ACP,0,s.c_str(),(
int)s.size(),&ws[0],(
int)ws.size());
17FILE*& F(
void*& file) {
return (FILE*&) file; }
18const FILE*& F(
const void*& file) {
return (
const FILE*&) file; }
19std::string str2wstr(
const std::string& s)
21 std::cerr <<
"str2wstr(const std::string& s) not implemented" << std::endl;
34 this->filename = filename;
43 if (!file_name.empty())
46 std::wstring wfile_name =
str2wstr(file_name);
47 const wchar_t* name = wfile_name.c_str();
49 const char* name = file_name.c_str();
55 F(file) = CreateFile(name,
60 FILE_ATTRIBUTE_NORMAL,
64 else if (m == WRITE) {
66 F(file) = CreateFile(name,
71 FILE_ATTRIBUTE_NORMAL,
76 F(file) = CreateFile(name,
77 GENERIC_READ|GENERIC_WRITE,
81 FILE_ATTRIBUTE_NORMAL,
84 if (F(file) != INVALID_HANDLE_VALUE) {
112 if(GetFileSizeEx(F(file),(PLARGE_INTEGER) &fs))
124 if(GetFileSizeEx(F(file),(PLARGE_INTEGER) &fs))
136 if(fileopened && access_mode == READ)
140 unsigned long _numread;
141 ReadFile(F(file),targetbuffer,num,&_numread,NULL);
142 return num == _numread;
146 ReadFile(F(file),targetbuffer,num,numread,NULL);
147 return num == *numread;
155 if(fileopened && access_mode == WRITE)
159 unsigned long _numwrote;
160 WriteFile(F(file),sourcebuffer, num, &_numwrote, NULL);
161 return _numwrote == num;
165 if(!WriteFile(F(file),sourcebuffer, num, numwrote, NULL))
168 std::cerr <<
"write error"<<std::endl;
172 return *numwrote == num;
178 bool big_binary_file::seek(
long long index)
185 if(SetFilePointerEx(F(file),l,NULL,FILE_BEGIN))
199 if(SetFilePointerEx(F(file),l1,&l2,FILE_CURRENT))
214 CloseHandle(F(file));
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