OpenTTD Source  14.0-beta1
random_access_file_type.h
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 #ifndef RANDOM_ACCESS_FILE_TYPE_H
11 #define RANDOM_ACCESS_FILE_TYPE_H
12 
13 #include "fileio_type.h"
14 
24  static constexpr int BUFFER_SIZE = 512;
25 
26  std::string filename;
27  std::string simplified_filename;
28 
29  FILE *file_handle;
30  size_t pos;
31 
32  byte *buffer;
33  byte *buffer_end;
35 
36 public:
37  RandomAccessFile(const std::string &filename, Subdirectory subdir);
38  RandomAccessFile(const RandomAccessFile&) = delete;
39  void operator=(const RandomAccessFile&) = delete;
40 
41  virtual ~RandomAccessFile();
42 
43  const std::string &GetFilename() const;
44  const std::string &GetSimplifiedFilename() const;
45 
46  size_t GetPos() const;
47  void SeekTo(size_t pos, int mode);
48 
49  byte ReadByte();
50  uint16_t ReadWord();
51  uint32_t ReadDword();
52 
53  void ReadBlock(void *ptr, size_t size);
54  void SkipBytes(size_t n);
55 };
56 
57 #endif /* RANDOM_ACCESS_FILE_TYPE_H */
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
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
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
RandomAccessFile::file_handle
FILE * file_handle
File handle of the open file.
Definition: random_access_file_type.h:29
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
RandomAccessFile::GetPos
size_t GetPos() const
Get position in the file.
Definition: random_access_file.cpp:74
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
fileio_type.h
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
RandomAccessFile
A file from which bytes, words and double words are read in (potentially) a random order.
Definition: random_access_file_type.h:22