OpenTTD Source: RandomAccessFile Class Reference

OpenTTD Source 15.0-beta1

Public Member Functions | Private Attributes | Static Private Attributes
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:

## Public Member Functions
| RandomAccessFile (const std::string &filename, Subdirectory subdir)
| Create the RandomAccesFile.
| RandomAccessFile (const RandomAccessFile &)=delete
void | operator= (const RandomAccessFile &)=delete
const std::string & | GetFilename () const
| Get the filename of the opened file with the path from the SubDirectory and the extension.
const std::string & | GetSimplifiedFilename () const
| Get the simplified filename of the opened file.
size_t | GetPos () const
| Get position in the file.
size_t | GetStartPos () const
size_t | GetEndPos () const
void | SeekTo (size_t pos, int mode)
| Seek in the current file.
bool | AtEndOfFile () const
| Test if we have reached the end of the file.
uint8_t | ReadByte ()
| Read a byte from the file.
uint16_t | ReadWord ()
| Read a word (16 bits) from the file (in low endian format).
uint32_t | ReadDword ()
| Read a double word (32 bits) from the file (in low endian format).
void | ReadBlock (void *ptr, size_t size)
| Read a block.
void | SkipBytes (size_t n)
| Skip n bytes ahead in the file.

## 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.
std::optional< FileHandle > | file_handle
| File handle of the open file.
size_t | pos
| Position in the file of the end of the read buffer.
size_t | start_pos
| Start position of file. May be non-zero if file is within a tar file.
size_t | end_pos
| End position of file.
uint8_t * | buffer
| Current position within the local buffer.
uint8_t * | buffer_end
| Last valid byte of buffer.
uint8_t | 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 22 of file random_access_file_type.h.

## Constructor & Destructor Documentation

## ◆ RandomAccessFile()

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

Create the RandomAccesFile.

Parameters

  filename | Name of the file at the disk.
  subdir | The sub directory to search this file in.

Definition at line 25 of file random_access_file.cpp.

References end_pos, file_handle, filename, FioFOpenFile(), pos, SeekTo(), simplified_filename, and start_pos.

## Member Function Documentation

## ◆ AtEndOfFile()

bool RandomAccessFile::AtEndOfFile | ( |  | ) | const

Test if we have reached the end of the file.

Returns
  True iff the current position as at or after the end of the file.

Definition at line 80 of file random_access_file.cpp.

References GetPos().

Referenced by BmpRead1(), BmpRead24(), BmpRead4(), BmpRead4Rle(), BmpRead8(), and BmpRead8Rle().

## ◆ GetEndPos()

size_t RandomAccessFile::GetEndPos | ( |  | ) | const
| inline

Definition at line 50 of file random_access_file_type.h.

## ◆ 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 52 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 71 of file random_access_file.cpp.

References buffer, buffer_end, and pos.

Referenced by AtEndOfFile(), DefineGotoLabel(), GetGRFContainerVersion(), LoadNextSprite(), 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 62 of file random_access_file.cpp.

References simplified_filename.

Referenced by DecodeSingleSprite(), SpriteAlignerWindow::DrawWidget(), LoadNewGRFSound(), and WarnCorruptSprite().

## ◆ GetStartPos()

size_t RandomAccessFile::GetStartPos | ( |  | ) | const
| inline

Definition at line 49 of file random_access_file_type.h.

## ◆ ReadBlock()

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

Read a block.

Parameters

  ptr | Destination buffer.
  size | Number of bytes to read.

Definition at line 145 of file random_access_file.cpp.

References buffer, buffer_end, file_handle, and pos.

Referenced by GetMusicCatEntryData(), GetMusicCatEntryName(), LoadNewGRFSound(), and ReadRecolourSprite().

## ◆ ReadByte()

uint8_t RandomAccessFile::ReadByte | ( |  | ) |

Read a byte from the file.

Returns
  Read byte.

Definition at line 107 of file random_access_file.cpp.

References buffer, buffer_end, BUFFER_SIZE, buffer_start, file_handle, and pos.

Referenced by BmpRead1(), BmpRead24(), BmpRead4(), BmpRead4Rle(), BmpRead8(), BmpRead8Rle(), DecodeSingleSprite(), GetGRFContainerVersion(), GetMusicCatEntryData(), GetMusicCatEntryName(), LoadGrfFile(), LoadGrfFileIndexed(), LoadNewGRFFileFromFile(), LoadNewGRFSound(), LoadNextSprite(), ReadGRFSpriteOffsets(), ReadWord(), and SkipSpriteData().

## ◆ ReadDword()

uint32_t RandomAccessFile::ReadDword | ( |  | ) |

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

Returns
  Read word.

Definition at line 134 of file random_access_file.cpp.

References ReadWord().

Referenced by GetMusicCatEntryData(), GetMusicCatEntryName(), ImportGRFSound(), LoadNewGRFFileFromFile(), LoadNewGRFSound(), LoadNextSprite(), and ReadGRFSpriteOffsets().

## ◆ ReadWord()

uint16_t RandomAccessFile::ReadWord | ( |  | ) |

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

Returns
  Read word.

Definition at line 124 of file random_access_file.cpp.

References ReadByte().

Referenced by GetGRFContainerVersion(), ImportGRFSound(), LoadNewGRFFileFromFile(), LoadNewGRFSound(), LoadNextSprite(), and ReadDword().

## ◆ SeekTo()

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

Seek in the current file.

Parameters

  pos | New position.
  mode | Type of seek (`SEEK_CUR` means pos is relative to current position, `SEEK_SET` means pos is absolute).

Definition at line 90 of file random_access_file.cpp.

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

Referenced by GetGRFContainerVersion(), GetMusicCatEntryData(), GetMusicCatEntryName(), LoadNewGRFSound(), RandomAccessFile(), ReadGRFSpriteOffsets(), ReadRecolourSprite(), SpriteFile::SeekToBegin(), and SkipBytes().

## ◆ SkipBytes()

void RandomAccessFile::SkipBytes | ( | size_t | n | ) |

Skip n bytes ahead in the file.

Parameters

  n | Number of bytes to skip reading.

Definition at line 163 of file random_access_file.cpp.

References buffer, buffer_end, and SeekTo().

Referenced by BmpRead1(), BmpRead24(), BmpRead4(), BmpRead4Rle(), BmpRead8(), BmpRead8Rle(), GetMusicCatEntryData(), LoadNewGRFFileFromFile(), LoadNextSprite(), ReadGRFSpriteOffsets(), and SkipSpriteData().

## Field Documentation

## ◆ buffer

uint8_t* RandomAccessFile::buffer
| private

Current position within the local buffer.

Definition at line 34 of file random_access_file_type.h.

Referenced by GetPos(), ReadBlock(), ReadByte(), SeekTo(), and SkipBytes().

## ◆ buffer_end

uint8_t* RandomAccessFile::buffer_end
| private

Last valid byte of buffer.

Definition at line 35 of file random_access_file_type.h.

Referenced by GetPos(), ReadBlock(), ReadByte(), SeekTo(), and SkipBytes().

## ◆ BUFFER_SIZE

constexpr int RandomAccessFile::BUFFER_SIZE = 512
| staticconstexprprivate

The number of bytes to allocate for the buffer.

Definition at line 24 of file random_access_file_type.h.

Referenced by ReadByte().

## ◆ buffer_start

uint8_t RandomAccessFile::buffer_start[BUFFER_SIZE]
| private

Local buffer when read from file.

Definition at line 36 of file random_access_file_type.h.

Referenced by ReadByte(), and SeekTo().

## ◆ end_pos

size_t RandomAccessFile::end_pos
| private

End position of file.

Definition at line 32 of file random_access_file_type.h.

Referenced by RandomAccessFile().

## ◆ file_handle

std::optional<FileHandle> RandomAccessFile::file_handle
| private

File handle of the open file.

Definition at line 29 of file random_access_file_type.h.

Referenced by RandomAccessFile(), ReadBlock(), ReadByte(), and SeekTo().

## ◆ filename

std::string RandomAccessFile::filename
| private

Full name of the file; relative path to subdir plus the extension of the file.

Definition at line 26 of file random_access_file_type.h.

Referenced by FinaliseHouseArray(), FinalisePriceBaseMultipliers(), GetFilename(), RandomAccessFile(), and SeekTo().

## ◆ pos

size_t RandomAccessFile::pos
| private

Position in the file of the end of the read buffer.

Definition at line 30 of file random_access_file_type.h.

Referenced by GetPos(), RandomAccessFile(), ReadBlock(), ReadByte(), and SeekTo().

## ◆ simplified_filename

std::string RandomAccessFile::simplified_filename
| private

Simplified lowecase name of the file; only the name, no path or extension.

Definition at line 27 of file random_access_file_type.h.

Referenced by GetSimplifiedFilename(), and RandomAccessFile().

## ◆ start_pos

size_t RandomAccessFile::start_pos
| private

Start position of file. May be non-zero if file is within a tar file.

Definition at line 31 of file random_access_file_type.h.

Referenced by RandomAccessFile().

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

- src/random_access_file_type.h
- src/random_access_file.cpp