OpenTTD Source  12.0-beta2
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 #include <string>
15 
25  static constexpr int BUFFER_SIZE = 512;
26 
27  std::string filename;
28  std::string simplified_filename;
29 
30  FILE *file_handle;
31  size_t pos;
32 
33  byte *buffer;
34  byte *buffer_end;
36 
37 public:
38  RandomAccessFile(const std::string &filename, Subdirectory subdir);
39  RandomAccessFile(const RandomAccessFile&) = delete;
40  void operator=(const RandomAccessFile&) = delete;
41 
42  virtual ~RandomAccessFile();
43 
44  const std::string &GetFilename() const;
45  const std::string &GetSimplifiedFilename() const;
46 
47  size_t GetPos() const;
48  void SeekTo(size_t pos, int mode);
49 
50  byte ReadByte();
51  uint16 ReadWord();
52  uint32 ReadDword();
53 
54  void ReadBlock(void *ptr, size_t size);
55  void SkipBytes(int n);
56 };
57 
58 #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:25
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
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
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
RandomAccessFile::file_handle
FILE * file_handle
File handle of the open file.
Definition: random_access_file_type.h:30
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
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
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
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:64
RandomAccessFile::SeekTo
void SeekTo(size_t pos, int mode)
Seek in the current file.
Definition: random_access_file.cpp:83
RandomAccessFile
A file from which bytes, words and double words are read in (potentially) a random order.
Definition: random_access_file_type.h:23