Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / random_access_file.cpp
blob37f51530df5869a101d8d54747e25d2583ebeb51
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 */
8 /** @file random_access_file.cpp Actual implementation of the RandomAccessFile class. */
10 #include "stdafx.h"
11 #include "random_access_file_type.h"
13 #include "debug.h"
14 #include "error_func.h"
15 #include "fileio_func.h"
16 #include "string_func.h"
18 #include "safeguards.h"
20 /**
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)
27 this->file_handle = FioFOpenFile(filename, "rb", subdir);
28 if (this->file_handle == nullptr) UserError("Cannot open file '{}'", filename);
30 /* When files are in a tar-file, the begin of the file might not be at 0. */
31 long pos = ftell(this->file_handle);
32 if (pos < 0) UserError("Cannot read file '{}'", filename);
34 /* Store the filename without path and extension */
35 auto t = filename.rfind(PATHSEPCHAR);
36 std::string name_without_path = filename.substr(t != std::string::npos ? t + 1 : 0);
37 this->simplified_filename = name_without_path.substr(0, name_without_path.rfind('.'));
38 strtolower(this->simplified_filename);
40 this->SeekTo((size_t)pos, SEEK_SET);
43 /**
44 * Close the file's file handle.
46 RandomAccessFile::~RandomAccessFile()
48 fclose(this->file_handle);
51 /**
52 * Get the filename of the opened file with the path from the SubDirectory and the extension.
53 * @return Name of the file.
55 const std::string &RandomAccessFile::GetFilename() const
57 return this->filename;
60 /**
61 * Get the simplified filename of the opened file. The simplified filename is the name of the
62 * file without the SubDirectory or extension in lower case.
63 * @return Name of the file.
65 const std::string &RandomAccessFile::GetSimplifiedFilename() const
67 return this->simplified_filename;
70 /**
71 * Get position in the file.
72 * @return Position in the file.
74 size_t RandomAccessFile::GetPos() const
76 return this->pos + (this->buffer - this->buffer_end);
79 /**
80 * Seek in the current file.
81 * @param pos New position.
82 * @param mode Type of seek (\c SEEK_CUR means \a pos is relative to current position, \c SEEK_SET means \a pos is absolute).
84 void RandomAccessFile::SeekTo(size_t pos, int mode)
86 if (mode == SEEK_CUR) pos += this->GetPos();
88 this->pos = pos;
89 if (fseek(this->file_handle, this->pos, SEEK_SET) < 0) {
90 Debug(misc, 0, "Seeking in {} failed", this->filename);
93 /* Reset the buffer, so the next ReadByte will read bytes from the file. */
94 this->buffer = this->buffer_end = this->buffer_start;
97 /**
98 * Read a byte from the file.
99 * @return Read byte.
101 byte RandomAccessFile::ReadByte()
103 if (this->buffer == this->buffer_end) {
104 this->buffer = this->buffer_start;
105 size_t size = fread(this->buffer, 1, RandomAccessFile::BUFFER_SIZE, this->file_handle);
106 this->pos += size;
107 this->buffer_end = this->buffer_start + size;
109 if (size == 0) return 0;
111 return *this->buffer++;
115 * Read a word (16 bits) from the file (in low endian format).
116 * @return Read word.
118 uint16_t RandomAccessFile::ReadWord()
120 byte b = this->ReadByte();
121 return (this->ReadByte() << 8) | b;
125 * Read a double word (32 bits) from the file (in low endian format).
126 * @return Read word.
128 uint32_t RandomAccessFile::ReadDword()
130 uint b = this->ReadWord();
131 return (this->ReadWord() << 16) | b;
135 * Read a block.
136 * @param ptr Destination buffer.
137 * @param size Number of bytes to read.
139 void RandomAccessFile::ReadBlock(void *ptr, size_t size)
141 this->SeekTo(this->GetPos(), SEEK_SET);
142 this->pos += fread(ptr, 1, size, this->file_handle);
146 * Skip \a n bytes ahead in the file.
147 * @param n Number of bytes to skip reading.
149 void RandomAccessFile::SkipBytes(size_t n)
151 assert(this->buffer_end >= this->buffer);
152 size_t remaining = this->buffer_end - this->buffer;
153 if (n <= remaining) {
154 this->buffer += n;
155 } else {
156 this->SeekTo(n, SEEK_CUR);