00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef H_BINIO_BINIO
00021 #define H_BINIO_BINIO
00022
00023 #include "arts.h"
00024
00025
00026
00027
00028
00029
00030
00031 #define BINIO_ENABLE_STRING 1
00032
00033
00034
00035
00036
00037 #define BINIO_ENABLE_IOSTREAM 1
00038
00039
00040
00041
00042
00043 #define BINIO_ISO_STDLIB 1
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #define BINIO_WITH_MATH 1
00054
00055
00056
00057
00058 #ifdef _MSC_VER
00059 # pragma warning(disable: 4250)
00060 #endif
00061
00062 #if BINIO_ENABLE_STRING
00063 #include <string>
00064 #endif
00065
00066 class binio
00067 {
00068 public:
00069 typedef enum {
00070 BigEndian = 1 << 0,
00071 FloatIEEE = 1 << 1
00072 } Flag;
00073
00074 typedef enum {
00075 NoError = 0,
00076 Fatal = 1 << 0,
00077 Unsupported = 1 << 1,
00078 NotOpen = 1 << 2,
00079 Denied = 1 << 3,
00080 NotFound = 1 << 4,
00081 Eof = 1 << 5
00082 } ErrorCode;
00083
00084 typedef enum { Set, Add, End } Offset;
00085 typedef enum { Single, Double } FType;
00086 typedef int Error;
00087
00088 binio();
00089 virtual ~binio();
00090
00091 void setFlag(Flag f, bool set = true);
00092 bool getFlag(Flag f);
00093
00094 Error error();
00095 bool eof();
00096
00097 virtual void seek(long, Offset = Set) = 0;
00098 virtual long pos() = 0;
00099
00100 protected:
00101 typedef long Int;
00102 typedef double Float;
00103 typedef unsigned char Byte;
00104
00105 typedef int Flags;
00106
00107 Flags my_flags;
00108 static const Flags system_flags;
00109 Error err;
00110
00111
00112 #if !BINIO_WITH_MATH
00113 Float pow(Float base, signed int exp);
00114 Float ldexp(Float x, signed int exp) { return x * pow(2, exp); }
00115 #endif
00116
00117 private:
00118 static Flags detect_system_flags();
00119 };
00120
00121 class binistream: virtual public binio
00122 {
00123 public:
00124 binistream();
00125 virtual ~binistream();
00126
00127 Int readInt(unsigned int size);
00128 Float readFloat(FType ft);
00129 unsigned long readString(char *str, unsigned long amount);
00130 unsigned long readString(char *str, unsigned long maxlen, const char delim);
00131 #if BINIO_ENABLE_STRING
00132 std::string readString(const char delim = '\0');
00133 #endif
00134
00135 Int peekInt(unsigned int size);
00136 Float peekFloat(FType ft);
00137
00138 bool ateof();
00139 void ignore(unsigned long amount = 1);
00140
00141 protected:
00142 virtual Byte getByte() = 0;
00143 virtual void getRaw(char *c, streamsize n) = 0;
00144
00145 private:
00146 Float ieee_single2float(Byte *data);
00147 Float ieee_double2float(Byte *data);
00148 };
00149
00150 class binostream: virtual public binio
00151 {
00152 public:
00153 binostream();
00154 virtual ~binostream();
00155
00156 void writeInt(Int val, unsigned int size);
00157 void writeFloat(Float f, FType ft);
00158 unsigned long writeString(const char *str, unsigned long amount = 0);
00159 #if BINIO_ENABLE_STRING
00160 unsigned long writeString(const std::string &str);
00161 #endif
00162
00163 protected:
00164 virtual void putByte(Byte) = 0;
00165 virtual void putRaw(const char *c, streamsize n) = 0;
00166
00167 private:
00168 void float2ieee_single(Float f, Byte *data);
00169 void float2ieee_double(Float f, Byte *data);
00170 };
00171
00172 class binstream: public binistream, public binostream
00173 {
00174 public:
00175 binstream();
00176 virtual ~binstream();
00177 };
00178
00179 #endif