Gamedev Framework (gf)  0.11.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 
160  FileOutputStream(const Path& path, Mode mode = Mode::Write);
161 
167  ~FileOutputStream();
168 
169  virtual std::size_t write(ArrayRef<uint8_t> buffer) override;
170 
171  virtual std::size_t getWrittenBytesCount() const override;
172 
173  private:
174  std::FILE *m_file;
175  std::size_t m_written;
176  };
177 
182  class GF_API MemoryOutputStream : public OutputStream {
183  public:
192  explicit MemoryOutputStream(BufferRef<uint8_t> memory);
193 
194  virtual std::size_t write(ArrayRef<uint8_t> buffer) override;
195 
196  virtual std::size_t getWrittenBytesCount() const override;
197  private:
198  BufferRef<uint8_t> m_memory;
199  std::size_t m_offset;
200  };
201 
202 
207  class GF_API CompressedOutputStream : public OutputStream {
208  public:
214  explicit CompressedOutputStream(OutputStream& compressed);
215 
220 
221  virtual std::size_t write(ArrayRef<uint8_t> buffer) override;
222 
223  virtual std::size_t getWrittenBytesCount() const override;
224  private:
225  static constexpr uInt BufferSize = 256;
226 
227  OutputStream *m_compressed;
228  z_stream m_stream;
229  Bytef m_buffer[BufferSize];
230  };
231 
232 
233 
234 #ifndef DOXYGEN_SHOULD_SKIP_THIS
235 }
236 #endif
237 }
238 
239 #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:207
File based output stream.
Definition: Streams.h:141
The namespace for gf classes.
Definition: Action.h:35
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:182
Compressed input stream.
Definition: Streams.h:105