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 
36 namespace AGS3 {
37 namespace AGS {
38 namespace Shared {
39 
40 class DataStream : public Stream {
41 public:
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 } // namespace Shared
126 } // namespace AGS
127 } // namespace AGS3
128 
129 #endif
Definition: achievements_tables.h:27
Definition: data_stream.h:40
Definition: stream.h:52
Definition: ags.h:40