Pol  Revision:cb584c9
binaryfile.h
Go to the documentation of this file.
1 
7 #ifndef CLIB_BINARYFILE_H
8 #define CLIB_BINARYFILE_H
9 
10 #include <fstream>
11 #include <string>
12 #include <vector>
13 
14 namespace Pol
15 {
16 namespace Clib
17 {
19 {
20 public:
21  BinaryFile();
22  BinaryFile( const std::string& filename, std::ios::openmode mode );
23  ~BinaryFile();
24 
25  void Open( const std::string& filename, std::ios::openmode mode );
26  void Close();
27 
28  template <class T>
29  void Read( T& val )
30  {
31  ReadBuffer( &val, sizeof( T ) );
32  }
33 
34  template <class T>
35  void Read( T* arr, size_t count )
36  {
37  ReadBuffer( arr, sizeof( T ) * count );
38  }
39 
40  template <class T>
41  void ReadVector( std::vector<T>& vec )
42  {
43  size_t count = GetElementCount( sizeof( T ) );
44  vec.resize( count );
45  if ( count > 0 )
46  Read( &vec[0], count );
47  }
48 
49  void Seek( std::fstream::pos_type abs_offset );
50  std::fstream::pos_type FileSize();
51  size_t sizeEstimate() const;
52 
53 private:
54  void ReadBuffer( void* buffer, std::streamsize length );
55  size_t GetElementCount( size_t elemsize );
56 
57  std::fstream _file;
58  std::string _filename;
59 };
60 }
61 }
62 #endif
size_t sizeEstimate() const
Definition: binaryfile.cpp:101
std::string _filename
Definition: binaryfile.h:58
size_t GetElementCount(size_t elemsize)
Definition: binaryfile.cpp:89
void Open(const std::string &filename, std::ios::openmode mode)
Definition: binaryfile.cpp:30
std::fstream::pos_type FileSize()
Definition: binaryfile.cpp:65
unsigned char buffer[10000]
Definition: UoToolMain.cpp:109
void ReadBuffer(void *buffer, std::streamsize length)
Definition: binaryfile.cpp:58
std::fstream _file
Definition: binaryfile.h:57
void Seek(std::fstream::pos_type abs_offset)
Definition: binaryfile.cpp:50
void Read(T &val)
Definition: binaryfile.h:29
void ReadVector(std::vector< T > &vec)
Definition: binaryfile.h:41
Definition: berror.cpp:12
void Read(T *arr, size_t count)
Definition: binaryfile.h:35