#ifndef H_BINIO_BINIO
#define H_BINIO_BINIO
#define BINIO_ENABLE_STRING 1
#define BINIO_ENABLE_IOSTREAM 1
#define BINIO_ISO_STDLIB 1
#define BINIO_WITH_MATH 1
#ifdef _MSC_VER
# pragma warning(disable: 4250)
#endif
#if BINIO_ENABLE_STRING
#include <string>
#endif
class binio
{
public:
typedef enum {
BigEndian = 1 << 0,
FloatIEEE = 1 << 1
} Flag;
typedef enum {
NoError = 0,
Fatal = 1 << 0,
Unsupported = 1 << 1,
NotOpen = 1 << 2,
Denied = 1 << 3,
NotFound = 1 << 4,
Eof = 1 << 5
} ErrorCode;
typedef enum { Set, Add, End } Offset;
typedef enum { Single, Double } FType;
typedef int Error;
binio();
virtual ~binio();
void setFlag(Flag f, bool set = true);
bool getFlag(Flag f);
Error error();
bool eof();
virtual void seek(long, Offset = Set) = 0;
virtual long pos() = 0;
protected:
typedef long long Int;
typedef float Float;
typedef unsigned char Byte;
typedef int Flags;
Flags my_flags;
static const Flags system_flags;
Error err;
#if !BINIO_WITH_MATH
Float pow(Float base, signed int exp);
Float ldexp(Float x, signed int exp) { return x * pow(2, exp); }
#endif
private:
static const Flags detect_system_flags();
};
class binistream: virtual public binio
{
public:
binistream();
virtual ~binistream();
Int readInt(unsigned int size);
Float readFloat(FType ft);
unsigned long readString(char *str, unsigned long amount);
unsigned long readString(char *str, unsigned long maxlen, const char delim);
#if BINIO_ENABLE_STRING
std::string readString(const char delim = '\0');
#endif
Int peekInt(unsigned int size);
Float peekFloat(FType ft);
bool ateof();
void ignore(unsigned long amount = 1);
protected:
virtual Byte getByte() = 0;
private:
Float ieee_single2float(Byte *data);
Float ieee_double2float(Byte *data);
};
class binostream: virtual public binio
{
public:
binostream();
virtual ~binostream();
void writeInt(Int val, unsigned int size);
void writeFloat(Float f, FType ft);
unsigned long writeString(const char *str, unsigned long amount = 0);
#if BINIO_ENABLE_STRING
unsigned long writeString(const std::string &str);
#endif
protected:
virtual void putByte(Byte) = 0;
private:
void float2ieee_single(Float f, Byte *data);
void float2ieee_double(Float f, Byte *data);
};
class binstream: public binistream, public binostream
{
public:
binstream();
virtual ~binstream();
};
#endif