ARTS  2.2.66
binio.h
Go to the documentation of this file.
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 #include "arts.h"
24 
25 /***** Configuration *****/
26 
27 // BINIO_ENABLE_STRING - Build std::string supporting methods
28 //
29 // Set to 1 to build std::string supporting methods. You need the STL to
30 // do this.
31 #define BINIO_ENABLE_STRING 1
32 
33 // BINIO_ENABLE_IOSTREAM - Build iostream wrapper classes
34 //
35 // Set to 1 to build the iostream wrapper classes. You need the standard
36 // C++ library to do this.
37 #define BINIO_ENABLE_IOSTREAM 1
38 
39 // BINIO_ISO_STDLIB - Build with ISO C++ standard library compliance
40 //
41 // Set to 1 to build for the ISO standard C++ library (i.e. namespaces, STL and
42 // templatized iostream). Set to 0 to build for the traditional C++ library.
43 #define BINIO_ISO_STDLIB 1
44 
45 // BINIO_WITH_MATH - Build with 'math.h' dependency to allow float conversions
46 //
47 // Set to 1 to also build routines that depend on the 'math.h' standard C header
48 // file (this sometimes also implies a 'libm' or 'libmath' dependency). These
49 // routines are needed in order to write IEEE-754 floating-point numbers on a
50 // system that doesn't support this format natively. For only reading these
51 // numbers, however, these routines are not needed. If set to 0, writing
52 // IEEE-754 numbers on an incompatible system will be disabled.
53 #define BINIO_WITH_MATH 1
54 
55 /***** Implementation *****/
56 
57 // Disable annoying multiple inheritance compiler warning on MSVC6
58 #ifdef _MSC_VER
59 # pragma warning(disable: 4250)
60 #endif
61 
62 #if BINIO_ENABLE_STRING
63 #include <string>
64 #endif
65 
66 class binio
67 {
68 public:
69  typedef enum {
70  BigEndian = 1 << 0,
71  FloatIEEE = 1 << 1
72  } Flag;
73 
74  typedef enum {
75  NoError = 0,
76  Fatal = 1 << 0,
77  Unsupported = 1 << 1,
78  NotOpen = 1 << 2,
79  Denied = 1 << 3,
80  NotFound = 1 << 4,
81  Eof = 1 << 5
82  } ErrorCode;
83 
84  typedef enum { Set, Add, End } Offset;
85  typedef enum { Single, Double } FType;
86  typedef int Error;
87 
88  binio();
89  virtual ~binio();
90 
91  void setFlag(Flag f, bool set = true);
92  bool getFlag(Flag f);
93 
94  Error error();
95  bool eof();
96 
97  virtual void seek(long, Offset = Set) = 0;
98  virtual streampos pos() = 0;
99 
100 protected:
101  typedef long Int;
102  typedef double Float;
103  typedef unsigned char Byte; // has to be unsigned!
104 
105  typedef int Flags;
106 
107  Flags my_flags;
108  static const Flags system_flags;
109  Error err;
110 
111  // Some math.h emulation functions...
112 #if !BINIO_WITH_MATH
113  Float pow(Float base, signed int exp);
114  Float ldexp(Float x, signed int exp) { return x * pow(2, exp); }
115 #endif
116 
117 private:
118  static Flags detect_system_flags();
119 };
120 
121 class binistream: virtual public binio
122 {
123 public:
124  binistream();
125  virtual ~binistream();
126 
127  Int readInt(unsigned int size);
128  Float readFloat(FType ft);
129  unsigned long readString(char *str, unsigned long amount);
130  unsigned long readString(char *str, unsigned long maxlen, const char delim);
131 #if BINIO_ENABLE_STRING
132  std::string readString(const char delim = '\0');
133 #endif
134 
135  Int peekInt(unsigned int size);
136  Float peekFloat(FType ft);
137 
138  bool ateof();
139  void ignore(unsigned long amount = 1);
140 
141 protected:
142  virtual Byte getByte() = 0;
143  virtual void getRaw(char *c, streamsize n) = 0;
144 
145 private:
146  Float ieee_single2float(Byte *data);
147  Float ieee_double2float(Byte *data);
148 };
149 
150 class binostream: virtual public binio
151 {
152 public:
153  binostream();
154  virtual ~binostream();
155 
156  void writeInt(Int val, unsigned int size);
157  void writeFloat(Float f, FType ft);
158  unsigned long writeString(const char *str, unsigned long amount = 0);
159 #if BINIO_ENABLE_STRING
160  unsigned long writeString(const std::string &str);
161 #endif
162 
163 protected:
164  virtual void putByte(Byte) = 0;
165  virtual void putRaw(const char *c, streamsize n) = 0;
166 
167 private:
168  void float2ieee_single(Float f, Byte *data);
169  void float2ieee_double(Float f, Byte *data);
170 };
171 
172 class binstream: public binistream, public binostream
173 {
174 public:
175  binstream();
176  virtual ~binstream();
177 };
178 
179 #endif
unsigned char Byte
Definition: binio.h:103
virtual ~binio()
Definition: binio.cc:90
Flag
Definition: binio.h:69
FType
Definition: binio.h:85
binio()
Definition: binio.cc:85
Error err
Definition: binio.h:109
double Float
Definition: binio.h:102
bool getFlag(Flag f)
Definition: binio.cc:102
virtual void seek(long, Offset=Set)=0
long Int
Definition: binio.h:101
The global header file for ARTS.
Definition: binio.h:66
Error error()
Definition: binio.cc:107
static const Flags system_flags
Definition: binio.h:108
Flags my_flags
Definition: binio.h:107
Offset
Definition: binio.h:84
bool eof()
Definition: binio.cc:115
int Error
Definition: binio.h:86
virtual streampos pos()=0
static Flags detect_system_flags()
Definition: binio.cc:51
ErrorCode
Definition: binio.h:74
void setFlag(Flag f, bool set=true)
Definition: binio.cc:94
int Flags
Definition: binio.h:105