]> 4ch.mooo.com Git - 16.git/blob - 16/adplug/libbinio/src/binio.h
renamed: 16/adplug/adplug-2.2.1/.DS_Store -> 16/adplug/adplug/.DS_Store
[16.git] / 16 / adplug / libbinio / src / binio.h
1 /* -*-C++-*-
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.
6  * 
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.
11  * 
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
15  *
16  * binio.h - Binary stream I/O classes
17  * Copyright (C) 2002, 2003 Simon Peter <dn.tlp@gmx.net>
18  */
19
20 #ifndef H_BINIO_BINIO
21 #define H_BINIO_BINIO
22
23 /***** Configuration *****/
24
25 // BINIO_ENABLE_STRING - Build std::string supporting methods
26 //
27 // Set to 1 to build std::string supporting methods. You need the STL to
28 // do this.
29 #define BINIO_ENABLE_STRING     1
30
31 // BINIO_ENABLE_IOSTREAM - Build iostream wrapper classes
32 //
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
36
37 // BINIO_ISO_STDLIB - Build with ISO C++ standard library compliance
38 //
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
42
43 // BINIO_WITH_MATH - Build with 'math.h' dependency to allow float conversions
44 //
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
52
53 /***** Implementation *****/
54
55 // Disable annoying multiple inheritance compiler warning on MSVC6
56 #ifdef _MSC_VER
57 #       pragma warning(disable: 4250)
58 #endif
59
60 #if BINIO_ENABLE_STRING
61 #include <string>
62 #endif
63
64 class binio
65 {
66 public:
67   typedef enum {
68     BigEndian   = 1 << 0,
69     FloatIEEE   = 1 << 1
70   } Flag;
71
72   typedef enum {
73     NoError     = 0,
74     Fatal       = 1 << 0,
75     Unsupported = 1 << 1,
76     NotOpen     = 1 << 2,
77     Denied      = 1 << 3,
78     NotFound    = 1 << 4,
79     Eof         = 1 << 5
80   } ErrorCode;
81
82   typedef enum { Set, Add, End } Offset;
83   typedef enum { Single, Double } FType;
84   typedef int Error;
85
86   binio();
87   virtual ~binio();
88
89   void setFlag(Flag f, bool set = true);
90   bool getFlag(Flag f);
91
92   Error error();
93   bool eof();
94
95   virtual void seek(long, Offset = Set) = 0;
96   virtual long pos() = 0;
97
98 protected:
99   typedef long long     Int;
100   typedef long double   Float;
101   typedef unsigned char Byte;   // has to be unsigned!
102
103   typedef int           Flags;
104
105   Flags                 my_flags;
106   static const Flags    system_flags;
107   Error                 err;
108
109   // Some math.h emulation functions...
110 #if !BINIO_WITH_MATH
111   Float pow(Float base, signed int exp);
112   Float ldexp(Float x, signed int exp) { return x * pow(2, exp); }
113 #endif
114
115 private:
116   static const Flags detect_system_flags();
117 };
118
119 class binistream: virtual public binio
120 {
121 public:
122   binistream();
123   virtual ~binistream();
124
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');
131 #endif
132
133   Int peekInt(unsigned int size);
134   Float peekFloat(FType ft);
135
136   bool ateof();
137   void ignore(unsigned long amount = 1);
138
139 protected:
140   virtual Byte getByte() = 0;
141
142 private:
143   Float ieee_single2float(Byte *data);
144   Float ieee_double2float(Byte *data);
145 };
146
147 class binostream: virtual public binio
148 {
149 public:
150   binostream();
151   virtual ~binostream();
152
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);
158 #endif
159
160 protected:
161   virtual void putByte(Byte) = 0;
162
163 private:
164   void float2ieee_single(Float f, Byte *data);
165   void float2ieee_double(Float f, Byte *data);
166 };
167
168 class binstream: public binistream, public binostream
169 {
170 public:
171   binstream();
172   virtual ~binstream();
173 };
174
175 #endif