1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/std/algorithm.h"
#include "ags/shared/util/data_stream.h"
namespace AGS3 {
namespace AGS {
namespace Shared {
DataStream::DataStream(DataEndianess stream_endianess)
: _streamEndianess(stream_endianess) {
}
DataStream::~DataStream() {}
int16_t DataStream::ReadInt16() {
int16_t val = 0;
Read(&val, sizeof(int16_t));
ConvertInt16(val);
return val;
}
int32_t DataStream::ReadInt32() {
int32_t val = 0;
Read(&val, sizeof(int32_t));
ConvertInt32(val);
return val;
}
int64_t DataStream::ReadInt64() {
int64_t val = 0;
Read(&val, sizeof(int64_t));
ConvertInt64(val);
return val;
}
size_t DataStream::WriteInt16(int16_t val) {
ConvertInt16(val);
return Write(&val, sizeof(int16_t));
}
size_t DataStream::WriteInt32(int32_t val) {
ConvertInt32(val);
return Write(&val, sizeof(int32_t));
}
size_t DataStream::WriteInt64(int64_t val) {
ConvertInt64(val);
return Write(&val, sizeof(int64_t));
}
size_t DataStream::ReadAndConvertArrayOfInt16(int16_t *buffer, size_t count) {
count = ReadArray(buffer, sizeof(int16_t), count);
for (size_t i = 0; i < count; ++i, ++buffer) {
*buffer = BBOp::SwapBytesInt16(*buffer);
}
return count;
}
size_t DataStream::ReadAndConvertArrayOfInt32(int32_t *buffer, size_t count) {
count = ReadArray(buffer, sizeof(int32_t), count);
for (size_t i = 0; i < count; ++i, ++buffer) {
*buffer = BBOp::SwapBytesInt32(*buffer);
}
return count;
}
size_t DataStream::ReadAndConvertArrayOfInt64(int64_t *buffer, size_t count) {
count = ReadArray(buffer, sizeof(int64_t), count);
for (size_t i = 0; i < count; ++i, ++buffer) {
*buffer = BBOp::SwapBytesInt64(*buffer);
}
return count;
}
size_t DataStream::WriteAndConvertArrayOfInt16(const int16_t *buffer, size_t count) {
size_t elem;
for (elem = 0; elem < count; ++elem, ++buffer) {
int16_t val = *buffer;
ConvertInt16(val);
if (Write(&val, sizeof(int16_t)) < sizeof(int16_t)) {
break;
}
}
return elem;
}
size_t DataStream::WriteAndConvertArrayOfInt32(const int32_t *buffer, size_t count) {
size_t elem;
for (elem = 0; elem < count; ++elem, ++buffer) {
int32_t val = *buffer;
ConvertInt32(val);
if (Write(&val, sizeof(int32_t)) < sizeof(int32_t)) {
break;
}
}
return elem;
}
size_t DataStream::WriteAndConvertArrayOfInt64(const int64_t *buffer, size_t count) {
size_t elem;
for (elem = 0; elem < count; ++elem, ++buffer) {
int64_t val = *buffer;
ConvertInt64(val);
if (Write(&val, sizeof(int64_t)) < sizeof(int64_t)) {
break;
}
}
return elem;
}
DataStreamSection::DataStreamSection(Stream *base, soff_t start, soff_t end)
: _base(base) {
_start = std::max((soff_t)0, std::min(start, end));
_end = std::min(std::max((soff_t)0, end), base->GetLength());
soff_t pos = base->Seek(_start, kSeekBegin);
if (pos >= 0)
_position = pos;
else
_position = base->GetPosition();
}
size_t DataStreamSection::Read(void *buffer, size_t len) {
if (_position >= _end)
return 0;
len = std::min(len, static_cast<size_t>(_end - _position));
_position += _base->Read(buffer, len);
return len;
}
int32_t DataStreamSection::ReadByte() {
if (_position >= _end)
return -1;
int32_t b = _base->ReadByte();
if (b >= 0)
_position++;
return b;
}
size_t DataStreamSection::Write(const void *buffer, size_t len) {
len = _base->Write(buffer, len);
_position += len;
_end = std::max(_end, _position); // we might be overwriting after seeking back
return len;
}
int32_t DataStreamSection::WriteByte(uint8_t b) {
int32_t rb = _base->WriteByte(b);
if (rb == b) {
_position++;
_end = std::max(_end, _position); // we might be overwriting after seeking back
}
return rb;
}
soff_t DataStreamSection::Seek(soff_t offset, StreamSeek origin) {
soff_t want_pos = -1;
switch (origin) {
case StreamSeek::kSeekCurrent:
want_pos = _position + offset;
break;
case StreamSeek::kSeekBegin:
want_pos = _start + offset;
break;
case StreamSeek::kSeekEnd:
want_pos = _end + offset;
break;
default:
return -1;
}
want_pos = std::min(std::max(want_pos, _start), _end);
soff_t new_pos = _base->Seek(want_pos, kSeekBegin);
if (new_pos >= 0) // the position remains in case of seek error
_position = want_pos;
return _position - _start; // convert to a stream section pos
}
void DataStreamSection::Close() {
// We do not close nor delete the base stream, but release it from use
_base = nullptr;
}
} // namespace Shared
} // namespace AGS
} // namespace AGS3
|