OpenTTD Source: src/random_access_file.cpp Source File

OpenTTD Source 13.2.1

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"
11 #include "random_access_file_type.h"
12
13 #include "debug.h"
14 #include "fileio_func.h"
15 #include "string_func.h"
16
17 #include "safeguards.h"
18
24 RandomAccessFile::RandomAccessFile(const std::string &filename, Subdirectory subdir) : filename(filename)
25 {
26 this->file_handle = FioFOpenFile(filename, "rb", subdir);
27 if (this->file_handle == nullptr) usererror("Cannot open file '%s'", filename.c_str());
28
29 /* When files are in a tar-file, the begin of the file might not be at 0. */
30 long pos = ftell(this->file_handle);
31 if (pos < 0) usererror("Cannot read file '%s'", filename.c_str());
32
33 /* Store the filename without path and extension */
34 auto t = filename.rfind(PATHSEPCHAR);
35 std::string name_without_path = filename.substr(t != std::string::npos ? t + 1 : 0);
36 this->simplified_filename = name_without_path.substr(0, name_without_path.rfind('.'));
37 strtolower(this->simplified_filename);
38
39 this->SeekTo((size_t)pos, SEEK_SET);
40 }
41
45 RandomAccessFile::~RandomAccessFile()
46 {
47 fclose(this->file_handle);
48 }
49
54 const std::string &RandomAccessFile::GetFilename() const
55 {
56 return this->filename;
57 }
58
64 const std::string &RandomAccessFile::GetSimplifiedFilename() const
65 {
66 return this->simplified_filename;
67 }
68
73 size_t RandomAccessFile::GetPos() const
74 {
75 return this->pos + (this->buffer - this->buffer_end);
76 }
77
83 void RandomAccessFile::SeekTo(size_t pos, int mode)
84 {
85 if (mode == SEEK_CUR) pos += this->GetPos();
86
87 this->pos = pos;
88 if (fseek(this->file_handle, this->pos, SEEK_SET) < 0) {
89 Debug(misc, 0, "Seeking in {} failed", this->filename);
90 }
91
92 /* Reset the buffer, so the next ReadByte will read bytes from the file. */
93 this->buffer = this->buffer_end = this->buffer_start;
94 }
95
100 byte RandomAccessFile::ReadByte()
101 {
102 if (this->buffer == this->buffer_end) {
103 this->buffer = this->buffer_start;
104 size_t size = fread(this->buffer, 1, RandomAccessFile::BUFFER_SIZE, this->file_handle);
105 this->pos += size;
106 this->buffer_end = this->buffer_start + size;
107
108 if (size == 0) return 0;
109 }
110 return *this->buffer++;
111 }
112
117 uint16 RandomAccessFile::ReadWord()
118 {
119 byte b = this->ReadByte();
120 return (this->ReadByte() << 8) | b;
121 }
122
127 uint32 RandomAccessFile::ReadDword()
128 {
129 uint b = this->ReadWord();
130 return (this->ReadWord() << 16) | b;
131 }
132
138 void RandomAccessFile::ReadBlock(void *ptr, size_t size)
139 {
140 this->SeekTo(this->GetPos(), SEEK_SET);
141 this->pos += fread(ptr, 1, size, this->file_handle);
142 }
143
148 void RandomAccessFile::SkipBytes(int n)
149 {
150 int remaining = this->buffer_end - this->buffer;
151 if (n <= remaining) {
152 this->buffer += n;
153 } else {
154 this->SeekTo(n, SEEK_CUR);
155 }
156 }
usererror
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:105
RandomAccessFile::BUFFER_SIZE
static constexpr int BUFFER_SIZE
The number of bytes to allocate for the buffer.
Definition: random_access_file_type.h:25
strtolower
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
Definition: string.cpp:465
RandomAccessFile::buffer
byte * buffer
Current position within the local buffer.
Definition: random_access_file_type.h:33
RandomAccessFile::pos
size_t pos
Position in the file of the end of the read buffer.
Definition: random_access_file_type.h:31
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:54
RandomAccessFile::buffer_end
byte * buffer_end
Last valid byte of buffer.
Definition: random_access_file_type.h:34
fileio_func.h
RandomAccessFile::ReadBlock
void ReadBlock(void *ptr, size_t size)
Read a block.
Definition: random_access_file.cpp:138
RandomAccessFile::ReadByte
byte ReadByte()
Read a byte from the file.
Definition: random_access_file.cpp:100
random_access_file_type.h
RandomAccessFile::SkipBytes
void SkipBytes(int n)
Skip n bytes ahead in the file.
Definition: random_access_file.cpp:148
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:28
FioFOpenFile
FILE * 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:245
RandomAccessFile::file_handle
FILE * file_handle
File handle of the open file.
Definition: random_access_file_type.h:30
safeguards.h
RandomAccessFile::~RandomAccessFile
virtual ~RandomAccessFile()
Close the file's file handle.
Definition: random_access_file.cpp:45
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:27
stdafx.h
RandomAccessFile::ReadWord
uint16 ReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition: random_access_file.cpp:117
RandomAccessFile::ReadDword
uint32 ReadDword()
Read a double word (32 bits) from the file (in low endian format).
Definition: random_access_file.cpp:127
RandomAccessFile::GetPos
size_t GetPos() const
Get position in the file.
Definition: random_access_file.cpp:73
string_func.h
RandomAccessFile::RandomAccessFile
RandomAccessFile(const std::string &filename, Subdirectory subdir)
Create the RandomAccesFile.
Definition: random_access_file.cpp:24
RandomAccessFile::buffer_start
byte buffer_start[BUFFER_SIZE]
Local buffer when read from file.
Definition: random_access_file_type.h:35
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
RandomAccessFile::GetSimplifiedFilename
const std::string & GetSimplifiedFilename() const
Get the simplified filename of the opened file.
Definition: random_access_file.cpp:64
RandomAccessFile::SeekTo
void SeekTo(size_t pos, int mode)
Seek in the current file.
Definition: random_access_file.cpp:83
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
debug.h