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/>.
8 /** @file random_access_file.cpp Actual implementation of the RandomAccessFile class. */
11 #include "random_access_file_type.h"
14 #include "error_func.h"
15 #include "fileio_func.h"
16 #include "string_func.h"
18 #include "safeguards.h"
21 * Create the RandomAccesFile.
22 * @param filename Name of the file at the disk.
23 * @param subdir The sub directory to search this file in.
25 RandomAccessFile::RandomAccessFile(const std::string
&filename
, Subdirectory subdir
) : filename(filename
)
28 this->file_handle
= FioFOpenFile(filename
, "rb", subdir
, &file_size
);
29 if (!this->file_handle
.has_value()) UserError("Cannot open file '{}'", filename
);
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
);
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
;
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
);
45 this->SeekTo(static_cast<size_t>(pos
), SEEK_SET
);
49 * Get the filename of the opened file with the path from the SubDirectory and the extension.
50 * @return Name of the file.
52 const std::string
&RandomAccessFile::GetFilename() const
54 return this->filename
;
58 * Get the simplified filename of the opened file. The simplified filename is the name of the
59 * file without the SubDirectory or extension in lower case.
60 * @return Name of the file.
62 const std::string
&RandomAccessFile::GetSimplifiedFilename() const
64 return this->simplified_filename
;
68 * Get position in the file.
69 * @return Position in the file.
71 size_t RandomAccessFile::GetPos() const
73 return this->pos
+ (this->buffer
- this->buffer_end
);
77 * Test if we have reached the end of the file.
78 * @return True iff the current position as at or after the end of the file.
80 bool RandomAccessFile::AtEndOfFile() const
82 return this->GetPos() >= this->GetEndPos();
86 * Seek in the current file.
87 * @param pos New position.
88 * @param mode Type of seek (\c SEEK_CUR means \a pos is relative to current position, \c SEEK_SET means \a pos is absolute).
90 void RandomAccessFile::SeekTo(size_t pos
, int mode
)
92 if (mode
== SEEK_CUR
) pos
+= this->GetPos();
95 if (fseek(*this->file_handle
, this->pos
, SEEK_SET
) < 0) {
96 Debug(misc
, 0, "Seeking in {} failed", this->filename
);
99 /* Reset the buffer, so the next ReadByte will read bytes from the file. */
100 this->buffer
= this->buffer_end
= this->buffer_start
;
104 * Read a byte from the file.
107 uint8_t RandomAccessFile::ReadByte()
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
);
113 this->buffer_end
= this->buffer_start
+ size
;
115 if (size
== 0) return 0;
117 return *this->buffer
++;
121 * Read a word (16 bits) from the file (in low endian format).
124 uint16_t RandomAccessFile::ReadWord()
126 uint8_t b
= this->ReadByte();
127 return (this->ReadByte() << 8) | b
;
131 * Read a double word (32 bits) from the file (in low endian format).
134 uint32_t RandomAccessFile::ReadDword()
136 uint b
= this->ReadWord();
137 return (this->ReadWord() << 16) | b
;
142 * @param ptr Destination buffer.
143 * @param size Number of bytes to read.
145 void RandomAccessFile::ReadBlock(void *ptr
, size_t size
)
147 this->SeekTo(this->GetPos(), SEEK_SET
);
148 this->pos
+= fread(ptr
, 1, size
, *this->file_handle
);
152 * Skip \a n bytes ahead in the file.
153 * @param n Number of bytes to skip reading.
155 void RandomAccessFile::SkipBytes(size_t n
)
157 assert(this->buffer_end
>= this->buffer
);
158 size_t remaining
= this->buffer_end
- this->buffer
;
159 if (n
<= remaining
) {
162 this->SeekTo(n
, SEEK_CUR
);