ARTS  2.3.1285(git:92a29ea9-dirty)
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  public:
68  typedef enum { BigEndian = 1 << 0, FloatIEEE = 1 << 1 } Flag;
69 
70  typedef enum {
71  NoError = 0,
72  Fatal = 1 << 0,
73  Unsupported = 1 << 1,
74  NotOpen = 1 << 2,
75  Denied = 1 << 3,
76  NotFound = 1 << 4,
77  Eof = 1 << 5
78  } ErrorCode;
79 
80  typedef enum { Set, Add, End } Offset;
81  typedef enum { Single, Double } FType;
82  typedef int Error;
83 
84  binio();
85  virtual ~binio();
86 
87  void setFlag(Flag f, bool set = true);
88  bool getFlag(Flag f);
89 
90  Error error();
91  bool eof();
92 
93  virtual void seek(long, Offset = Set) = 0;
94  virtual streampos pos() = 0;
95 
96  protected:
97  typedef long Int;
98  typedef double Float;
99  typedef unsigned char Byte; // has to be unsigned!
100 
101  typedef int Flags;
102 
103  Flags my_flags;
104  static const Flags system_flags;
105  Error err;
106 
107  // Some math.h emulation functions...
108 #if !BINIO_WITH_MATH
109  Float pow(Float base, signed int exp);
110  Float ldexp(Float x, signed int exp) { return x * pow(2, exp); }
111 #endif
112 
113  private:
114  static Flags detect_system_flags();
115 };
116 
117 class binistream : virtual public binio {
118  public:
119  binistream();
120  virtual ~binistream();
121 
122  Int readInt(unsigned int size);
123  Float readFloat(FType ft);
124  unsigned long readString(char *str, unsigned long amount);
125  unsigned long readString(char *str, unsigned long maxlen, const char delim);
126 #if BINIO_ENABLE_STRING
127  std::string readString(const char delim = '\0');
128 #endif
129 
130  Int peekInt(unsigned int size);
131  Float peekFloat(FType ft);
132 
133  bool ateof();
134  void ignore(unsigned long amount = 1);
135 
136  protected:
137  virtual Byte getByte() = 0;
138  virtual void getRaw(char *c, streamsize n) = 0;
139 
140  private:
141  Float ieee_single2float(Byte *data);
142  Float ieee_double2float(Byte *data);
143 };
144 
145 class binostream : virtual public binio {
146  public:
147  binostream();
148  virtual ~binostream();
149 
150  void writeInt(Int val, unsigned int size);
151  void writeFloat(Float f, FType ft);
152  unsigned long writeString(const char *str, unsigned long amount = 0);
153 #if BINIO_ENABLE_STRING
154  unsigned long writeString(const std::string &str);
155 #endif
156 
157  protected:
158  virtual void putByte(Byte) = 0;
159  virtual void putRaw(const char *c, streamsize n) = 0;
160 
161  private:
162  void float2ieee_single(Float f, Byte *data);
163  void float2ieee_double(Float f, Byte *data);
164 };
165 
166 class binstream : public binistream, public binostream {
167  public:
168  binstream();
169  virtual ~binstream();
170 };
171 
172 #endif
unsigned char Byte
Definition: binio.h:99
virtual ~binio()
Definition: binio.cc:82
Flag
Definition: binio.h:68
FType
Definition: binio.h:81
binio()
Definition: binio.cc:80
Error err
Definition: binio.h:105
double Float
Definition: binio.h:98
bool getFlag(Flag f)
Definition: binio.cc:91
virtual void seek(long, Offset=Set)=0
long Int
Definition: binio.h:97
G0 G2 FVC Y DV Numeric Numeric Numeric Zeeman LowerQuantumNumbers void * data
The global header file for ARTS.
Definition: binio.h:66
Error error()
Definition: binio.cc:93
static const Flags system_flags
Definition: binio.h:104
Flags my_flags
Definition: binio.h:103
Offset
Definition: binio.h:80
Numeric pow(const Rational base, Numeric exp)
Power of.
Definition: rational.h:628
bool eof()
Definition: binio.cc:100
int Error
Definition: binio.h:82
virtual streampos pos()=0
static Flags detect_system_flags()
Definition: binio.cc:51
ErrorCode
Definition: binio.h:70
void setFlag(Flag f, bool set=true)
Definition: binio.cc:84
int Flags
Definition: binio.h:101