Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Streams.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 Julien Bernard
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
20 *
21 * Part of this file comes from SFML, with the same license:
22 * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23 */
24#ifndef GF_STREAMS_H
25#define GF_STREAMS_H
26
27#include <cstddef>
28#include <cstdio>
29
30#include <zlib.h>
31
32#include "CoreApi.h"
33#include "Stream.h"
34#include "Path.h"
35
36namespace gf {
37#ifndef DOXYGEN_SHOULD_SKIP_THIS
38inline namespace v1 {
39#endif
40
45 class GF_CORE_API FileInputStream : public InputStream {
46 public:
55 explicit FileInputStream(const Path& path);
56
63
64 std::size_t read(Span<uint8_t> buffer) override; // Flawfinder: ignore
65 void seek(std::ptrdiff_t position) override;
66 void skip(std::ptrdiff_t position) override;
67 bool isFinished() override;
68
69 private:
70 std::FILE *m_file;
71 };
72
73
78 class GF_CORE_API MemoryInputStream : public InputStream {
79 public:
89
90 std::size_t read(Span<uint8_t> buffer) override; // Flawfinder: ignore
91 void seek(std::ptrdiff_t position) override;
92 void skip(std::ptrdiff_t position) override;
93 bool isFinished() override;
94
95 private:
96 Span<const uint8_t> m_memory;
97 std::size_t m_offset;
98 };
99
100
105 class GF_CORE_API CompressedInputStream : public InputStream {
106 public:
112 explicit CompressedInputStream(InputStream& compressed);
113
118
119 std::size_t read(Span<uint8_t> buffer) override; // Flawfinder: ignore
120 void seek(std::ptrdiff_t position) override;
121 void skip(std::ptrdiff_t position) override;
122 bool isFinished() override;
123
124 private:
125 static constexpr uInt BufferSize = 256;
126
127 InputStream *m_compressed;
128 z_stream m_stream;
129 uInt m_start;
130 uInt m_stop;
131 bool m_eof;
132 Bytef m_buffer[BufferSize];
133 };
134
135
140 class GF_CORE_API BufferInputStream : public InputStream {
141 public:
147 BufferInputStream(std::vector<uint8_t> *bytes);
148
152 std::vector<uint8_t> *getBytes() {
153 return m_bytes;
154 }
155
156 virtual std::size_t read(Span<uint8_t> buffer) override; // Flawfinder: ignore
157 virtual void seek(std::ptrdiff_t position) override;
158 virtual void skip(std::ptrdiff_t position) override;
159 virtual bool isFinished() override;
160 private:
161 std::vector<uint8_t> *m_bytes;
162 std::size_t m_offset;
163 };
164
165
170 class GF_CORE_API FileOutputStream : public OutputStream {
171 public:
175 enum class Mode {
176 Write,
177 Append,
178 };
179
189 FileOutputStream(const Path& path, Mode mode = Mode::Write);
190
197
198 std::size_t write(Span<const uint8_t> buffer) override;
199
200 std::size_t getWrittenBytesCount() const override;
201
202 private:
203 std::FILE *m_file;
204 std::size_t m_written;
205 };
206
211 class GF_CORE_API MemoryOutputStream : public OutputStream {
212 public:
222
223 std::size_t write(Span<const uint8_t> buffer) override;
224
225 std::size_t getWrittenBytesCount() const override;
226 private:
227 Span<uint8_t> m_memory;
228 std::size_t m_offset;
229 };
230
231
236 class GF_CORE_API CompressedOutputStream : public OutputStream {
237 public:
244
249
250 std::size_t write(Span<const uint8_t> buffer) override;
251
252 std::size_t getWrittenBytesCount() const override;
253 private:
254 static constexpr uInt BufferSize = 256;
255
256 OutputStream *m_compressed;
257 z_stream m_stream;
258 Bytef m_buffer[BufferSize];
259 };
260
261
266 class GF_CORE_API BufferOutputStream : public OutputStream {
267 public:
273 BufferOutputStream(std::vector<uint8_t> *bytes);
274
278 std::vector<uint8_t> *getBytes() {
279 return m_bytes;
280 }
281
282 virtual std::size_t write(Span<const uint8_t> buffer) override;
283 virtual std::size_t getWrittenBytesCount() const override;
284
285 private:
286 std::vector<uint8_t> *m_bytes;
287 };
288
289#ifndef DOXYGEN_SHOULD_SKIP_THIS
290}
291#endif
292}
293
294#endif // GF_STREAMS_H
Buffer input stream.
Definition: Streams.h:140
BufferInputStream(std::vector< uint8_t > *bytes)
Constructor.
virtual void seek(std::ptrdiff_t position) override
Change the current reading position.
virtual std::size_t read(Span< uint8_t > buffer) override
Read data from the stream.
std::vector< uint8_t > * getBytes()
Get the underlying bytes.
Definition: Streams.h:152
virtual bool isFinished() override
Tell if the stream is at the end.
virtual void skip(std::ptrdiff_t position) override
Change the current reading position.
Buffer output stream.
Definition: Streams.h:266
virtual std::size_t write(Span< const uint8_t > buffer) override
Write data to the stream.
BufferOutputStream(std::vector< uint8_t > *bytes)
Constructor.
virtual std::size_t getWrittenBytesCount() const override
Get the number of bytes already written.
std::vector< uint8_t > * getBytes()
Get the underlying bytes.
Definition: Streams.h:278
Compressed input stream.
Definition: Streams.h:105
CompressedInputStream(InputStream &compressed)
Constructor.
bool isFinished() override
Tell if the stream is at the end.
std::size_t read(Span< uint8_t > buffer) override
Read data from the stream.
void seek(std::ptrdiff_t position) override
Change the current reading position.
void skip(std::ptrdiff_t position) override
Change the current reading position.
virtual ~CompressedInputStream()
Destructor.
Compressed output stream.
Definition: Streams.h:236
std::size_t write(Span< const uint8_t > buffer) override
Write data to the stream.
CompressedOutputStream(OutputStream &compressed)
Constructor.
~CompressedOutputStream()
Destructor.
std::size_t getWrittenBytesCount() const override
Get the number of bytes already written.
File based input stream.
Definition: Streams.h:45
std::size_t read(Span< uint8_t > buffer) override
Read data from the stream.
FileInputStream(const Path &path)
Constructor.
~FileInputStream()
Destructor.
void skip(std::ptrdiff_t position) override
Change the current reading position.
bool isFinished() override
Tell if the stream is at the end.
void seek(std::ptrdiff_t position) override
Change the current reading position.
File based output stream.
Definition: Streams.h:170
Mode
Open mode for the file.
Definition: Streams.h:175
~FileOutputStream()
Destructor.
std::size_t write(Span< const uint8_t > buffer) override
Write data to the stream.
std::size_t getWrittenBytesCount() const override
Get the number of bytes already written.
FileOutputStream(const Path &path, Mode mode=Mode::Write)
Constructor.
Abstract class for custom file input streams.
Definition: Stream.h:54
Memory based input stream.
Definition: Streams.h:78
MemoryInputStream(Span< const uint8_t > memory)
Constructor.
void skip(std::ptrdiff_t position) override
Change the current reading position.
void seek(std::ptrdiff_t position) override
Change the current reading position.
bool isFinished() override
Tell if the stream is at the end.
std::size_t read(Span< uint8_t > buffer) override
Read data from the stream.
Memory based output stream.
Definition: Streams.h:211
std::size_t write(Span< const uint8_t > buffer) override
Write data to the stream.
std::size_t getWrittenBytesCount() const override
Get the number of bytes already written.
MemoryOutputStream(Span< uint8_t > memory)
Constructor.
Abstract class for custom file output streams.
Definition: Stream.h:135
std::filesystem::path Path
A path in the filesystem.
Definition: Path.h:40
The namespace for gf classes.