ScummVM API documentation
data_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// Standard AGS stream implementation for reading raw data with support for
25// converting to opposite endianess. Most I/O devices should inherit this
26// class and provide implementation for basic reading and writing only.
27//
28//=============================================================================
29
30#ifndef AGS_SHARED_UTIL_DATASTREAM_H
31#define AGS_SHARED_UTIL_DATASTREAM_H
32
33#include "ags/shared/util/bbop.h"
34#include "ags/shared/util/stream.h"
35
36namespace AGS3 {
37namespace AGS {
38namespace Shared {
39
40class DataStream : public Stream {
41public:
42 DataStream(DataEndianess stream_endianess = kLittleEndian);
43 ~DataStream() override;
44
45 int16_t ReadInt16() override;
46 int32_t ReadInt32() override;
47 int64_t ReadInt64() override;
48
49 //
50 // Read- and WriteArray methods return number of full elements (NOT bytes)
51 // read or written, or -1 if end of stream is reached
52 //
53 // Note that ReadArray and WriteArray do NOT convert byte order even when
54 // work with data of different endianess; they are meant for optimal
55 // reading and writing blocks of raw bytes
56 inline size_t ReadArray(void *buffer, size_t elem_size, size_t count) override {
57 return Read(buffer, elem_size * count) / elem_size;
58 }
59
60 inline size_t ReadArrayOfInt16(int16_t *buffer, size_t count) override {
61 return MustSwapBytes() ?
62 ReadAndConvertArrayOfInt16(buffer, count) : ReadArray(buffer, sizeof(int16_t), count);
63 }
64
65 inline size_t ReadArrayOfInt32(int32_t *buffer, size_t count) override {
66 return MustSwapBytes() ?
67 ReadAndConvertArrayOfInt32(buffer, count) : ReadArray(buffer, sizeof(int32_t), count);
68 }
69
70 inline size_t ReadArrayOfInt64(int64_t *buffer, size_t count) override {
71 return MustSwapBytes() ?
72 ReadAndConvertArrayOfInt64(buffer, count) : ReadArray(buffer, sizeof(int64_t), count);
73 }
74
75 size_t WriteInt16(int16_t val) override;
76 size_t WriteInt32(int32_t val) override;
77 size_t WriteInt64(int64_t val) override;
78
79 inline size_t WriteArray(const void *buffer, size_t elem_size, size_t count) override {
80 return Write(buffer, elem_size * count) / elem_size;
81 }
82
83 inline size_t WriteArrayOfInt16(const int16_t *buffer, size_t count) override {
84 return MustSwapBytes() ?
85 WriteAndConvertArrayOfInt16(buffer, count) : WriteArray(buffer, sizeof(int16_t), count);
86 }
87
88 inline size_t WriteArrayOfInt32(const int32_t *buffer, size_t count) override {
89 return MustSwapBytes() ?
90 WriteAndConvertArrayOfInt32(buffer, count) : WriteArray(buffer, sizeof(int32_t), count);
91 }
92
93 inline size_t WriteArrayOfInt64(const int64_t *buffer, size_t count) override {
94 return MustSwapBytes() ?
95 WriteAndConvertArrayOfInt64(buffer, count) : WriteArray(buffer, sizeof(int64_t), count);
96 }
97
98 protected:
99 DataEndianess _streamEndianess;
100
101 // Helper methods for reading/writing arrays of basic types and
102 // converting their elements to opposite endianess (swapping bytes).
103 size_t ReadAndConvertArrayOfInt16(int16_t *buffer, size_t count);
104 size_t ReadAndConvertArrayOfInt32(int32_t *buffer, size_t count);
105 size_t ReadAndConvertArrayOfInt64(int64_t *buffer, size_t count);
106 size_t WriteAndConvertArrayOfInt16(const int16_t *buffer, size_t count);
107 size_t WriteAndConvertArrayOfInt32(const int32_t *buffer, size_t count);
108 size_t WriteAndConvertArrayOfInt64(const int64_t *buffer, size_t count);
109
110 inline bool MustSwapBytes() {
111 return kDefaultSystemEndianess != _streamEndianess;
112 }
113
114 inline void ConvertInt16(int16_t &val) {
115 if (MustSwapBytes()) val = BBOp::SwapBytesInt16(val);
116 }
117 inline void ConvertInt32(int32_t &val) {
118 if (MustSwapBytes()) val = BBOp::SwapBytesInt32(val);
119 }
120 inline void ConvertInt64(int64_t &val) {
121 if (MustSwapBytes()) val = BBOp::SwapBytesInt64(val);
122 }
123};
124
125//
126// DataStreamSection wraps another stream and restricts its access
127// to a particular range of offsets of the base stream.
128// StreamSection does NOT own the base stream, and closing
129// a "stream section" does NOT close the base stream.
130// Base stream must stay in memory for as long as there are
131// "stream sections" referring it.
133public:
134 // Constructs a StreamSection over a base stream,
135 // restricting working range to [start, end), i.e. end offset is
136 // +1 past allowed position.
137 DataStreamSection(Stream *base, soff_t start, soff_t end);
138 const char *GetPath() const { return _base->GetPath().GetCStr(); }
139 bool EOS() const override { return _position >= _end; }
140 bool GetError() const override { return _base->GetError(); }
141 soff_t GetLength() const override { return _end - _start; }
142 soff_t GetPosition() const override { return _position - _start; }
143 size_t Read(void *buffer, size_t len) override;
144 int32_t ReadByte() override;
145 size_t Write(const void *buffer, size_t len) override;
146 int32_t WriteByte(uint8_t b) override;
147 soff_t Seek(soff_t offset, StreamSeek origin = kSeekCurrent) override;
148 bool Flush() override { return _base->Flush(); }
149 void Close() override;
150
151private:
152 Stream *_base = nullptr;
153 soff_t _start = 0;
154 soff_t _end = 0;
155 soff_t _position = 0;
156};
157
158} // namespace Shared
159} // namespace AGS
160} // namespace AGS3
161
162#endif
Definition: data_stream.h:40
Definition: data_stream.h:132
Definition: stream.h:52