ScummVM API documentation
memory_stream.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22  //=============================================================================
23  //
24  // MemoryStream does reading and writing over the buffer of bytes stored in
25  // memory. Currently has rather trivial implementation. Does not own a buffer
26  // itself, but works with the provided C-buffer pointer, which means that the
27  // buffer object *must* persist until stream is closed.
28  //
29  // VectorStream is a specialized implementation that works with std::vector.
30  // Unlike base MemoryStream provides continiously resizing buffer for writing.
31  // TODO: separate StringStream for reading & writing String object?
32  //
33  //=============================================================================
34 
35 #ifndef AGS_SHARED_UTIL_MEMORY_STREAM_H
36 #define AGS_SHARED_UTIL_MEMORY_STREAM_H
37 
38 #include "common/std/vector.h"
39 #include "ags/shared/util/data_stream.h"
40 #include "ags/shared/util/string.h"
41 
42 namespace AGS3 {
43 namespace AGS {
44 namespace Shared {
45 
46 class MemoryStream : public DataStream {
47 public:
48  // Construct memory stream in the read-only mode over a const C-buffer;
49  // reading will never exceed buf_sz bytes;
50  // buffer must persist in memory until the stream is closed.
51  MemoryStream(const uint8_t *cbuf, size_t buf_sz, DataEndianess stream_endianess = kLittleEndian);
52  // Construct memory stream in the chosen mode over a given C-buffer;
53  // neither reading nor writing will ever exceed buf_sz bytes;
54  // buffer must persist in memory until the stream is closed.
55  MemoryStream(uint8_t *buf, size_t buf_sz, StreamWorkMode mode, DataEndianess stream_endianess = kLittleEndian);
56  ~MemoryStream() override {}
57 
58  void Close() override;
59  bool Flush() override;
60 
61  // Is stream valid (underlying data initialized properly)
62  bool IsValid() const override;
63  // Is end of stream
64  bool EOS() const override;
65  // Total length of stream (if known)
66  soff_t GetLength() const override;
67  // Current position (if known)
68  soff_t GetPosition() const override;
69  bool CanRead() const override;
70  bool CanWrite() const override;
71  bool CanSeek() const override;
72 
73  size_t Read(void *buffer, size_t size) override;
74  int32_t ReadByte() override;
75  size_t Write(const void *buffer, size_t size) override;
76  int32_t WriteByte(uint8_t b) override;
77 
78  soff_t Seek(soff_t offset, StreamSeek origin) override;
79 
80 protected:
81  const uint8_t *_cbuf = nullptr; // readonly buffer ptr
82  size_t _buf_sz = 0u; // hard buffer limit
83  size_t _len = 0u; // calculated length of stream
84  const StreamWorkMode _mode;
85  size_t _pos = 0u; // current stream pos
86 
87 private:
88  uint8_t *_buf = nullptr; // writeable buffer ptr
89 };
90 
91 
92 class VectorStream : public MemoryStream {
93 public:
94  // Construct memory stream in the read-only mode over a const std::vector;
95  // vector must persist in memory until the stream is closed.
96  VectorStream(const std::vector<uint8_t> &cbuf, DataEndianess stream_endianess = kLittleEndian);
97  // Construct memory stream in the chosen mode over a given std::vector;
98  // vector must persist in memory until the stream is closed.
99  VectorStream(std::vector<uint8_t> &buf, StreamWorkMode mode, DataEndianess stream_endianess = kLittleEndian);
100  ~VectorStream() override {}
101 
102  void Close() override;
103 
104  bool CanRead() const override;
105  bool CanWrite() const override;
106 
107  size_t Write(const void *buffer, size_t size) override;
108  int32_t WriteByte(uint8_t b) override;
109 
110 private:
111  std::vector<uint8_t> *_vec = nullptr; // writeable vector (may be null)
112 };
113 
114 } // namespace Shared
115 } // namespace AGS
116 } // namespace AGS3
117 
118 #endif
Definition: achievements_tables.h:27
Definition: memory_stream.h:46
Definition: data_stream.h:40
Definition: memory_stream.h:92
Definition: ags.h:40