OpenTTD Source: src/random_access_file_type.h Source File

OpenTTD Source 15.3

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
22 class RandomAccessFile {
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;
36 uint8_t buffer_start[BUFFER_SIZE];
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 */
RandomAccessFile
A file from which bytes, words and double words are read in (potentially) a random order.
Definition random_access_file_type.h:22
RandomAccessFile::ReadBlock
void ReadBlock(void *ptr, size_t size)
Read a block.
Definition random_access_file.cpp:145
RandomAccessFile::start_pos
size_t start_pos
Start position of file. May be non-zero if file is within a tar file.
Definition random_access_file_type.h:31
RandomAccessFile::GetPos
size_t GetPos() const
Get position in the file.
Definition random_access_file.cpp:71
RandomAccessFile::file_handle
std::optional< FileHandle > file_handle
File handle of the open file.
Definition random_access_file_type.h:29
RandomAccessFile::AtEndOfFile
bool AtEndOfFile() const
Test if we have reached the end of the file.
Definition random_access_file.cpp:80
RandomAccessFile::buffer_start
uint8_t buffer_start[BUFFER_SIZE]
Local buffer when read from file.
Definition random_access_file_type.h:36
RandomAccessFile::GetFilename
const std::string & GetFilename() const
Get the filename of the opened file with the path from the SubDirectory and the extension.
Definition random_access_file.cpp:52
RandomAccessFile::SeekTo
void SeekTo(size_t pos, int mode)
Seek in the current file.
Definition random_access_file.cpp:90
RandomAccessFile::filename
std::string filename
Full name of the file; relative path to subdir plus the extension of the file.
Definition random_access_file_type.h:26
RandomAccessFile::BUFFER_SIZE
static constexpr int BUFFER_SIZE
The number of bytes to allocate for the buffer.
Definition random_access_file_type.h:24
RandomAccessFile::ReadByte
uint8_t ReadByte()
Read a byte from the file.
Definition random_access_file.cpp:107
RandomAccessFile::GetSimplifiedFilename
const std::string & GetSimplifiedFilename() const
Get the simplified filename of the opened file.
Definition random_access_file.cpp:62
RandomAccessFile::simplified_filename
std::string simplified_filename
Simplified lowercase name of the file; only the name, no path or extension.
Definition random_access_file_type.h:27
RandomAccessFile::ReadDword
uint32_t ReadDword()
Read a double word (32 bits) from the file (in low endian format).
Definition random_access_file.cpp:134
RandomAccessFile::pos
size_t pos
Position in the file of the end of the read buffer.
Definition random_access_file_type.h:30
RandomAccessFile::end_pos
size_t end_pos
End position of file.
Definition random_access_file_type.h:32
RandomAccessFile::buffer
uint8_t * buffer
Current position within the local buffer.
Definition random_access_file_type.h:34
RandomAccessFile::buffer_end
uint8_t * buffer_end
Last valid byte of buffer.
Definition random_access_file_type.h:35
RandomAccessFile::SkipBytes
void SkipBytes(size_t n)
Skip n bytes ahead in the file.
Definition random_access_file.cpp:163
RandomAccessFile::ReadWord
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition random_access_file.cpp:124
fileio_type.h
Types for Standard In/Out file operations.
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88