OpenTTD Source  14.0-beta3
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  this->file_handle = FioFOpenFile(filename, "rb", subdir);
28  if (this->file_handle == nullptr) UserError("Cannot open file '{}'", filename);
29 
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);
33 
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);
39 
40  this->SeekTo((size_t)pos, SEEK_SET);
41 }
42 
47 {
48  fclose(this->file_handle);
49 }
50 
55 const std::string &RandomAccessFile::GetFilename() const
56 {
57  return this->filename;
58 }
59 
65 const std::string &RandomAccessFile::GetSimplifiedFilename() const
66 {
67  return this->simplified_filename;
68 }
69 
75 {
76  return this->pos + (this->buffer - this->buffer_end);
77 }
78 
84 void RandomAccessFile::SeekTo(size_t pos, int mode)
85 {
86  if (mode == SEEK_CUR) pos += this->GetPos();
87 
88  this->pos = pos;
89  if (fseek(this->file_handle, this->pos, SEEK_SET) < 0) {
90  Debug(misc, 0, "Seeking in {} failed", this->filename);
91  }
92 
93  /* Reset the buffer, so the next ReadByte will read bytes from the file. */
94  this->buffer = this->buffer_end = this->buffer_start;
95 }
96 
102 {
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;
108 
109  if (size == 0) return 0;
110  }
111  return *this->buffer++;
112 }
113 
119 {
120  byte b = this->ReadByte();
121  return (this->ReadByte() << 8) | b;
122 }
123 
129 {
130  uint b = this->ReadWord();
131  return (this->ReadWord() << 16) | b;
132 }
133 
139 void RandomAccessFile::ReadBlock(void *ptr, size_t size)
140 {
141  this->SeekTo(this->GetPos(), SEEK_SET);
142  this->pos += fread(ptr, 1, size, this->file_handle);
143 }
144 
150 {
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);
157  }
158 }
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::buffer
byte * buffer
Current position within the local buffer.
Definition: random_access_file_type.h:32
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:55
RandomAccessFile::buffer_end
byte * buffer_end
Last valid byte of buffer.
Definition: random_access_file_type.h:33
fileio_func.h
RandomAccessFile::ReadBlock
void ReadBlock(void *ptr, size_t size)
Read a block.
Definition: random_access_file.cpp:139
RandomAccessFile::ReadByte
byte ReadByte()
Read a byte from the file.
Definition: random_access_file.cpp:101
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
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:263
RandomAccessFile::file_handle
FILE * file_handle
File handle of the open file.
Definition: random_access_file_type.h:29
safeguards.h
RandomAccessFile::~RandomAccessFile
virtual ~RandomAccessFile()
Close the file's file handle.
Definition: random_access_file.cpp:46
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:74
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:118
RandomAccessFile::SkipBytes
void SkipBytes(size_t n)
Skip n bytes ahead in the file.
Definition: random_access_file.cpp:149
RandomAccessFile::RandomAccessFile
RandomAccessFile(const std::string &filename, Subdirectory subdir)
Create the RandomAccesFile.
Definition: random_access_file.cpp:25
RandomAccessFile::buffer_start
byte buffer_start[BUFFER_SIZE]
Local buffer when read from file.
Definition: random_access_file_type.h:34
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:65
RandomAccessFile::SeekTo
void SeekTo(size_t pos, int mode)
Seek in the current file.
Definition: random_access_file.cpp:84
RandomAccessFile::ReadDword
uint32_t ReadDword()
Read a double word (32 bits) from the file (in low endian format).
Definition: random_access_file.cpp:128
debug.h