OpenTTD Source 20250331-master-g3c15e0c889
newgrf_bytereader.cpp
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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "../core/backup_type.hpp"
12#include "../string_func.h"
13#include "newgrf_bytereader.h"
14
15#include "../safeguards.h"
16
23{
24 AutoRestoreBackup backup(this->data, this->data);
25 return this->ReadDWord();
26}
27
32uint32_t ByteReader::ReadVarSize(uint8_t size)
33{
34 switch (size) {
35 case 1: return this->ReadByte();
36 case 2: return this->ReadWord();
37 case 4: return this->ReadDWord();
38 default:
39 NOT_REACHED();
40 return 0;
41 }
42}
43
48std::string_view ByteReader::ReadString()
49{
50 const char *string = reinterpret_cast<const char *>(this->data);
51 size_t string_length = ttd_strnlen(string, this->Remaining());
52
53 /* Skip past the terminating NUL byte if it is present, but not more than remaining. */
54 this->Skip(std::min(string_length + 1, this->Remaining()));
55
56 return std::string_view(string, string_length);
57}
const uint8_t * data
Current position within data.
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).
std::string_view ReadString()
Read a string.
uint32_t ReadVarSize(uint8_t size)
Read a value of the given number of bytes.
uint8_t ReadByte()
Read a single byte (8 bits).
NewGRF buffer reader definition.
size_t ttd_strnlen(const char *str, size_t maxlen)
Get the length of a string, within a limited buffer.
Definition string_func.h:70
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...