ScummVM API documentation
aligned_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 // Class AlignedStream
25 // A simple wrapper around stream that controls data padding.
26 //
27 // Originally, a number of objects in AGS were read and written directly
28 // as a data struct in a whole. In order to support backwards compatibility
29 // with games made by older versions of AGS, some of the game objects must
30 // be read having automatic data alignment in mind.
31 //-----------------------------------------------------------------------------
32 //
33 // AlignedStream uses the underlying stream, it overrides the reading and
34 // writing, and inserts extra data padding when needed.
35 //
36 // Aligned stream works either in read or write mode, it cannot be opened in
37 // combined mode.
38 //
39 // AlignedStream does not support seek, hence moving stream pointer to random
40 // position will break padding count logic.
41 //
42 //=============================================================================
43 
44 #ifndef AGS_SHARED_UTIL_ALIGNEDSTREAM_H
45 #define AGS_SHARED_UTIL_ALIGNEDSTREAM_H
46 
47 #include "ags/shared/util/proxy_stream.h"
48 
49 namespace AGS3 {
50 namespace AGS {
51 namespace Shared {
52 
53 enum AlignedStreamMode {
54 kAligned_Read,
55 kAligned_Write
56 };
57 
58 class AlignedStream : public ProxyStream {
59 public:
60  AlignedStream(Stream *stream, AlignedStreamMode mode,
61  ObjectOwnershipPolicy stream_ownership_policy = kReleaseAfterUse,
62  size_t base_alignment = sizeof(int16_t));
63  ~AlignedStream() override;
64 
65  // Read/Write cumulated padding and reset block counter
66  void Reset();
67 
68  void Close() override;
69 
70  bool CanRead() const override;
71  bool CanWrite() const override;
72  bool CanSeek() const override;
73 
74  size_t Read(void *buffer, size_t size) override;
75  int32_t ReadByte() override;
76  int16_t ReadInt16() override;
77  int32_t ReadInt32() override;
78  int64_t ReadInt64() override;
79  size_t ReadArray(void *buffer, size_t elem_size, size_t count) override;
80  size_t ReadArrayOfInt16(int16_t *buffer, size_t count) override;
81  size_t ReadArrayOfInt32(int32_t *buffer, size_t count) override;
82  size_t ReadArrayOfInt64(int64_t *buffer, size_t count) override;
83 
84  size_t Write(const void *buffer, size_t size) override;
85  int32_t WriteByte(uint8_t b) override;
86  size_t WriteInt16(int16_t val) override;
87  size_t WriteInt32(int32_t val) override;
88  size_t WriteInt64(int64_t val) override;
89  size_t WriteArray(const void *buffer, size_t elem_size, size_t count) override;
90  size_t WriteArrayOfInt16(const int16_t *buffer, size_t count) override;
91  size_t WriteArrayOfInt32(const int32_t *buffer, size_t count) override;
92  size_t WriteArrayOfInt64(const int64_t *buffer, size_t count) override;
93 
94  bool Seek(soff_t offset, StreamSeek origin) override;
95 
96 protected:
97  void ReadPadding(size_t next_type);
98  void WritePadding(size_t next_type);
99  void FinalizeBlock();
100 
101  private:
102  static const size_t LargestPossibleType = sizeof(int64_t);
103 
104  AlignedStreamMode _mode;
105  size_t _baseAlignment;
106  size_t _maxAlignment;
107  int64_t _block;
108 };
109 
110 } // namespace Shared
111 } // namespace AGS
112 } // namespace AGS3
113 
114 #endif
Definition: achievements_tables.h:27
Definition: aligned_stream.h:58
Definition: proxy_stream.h:37
Definition: stream.h:52
Definition: ags.h:40