OpenTTD Source: src/random_access_file.cpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "random_access_file_type.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(std::string_view 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
52 const std::string &RandomAccessFile::GetFilename() const
53{
54 return this->filename;
55}
56
62 const std::string &RandomAccessFile::GetSimplifiedFilename() const
63{
64 return this->simplified_filename;
65}
66
71 size_t RandomAccessFile::GetPos() const
72{
73 return this->pos + (this->buffer - this->buffer_end);
74}
75
80 bool RandomAccessFile::AtEndOfFile() const
81{
82 return this->GetPos() >= this->GetEndPos();
83}
84
90 void 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
107uint8_t RandomAccessFile::ReadByte()
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
124uint16_t RandomAccessFile::ReadWord()
125{
126 uint8_t b = this->ReadByte();
127 return (this->ReadByte() << 8) | b;
128}
129
134uint32_t RandomAccessFile::ReadDword()
135{
136 uint b = this->ReadWord();
137 return (this->ReadWord() << 16) | b;
138}
139
145 void 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 std::copy_n(this->buffer, to_copy, static_cast<uint8_t *>(ptr));
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
163 void RandomAccessFile::SkipBytes(size_t n)
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}
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::RandomAccessFile
RandomAccessFile(std::string_view filename, Subdirectory subdir)
Create the RandomAccesFile.
Definition random_access_file.cpp:25
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
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
error_func.h
Error reporting related functions.
FioFOpenFile
std::optional< FileHandle > FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition fileio.cpp:242
fileio_func.h
Functions for Standard In/Out file operations.
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
random_access_file_type.h
Class related to random access to files.
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
string_func.h
Functions related to low-level strings.