OpenTTD Source  20240917-master-g9ab0a47812
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 
25 RandomAccessFile::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 
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 
72 {
73  return this->pos + (this->buffer - this->buffer_end);
74 }
75 
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 
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 
145 void RandomAccessFile::ReadBlock(void *ptr, size_t size)
146 {
147  this->SeekTo(this->GetPos(), SEEK_SET);
148  this->pos += fread(ptr, 1, size, *this->file_handle);
149 }
150 
156 {
157  assert(this->buffer_end >= this->buffer);
158  size_t remaining = this->buffer_end - this->buffer;
159  if (n <= remaining) {
160  this->buffer += n;
161  } else {
162  this->SeekTo(n, SEEK_CUR);
163  }
164 }
RandomAccessFile::ReadByte
uint8_t ReadByte()
Read a byte from the file.
Definition: random_access_file.cpp:107
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::pos
size_t pos
Position in the file of the end of the read buffer.
Definition: random_access_file_type.h:30
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
fileio_func.h
RandomAccessFile::end_pos
size_t end_pos
End position of file.
Definition: random_access_file_type.h:32
RandomAccessFile::ReadBlock
void ReadBlock(void *ptr, size_t size)
Read a block.
Definition: random_access_file.cpp:145
RandomAccessFile::buffer
uint8_t * buffer
Current position within the local buffer.
Definition: random_access_file_type.h:34
RandomAccessFile::file_handle
std::optional< FileHandle > file_handle
File handle of the open file.
Definition: random_access_file_type.h:29
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
random_access_file_type.h
RandomAccessFile::simplified_filename
std::string simplified_filename
Simplified lowecase name of the file; only the name, no path or extension.
Definition: random_access_file_type.h:27
error_func.h
safeguards.h
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
stdafx.h
RandomAccessFile::GetPos
size_t GetPos() const
Get position in the file.
Definition: random_access_file.cpp:71
RandomAccessFile::buffer_end
uint8_t * buffer_end
Last valid byte of buffer.
Definition: random_access_file_type.h:35
string_func.h
RandomAccessFile::ReadWord
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition: random_access_file.cpp:124
RandomAccessFile::SkipBytes
void SkipBytes(size_t n)
Skip n bytes ahead in the file.
Definition: random_access_file.cpp:155
RandomAccessFile::RandomAccessFile
RandomAccessFile(const std::string &filename, Subdirectory subdir)
Create the RandomAccesFile.
Definition: random_access_file.cpp:25
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
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:115
RandomAccessFile::AtEndOfFile
bool AtEndOfFile() const
Test if we have reached the end of the file.
Definition: random_access_file.cpp:80
FioFOpenFile
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
RandomAccessFile::GetSimplifiedFilename
const std::string & GetSimplifiedFilename() const
Get the simplified filename of the opened file.
Definition: random_access_file.cpp:62
RandomAccessFile::SeekTo
void SeekTo(size_t pos, int mode)
Seek in the current file.
Definition: random_access_file.cpp:90
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::buffer_start
uint8_t buffer_start[BUFFER_SIZE]
Local buffer when read from file.
Definition: random_access_file_type.h:36
debug.h