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 base class for streams.
27 //
28 
29 
30 #ifndef BSTONE_ISTREAM_INCLUDED
31 #define BSTONE_ISTREAM_INCLUDED
32 
33 
34 #include <cstdint>
35 #include <string>
36 
37 
38 namespace bstone {
39 
40 
41 enum class StreamSeekOrigin {
42     begin,
43     current,
44     end,
45 }; // StreamSeekOrigin
46 
47 enum class StreamOpenMode {
48     read,
49     write,
50     read_write,
51 }; // StreamOpenMode
52 
53 
54 // A base class for streams.
55 class IStream {
56 public:
57     IStream();
58 
59     virtual ~IStream();
60 
61     virtual void close();
62 
63     virtual bool is_open() const = 0;
64 
65     // Returns a size of the stream or a negative value on error.
66     virtual int64_t get_size() = 0;
67 
68     // Sets a new size of the stream.
69     // Returns false on error or if the stream is not seekable.
70     virtual bool set_size(
71         int64_t size) = 0;
72 
73     // Returns a new position or a negative value on error.
74     virtual int64_t seek(
75         int64_t offset,
76         StreamSeekOrigin origin) = 0;
77 
78     // Skips a number of octets forward if count is positive or
79     // backward otherwise.
80     // Returns a negative value on error.
81     virtual int64_t skip(
82         int count);
83 
84     // Returns a current position or a negative value on error.
85     virtual int64_t get_position();
86 
87     virtual bool set_position(
88         int64_t position);
89 
90     // Reads a specified number of octets and returns an actual
91     // read number of octets.
92     virtual int read(
93         void* buffer,
94         int count) = 0;
95 
96     virtual bool write(
97         const void* buffer,
98         int count) = 0;
99 
100     // Reads a one octet and returns it.
101     // Returns a negative value on error.
102     virtual int read_octet();
103 
104     virtual bool write_octet(
105         uint8_t value);
106 
107     // Writes a string without a terminator
108     virtual bool write_string(
109         const std::string& string);
110 
111     virtual bool can_read() const = 0;
112 
113     virtual bool can_seek() const = 0;
114 
115     virtual bool can_write() const = 0;
116 
117     // Copies the stream from a current position to
118     // an another stream using internal buffer.
119     bool copy_to(
120         IStream* dst_stream,
121         int buffer_size = 0);
122 
123 
124     static int get_default_copy_buffer_size();
125 }; // IStream
126 
127 
128 } // bstone
129 
130 
131 #endif // BSTONE_ISTREAM_INCLUDED
132