1 /*
2 BStone: A Source port of
3 Blake Stone: Aliens of Gold and Blake Stone: Planet Strike
4 
5 Copyright (c) 1992-2013 Apogee Entertainment, LLC
6 Copyright (c) 2013-2015 Boris I. Bendovsky (bibendovsky@hotmail.com)
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the
20 Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23 
24 
25 //
26 // A memory stream.
27 //
28 
29 
30 #ifndef BSTONE_MEMORY_STREAM_INCLUDED
31 #define BSTONE_MEMORY_STREAM_INCLUDED
32 
33 
34 #include <vector>
35 #include "bstone_istream.h"
36 #include "bstone_un_value.h"
37 
38 
39 namespace bstone {
40 
41 
42 // A memory stream.
43 class MemoryStream :
44     public IStream
45 {
46 public:
47     MemoryStream(
48         int initial_capacity = 0,
49         StreamOpenMode open_mode = StreamOpenMode::read_write);
50 
51     MemoryStream(
52         int buffer_size,
53         int buffer_offset,
54         const uint8_t* buffer,
55         StreamOpenMode open_mode = StreamOpenMode::read);
56 
57     MemoryStream(
58         const MemoryStream& that) = delete;
59 
60     MemoryStream& operator=(
61         const MemoryStream& that) = delete;
62 
63     virtual ~MemoryStream();
64 
65 
66     bool open(
67         int initial_capacity = 0,
68         StreamOpenMode open_mode = StreamOpenMode::read_write);
69 
70     bool open(
71         int buffer_size,
72         int buffer_offset,
73         const uint8_t* buffer,
74         StreamOpenMode open_mode = StreamOpenMode::read);
75 
76     virtual void close();
77 
78     virtual bool is_open() const;
79 
80     virtual int64_t get_size();
81 
82     virtual bool set_size(
83         int64_t size);
84 
85     virtual bool flush();
86 
87     virtual int64_t seek(
88         int64_t offset,
89         StreamSeekOrigin origin);
90 
91     virtual int64_t get_position();
92 
93     virtual int read(
94         void* buffer,
95         int count);
96 
97     virtual bool write(
98         const void* buffer,
99         int count);
100 
101     virtual bool can_read() const;
102 
103     virtual bool can_seek() const;
104 
105     virtual bool can_write() const;
106 
107     uint8_t* get_data();
108 
109     const uint8_t* get_data() const;
110 
111     bool remove_block(
112         int64_t offset,
113         int count);
114 
115 private:
116     using Buffer = std::vector<UnValue<uint8_t>>;
117 
118     bool is_open_;
119     bool can_read_;
120     bool can_write_;
121     int64_t position_;
122     int64_t size_;
123     int64_t ext_size_;
124     uint8_t* buffer_;
125     uint8_t* ext_buffer_;
126     Buffer int_buffer_;
127 }; // IStream
128 
129 
130 } // bstone
131 
132 
133 #endif // BSTONE_MEMORY_STREAM_INCLUDED
134