Fix: CmdSetAutoReplace didn't validate group type and engine type match (#9950)
[openttd-github.git] / src / random_access_file_type.h
blobce0e9e3584bb72884e84a847c55bd42bdaf86c05
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 */
8 /** @file random_access_file_type.h Class related to random access to files. */
10 #ifndef RANDOM_ACCESS_FILE_TYPE_H
11 #define RANDOM_ACCESS_FILE_TYPE_H
13 #include "fileio_type.h"
14 #include <string>
16 /**
17 * A file from which bytes, words and double words are read in (potentially) a random order.
19 * This is mostly intended to be used for things that can be read from GRFs when needed, so
20 * the graphics but also the sounds. This also ties into the spritecache as it uses these
21 * files to load the sprites from when needed.
23 class RandomAccessFile {
24 /** The number of bytes to allocate for the buffer. */
25 static constexpr int BUFFER_SIZE = 512;
27 std::string filename; ///< Full name of the file; relative path to subdir plus the extension of the file.
28 std::string simplified_filename; ///< Simplified lowecase name of the file; only the name, no path or extension.
30 FILE *file_handle; ///< File handle of the open file.
31 size_t pos; ///< Position in the file of the end of the read buffer.
33 byte *buffer; ///< Current position within the local buffer.
34 byte *buffer_end; ///< Last valid byte of buffer.
35 byte buffer_start[BUFFER_SIZE]; ///< Local buffer when read from file.
37 public:
38 RandomAccessFile(const std::string &filename, Subdirectory subdir);
39 RandomAccessFile(const RandomAccessFile&) = delete;
40 void operator=(const RandomAccessFile&) = delete;
42 virtual ~RandomAccessFile();
44 const std::string &GetFilename() const;
45 const std::string &GetSimplifiedFilename() const;
47 size_t GetPos() const;
48 void SeekTo(size_t pos, int mode);
50 byte ReadByte();
51 uint16 ReadWord();
52 uint32 ReadDword();
54 void ReadBlock(void *ptr, size_t size);
55 void SkipBytes(int n);
58 #endif /* RANDOM_ACCESS_FILE_TYPE_H */