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/>.
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"
16 * A file from which bytes, words and double words are read in (potentially) a random order.
18 * This is mostly intended to be used for things that can be read from GRFs when needed, so
19 * the graphics but also the sounds. This also ties into the spritecache as it uses these
20 * files to load the sprites from when needed.
22 class RandomAccessFile
{
23 /** The number of bytes to allocate for the buffer. */
24 static constexpr int BUFFER_SIZE
= 512;
26 std::string filename
; ///< Full name of the file; relative path to subdir plus the extension of the file.
27 std::string simplified_filename
; ///< Simplified lowecase name of the file; only the name, no path or extension.
29 std::optional
<FileHandle
> file_handle
; ///< File handle of the open file.
30 size_t pos
; ///< Position in the file of the end of the read buffer.
31 size_t start_pos
; ///< Start position of file. May be non-zero if file is within a tar file.
32 size_t end_pos
; ///< End position of file.
34 uint8_t *buffer
; ///< Current position within the local buffer.
35 uint8_t *buffer_end
; ///< Last valid byte of buffer.
36 uint8_t buffer_start
[BUFFER_SIZE
]; ///< Local buffer when read from file.
39 RandomAccessFile(const std::string
&filename
, Subdirectory subdir
);
40 RandomAccessFile(const RandomAccessFile
&) = delete;
41 void operator=(const RandomAccessFile
&) = delete;
43 virtual ~RandomAccessFile() {}
45 const std::string
&GetFilename() const;
46 const std::string
&GetSimplifiedFilename() const;
48 size_t GetPos() const;
49 size_t GetStartPos() const { return this->start_pos
; }
50 size_t GetEndPos() const { return this->end_pos
; }
51 void SeekTo(size_t pos
, int mode
);
52 bool AtEndOfFile() const;
58 void ReadBlock(void *ptr
, size_t size
);
59 void SkipBytes(size_t n
);
62 #endif /* RANDOM_ACCESS_FILE_TYPE_H */