43 namespace binfile_details
49 _str(new
std::ofstream(filename,mode)){};
57 _str(new
std::ifstream(filename,mode)){};
65 _str(new
std::fstream(filename,mode)){};
71 template<
typename T1,
typename T2>
inline 72 void read_endian(T1& st, T2& data,
bool switch_endian =
false)
74 int bytes =
sizeof(T2);
75 char *c =
reinterpret_cast<char *
>(&data);
79 for (
int i = bytes - 1; i >= 0; i--)
84 template<
typename T1,
typename T2>
inline 87 int bytes =
sizeof(T2);
88 char *c =
reinterpret_cast<char *
>(&data);
92 for (
int i = bytes - 1; i >= 0; i--)
98 bool exist(
const std::string& name)
100 bool file_exists =
false;
101 ifstream file(name.c_str(), ios::in);
102 if (file.is_open()) {
114 switch_endianity(false),
126 bfstream_base(e), binfile_details::Ofstream_Binfile_Facade(name.c_str()) {}
136 Ofstream_Binfile_Facade::open(name.c_str(),
137 truncate? ios::out | ios::binary | ios::trunc : ios::out | ios::binary);
148 it_assert(
sizeof(
char) ==
sizeof(int8_t),
"Unexpected int8_t size.");
149 put(static_cast<char>(a));
155 it_assert(
sizeof(
char) ==
sizeof(uint8_t),
"Unexpected uint8_t size.");
156 put(static_cast<char>(a));
210 write_endian<bofstream, int32_t>(*
this,
static_cast<int32_t
>(a),
switch_endianity);
217 write(a, strlen(a) + 1);
223 write(a.c_str(), a.size() + 1);
232 bfstream_base(e), binfile_details::Ifstream_Binfile_Facade(name.c_str(), ios::in | ios::binary) {}
247 std::streampos pos1, len;
263 it_assert(
sizeof(
char) ==
sizeof(int8_t),
"Unexpected int8_t size.");
272 it_assert(
sizeof(
char) ==
sizeof(uint8_t),
"Unexpected uint8_t size.");
332 "bifstream::operator>>(): binary input value must be 0 or 1");
345 std::getline(*
stream(), a,
'\0');
354 bfstream_base(e), binfile_details::Fstream_Binfile_Facade(name.c_str(), ios::in | ios::out | ios::binary)
384 std::streampos pos1, len;
400 it_assert(
sizeof(
char) ==
sizeof(int8_t),
"Unexpected int8_t size.");
401 put(static_cast<char>(a));
407 it_assert(
sizeof(
char) ==
sizeof(uint8_t),
"Unexpected uint8_t size.");
408 put(static_cast<char>(a));
462 write_endian<bfstream, int32_t>(*
this,
static_cast<int32_t
>(a),
switch_endianity);
468 write(a, strlen(a) + 1);
474 write(a.c_str(), a.size() + 1);
487 it_assert(
sizeof(
char) ==
sizeof(int8_t),
"Unexpected int8_t size.");
496 it_assert(
sizeof(
char) ==
sizeof(uint8_t),
"Unexpected uint8_t size.");
556 "bfstream::operator>>(): binary input value must be 0 or 1");
569 std::getline(*
stream(), a,
'\0');
void read_endian(T1 &st, T2 &data, bool switch_endian=false)
Read binary data and optionally switch endianness.
bool is_bigendian()
Returns true if machine endianness is BIG_ENDIAN.
bfstream_base(endian e=b_endian)
Class Constructor.
Miscellaneous functions - header file.
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::binary)
Method to open corresponding file.
Ifstream_Binfile_Facade()
Default Constructor.
bfstream & operator>>(char &a)
Reads a char variable from the binary file.
Fstream_Binfile_Facade & write(const char *c, std::streamsize n)
Output multiple characters.
void open(const std::string &name, bool trunc=false, endian e=b_endian)
Open a file for reading and writing and set the endianity.
Fstream_Binfile_Facade & seekg(std::streampos pos)
Set position.
int length()
Returns the length in bytes of the file.
virtual ~Fstream_Binfile_Facade()
Destructor.
std::fstream * stream()
Access to internal stream for derived classes.
#define it_assert(t, s)
Abort if t is not true.
Binary in/out-file Class.
bfstream & operator<<(char a)
Writes an char variable to the binary file.
virtual ~Ofstream_Binfile_Facade()
Destructor.
bofstream & operator<<(char a)
Writes a signed char variable to the binary output file.
bool switch_endianity
Indicates if the endianity of the processed data needs to be changed.
bfstream()
Class Constructor.
void open_readonly(const std::string &name, endian e=b_endian)
Open a file for reading only and set the endianity.
endian native_endianity
The native endianity for this computer architecture.
Ofstream_Binfile_Facade & write(const char *c, std::streamsize n)
Output multiple characters.
Fstream_Binfile_Facade & put(const char c)
Output single char.
void open(const std::string &name, bool trunc=false, endian e=b_endian)
Open a file for writing and set the endianity.
void write_endian(T1 &st, T2 data, bool switch_endian=false)
Write binary data and optionally switch endianness.
Base class for binary file classesThis class serves as a base class for the classes bofstream...
bifstream()
Class Constructor.
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out|std::ios_base::binary)
Method to open corresponding file.
Fstream_Binfile_Facade()
Default Constructor.
Ifstream_Binfile_Facade & seekg(std::streampos pos)
Set position.
endian
Definition of the endian data type.
Fstream_Binfile_Facade & getline(char *s, std::streamsize n)
Get multiple chars to c-string without trailing 0.
bofstream()
Class Constructor.
virtual ~Ifstream_Binfile_Facade()
Destructor.
Binary arithmetic (boolean) class.
Ifstream_Binfile_Facade & getline(char *s, std::streamsize n)
Get multiple chars to c-string without trailing 0.
Ofstream_Binfile_Facade()
Default Constructor.
std::streampos tellg()
Get position.
std::streampos tellg()
Get position.
void open(const std::string &name, endian e=b_endian)
Open a file for reading and set the endianity.
bifstream & operator>>(char &a)
Reads a signed char variable from the binary input file.
Binary file formats definitions.
int length()
Returns the length in bytes of the file.
bool exist(const std::string &name)
Checks if a file named name already exists on the disk.
Ofstream_Binfile_Facade & put(const char c)
Output single char.
std::ifstream * stream()
Access to internal stream for derived classes.