2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * binio.h - Binary stream I/O classes
17 * Copyright (C) 2002, 2003 Simon Peter <dn.tlp@gmx.net>
23 /***** Configuration *****/
25 // BINIO_ENABLE_STRING - Build std::string supporting methods
27 // Set to 1 to build std::string supporting methods. You need the STL to
29 #define BINIO_ENABLE_STRING 1
31 // BINIO_ENABLE_IOSTREAM - Build iostream wrapper classes
33 // Set to 1 to build the iostream wrapper classes. You need the standard
34 // C++ library to do this.
35 #define BINIO_ENABLE_IOSTREAM 1
37 // BINIO_ISO_STDLIB - Build with ISO C++ standard library compliance
39 // Set to 1 to build for the ISO standard C++ library (i.e. namespaces, STL and
40 // templatized iostream). Set to 0 to build for the traditional C++ library.
41 #define BINIO_ISO_STDLIB 1
43 // BINIO_WITH_MATH - Build with 'math.h' dependency to allow float conversions
45 // Set to 1 to also build routines that depend on the 'math.h' standard C header
46 // file (this sometimes also implies a 'libm' or 'libmath' dependency). These
47 // routines are needed in order to write IEEE-754 floating-point numbers on a
48 // system that doesn't support this format natively. For only reading these
49 // numbers, however, these routines are not needed. If set to 0, writing
50 // IEEE-754 numbers on an incompatible system will be disabled.
51 #define BINIO_WITH_MATH 1
53 /***** Implementation *****/
55 // Disable annoying multiple inheritance compiler warning on MSVC6
57 # pragma warning(disable: 4250)
60 #if BINIO_ENABLE_STRING
82 typedef enum { Set, Add, End } Offset;
83 typedef enum { Single, Double } FType;
89 void setFlag(Flag f, bool set = true);
95 virtual void seek(long, Offset = Set) = 0;
96 virtual long pos() = 0;
99 typedef long long Int;
100 typedef long double Float;
101 typedef unsigned char Byte; // has to be unsigned!
106 static const Flags system_flags;
109 // Some math.h emulation functions...
111 Float pow(Float base, signed int exp);
112 Float ldexp(Float x, signed int exp) { return x * pow(2, exp); }
116 static const Flags detect_system_flags();
119 class binistream: virtual public binio
123 virtual ~binistream();
125 Int readInt(unsigned int size);
126 Float readFloat(FType ft);
127 unsigned long readString(char *str, unsigned long amount);
128 unsigned long readString(char *str, unsigned long maxlen, const char delim);
129 #if BINIO_ENABLE_STRING
130 std::string readString(const char delim = '\0');
133 Int peekInt(unsigned int size);
134 Float peekFloat(FType ft);
137 void ignore(unsigned long amount = 1);
140 virtual Byte getByte() = 0;
143 Float ieee_single2float(Byte *data);
144 Float ieee_double2float(Byte *data);
147 class binostream: virtual public binio
151 virtual ~binostream();
153 void writeInt(Int val, unsigned int size);
154 void writeFloat(Float f, FType ft);
155 unsigned long writeString(const char *str, unsigned long amount = 0);
156 #if BINIO_ENABLE_STRING
157 unsigned long writeString(const std::string &str);
161 virtual void putByte(Byte) = 0;
164 void float2ieee_single(Float f, Byte *data);
165 void float2ieee_double(Float f, Byte *data);
168 class binstream: public binistream, public binostream
172 virtual ~binstream();