Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Streams.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 "Stream.h"
33 #include "Path.h"
34 #include "Portability.h"
35 
36 namespace gf {
37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
38 inline namespace v1 {
39 #endif
40 
45  class GF_API FileInputStream : public InputStream {
46  public:
55  explicit FileInputStream(const Path& path);
56 
62  ~FileInputStream();
63 
64  std::size_t read(BufferRef<uint8_t> buffer) override;
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_API MemoryInputStream : public InputStream {
79  public:
88  explicit MemoryInputStream(ArrayRef<uint8_t> memory);
89 
90  std::size_t read(BufferRef<uint8_t> buffer) override;
91  void seek(std::ptrdiff_t position) override;
92  void skip(std::ptrdiff_t position) override;
93  bool isFinished() override;
94 
95  private:
96  ArrayRef<uint8_t> m_memory;
97  std::size_t m_offset;
98  };
99 
100 
105  class GF_API CompressedInputStream : public InputStream {
106  public:
112  explicit CompressedInputStream(InputStream& compressed);
113 
117  virtual ~CompressedInputStream();
118 
119  std::size_t read(BufferRef<uint8_t> buffer) override;
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_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(BufferRef<uint8_t> buffer) override;
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_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 
196  ~FileOutputStream();
197 
198  std::size_t write(ArrayRef<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_API MemoryOutputStream : public OutputStream {
212  public:
221  explicit MemoryOutputStream(BufferRef<uint8_t> memory);
222 
223  std::size_t write(ArrayRef<uint8_t> buffer) override;
224 
225  std::size_t getWrittenBytesCount() const override;
226  private:
227  BufferRef<uint8_t> m_memory;
228  std::size_t m_offset;
229  };
230 
231 
236  class GF_API CompressedOutputStream : public OutputStream {
237  public:
243  explicit CompressedOutputStream(OutputStream& compressed);
244 
249 
250  std::size_t write(ArrayRef<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_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(ArrayRef<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
File based input stream.
Definition: Streams.h:45
Mode
Open mode for the file.
Definition: Streams.h:175
Buffer input stream.
Definition: Streams.h:140
std::vector< uint8_t > * getBytes()
Get the underlying bytes.
Definition: Streams.h:278
constexpr BufferRef< T > buffer(T *data, std::size_t size)
Create a reference to a buffer.
Definition: BufferRef.h:211
Abstract class for custom file output streams.
Definition: Stream.h:136
Compressed output stream.
Definition: Streams.h:236
File based output stream.
Definition: Streams.h:170
The namespace for gf classes.
Definition: Action.h:35
Abstract class for custom file input streams.
Definition: Stream.h:55
Memory based input stream.
Definition: Streams.h:78
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:44
Buffer output stream.
Definition: Streams.h:266
std::vector< uint8_t > * getBytes()
Get the underlying bytes.
Definition: Streams.h:152
Memory based output stream.
Definition: Streams.h:211
Compressed input stream.
Definition: Streams.h:105