OpenTTD Source  12.0-beta2
RandomAccessFile Class Reference

A file from which bytes, words and double words are read in (potentially) a random order. More...

#include <random_access_file_type.h>

Inheritance diagram for RandomAccessFile:
SpriteFile

Public Member Functions

 RandomAccessFile (const std::string &filename, Subdirectory subdir)
 Create the RandomAccesFile. More...
 
 RandomAccessFile (const RandomAccessFile &)=delete
 
void operator= (const RandomAccessFile &)=delete
 
virtual ~RandomAccessFile ()
 Close the file's file handle.
 
const std::string & GetFilename () const
 Get the filename of the opened file with the path from the SubDirectory and the extension. More...
 
const std::string & GetSimplifiedFilename () const
 Get the simplified filename of the opened file. More...
 
size_t GetPos () const
 Get position in the file. More...
 
void SeekTo (size_t pos, int mode)
 Seek in the current file. More...
 
byte ReadByte ()
 Read a byte from the file. More...
 
uint16 ReadWord ()
 Read a word (16 bits) from the file (in low endian format). More...
 
uint32 ReadDword ()
 Read a double word (32 bits) from the file (in low endian format). More...
 
void ReadBlock (void *ptr, size_t size)
 Read a block. More...
 
void SkipBytes (int n)
 Skip n bytes ahead in the file. More...
 

Private Attributes

std::string filename
 Full name of the file; relative path to subdir plus the extension of the file.
 
std::string simplified_filename
 Simplified lowecase name of the file; only the name, no path or extension.
 
FILE * file_handle
 File handle of the open file.
 
size_t pos
 Position in the file of the end of the read buffer.
 
byte * buffer
 Current position within the local buffer.
 
byte * buffer_end
 Last valid byte of buffer.
 
byte buffer_start [BUFFER_SIZE]
 Local buffer when read from file.
 

Static Private Attributes

static constexpr int BUFFER_SIZE = 512
 The number of bytes to allocate for the buffer.
 

Detailed Description

A file from which bytes, words and double words are read in (potentially) a random order.

This is mostly intended to be used for things that can be read from GRFs when needed, so the graphics but also the sounds. This also ties into the spritecache as it uses these files to load the sprites from when needed.

Definition at line 23 of file random_access_file_type.h.

Constructor & Destructor Documentation

◆ RandomAccessFile()

RandomAccessFile::RandomAccessFile ( const std::string &  filename,
Subdirectory  subdir 
)

Create the RandomAccesFile.

Parameters
filenameName of the file at the disk.
subdirThe sub directory to search this file in.

Definition at line 24 of file random_access_file.cpp.

References file_handle, filename, FioFOpenFile(), pos, and usererror().

Member Function Documentation

◆ GetFilename()

const std::string & RandomAccessFile::GetFilename ( ) const

Get the filename of the opened file with the path from the SubDirectory and the extension.

Returns
Name of the file.

Definition at line 54 of file random_access_file.cpp.

References filename.

◆ GetPos()

size_t RandomAccessFile::GetPos ( ) const

Get position in the file.

Returns
Position in the file.

Definition at line 73 of file random_access_file.cpp.

References buffer, buffer_end, and pos.

Referenced by GetGRFContainerVersion(), LoadNextSprite(), ReadBlock(), ReadGRFSpriteOffsets(), SeekTo(), and SpriteFile::SpriteFile().

◆ GetSimplifiedFilename()

const std::string & RandomAccessFile::GetSimplifiedFilename ( ) const

Get the simplified filename of the opened file.

The simplified filename is the name of the file without the SubDirectory or extension in lower case.

Returns
Name of the file.

Definition at line 64 of file random_access_file.cpp.

References simplified_filename.

Referenced by WarnCorruptSprite().

◆ ReadBlock()

void RandomAccessFile::ReadBlock ( void *  ptr,
size_t  size 
)

Read a block.

Parameters
ptrDestination buffer.
sizeNumber of bytes to read.

Definition at line 138 of file random_access_file.cpp.

References file_handle, GetPos(), pos, and SeekTo().

Referenced by GetMusicCatEntryData(), and GetMusicCatEntryName().

◆ ReadByte()

byte RandomAccessFile::ReadByte ( )

◆ ReadDword()

uint32 RandomAccessFile::ReadDword ( )

Read a double word (32 bits) from the file (in low endian format).

Returns
Read word.

Definition at line 127 of file random_access_file.cpp.

References ReadWord().

Referenced by GetMusicCatEntryData(), GetMusicCatEntryName(), LoadNextSprite(), and ReadGRFSpriteOffsets().

◆ ReadWord()

uint16 RandomAccessFile::ReadWord ( )

Read a word (16 bits) from the file (in low endian format).

Returns
Read word.

Definition at line 117 of file random_access_file.cpp.

References ReadByte().

Referenced by GetGRFContainerVersion(), LoadNextSprite(), and ReadDword().

◆ SeekTo()

void RandomAccessFile::SeekTo ( size_t  pos,
int  mode 
)

Seek in the current file.

Parameters
posNew position.
modeType of seek (SEEK_CUR means pos is relative to current position, SEEK_SET means pos is absolute).

Definition at line 83 of file random_access_file.cpp.

References buffer, buffer_end, buffer_start, Debug, file_handle, filename, GetPos(), and pos.

Referenced by GetGRFContainerVersion(), GetMusicCatEntryData(), GetMusicCatEntryName(), ReadBlock(), ReadGRFSpriteOffsets(), SpriteFile::SeekToBegin(), and SkipBytes().

◆ SkipBytes()

void RandomAccessFile::SkipBytes ( int  n)

Skip n bytes ahead in the file.

Parameters
nNumber of bytes to skip reading.

Definition at line 148 of file random_access_file.cpp.

References buffer, buffer_end, and SeekTo().

Referenced by GetMusicCatEntryData(), LoadNextSprite(), ReadGRFSpriteOffsets(), and SkipSpriteData().


The documentation for this class was generated from the following files: