cgv
big_binary_file.h
1 #pragma once
2 
3 #include <string>
4 #include "lib_begin.h"
5 
6 namespace cgv {
7  namespace utils {
14 class CGV_API big_binary_file
15 {
16 public:
17  enum MODE {READ = 1, WRITE = 2, READ_WRITE = 3};
18 
19 private:
20  MODE access_mode;
21  std::string filename;
22  bool fileopened;
23  void* file;
24 public:
25 
27  big_binary_file(const std::string &filename = "");
28 
30  virtual ~big_binary_file();
31 
33  bool open(MODE m, const std::string& file_name = "");
34 
36  void close();
37 
39  bool is_open();
40 
42  long long size();
43 
45  bool read(unsigned char *targetbuffer,unsigned long num, unsigned long *numread=NULL);
46 
48  bool write(const unsigned char *sourcebuffer,unsigned long num, unsigned long *numwrote=NULL);
49 
51  template <typename T>
52  bool read(T& v) { return read((unsigned char*)(&v),sizeof(T)); }
54  template <typename T>
55  bool read_array(T* a, unsigned int n) { return read((unsigned char*)a,sizeof(T)*n); }
56 
58  template <typename T>
59  bool write(const T& v) { return write((const unsigned char*)(&v),sizeof(T)); }
61  template <typename T>
62  bool write_array(const T* a, unsigned int n) { return write((const unsigned char*)a,sizeof(T)*n); }
63 
64  //set the file pointer to index (position in bytes from the beginning of the file)
65  bool seek(long long index);
66 
68  long long position();
69 };
70 
71  }
72 }
73 
74 #include <cgv/config/lib_end.h>
cgv::utils::big_binary_file::big_binary_file
big_binary_file(const std::string &filename="")
assosiates the new instance to the file filename
cgv::utils::big_binary_file::read
bool read(T &v)
read a typedef value
Definition: big_binary_file.h:52
cgv::utils::big_binary_file::read_array
bool read_array(T *a, unsigned int n)
read an array of typed values
Definition: big_binary_file.h:55
cgv::utils::big_binary_file::close
void close()
close the file
cgv::utils::big_binary_file::~big_binary_file
virtual ~big_binary_file()
close the file (if it is still opened)
cgv::utils::big_binary_file::size
long long size()
return the size of the file in bytes
cgv::utils::big_binary_file::is_open
bool is_open()
return true if the file is opened
cgv::utils::big_binary_file::write
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)
cgv::utils::big_binary_file::open
bool open(MODE m, const std::string &file_name="")
open a file in read or write mode
cgv::utils::big_binary_file::read
bool read(unsigned char *targetbuffer, unsigned long num, unsigned long *numread=NULL)
read num bytes from the file into the targetbuffer
cgv::utils::big_binary_file::write_array
bool write_array(const T *a, unsigned int n)
write an array of typed values
Definition: big_binary_file.h:62
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::utils::big_binary_file::write
bool write(const T &v)
write a typedef value
Definition: big_binary_file.h:59
cgv::utils::big_binary_file
Definition: big_binary_file.h:15
cgv::utils::big_binary_file::position
long long position()
return the position of the file pointer in bytes