ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
stream_util.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 #ifndef ULTIMA8_MISC_STREAM_H
23 #define ULTIMA8_MISC_STREAM_H
24 
25 #include "common/stream.h"
26 
27 namespace Ultima {
28 namespace Ultima8 {
29 
30 // Read a 3-byte value, lsb first.
31 inline uint32 readUint24LE(Common::ReadStream &rs) {
32  uint32 val = 0;
33  val |= static_cast<uint32>(rs.readByte());
34  val |= static_cast<uint32>(rs.readByte() << 8);
35  val |= static_cast<uint32>(rs.readByte() << 16);
36  return val;
37 }
38 
39 inline uint32 readX(Common::ReadStream &rs, uint32 num_bytes) {
40  assert(num_bytes > 0 && num_bytes <= 4);
41  if (num_bytes == 1) return rs.readByte();
42  else if (num_bytes == 2) return rs.readUint16LE();
43  else if (num_bytes == 3) return readUint24LE(rs);
44  else return rs.readUint32LE();
45 }
46 
47 inline int32 readXS(Common::ReadStream &rs, uint32 num_bytes) {
48  assert(num_bytes > 0 && num_bytes <= 4);
49  if (num_bytes == 1) return static_cast<int8>(rs.readByte());
50  else if (num_bytes == 2) return static_cast<int16>(rs.readUint16LE());
51  else if (num_bytes == 3) return (((static_cast<int32>(readUint24LE(rs))) << 8) >> 8);
52  else return static_cast<int32>(rs.readUint32LE());
53 }
54 
55 inline void writeUint24LE(Common::WriteStream &ws, uint32 val) {
56  ws.writeByte(static_cast<byte>(val & 0xff));
57  ws.writeByte(static_cast<byte>((val >> 8) & 0xff));
58  ws.writeByte(static_cast<byte>((val >> 16) & 0xff));
59 }
60 
61 inline void writeX(Common::WriteStream &ws, uint32 val, uint32 num_bytes) {
62  assert(num_bytes > 0 && num_bytes <= 4);
63  if (num_bytes == 1) ws.writeByte(static_cast<byte>(val));
64  else if (num_bytes == 2) ws.writeUint16LE(static_cast<uint16>(val));
65  else if (num_bytes == 3) writeUint24LE(ws, val);
66  else ws.writeUint32LE(val);
67 }
68 
69 } // End of namespace Ultima8
70 } // End of namespace Ultima
71 
72 #endif
uint16 readUint16LE()
Definition: stream.h:459
void writeUint32LE(uint32 value)
Definition: stream.h:159
Definition: stream.h:77
uint32 readUint32LE()
Definition: stream.h:473
Definition: detection.h:27
byte readByte()
Definition: stream.h:434
void writeUint16LE(uint16 value)
Definition: stream.h:152
Definition: stream.h:385
void writeByte(byte value)
Definition: stream.h:140