Pol  Revision:f37d500
binaryfile.cpp
Go to the documentation of this file.
1 
10 #include "binaryfile.h"
11 
12 #include <stdexcept>
13 
14 #include "passert.h"
15 #include "strutil.h"
16 
17 namespace Pol
18 {
19 namespace Clib
20 {
22 
23 BinaryFile::BinaryFile( const std::string& filename, std::ios::openmode mode ) : _filename( "" )
24 {
25  Open( filename, mode );
26 }
27 
29 
30 void BinaryFile::Open( const std::string& filename, std::ios::openmode mode )
31 {
32  passert( !_file.is_open() );
33 
34  _filename = filename;
35 
36  _file.open( _filename.c_str(), mode | std::ios::binary );
37  if ( !_file.is_open() )
38  throw std::runtime_error( "BinaryFile::Open('" + _filename + ", " + hexint( mode ) +
39  ") failed." );
40 }
41 
43 {
44  passert( _file.is_open() );
45 
46  _file.close();
47  _filename = "";
48 }
49 
50 void BinaryFile::Seek( std::fstream::pos_type abs_offset )
51 {
52  passert( _file.is_open() );
53 
54  if ( !_file.seekg( abs_offset, std::ios::beg ) )
55  throw std::runtime_error( "BinaryFile::Seek('" + _filename + "') failed." );
56 }
57 
58 void BinaryFile::ReadBuffer( void* buffer, std::streamsize length )
59 {
60  if ( !_file.read( reinterpret_cast<char*>( buffer ), length ) )
61  throw std::runtime_error( "BinaryFile::Read('" + _filename + "') failed to read " +
62  decint( static_cast<int>( length ) ) + " bytes." );
63 }
64 
65 std::fstream::pos_type BinaryFile::FileSize()
66 {
67  passert( _file.is_open() );
68 
69  std::fstream::pos_type save_pos = _file.tellg();
70  if ( save_pos == std::fstream::pos_type( -1 ) )
71  throw std::runtime_error( "BinaryFile::FileSize('" + _filename +
72  "' failed to determine current position." );
73 
74  if ( !_file.seekg( 0, std::ios::end ) )
75  throw std::runtime_error( "BinaryFile::FileSize('" + _filename +
76  "') failed to seek to end of file." );
77  std::fstream::pos_type size = _file.tellg();
78  if ( size == std::fstream::pos_type( -1 ) )
79  throw std::runtime_error( "BinaryFile::FileSize('" + _filename +
80  "') could not determine file size." );
81 
82  if ( !_file.seekg( save_pos ) )
83  throw std::runtime_error( "BinaryFile::FileSize('" + _filename +
84  "') failed to seek to original position." );
85 
86  return size;
87 }
88 
89 size_t BinaryFile::GetElementCount( size_t elemsize )
90 {
91  std::fstream::pos_type filesize = FileSize();
92  if ( ( filesize % elemsize ) != 0 )
93  {
94  throw std::runtime_error( _filename +
95  " does not contain an integral number of elements of size " +
96  decint( elemsize ) );
97  }
98  return static_cast<unsigned int>( filesize / elemsize );
99 }
100 
102 {
103  size_t size = sizeof( *this ) + _filename.capacity();
104  return size;
105 }
106 }
107 }
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
char * binary(unsigned int val, int nbits)
std::string decint(unsigned short v)
Definition: strutil.cpp:64
std::fstream::pos_type FileSize()
Definition: binaryfile.cpp:65
std::string hexint(unsigned short v)
Definition: strutil.cpp:23
unsigned char buffer[10000]
Definition: UoToolMain.cpp:109
int filesize(const char *fname)
Definition: fileutil.cpp:127
void ReadBuffer(void *buffer, std::streamsize length)
Definition: binaryfile.cpp:58
#define passert(exp)
Definition: passert.h:62
std::fstream _file
Definition: binaryfile.h:57
void Seek(std::fstream::pos_type abs_offset)
Definition: binaryfile.cpp:50
Definition: berror.cpp:12