binfile.cpp
Go to the documentation of this file.
#include <itpp/base/binfile.h>
#include <itpp/base/math/misc.h>
#include <cstring>
using std::ofstream;
using std::ifstream;
using std::fstream;
using std::ios;
namespace itpp
{
namespace binfile_details
{
Ofstream_Binfile_Facade::Ofstream_Binfile_Facade ( ) : _str(new std::ofstream()) {};
Ofstream_Binfile_Facade::Ofstream_Binfile_Facade ( const char * filename, std::ios_base::openmode mode) :
_str(new std::ofstream(filename,mode)){};
Ofstream_Binfile_Facade::~Ofstream_Binfile_Facade() {delete _str;}
Ifstream_Binfile_Facade::Ifstream_Binfile_Facade ( ) : _str(new std::ifstream()) {};
Ifstream_Binfile_Facade::Ifstream_Binfile_Facade ( const char * filename, std::ios_base::openmode mode) :
_str(new std::ifstream(filename,mode)){};
Ifstream_Binfile_Facade::~Ifstream_Binfile_Facade() {delete _str;}
Fstream_Binfile_Facade::Fstream_Binfile_Facade ( ) : _str(new std::fstream()) {};
Fstream_Binfile_Facade::Fstream_Binfile_Facade ( const char * filename, std::ios_base::openmode mode) :
_str(new std::fstream(filename,mode)){};
Fstream_Binfile_Facade::~Fstream_Binfile_Facade() {delete _str;}
}
template<typename T1, typename T2> inline
void read_endian(T1& st, T2& data, bool switch_endian = false)
{
int bytes = sizeof(T2);
char *c = reinterpret_cast<char *>(&data);
if (!switch_endian)
st.read(c, bytes);
else
for (int i = bytes - 1; i >= 0; i--)
st.get(c[i]);
}
template<typename T1, typename T2> inline
void write_endian(T1& st, T2 data, bool switch_endian = false)
{
int bytes = sizeof(T2);
char *c = reinterpret_cast<char *>(&data);
if (!switch_endian)
st.write(c, bytes);
else
for (int i = bytes - 1; i >= 0; i--)
st.put(c[i]);
}
// ----------------------------------------------------------------------
bool exist(const std::string& name)
{
bool file_exists = false;
ifstream file(name.c_str(), ios::in);
if (file.is_open()) {
file_exists = true;
}
file.close();
return file_exists;
}
// ----------------------------------------------------------------------
// bfstream_base
// ----------------------------------------------------------------------
bfstream_base::bfstream_base(endian e):
switch_endianity(false),
native_endianity(is_bigendian() ? b_endian : l_endian)
{
if (native_endianity != e)
switch_endianity = true;
}
// ----------------------------------------------------------------------
// bofstream
// ----------------------------------------------------------------------
bofstream::bofstream(const std::string& name, endian e) :
bfstream_base(e), binfile_details::Ofstream_Binfile_Facade(name.c_str()) {}
bofstream::bofstream() : bfstream_base(), binfile_details::Ofstream_Binfile_Facade() {}
void bofstream::open(const std::string& name, bool truncate, endian e)
{
if (native_endianity != e)
switch_endianity = true;
else
switch_endianity = false;
Ofstream_Binfile_Facade::open(name.c_str(),
truncate? ios::out | ios::binary | ios::trunc : ios::out | ios::binary);
}
bofstream& bofstream::operator<<(char a)
{
put(a);
return *this;
}
bofstream& bofstream::operator<<(int8_t a)
{
it_assert(sizeof(char) == sizeof(int8_t),"Unexpected int8_t size.");
put(static_cast<char>(a));
return *this;
}
bofstream& bofstream::operator<<(uint8_t a)
{
it_assert(sizeof(char) == sizeof(uint8_t),"Unexpected uint8_t size.");
put(static_cast<char>(a));
return *this;
}
bofstream& bofstream::operator<<(int16_t a)
{
write_endian<bofstream, int16_t>(*this, a, switch_endianity);
return *this;
}
bofstream& bofstream::operator<<(uint16_t a)
{
write_endian<bofstream, uint16_t>(*this, a, switch_endianity);
return *this;
}
bofstream& bofstream::operator<<(int32_t a)
{
write_endian<bofstream, int32_t>(*this, a, switch_endianity);
return *this;
}
bofstream& bofstream::operator<<(uint32_t a)
{
write_endian<bofstream, uint32_t>(*this, a, switch_endianity);
return *this;
}
bofstream& bofstream::operator<<(int64_t a)
{
write_endian<bofstream, int64_t>(*this, a, switch_endianity);
return *this;
}
bofstream& bofstream::operator<<(uint64_t a)
{
write_endian<bofstream, uint64_t>(*this, a, switch_endianity);
return *this;
}
bofstream& bofstream::operator<<(float a)
{
write_endian<bofstream, float>(*this, a, switch_endianity);
return *this;
}
bofstream& bofstream::operator<<(double a)
{
write_endian<bofstream, double>(*this, a, switch_endianity);
return *this;
}
bofstream& bofstream::operator<<(bin a)
{
write_endian<bofstream, int32_t>(*this, static_cast< int32_t >(a), switch_endianity);
return *this;
}
bofstream& bofstream::operator<<(const char *a)
{
write(a, strlen(a) + 1);
return *this;
}
bofstream& bofstream::operator<<(const std::string& a)
{
write(a.c_str(), a.size() + 1);
return *this;
}
// ----------------------------------------------------------------------
// bifstream
// ----------------------------------------------------------------------
bifstream::bifstream(const std::string& name, endian e) :
bfstream_base(e), binfile_details::Ifstream_Binfile_Facade(name.c_str(), ios::in | ios::binary) {}
bifstream::bifstream() : bfstream_base(), binfile_details::Ifstream_Binfile_Facade() {}
void bifstream::open(const std::string& name, endian e)
{
if (native_endianity != e)
switch_endianity = true;
else
switch_endianity = false;
binfile_details::Ifstream_Binfile_Facade::open(name.c_str(), ios::in | ios::binary);
}
int bifstream::length() // in bytes
{
std::streampos pos1, len;
pos1 = tellg();
seekg(0, ios::end);
len = tellg();
seekg(pos1);
return int(len);
}
bifstream& bifstream::operator>>(char& a)
{
get(a);
return *this;
}
bifstream& bifstream::operator>>(int8_t& a)
{
it_assert(sizeof(char) == sizeof(int8_t),"Unexpected int8_t size.");
char tmp;
get(tmp);
a = tmp;
return *this;
}
bifstream& bifstream::operator>>(uint8_t& a)
{
it_assert(sizeof(char) == sizeof(uint8_t),"Unexpected uint8_t size.");
char tmp;
get(tmp);
a = tmp;
return *this;
}
bifstream& bifstream::operator>>(int16_t& a)
{
read_endian<bifstream, int16_t>(*this, a, switch_endianity);
return *this;
}
bifstream& bifstream::operator>>(uint16_t& a)
{
read_endian<bifstream, uint16_t>(*this, a, switch_endianity);
return *this;
}
bifstream& bifstream::operator>>(int32_t& a)
{
read_endian<bifstream, int32_t>(*this, a, switch_endianity);
return *this;
}
bifstream& bifstream::operator>>(uint32_t& a)
{
read_endian<bifstream, uint32_t>(*this, a, switch_endianity);
return *this;
}
bifstream& bifstream::operator>>(int64_t& a)
{
read_endian<bifstream, int64_t>(*this, a, switch_endianity);
return *this;
}
bifstream& bifstream::operator>>(uint64_t& a)
{
read_endian<bifstream, uint64_t>(*this, a, switch_endianity);
return *this;
}
bifstream& bifstream::operator>>(float& a)
{
read_endian<bifstream, float>(*this, a, switch_endianity);
return *this;
}
bifstream& bifstream::operator>>(double& a)
{
read_endian<bifstream, double>(*this, a, switch_endianity);
return *this;
}
bifstream& bifstream::operator>>(bin& a)
{
uint32_t tmp;
read_endian<bifstream, uint32_t>(*this, tmp, switch_endianity);
it_assert((tmp == 0) || (tmp == 1),
"bifstream::operator>>(): binary input value must be 0 or 1");
a = tmp;
return *this;
}
bifstream& bifstream::operator>>(char *a)
{
getline(a, '\0');
return *this;
}
bifstream& bifstream::operator>>(std::string& a)
{
std::getline(*stream(), a, '\0');
return *this;
}
// ----------------------------------------------------------------------
// bfstream
// ----------------------------------------------------------------------
bfstream::bfstream(const std::string& name, endian e) :
bfstream_base(e), binfile_details::Fstream_Binfile_Facade(name.c_str(), ios::in | ios::out | ios::binary)
{}
bfstream::bfstream() : bfstream_base(), binfile_details::Fstream_Binfile_Facade() {}
void bfstream::open(const std::string& name, bool trnc, endian e)
{
if (native_endianity != e)
switch_endianity = true;
else
switch_endianity = false;
if (trnc)
binfile_details::Fstream_Binfile_Facade::open(name.c_str(), ios::in | ios::out | ios::binary
| ios::trunc);
else
binfile_details::Fstream_Binfile_Facade::open(name.c_str(), ios::in | ios::out | ios::binary);
}
void bfstream::open_readonly(const std::string& name, endian e)
{
if (native_endianity != e)
switch_endianity = true;
else
switch_endianity = false;
binfile_details::Fstream_Binfile_Facade::open(name.c_str(), ios::in | ios::binary);
}
int bfstream::length() // in bytes
{
std::streampos pos1, len;
pos1 = tellg();
seekg(0, ios::end);
len = tellg();
seekg(pos1);
return int(len);
}
bfstream& bfstream::operator<<(char a)
{
put(a);
return *this;
}
bfstream& bfstream::operator<<(int8_t a)
{
it_assert(sizeof(char) == sizeof(int8_t),"Unexpected int8_t size.");
put(static_cast<char>(a));
return *this;
}
bfstream& bfstream::operator<<(uint8_t a)
{
it_assert(sizeof(char) == sizeof(uint8_t),"Unexpected uint8_t size.");
put(static_cast<char>(a));
return *this;
}
bfstream& bfstream::operator<<(int16_t a)
{
write_endian<bfstream, int16_t>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator<<(uint16_t a)
{
write_endian<bfstream, uint16_t>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator<<(int32_t a)
{
write_endian<bfstream, int32_t>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator<<(uint32_t a)
{
write_endian<bfstream, uint32_t>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator<<(int64_t a)
{
write_endian<bfstream, int64_t>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator<<(uint64_t a)
{
write_endian<bfstream, uint64_t>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator<<(float a)
{
write_endian<bfstream, float>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator<<(double a)
{
write_endian<bfstream, double>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator<<(bin a)
{
write_endian<bfstream, int32_t>(*this, static_cast< int32_t >(a), switch_endianity);
return *this;
}
bfstream& bfstream::operator<<(const char *a)
{
write(a, strlen(a) + 1);
return *this;
}
bfstream& bfstream::operator<<(const std::string& a)
{
write(a.c_str(), a.size() + 1);
return *this;
}
bfstream& bfstream::operator>>(char& a)
{
get(a);
return *this;
}
bfstream& bfstream::operator>>(int8_t& a)
{
it_assert(sizeof(char) == sizeof(int8_t),"Unexpected int8_t size.");
char tmp;
get(tmp);
a = tmp;
return *this;
}
bfstream& bfstream::operator>>(uint8_t& a)
{
it_assert(sizeof(char) == sizeof(uint8_t),"Unexpected uint8_t size.");
char tmp;
get(tmp);
a = tmp;
return *this;
}
bfstream& bfstream::operator>>(int16_t& a)
{
read_endian<bfstream, int16_t>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator>>(uint16_t& a)
{
read_endian<bfstream, uint16_t>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator>>(int32_t& a)
{
read_endian<bfstream, int32_t>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator>>(uint32_t& a)
{
read_endian<bfstream, uint32_t>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator>>(int64_t& a)
{
read_endian<bfstream, int64_t>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator>>(uint64_t& a)
{
read_endian<bfstream, uint64_t>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator>>(float& a)
{
read_endian<bfstream, float>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator>>(double& a)
{
read_endian<bfstream, double>(*this, a, switch_endianity);
return *this;
}
bfstream& bfstream::operator>>(bin& a)
{
uint32_t tmp;
read_endian<bfstream, uint32_t>(*this, tmp, switch_endianity);
it_assert((tmp == 0) || (tmp == 1),
"bfstream::operator>>(): binary input value must be 0 or 1");
a = tmp;
return *this;
}
bfstream& bfstream::operator>>(char *a)
{
getline(a, '\0');
return *this;
}
bfstream& bfstream::operator>>(std::string& a)
{
std::getline(*stream(), a, '\0');
return *this;
}
} // namespace itpp
binfile.h
Binary file formats definitions.
itpp::Array
General array class.
Definition array.h:105
itpp::Array::size
int size() const
Returns the number of data elements in the array object.
Definition array.h:155
itpp::bfstream_base
Base class for binary file classes.
Definition binfile.h:60
itpp::bfstream_base::endian
endian
Definition of the endian data type.
Definition binfile.h:74
itpp::bfstream_base::bfstream_base
bfstream_base(endian e=b_endian)
Class Constructor.
Definition binfile.cpp:113
itpp::bfstream_base::switch_endianity
bool switch_endianity
Indicates if the endianity of the processed data needs to be changed.
Definition binfile.h:125
itpp::bfstream_base::native_endianity
endian native_endianity
The native endianity for this computer architecture.
Definition binfile.h:127
itpp::bfstream
Binary in/out-file Class.
Definition binfile.h:604
itpp::bfstream::open_readonly
void open_readonly(const std::string &name, endian e=b_endian)
Open a file for reading only and set the endianity.
Definition binfile.cpp:373
itpp::bfstream::operator>>
bfstream & operator>>(char &a)
Reads a char variable from the binary file.
Definition binfile.cpp:479
itpp::bfstream::operator<<
bfstream & operator<<(char a)
Writes an char variable to the binary file.
Definition binfile.cpp:392
itpp::bfstream::length
int length()
Returns the length in bytes of the file.
Definition binfile.cpp:382
itpp::bfstream::bfstream
bfstream()
Class Constructor.
Definition binfile.cpp:357
itpp::bfstream::open
void open(const std::string &name, bool trunc=false, endian e=b_endian)
Open a file for reading and writing and set the endianity.
Definition binfile.cpp:359
itpp::bifstream
Binary Infile Class.
Definition binfile.h:539
itpp::bifstream::open
void open(const std::string &name, endian e=b_endian)
Open a file for reading and set the endianity.
Definition binfile.cpp:236
itpp::bifstream::bifstream
bifstream()
Class Constructor.
Definition binfile.cpp:234
itpp::bifstream::length
int length()
Returns the length in bytes of the file.
Definition binfile.cpp:245
itpp::bifstream::operator>>
bifstream & operator>>(char &a)
Reads a signed char variable from the binary input file.
Definition binfile.cpp:255
itpp::bin
Binary arithmetic (boolean) class.
Definition binary.h:57
itpp::binfile_details::Fstream_Binfile_Facade::seekg
Fstream_Binfile_Facade & seekg(std::streampos pos)
Set position.
Definition binfile.h:428
itpp::binfile_details::Fstream_Binfile_Facade::put
Fstream_Binfile_Facade & put(const char c)
Output single char.
Definition binfile.h:379
itpp::binfile_details::Fstream_Binfile_Facade::get
int get()
Get single char.
Definition binfile.h:394
itpp::binfile_details::Fstream_Binfile_Facade::~Fstream_Binfile_Facade
virtual ~Fstream_Binfile_Facade()
Destructor.
Definition binfile.cpp:67
itpp::binfile_details::Fstream_Binfile_Facade::getline
Fstream_Binfile_Facade & getline(char *s, std::streamsize n)
Get multiple chars to c-string without trailing 0.
Definition binfile.h:404
itpp::binfile_details::Fstream_Binfile_Facade::open
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.
Definition binfile.h:369
itpp::binfile_details::Fstream_Binfile_Facade::Fstream_Binfile_Facade
Fstream_Binfile_Facade()
Default Constructor.
Definition binfile.cpp:62
itpp::binfile_details::Fstream_Binfile_Facade::stream
std::fstream * stream()
Access to internal stream for derived classes.
Definition binfile.h:464
itpp::binfile_details::Fstream_Binfile_Facade::tellg
std::streampos tellg()
Get position.
Definition binfile.h:426
itpp::binfile_details::Fstream_Binfile_Facade::write
Fstream_Binfile_Facade & write(const char *c, std::streamsize n)
Output multiple characters.
Definition binfile.h:376
itpp::binfile_details::Ifstream_Binfile_Facade::stream
std::ifstream * stream()
Access to internal stream for derived classes.
Definition binfile.h:332
itpp::binfile_details::Ifstream_Binfile_Facade::Ifstream_Binfile_Facade
Ifstream_Binfile_Facade()
Default Constructor.
Definition binfile.cpp:54
itpp::binfile_details::Ifstream_Binfile_Facade::seekg
Ifstream_Binfile_Facade & seekg(std::streampos pos)
Set position.
Definition binfile.h:297
itpp::binfile_details::Ifstream_Binfile_Facade::getline
Ifstream_Binfile_Facade & getline(char *s, std::streamsize n)
Get multiple chars to c-string without trailing 0.
Definition binfile.h:274
itpp::binfile_details::Ifstream_Binfile_Facade::open
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::binary)
Method to open corresponding file.
Definition binfile.h:256
itpp::binfile_details::Ifstream_Binfile_Facade::~Ifstream_Binfile_Facade
virtual ~Ifstream_Binfile_Facade()
Destructor.
Definition binfile.cpp:59
itpp::binfile_details::Ifstream_Binfile_Facade::get
int get()
Get single char.
Definition binfile.h:264
itpp::binfile_details::Ifstream_Binfile_Facade::tellg
std::streampos tellg()
Get position.
Definition binfile.h:295
itpp::binfile_details::Ofstream_Binfile_Facade::Ofstream_Binfile_Facade
Ofstream_Binfile_Facade()
Default Constructor.
Definition binfile.cpp:46
itpp::binfile_details::Ofstream_Binfile_Facade::put
Ofstream_Binfile_Facade & put(const char c)
Output single char.
Definition binfile.h:175
itpp::binfile_details::Ofstream_Binfile_Facade::~Ofstream_Binfile_Facade
virtual ~Ofstream_Binfile_Facade()
Destructor.
Definition binfile.cpp:51
itpp::binfile_details::Ofstream_Binfile_Facade::write
Ofstream_Binfile_Facade & write(const char *c, std::streamsize n)
Output multiple characters.
Definition binfile.h:172
itpp::bofstream
Binary Outfile Class.
Definition binfile.h:475
itpp::bofstream::operator<<
bofstream & operator<<(char a)
Writes a signed char variable to the binary output file.
Definition binfile.cpp:140
itpp::bofstream::open
void open(const std::string &name, bool trunc=false, endian e=b_endian)
Open a file for writing and set the endianity.
Definition binfile.cpp:130
itpp::bofstream::bofstream
bofstream()
Class Constructor.
Definition binfile.cpp:128
it_assert
#define it_assert(t, s)
Abort if t is not true.
Definition itassert.h:94
itpp::exist
bool exist(const std::string &name)
Checks if a file named name already exists on the disk.
Definition binfile.cpp:98
itpp::is_bigendian
bool is_bigendian()
Returns true if machine endianness is BIG_ENDIAN.
Definition misc.cpp:50
misc.h
Miscellaneous functions - header file.
itpp
itpp namespace
Definition itmex.h:37
itpp::read_endian
void read_endian(T1 &st, T2 &data, bool switch_endian=false)
Read binary data and optionally switch endianness.
Definition binfile.cpp:72
itpp::write_endian
void write_endian(T1 &st, T2 data, bool switch_endian=false)
Write binary data and optionally switch endianness.
Definition binfile.cpp:85
std
STL namespace.

Generated on Mon Jun 10 2024 11:49:16 for IT++ by Doxygen 1.9.8