OpenTTD Source 20260721-master-g25ec12c62d
newgrf_bytereader.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD 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, version 2.
4 * OpenTTD 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef NEWGRF_BYTEREADER_H
11#define NEWGRF_BYTEREADER_H
12
15
17
19class ByteReader {
20 StringConsumer consumer;
21public:
22 ByteReader(const uint8_t *data, size_t len) : consumer(std::string_view{reinterpret_cast<const char *>(data), len}) { }
23
24 const uint8_t *ReadBytes(size_t size)
25 {
26 auto result = this->consumer.Read(size);
27 if (result.size() != size) throw OTTDByteReaderSignal();
28 return reinterpret_cast<const uint8_t *>(result.data());
29 }
30
35 uint8_t ReadByte()
36 {
37 auto result = this->consumer.TryReadUint8();
38 if (!result.has_value()) throw OTTDByteReaderSignal();
39 return *result;
40 }
41
46 uint16_t ReadWord()
47 {
48 auto result = this->consumer.TryReadUint16LE();
49 if (!result.has_value()) throw OTTDByteReaderSignal();
50 return *result;
51 }
52
58 {
59 uint16_t val = this->ReadByte();
60 return val == 0xFF ? this->ReadWord() : val;
61 }
62
67 uint32_t ReadDWord()
68 {
69 auto result = this->consumer.TryReadUint32LE();
70 if (!result.has_value()) throw OTTDByteReaderSignal();
71 return *result;
72 }
73
79 uint32_t PeekDWord()
80 {
81 auto result = this->consumer.PeekUint32LE();
82 if (!result.has_value()) throw OTTDByteReaderSignal();
83 return *result;
84 }
85
86 uint32_t ReadVarSize(uint8_t size);
87
92 std::string_view ReadString()
93 {
94 /* Terminating NUL may be missing at the end of sprite. */
95 return this->consumer.ReadUntilChar('\0', StringConsumer::SKIP_ONE_SEPARATOR);
96 }
97
102 template <typename T> requires std::is_base_of_v<BaseLabel, T>
104 {
105 T label{};
106 std::ranges::copy(this->consumer.Read(label.size()), label.data());
107 return label;
108 }
109
110 size_t Remaining() const
111 {
112 return this->consumer.GetBytesLeft();
113 }
114
115 bool HasData(size_t count = 1) const
116 {
117 return count <= this->consumer.GetBytesLeft();
118 }
119
120 void Skip(size_t len)
121 {
122 auto result = this->consumer.Read(len);
123 if (result.size() != len) throw OTTDByteReaderSignal();
124 }
125
126};
127
128#endif /* NEWGRF_BYTEREADER_H */
uint32_t PeekDWord()
Read a single DWord (32 bits).
uint32_t ReadDWord()
Read a single DWord (32 bits).
uint16_t ReadWord()
Read a single Word (16 bits).
T ReadLabel()
Read a label.
std::string_view ReadString()
Read a NUL-terminated string.
uint16_t ReadExtendedByte()
Read a single Extended Byte (8 or 16 bits).
uint32_t ReadVarSize(uint8_t size)
Read a value of the given number of bytes.
uint8_t ReadByte()
Read a single byte (8 bits).
Parse data from a string / buffer.
std::optional< uint32_t > TryReadUint32LE()
Try to read binary uint32, and then advance reader.
std::string_view ReadUntilChar(char c, SeparatorUsage sep)
Read data until the first occurrence of 8-bit char 'c', and advance reader.
@ SKIP_ONE_SEPARATOR
Read and discard one separator, do not include it in the result.
std::optional< uint32_t > PeekUint32LE() const
Peek binary uint32 using little endian.
std::optional< uint16_t > TryReadUint16LE()
Try to read binary uint16, and then advance reader.
size_type GetBytesLeft() const noexcept
Get number of bytes left to read.
std::optional< uint8_t > TryReadUint8()
Try to read binary uint8, and then advance reader.
std::string_view Read(size_type len)
Read the next 'len' bytes, and advance reader.
#define T
Climate temperate.
Definition engines.h:91
A type for 4 character labels/tags/ids in files that should be read/shown as is.
Parse strings.