ARTS 2.5.11 (git: 6827797f)
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_ISO_STDLIB - Build with ISO C++ standard library compliance
34//
35// Set to 1 to build for the ISO standard C++ library (i.e. namespaces, STL and
36// templatized iostream). Set to 0 to build for the traditional C++ library.
37#define BINIO_ISO_STDLIB 1
38
39/***** Implementation *****/
40
41// Disable annoying multiple inheritance compiler warning on MSVC6
42#ifdef _MSC_VER
43#pragma warning(disable : 4250)
44#endif
45
46#if BINIO_ENABLE_STRING
47#include <string>
48#endif
49
50class binio {
51 public:
52 using Flag = enum { BigEndian = 1 << 0, FloatIEEE = 1 << 1 };
53
54 using ErrorCode = enum {
55 NoError = 0,
56 Fatal = 1 << 0,
57 Unsupported = 1 << 1,
58 NotOpen = 1 << 2,
59 Denied = 1 << 3,
60 NotFound = 1 << 4,
61 Eof = 1 << 5
62 };
63
64 using Offset = enum { Set, Add, End };
65 using FType = enum { Single, Double };
66 using Error = int;
67
68 binio();
69
70 void setFlag(Flag f, bool set = true);
71 bool getFlag(Flag f);
72
73 Error error();
74 bool eof();
75
76 virtual void seek(long, Offset = Set) = 0;
77 virtual std::streampos pos() = 0;
78
79 protected:
80 using Int = std::int64_t;
81 using Float = double;
82 using Byte = unsigned char; // has to be unsigned!
83
84 using Flags = int;
85
87 static const Flags system_flags;
88 Error err{NoError};
89
90 private:
92};
93
94class binistream : virtual public binio {
95 public:
96 Int readInt(unsigned int size);
98
99 void readDoubleArray(double *d, unsigned long size);
100
101 unsigned long readString(char *str, unsigned long amount);
102 unsigned long readString(char *str, unsigned long maxlen, const char delim);
103
104
105#if BINIO_ENABLE_STRING
106 std::string readString(const char delim = '\0');
107#endif
108
109 Int peekInt(unsigned int size);
111
112 bool ateof();
113 void ignore(unsigned long amount = 1);
114
115 protected:
116 virtual Byte getByte() = 0;
117 virtual void getRaw(char *c, std::streamsize n) = 0;
118};
119
120class binostream : virtual public binio {
121 public:
122 void writeInt(Int val, unsigned int size);
123 void writeFloat(Float f, FType ft);
124 unsigned long writeString(const char *str, unsigned long amount = 0);
125#if BINIO_ENABLE_STRING
126 unsigned long writeString(const std::string &str);
127#endif
128
129 protected:
130 virtual void putByte(Byte) = 0;
131 virtual void putRaw(const char *c, std::streamsize n) = 0;
132};
133
134#endif
The global header file for ARTS.
Definition: binio.h:50
void setFlag(Flag f, bool set=true)
Definition: binio.cc:78
std::int64_t Int
Definition: binio.h:80
Error error()
Definition: binio.cc:87
bool eof()
Definition: binio.cc:94
virtual void seek(long, Offset=Set)=0
static Flags detect_system_flags()
Definition: binio.cc:37
bool getFlag(Flag f)
Definition: binio.cc:85
virtual std::streampos pos()=0
int Error
Definition: binio.h:66
enum { Set, Add, End } Offset
Definition: binio.h:64
static const Flags system_flags
Definition: binio.h:87
enum { BigEndian=1<< 0, FloatIEEE=1<< 1 } Flag
Definition: binio.h:52
unsigned char Byte
Definition: binio.h:82
enum { Single, Double } FType
Definition: binio.h:65
int Flags
Definition: binio.h:84
double Float
Definition: binio.h:81
Flags my_flags
Definition: binio.h:86
binio()
Definition: binio.cc:66
enum { NoError=0, Fatal=1<< 0, Unsupported=1<< 1, NotOpen=1<< 2, Denied=1<< 3, NotFound=1<< 4, Eof=1<< 5 } ErrorCode
Definition: binio.h:62
Error err
Definition: binio.h:88
bool ateof()
Definition: binio.cc:259
unsigned long readString(char *str, unsigned long amount)
Definition: binio.cc:192
virtual Byte getByte()=0
Float peekFloat(FType ft)
Definition: binio.cc:244
void ignore(unsigned long amount=1)
Definition: binio.cc:269
Int readInt(unsigned int size)
Definition: binio.cc:97
virtual void getRaw(char *c, std::streamsize n)=0
Int peekInt(unsigned int size)
Definition: binio.cc:238
Float readFloat(FType ft)
Definition: binio.cc:121
void readDoubleArray(double *d, unsigned long size)
Definition: binio.cc:184
void writeInt(Int val, unsigned int size)
Definition: binio.cc:277
virtual void putRaw(const char *c, std::streamsize n)=0
virtual void putByte(Byte)=0
unsigned long writeString(const char *str, unsigned long amount=0)
Definition: binio.cc:351
void writeFloat(Float f, FType ft)
Definition: binio.cc:298
#define d
#define c