1 /*
2 BStone: A Source port of
3 Blake Stone: Aliens of Gold and Blake Stone: Planet Strike
4 
5 Copyright (c) 1992-2013 Apogee Entertainment, LLC
6 Copyright (c) 2013-2015 Boris I. Bendovsky (bibendovsky@hotmail.com)
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the
20 Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23 
24 
25 #ifndef BSTONE_BINARY_READER_INCLUDED
26 #define BSTONE_BINARY_READER_INCLUDED
27 
28 
29 #include "bstone_istream.h"
30 
31 
32 namespace bstone {
33 
34 
35 class BinaryReader {
36 public:
37     BinaryReader(
38         IStream* stream = nullptr);
39 
40     BinaryReader(
41         const BinaryReader& that) = delete;
42 
43     BinaryReader& operator=(
44         const BinaryReader& that) = delete;
45 
46 
47     bool open(
48         IStream* stream);
49 
50     // Closes the reader but stream.
51     void close();
52 
53     bool is_open() const;
54 
55     // Reads a signed 8-bit integer value.
56     int8_t read_s8();
57 
58     // Reads an unsigned 8-bit integer value.
59     uint8_t read_u8();
60 
61     // Reads a signed 16-bit integer value.
62     int16_t read_s16();
63 
64     // Reads an unsigned 16-bit integer value.
65     uint16_t read_u16();
66 
67     // Reads a signed 32-bit integer value.
68     int32_t read_s32();
69 
70     // Reads an unsigned 32-bit integer value.
71     uint32_t read_u32();
72 
73     // Reads a signed 64-bit integer value.
74     int64_t read_s64();
75 
76     // Reads an unsigned 64-bit integer value.
77     uint64_t read_u64();
78 
79     // Reads a 32-bit float-point value.
80     float read_r32();
81 
82     // Reads a 64-bit float-point value.
83     double read_r64();
84 
85     // Reads a string prepended with signed 32-bit (little-endian) length.
86     // Returns empty string on error or when max length reached.
87     std::string read_string(
88         int max_length = -1);
89 
90     bool read(
91         void* buffer,
92         int count);
93 
94 
95     template<typename T>
read(T & value)96     bool read(
97         T& value)
98     {
99         if (!is_open()) {
100             return false;
101         }
102 
103         return stream_->read(&value, sizeof(T)) == sizeof(T);
104     }
105 
106     template<typename T,size_t N>
read(T (& value)[N])107     bool read(
108         T (&value)[N])
109     {
110         if (!is_open()) {
111             return false;
112         }
113 
114         return stream_->read(value, N * sizeof(T)) == N * sizeof(T);
115     }
116 
117     // Skips a number of octets forward if count is positive or
118     // backward otherwise.
119     // Returns false on error.
120     bool skip(
121         int count);
122 
123     // Returns a current position.
124     int64_t get_position() const;
125 
126     // Sets a current position to a specified one.
127     bool set_position(
128         int64_t position);
129 
130 private:
131     IStream* stream_;
132 
133 
134     template<typename T>
read()135     T read()
136     {
137         if (!is_open()) {
138             return 0;
139         }
140 
141         T value;
142 
143         if (stream_->read(&value, sizeof(T)) != sizeof(T)) {
144             return 0;
145         }
146 
147         return value;
148     }
149 }; // BinaryReader
150 
151 
152 } // bstone
153 
154 
155 #endif // BSTONE_BINARY_READER_INCLUDED
156