123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- #ifndef H_BINIO_BINIO
- #define H_BINIO_BINIO
- #ifdef _MSC_VER
- #pragma warning (disable:4244)
- #pragma warning (disable:4996)
- #pragma warning (disable:4267)
- #endif
- #define BINIO_ENABLE_STRING 1
- #define BINIO_ENABLE_IOSTREAM 1
- #define BINIO_ISO_STDLIB 1
- #ifdef _MSC_VER
- # pragma warning(disable: 4250)
- #else
- # define __int64 long long
- #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 __int64 Int;
- typedef long double 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
|