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
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 
108  static const Flags system_flags;
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:
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:
176  virtual ~binstream();
177 };
178 
179 #endif
binistream::peekFloat
Float peekFloat(FType ft)
Definition: binio.cc:374
binistream::readString
unsigned long readString(char *str, unsigned long amount)
Definition: binio.cc:325
binio::BigEndian
@ BigEndian
Definition: binio.h:70
binostream::float2ieee_double
void float2ieee_double(Float f, Byte *data)
Definition: binio.cc:609
binio::Set
@ Set
Definition: binio.h:84
binio::err
Error err
Definition: binio.h:109
binio::FloatIEEE
@ FloatIEEE
Definition: binio.h:71
binio::Fatal
@ Fatal
Definition: binio.h:76
binio::FType
FType
Definition: binio.h:85
binio::binio
binio()
Definition: binio.cc:85
binio::NotFound
@ NotFound
Definition: binio.h:80
binio::Flags
int Flags
Definition: binio.h:105
binio::Flag
Flag
Definition: binio.h:69
binistream::~binistream
virtual ~binistream()
Definition: binio.cc:126
binio::Add
@ Add
Definition: binio.h:84
binostream::writeFloat
void writeFloat(Float f, FType ft)
Definition: binio.cc:437
binostream::writeInt
void writeInt(Int val, unsigned int size)
Definition: binio.cc:416
binistream::ignore
void ignore(unsigned long amount=1)
Definition: binio.cc:398
binio::seek
virtual void seek(long, Offset=Set)=0
binio::Float
double Float
Definition: binio.h:102
binio::Unsupported
@ Unsupported
Definition: binio.h:77
binio::Int
long Int
Definition: binio.h:101
binio::getFlag
bool getFlag(Flag f)
Definition: binio.cc:102
binostream
Definition: binio.h:151
binostream::putByte
virtual void putByte(Byte)=0
binistream::peekInt
Int peekInt(unsigned int size)
Definition: binio.cc:367
binistream::ieee_double2float
Float ieee_double2float(Byte *data)
Definition: binio.cc:260
binio::NoError
@ NoError
Definition: binio.h:75
binio::Denied
@ Denied
Definition: binio.h:79
binistream::getByte
virtual Byte getByte()=0
binio::pos
virtual streampos pos()=0
binio::ErrorCode
ErrorCode
Definition: binio.h:74
binio::End
@ End
Definition: binio.h:84
binio::my_flags
Flags my_flags
Definition: binio.h:107
binio::detect_system_flags
static Flags detect_system_flags()
Definition: binio.cc:51
binostream::putRaw
virtual void putRaw(const char *c, streamsize n)=0
binio::Offset
Offset
Definition: binio.h:84
binio::error
Error error()
Definition: binio.cc:107
binio::system_flags
static const Flags system_flags
Definition: binio.h:108
binistream::ieee_single2float
Float ieee_single2float(Byte *data)
Definition: binio.cc:220
binstream
Definition: binio.h:173
binstream::~binstream
virtual ~binstream()
binistream::getRaw
virtual void getRaw(char *c, streamsize n)=0
binio::Single
@ Single
Definition: binio.h:85
binistream::binistream
binistream()
Definition: binio.cc:122
binistream
Definition: binio.h:122
binostream::~binostream
virtual ~binostream()
Definition: binio.cc:412
binio::eof
bool eof()
Definition: binio.cc:115
binostream::binostream
binostream()
Definition: binio.cc:408
binio::NotOpen
@ NotOpen
Definition: binio.h:78
binio::setFlag
void setFlag(Flag f, bool set=true)
Definition: binio.cc:94
binio::Error
int Error
Definition: binio.h:86
binostream::writeString
unsigned long writeString(const char *str, unsigned long amount=0)
Definition: binio.cc:682
binistream::ateof
bool ateof()
Definition: binio.cc:387
binstream::binstream
binstream()
binistream::readInt
Int readInt(unsigned int size)
Definition: binio.cc:130
binistream::readFloat
Float readFloat(FType ft)
Definition: binio.cc:154
arts.h
The global header file for ARTS.
binio
Definition: binio.h:67
binio::Byte
unsigned char Byte
Definition: binio.h:103
binio::Eof
@ Eof
Definition: binio.h:81
binostream::float2ieee_single
void float2ieee_single(Float f, Byte *data)
Definition: binio.cc:551
binio::~binio
virtual ~binio()
Definition: binio.cc:90
binio::Double
@ Double
Definition: binio.h:85