Gamedev Framework (gf)  0.10.0
A C++14 framework for 2D games
Streams.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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  virtual std::size_t read(BufferRef<uint8_t> buffer) override;
65  virtual void seek(std::ptrdiff_t position) override;
66  virtual void skip(std::ptrdiff_t position) override;
67  virtual 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  virtual std::size_t read(BufferRef<uint8_t> buffer) override;
91  virtual void seek(std::ptrdiff_t position) override;
92  virtual void skip(std::ptrdiff_t position) override;
93  virtual 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  virtual std::size_t read(BufferRef<uint8_t> buffer) override;
120  virtual void seek(std::ptrdiff_t position) override;
121  virtual void skip(std::ptrdiff_t position) override;
122  virtual 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 
136 
141  class GF_API FileOutputStream : public OutputStream {
142  public:
146  enum class Mode {
147  Write,
148  Append,
149  };
150 
159  FileOutputStream(const Path& path, Mode mode = Mode::Write);
160 
166  ~FileOutputStream();
167 
168  virtual std::size_t write(ArrayRef<uint8_t> buffer) override;
169 
170  private:
171  std::FILE *m_file;
172  };
173 
178  class GF_API MemoryOutputStream : public OutputStream {
179  public:
188  explicit MemoryOutputStream(BufferRef<uint8_t> memory);
189 
190  virtual std::size_t write(ArrayRef<uint8_t> buffer) override;
191 
192  private:
193  BufferRef<uint8_t> m_memory;
194  std::size_t m_offset;
195  };
196 
197 
202  class GF_API CompressedOutputStream : public OutputStream {
203  public:
209  explicit CompressedOutputStream(OutputStream& compressed);
210 
215 
216  virtual std::size_t write(ArrayRef<uint8_t> buffer) override;
217 
218  private:
219  static constexpr uInt BufferSize = 256;
220 
221  OutputStream *m_compressed;
222  z_stream m_stream;
223  Bytef m_buffer[BufferSize];
224  };
225 
226 
227 
228 #ifndef DOXYGEN_SHOULD_SKIP_THIS
229 }
230 #endif
231 }
232 
233 #endif // GF_STREAMS_H
File based input stream.
Definition: Streams.h:45
Mode
Open mode for the file.
Definition: Streams.h:146
Abstract class for custom file output streams.
Definition: Stream.h:135
Compressed output stream.
Definition: Streams.h:202
File based output stream.
Definition: Streams.h:141
The namespace for gf classes.
Definition: Action.h:34
Abstract class for custom file input streams.
Definition: Stream.h:54
Memory based input stream.
Definition: Streams.h:78
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:44
Memory based output stream.
Definition: Streams.h:178
Compressed input stream.
Definition: Streams.h:105