OpenTTD Source 20241224-master-gf74b0cf984
random_access_file.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"
12
13#include "debug.h"
14#include "error_func.h"
15#include "fileio_func.h"
16#include "string_func.h"
17
18#include "safeguards.h"
19
25RandomAccessFile::RandomAccessFile(const std::string &filename, Subdirectory subdir) : filename(filename)
26{
27 size_t file_size;
28 this->file_handle = FioFOpenFile(filename, "rb", subdir, &file_size);
29 if (!this->file_handle.has_value()) UserError("Cannot open file '{}'", filename);
30
31 /* When files are in a tar-file, the begin of the file might not be at 0. */
32 long pos = ftell(*this->file_handle);
33 if (pos < 0) UserError("Cannot read file '{}'", filename);
34
35 /* Make a note of start and end position for readers who check bounds. */
36 this->start_pos = pos;
37 this->end_pos = this->start_pos + file_size;
38
39 /* Store the filename without path and extension */
40 auto t = filename.rfind(PATHSEPCHAR);
41 std::string name_without_path = filename.substr(t != std::string::npos ? t + 1 : 0);
42 this->simplified_filename = name_without_path.substr(0, name_without_path.rfind('.'));
43 strtolower(this->simplified_filename);
44
45 this->SeekTo(static_cast<size_t>(pos), SEEK_SET);
46}
47
52const std::string &RandomAccessFile::GetFilename() const
53{
54 return this->filename;
55}
56
63{
64 return this->simplified_filename;
65}
66
72{
73 return this->pos + (this->buffer - this->buffer_end);
74}
75
81{
82 return this->GetPos() >= this->GetEndPos();
83}
84
90void RandomAccessFile::SeekTo(size_t pos, int mode)
91{
92 if (mode == SEEK_CUR) pos += this->GetPos();
93
94 this->pos = pos;
95 if (fseek(*this->file_handle, this->pos, SEEK_SET) < 0) {
96 Debug(misc, 0, "Seeking in {} failed", this->filename);
97 }
98
99 /* Reset the buffer, so the next ReadByte will read bytes from the file. */
100 this->buffer = this->buffer_end = this->buffer_start;
101}
102
108{
109 if (this->buffer == this->buffer_end) {
110 this->buffer = this->buffer_start;
111 size_t size = fread(this->buffer, 1, RandomAccessFile::BUFFER_SIZE, *this->file_handle);
112 this->pos += size;
113 this->buffer_end = this->buffer_start + size;
114
115 if (size == 0) return 0;
116 }
117 return *this->buffer++;
118}
119
125{
126 uint8_t b = this->ReadByte();
127 return (this->ReadByte() << 8) | b;
128}
129
135{
136 uint b = this->ReadWord();
137 return (this->ReadWord() << 16) | b;
138}
139
145void RandomAccessFile::ReadBlock(void *ptr, size_t size)
146{
147 if (this->buffer != this->buffer_end) {
148 size_t to_copy = std::min<size_t>(size, this->buffer_end - this->buffer);
149 memcpy(ptr, this->buffer, to_copy);
150 this->buffer += to_copy;
151 size -= to_copy;
152 if (size == 0) return;
153 ptr = static_cast<char *>(ptr) + to_copy;
154 }
155
156 this->pos += fread(ptr, 1, size, *this->file_handle);
157}
158
164{
165 assert(this->buffer_end >= this->buffer);
166 size_t remaining = this->buffer_end - this->buffer;
167 if (n <= remaining) {
168 this->buffer += n;
169 } else {
170 this->SeekTo(n, SEEK_CUR);
171 }
172}
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.
RandomAccessFile(const std::string &filename, Subdirectory subdir)
Create the RandomAccesFile.
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 lowecase 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).
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
Error reporting related functions.
std::optional< FileHandle > FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition fileio.cpp:242
Functions for Standard In/Out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Class related to random access to files.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.