OpenTTD Source 15.0-RC2
random_access_file_type.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
10#ifndef RANDOM_ACCESS_FILE_TYPE_H
11#define RANDOM_ACCESS_FILE_TYPE_H
12
13#include "fileio_type.h"
14
24 static constexpr int BUFFER_SIZE = 512;
25
26 std::string filename;
27 std::string simplified_filename;
28
29 std::optional<FileHandle> file_handle;
30 size_t pos;
31 size_t start_pos;
32 size_t end_pos;
33
34 uint8_t *buffer;
35 uint8_t *buffer_end;
37
38public:
39 RandomAccessFile(std::string_view filename, Subdirectory subdir);
40 RandomAccessFile(const RandomAccessFile&) = delete;
41 void operator=(const RandomAccessFile&) = delete;
42
43 virtual ~RandomAccessFile() = default;
44
45 const std::string &GetFilename() const;
46 const std::string &GetSimplifiedFilename() const;
47
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;
53
54 uint8_t ReadByte();
55 uint16_t ReadWord();
56 uint32_t ReadDword();
57
58 void ReadBlock(void *ptr, size_t size);
59 void SkipBytes(size_t n);
60};
61
62#endif /* RANDOM_ACCESS_FILE_TYPE_H */
A file from which bytes, words and double words are read in (potentially) a random order.
void ReadBlock(void *ptr, size_t size)
Read a block.
size_t start_pos
Start position of file. May be non-zero if file is within a tar file.
size_t GetPos() const
Get position in the file.
std::optional< FileHandle > file_handle
File handle of the open file.
bool AtEndOfFile() const
Test if we have reached the end of the file.
uint8_t buffer_start[BUFFER_SIZE]
Local buffer when read from file.
const std::string & GetFilename() const
Get the filename of the opened file with the path from the SubDirectory and the extension.
void SeekTo(size_t pos, int mode)
Seek in the current file.
std::string filename
Full name of the file; relative path to subdir plus the extension of the file.
static constexpr int BUFFER_SIZE
The number of bytes to allocate for the buffer.
uint8_t ReadByte()
Read a byte from the file.
const std::string & GetSimplifiedFilename() const
Get the simplified filename of the opened file.
std::string simplified_filename
Simplified lowercase name of the file; only the name, no path or extension.
uint32_t ReadDword()
Read a double word (32 bits) from the file (in low endian format).
size_t pos
Position in the file of the end of the read buffer.
size_t end_pos
End position of file.
uint8_t * buffer
Current position within the local buffer.
uint8_t * buffer_end
Last valid byte of buffer.
void SkipBytes(size_t n)
Skip n bytes ahead in the file.
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
Types for Standard In/Out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88