ARTS  2.4.0(git:4fb77825)
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
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 
104  static const Flags system_flags;
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:
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:
169  virtual ~binstream();
170 };
171 
172 #endif
binistream::peekFloat
Float peekFloat(FType ft)
Definition: binio.cc:362
binistream::readString
unsigned long readString(char *str, unsigned long amount)
Definition: binio.cc:310
binio::BigEndian
@ BigEndian
Definition: binio.h:68
binostream::float2ieee_double
void float2ieee_double(Float f, Byte *data)
Definition: binio.cc:609
binio::Set
@ Set
Definition: binio.h:80
binio::err
Error err
Definition: binio.h:105
binio::FloatIEEE
@ FloatIEEE
Definition: binio.h:68
binio::Fatal
@ Fatal
Definition: binio.h:72
binio::FType
FType
Definition: binio.h:81
binio::binio
binio()
Definition: binio.cc:80
binio::NotFound
@ NotFound
Definition: binio.h:76
binio::Flags
int Flags
Definition: binio.h:101
binio::Flag
Flag
Definition: binio.h:68
binistream::~binistream
virtual ~binistream()
Definition: binio.cc:106
binio::Add
@ Add
Definition: binio.h:80
binostream::writeFloat
void writeFloat(Float f, FType ft)
Definition: binio.cc:420
binostream::writeInt
void writeInt(Int val, unsigned int size)
Definition: binio.cc:399
binistream::ignore
void ignore(unsigned long amount=1)
Definition: binio.cc:387
binio::seek
virtual void seek(long, Offset=Set)=0
data
G0 G2 FVC Y DV Numeric Numeric Numeric Zeeman LowerQuantumNumbers void * data
Definition: arts_api_classes.cc:232
binio::Float
double Float
Definition: binio.h:98
binio::Unsupported
@ Unsupported
Definition: binio.h:73
binio::Int
long Int
Definition: binio.h:97
binio::getFlag
bool getFlag(Flag f)
Definition: binio.cc:91
binostream
Definition: binio.h:145
pow
Numeric pow(const Rational base, Numeric exp)
Power of.
Definition: rational.h:628
binostream::putByte
virtual void putByte(Byte)=0
binistream::peekInt
Int peekInt(unsigned int size)
Definition: binio.cc:356
binistream::ieee_double2float
Float ieee_double2float(Byte *data)
Definition: binio.cc:242
binio::NoError
@ NoError
Definition: binio.h:71
binio::Denied
@ Denied
Definition: binio.h:75
binistream::getByte
virtual Byte getByte()=0
binio::pos
virtual streampos pos()=0
binio::ErrorCode
ErrorCode
Definition: binio.h:70
binio::End
@ End
Definition: binio.h:80
binio::my_flags
Flags my_flags
Definition: binio.h:103
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:80
binio::error
Error error()
Definition: binio.cc:93
binio::system_flags
static const Flags system_flags
Definition: binio.h:104
binistream::ieee_single2float
Float ieee_single2float(Byte *data)
Definition: binio.cc:199
binstream
Definition: binio.h:166
binstream::~binstream
virtual ~binstream()
binistream::getRaw
virtual void getRaw(char *c, streamsize n)=0
binio::Single
@ Single
Definition: binio.h:81
binistream::binistream
binistream()
Definition: binio.cc:104
binistream
Definition: binio.h:117
binostream::~binostream
virtual ~binostream()
Definition: binio.cc:397
binio::eof
bool eof()
Definition: binio.cc:100
binostream::binostream
binostream()
Definition: binio.cc:395
binio::NotOpen
@ NotOpen
Definition: binio.h:74
binio::setFlag
void setFlag(Flag f, bool set=true)
Definition: binio.cc:84
binio::Error
int Error
Definition: binio.h:82
binostream::writeString
unsigned long writeString(const char *str, unsigned long amount=0)
Definition: binio.cc:680
ARTS::Var::x
Vector x(Workspace &ws) noexcept
Definition: autoarts.h:7346
binistream::ateof
bool ateof()
Definition: binio.cc:377
binstream::binstream
binstream()
binistream::readInt
Int readInt(unsigned int size)
Definition: binio.cc:108
binistream::readFloat
Float readFloat(FType ft)
Definition: binio.cc:132
arts.h
The global header file for ARTS.
binio
Definition: binio.h:66
binio::Byte
unsigned char Byte
Definition: binio.h:99
binio::Eof
@ Eof
Definition: binio.h:77
binostream::float2ieee_single
void float2ieee_single(Float f, Byte *data)
Definition: binio.cc:552
binio::~binio
virtual ~binio()
Definition: binio.cc:82
binio::Double
@ Double
Definition: binio.h:81