Pol  Revision:cb584c9
binaryfilescrobj.h
Go to the documentation of this file.
1 
8 #ifndef BINARYSCROBJ_H
9 #define BINARYSCROBJ_H
10 
11 #ifndef BSCRIPT_BOBJECT_H
12 #include "../bscript/bobject.h"
13 #endif
14 
15 #include <fstream>
16 #include <string>
17 
18 #include "../clib/compilerspecifics.h"
19 #include "../clib/rawtypes.h"
20 
21 namespace Pol
22 {
23 namespace Bscript
24 {
25 class Executor;
26 } // namespace Bscript
27 } // namespace Pol
28 
29 namespace Pol
30 {
31 namespace Core
32 {
33 class BinFile final
34 {
35 public:
36  BinFile() = default;
37  BinFile( const std::string& filename, std::ios::openmode mode );
38  BinFile(const BinFile& ) = delete;
39  BinFile operator=( const BinFile& ) = delete;
40  ~BinFile();
41 
42  bool Open( const std::string& filename, std::ios::openmode mode );
43  void Close();
44 
45  template <class T>
46  bool Read( T& val )
47  {
48  return ReadBuffer( &val, sizeof( T ) );
49  }
50 
51  template <class T>
52  bool Read( T* arr, size_t count )
53  {
54  return ReadBuffer( arr, sizeof( T ) * count );
55  }
56 
57  template <class T>
58  bool Write( T& val )
59  {
60  return WriteBuffer( &val, sizeof( T ) );
61  }
62 
63  bool WriteString( const char* chr, unsigned len );
64  bool Seek( std::fstream::pos_type abs_offset, std::ios::seekdir origin );
65  std::fstream::pos_type FileSize( Bscript::Executor& exec );
66  std::fstream::pos_type Tell();
67  int Peek();
68  void Flush();
69  bool IsOpen();
70 
71 
72 private:
73  bool ReadBuffer( void* buffer, std::streamsize length );
74  bool WriteBuffer( void* buffer, std::streamsize length );
75 
76  std::fstream _file;
77 };
78 
80 {
81 public:
82  BBinaryfile();
83  BBinaryfile( std::string filename, unsigned short mode, bool _bigendian );
84  ~BBinaryfile();
85  virtual Bscript::BObjectRef get_member( const char* membername ) POL_OVERRIDE;
86  virtual Bscript::BObjectRef get_member_id( const int id ) POL_OVERRIDE; // id test
87  virtual Bscript::BObjectImp* call_method( const char* methodname,
89  virtual Bscript::BObjectImp* call_method_id( const int id, Bscript::Executor& ex,
90  bool forcebuiltin = false ) POL_OVERRIDE;
91  virtual Bscript::BObjectImp* copy() const POL_OVERRIDE;
92  virtual std::string getStringRep() const POL_OVERRIDE;
93  virtual size_t sizeEstimate() const POL_OVERRIDE;
94  virtual const char* typeOf() const POL_OVERRIDE { return "BinaryFile"; }
95  virtual u8 typeOfInt() const POL_OVERRIDE { return OTBinaryFile; }
96  virtual bool isTrue() const POL_OVERRIDE;
97  virtual bool operator==( const Bscript::BObjectImp& objimp ) const POL_OVERRIDE;
98 
99 private:
100  mutable BinFile file;
101  std::string _filename;
102  std::ios::openmode openmode;
103  bool bigendian;
104 };
105 }
106 }
107 
108 #endif
unsigned char u8
Definition: rawtypes.h:25
#define POL_OVERRIDE
virtual u8 typeOfInt() const POL_OVERRIDE
bool operator==(T *ptr, const ref_ptr< T > &rptr)
Definition: refptr.h:311
unsigned char buffer[10000]
Definition: UoToolMain.cpp:109
bool Read(T *arr, size_t count)
std::ios::openmode openmode
virtual const char * typeOf() const POL_OVERRIDE
Definition: berror.cpp:12