1 /*
2  *  This file is part of Dune Legacy.
3  *
4  *  Dune Legacy is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  Dune Legacy is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with Dune Legacy.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INPUTSTREAM_H
19 #define INPUTSTREAM_H
20 
21 #include <fixmath/FixPoint.h>
22 
23 #include <SDL.h>
24 #include <string>
25 #include <list>
26 #include <vector>
27 #include <set>
28 #include <exception>
29 
30 class InputStream
31 {
32 public:
InputStream()33     InputStream() { ; };
~InputStream()34     virtual ~InputStream() { ; };
35 
36     /**
37         readString reads in a strings from the stream.
38         \return the read string
39     */
40     virtual std::string readString() = 0;
41 
42     virtual Uint8 readUint8() = 0;
43     virtual Uint16 readUint16() = 0;
44     virtual Uint32 readUint32() = 0;
45     virtual Uint64 readUint64() = 0;
46     virtual bool readBool() = 0;
47     virtual float readFloat() = 0;
48 
49     /**
50         Reads in a Sint8 value.
51         \return the read value
52     */
readSint8()53     Sint8 readSint8() {
54         Uint8 tmp = readUint8();
55         return *((Sint8*) &tmp);
56     }
57 
58     /**
59         Reads in a Sint16 value.
60         \return the read value
61     */
readSint16()62     Sint16 readSint16() {
63         Uint16 tmp = readUint16();
64         return *((Sint16*) &tmp);
65     }
66 
67     /**
68         Reads in a Sint32 value.
69         \return the read value
70     */
readSint32()71     Sint32 readSint32() {
72         Uint32 tmp = readUint32();
73         return *((Sint32*) &tmp);
74     }
75 
76     /**
77         Reads in a Sint64 value.
78         \return the read value
79     */
readSint64()80     Sint64 readSint64() {
81         Uint64 tmp = readUint64();
82         return *((Sint64*) &tmp);
83     }
84 
85     /**
86         Reads in a FixPoint value.
87         \return the read value
88     */
readFixPoint()89     FixPoint readFixPoint() {
90         return FixPoint::FromRawValue(readSint64());
91     }
92 
93     /**
94         Reads in up to 8 boolean values from a single byte
95         \param  pVal1   the 1st boolean value
96         \param  pVal2   the 2nd boolean value
97         \param  pVal3   the 3rd boolean value
98         \param  pVal4   the 4th boolean value
99         \param  pVal5   the 5th boolean value
100         \param  pVal6   the 6th boolean value
101         \param  pVal7   the 7th boolean value
102         \param  pVal8   the 8th boolean value
103 
104     */
105     void readBools(bool* pVal1 = nullptr, bool* pVal2 = nullptr, bool* pVal3 = nullptr, bool* pVal4 = nullptr, bool* pVal5 = nullptr, bool* pVal6 = nullptr, bool* pVal7 = nullptr, bool* pVal8 = nullptr) {
106         Uint8 val = readUint8();
107 
108         if(pVal1 != nullptr)   *pVal1 = ((val & 0x01) != 0);
109         if(pVal2 != nullptr)   *pVal2 = ((val & 0x02) != 0);
110         if(pVal3 != nullptr)   *pVal3 = ((val & 0x04) != 0);
111         if(pVal4 != nullptr)   *pVal4 = ((val & 0x08) != 0);
112         if(pVal5 != nullptr)   *pVal5 = ((val & 0x10) != 0);
113         if(pVal6 != nullptr)   *pVal6 = ((val & 0x20) != 0);
114         if(pVal7 != nullptr)   *pVal7 = ((val & 0x40) != 0);
115         if(pVal8 != nullptr)   *pVal8 = ((val & 0x80) != 0);
116     }
117 
118     /**
119         Reads a list of Uint32 written by writeUint32List().
120         \return the read list
121     */
readUint32List()122     std::list<Uint32> readUint32List() {
123         std::list<Uint32> List;
124         Uint32 size = readUint32();
125         for(unsigned int i=0; i < size; i++) {
126             List.push_back(readUint32());
127         }
128         return List;
129     }
130 
131     /**
132         Reads a vector of Uint32 written by writeUint32Vector().
133         \return the read vector
134     */
readUint32Vector()135     std::vector<Uint32> readUint32Vector() {
136         std::vector<Uint32> vec;
137         Uint32 size = readUint32();
138         for(unsigned int i=0; i < size; i++) {
139             vec.push_back(readUint32());
140         }
141         return vec;
142     }
143 
144     /**
145         Reads a set of Uint32 written by writeUint32Set().
146         \return the read set
147     */
readUint32Set()148     std::set<Uint32> readUint32Set() {
149         std::set<Uint32> retSet;
150         Uint32 size = readUint32();
151         for(unsigned int i=0; i < size; i++) {
152             retSet.insert(readUint32());
153         }
154         return retSet;
155     }
156 
157     class exception : public std::exception {
158     public:
exception()159         exception() throw () { };
~exception()160         virtual ~exception() throw () { };
161     };
162 
163     class eof : public InputStream::exception {
164     public:
eof(const std::string & str)165         explicit eof(const std::string& str) throw () : str(str) { };
~eof()166         virtual ~eof() throw () { };
167 
what()168         virtual const char* what() const throw () { return str.c_str(); };
169 
170     private:
171         std::string str;
172     };
173 
174     class error : public InputStream::exception {
175     public:
error(const std::string & str)176         explicit error(const std::string& str) throw () : str(str) { };
~error()177         virtual ~error() throw () { };
178 
what()179         virtual const char* what() const throw () { return str.c_str(); };
180 
181     private:
182         std::string str;
183     };
184 };
185 
186 #endif // INPUTSTREAM_H
187