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
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "ags/shared/util/data_stream.h"
24
25 namespace AGS3 {
26 namespace AGS {
27 namespace Shared {
28
DataStream(DataEndianess stream_endianess)29 DataStream::DataStream(DataEndianess stream_endianess)
30 : _streamEndianess(stream_endianess) {
31 }
32
~DataStream()33 DataStream::~DataStream() {}
34
ReadInt16()35 int16_t DataStream::ReadInt16() {
36 int16_t val = 0;
37 Read(&val, sizeof(int16_t));
38 ConvertInt16(val);
39 return val;
40 }
41
ReadInt32()42 int32_t DataStream::ReadInt32() {
43 int32_t val = 0;
44 Read(&val, sizeof(int32_t));
45 ConvertInt32(val);
46 return val;
47 }
48
ReadInt64()49 int64_t DataStream::ReadInt64() {
50 int64_t val = 0;
51 Read(&val, sizeof(int64_t));
52 ConvertInt64(val);
53 return val;
54 }
55
WriteInt16(int16_t val)56 size_t DataStream::WriteInt16(int16_t val) {
57 ConvertInt16(val);
58 return Write(&val, sizeof(int16_t));
59 }
60
WriteInt32(int32_t val)61 size_t DataStream::WriteInt32(int32_t val) {
62 ConvertInt32(val);
63 return Write(&val, sizeof(int32_t));
64 }
65
WriteInt64(int64_t val)66 size_t DataStream::WriteInt64(int64_t val) {
67 ConvertInt64(val);
68 return Write(&val, sizeof(int64_t));
69 }
70
ReadAndConvertArrayOfInt16(int16_t * buffer,size_t count)71 size_t DataStream::ReadAndConvertArrayOfInt16(int16_t *buffer, size_t count) {
72 if (!CanRead() || !buffer) {
73 return 0;
74 }
75
76 count = ReadArray(buffer, sizeof(int16_t), count);
77 for (size_t i = 0; i < count; ++i, ++buffer) {
78 *buffer = BBOp::SwapBytesInt16(*buffer);
79 }
80 return count;
81 }
82
ReadAndConvertArrayOfInt32(int32_t * buffer,size_t count)83 size_t DataStream::ReadAndConvertArrayOfInt32(int32_t *buffer, size_t count) {
84 if (!CanRead() || !buffer) {
85 return 0;
86 }
87
88 count = ReadArray(buffer, sizeof(int32_t), count);
89 for (size_t i = 0; i < count; ++i, ++buffer) {
90 *buffer = BBOp::SwapBytesInt32(*buffer);
91 }
92 return count;
93 }
94
ReadAndConvertArrayOfInt64(int64_t * buffer,size_t count)95 size_t DataStream::ReadAndConvertArrayOfInt64(int64_t *buffer, size_t count) {
96 if (!CanRead() || !buffer) {
97 return 0;
98 }
99
100 count = ReadArray(buffer, sizeof(int64_t), count);
101 for (size_t i = 0; i < count; ++i, ++buffer) {
102 *buffer = BBOp::SwapBytesInt64(*buffer);
103 }
104 return count;
105 }
106
WriteAndConvertArrayOfInt16(const int16_t * buffer,size_t count)107 size_t DataStream::WriteAndConvertArrayOfInt16(const int16_t *buffer, size_t count) {
108 if (!CanWrite() || !buffer) {
109 return 0;
110 }
111
112 size_t elem;
113 for (elem = 0; elem < count && !EOS(); ++elem, ++buffer) {
114 int16_t val = *buffer;
115 ConvertInt16(val);
116 if (Write(&val, sizeof(int16_t)) < sizeof(int16_t)) {
117 break;
118 }
119 }
120 return elem;
121 }
122
WriteAndConvertArrayOfInt32(const int32_t * buffer,size_t count)123 size_t DataStream::WriteAndConvertArrayOfInt32(const int32_t *buffer, size_t count) {
124 if (!CanWrite() || !buffer) {
125 return 0;
126 }
127
128 size_t elem;
129 for (elem = 0; elem < count && !EOS(); ++elem, ++buffer) {
130 int32_t val = *buffer;
131 ConvertInt32(val);
132 if (Write(&val, sizeof(int32_t)) < sizeof(int32_t)) {
133 break;
134 }
135 }
136 return elem;
137 }
138
WriteAndConvertArrayOfInt64(const int64_t * buffer,size_t count)139 size_t DataStream::WriteAndConvertArrayOfInt64(const int64_t *buffer, size_t count) {
140 if (!CanWrite() || !buffer) {
141 return 0;
142 }
143
144 size_t elem;
145 for (elem = 0; elem < count && !EOS(); ++elem, ++buffer) {
146 int64_t val = *buffer;
147 ConvertInt64(val);
148 if (Write(&val, sizeof(int64_t)) < sizeof(int64_t)) {
149 break;
150 }
151 }
152 return elem;
153 }
154
155 } // namespace Shared
156 } // namespace AGS
157 } // namespace AGS3
158