OpenTTD Source  1.11.2
fileio_func.h File Reference
#include "core/enum_type.hpp"
#include "fileio_type.h"
#include <string>
#include <sys/types.h>
#include <dirent.h>

Go to the source code of this file.

Data Structures

class  FileScanner
 Helper for scanning for files with a given name. More...
 
class  TarScanner
 Helper for scanning for files with tar as extension. More...
 
class  FileCloser
 Auto-close a file upon scope exit. More...
 
struct  FileDeleter
 Helper to manage a FILE with a std::unique_ptr. More...
 

Macros

#define FOR_ALL_SEARCHPATHS(sp)   for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))
 Iterator for all the search paths.
 

Functions

void FioSeekTo (size_t pos, int mode)
 Seek in the current file. More...
 
void FioSeekToFile (uint8 slot, size_t pos)
 Switch to a different file and seek to a position. More...
 
size_t FioGetPos ()
 Get position in the current file. More...
 
const char * FioGetFilename (uint8 slot)
 Get the filename associated with a slot. More...
 
byte FioReadByte ()
 Read a byte from the file. More...
 
uint16 FioReadWord ()
 Read a word (16 bits) from the file (in low endian format). More...
 
uint32 FioReadDword ()
 Read a double word (32 bits) from the file (in low endian format). More...
 
void FioCloseAll ()
 Close all slotted open files.
 
void FioOpenFile (int slot, const std::string &filename, Subdirectory subdir)
 Open a slotted file. More...
 
void FioReadBlock (void *ptr, size_t size)
 Read a block. More...
 
void FioSkipBytes (int n)
 Skip n bytes ahead in the file. More...
 
bool IsValidSearchPath (Searchpath sp)
 Checks whether the given search path is a valid search path. More...
 
void FioFCloseFile (FILE *f)
 Close a file in a safe way.
 
FILE * FioFOpenFile (const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize=nullptr)
 Opens a OpenTTD file somewhere in a personal or global directory. More...
 
bool FioCheckFileExists (const std::string &filename, Subdirectory subdir)
 Check whether the given file exists. More...
 
std::string FioFindFullPath (Subdirectory subdir, const char *filename)
 Find a path to the filename in one of the search directories. More...
 
std::string FioGetDirectory (Searchpath sp, Subdirectory subdir)
 
std::string FioFindDirectory (Subdirectory subdir)
 
void FioCreateDirectory (const std::string &name)
 Create a directory with the given name If the parent directory does not exist, it will try to create that as well. More...
 
const char * FiosGetScreenshotDir ()
 Get the directory for screenshots. More...
 
void SanitizeFilename (char *filename)
 Sanitizes a filename, i.e. More...
 
void AppendPathSeparator (std::string &buf)
 Appends, if necessary, the path separator character to the end of the string. More...
 
void DeterminePaths (const char *exe)
 Acquire the base paths (personal dir and game data dir), fill all other paths (save dir, autosave dir etc) and make the save and scenario directories. More...
 
std::unique_ptr< char > ReadFileToMem (const std::string &filename, size_t &lenp, size_t maxsize)
 Load a file into memory. More...
 
bool FileExists (const std::string &filename)
 Test whether the given filename exists. More...
 
bool ExtractTar (const std::string &tar_filename, Subdirectory subdir)
 Extract the tar with the given filename in the directory where the tar resides. More...
 
static DIRttd_opendir (const char *path)
 A wrapper around opendir() which will convert the string from OPENTTD encoding to that of the filesystem. More...
 

Variables

std::string _personal_dir
 custom directory for personal settings, saves, newgrf, etc.
 

Detailed Description

Functions for Standard In/Out file operations

Definition in file fileio_func.h.

Function Documentation

◆ AppendPathSeparator()

void AppendPathSeparator ( std::string &  buf)

Appends, if necessary, the path separator character to the end of the string.

It does not add the path separator to zero-sized strings.

Parameters
bufstring to append the separator to
Returns
true iff the operation succeeded

Definition at line 523 of file fileio.cpp.

Referenced by FileScanner::Scan().

◆ DeterminePaths()

void DeterminePaths ( const char *  exe)

Acquire the base paths (personal dir and game data dir), fill all other paths (save dir, autosave dir etc) and make the save and scenario directories.

Parameters
exethe path from the current path to the executable

Definition at line 1130 of file fileio.cpp.

References DetermineBasePaths(), and GetHomeDir().

◆ ExtractTar()

bool ExtractTar ( const std::string &  tar_filename,
Subdirectory  subdir 
)

Extract the tar with the given filename in the directory where the tar resides.

Parameters
tar_filenamethe name of the tar to extract.
subdirThe sub directory the tar is in.
Returns
false on failure.

Definition at line 848 of file fileio.cpp.

◆ FileExists()

bool FileExists ( const std::string &  filename)

Test whether the given filename exists.

Parameters
filenamethe file to test.
Returns
true if and only if the file exists.

Definition at line 280 of file fileio.cpp.

References OTTD2FS().

◆ FioCheckFileExists()

bool FioCheckFileExists ( const std::string &  filename,
Subdirectory  subdir 
)

Check whether the given file exists.

Parameters
filenamethe file to try for existence.
subdirthe subdirectory to look in
Returns
true if and only if the file can be opened

Definition at line 266 of file fileio.cpp.

References FioFCloseFile(), and FioFOpenFile().

Referenced by FillGRFDetails(), GetMusicCatEntryData(), GetMusicCatEntryName(), GRFLoadConfig(), and SaveLoadWindow::OnTimeout().

◆ FioCreateDirectory()

void FioCreateDirectory ( const std::string &  name)

Create a directory with the given name If the parent directory does not exist, it will try to create that as well.

Parameters
namethe new name of the directory

Definition at line 493 of file fileio.cpp.

◆ FioFindFullPath()

std::string FioFindFullPath ( Subdirectory  subdir,
const char *  filename 
)

Find a path to the filename in one of the search directories.

Parameters
subdirSubdirectory to try.
filenameFilename to look for.
Returns
String containing the path if the path was found, else an empty string.

Definition at line 299 of file fileio.cpp.

References FOR_ALL_SEARCHPATHS, and NUM_SUBDIRS.

Referenced by MidiFile::GetSMFFile().

◆ 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.

Parameters
filenameName of the file to open.
subdirSubdirectory to open.
Returns
File handle of the opened file, or nullptr if the file is not available.

Definition at line 406 of file fileio.cpp.

References FOR_ALL_SEARCHPATHS, NO_DIRECTORY, and NUM_SUBDIRS.

Referenced by ScriptFileChecksumCreator::AddFile(), ScenarioScanner::AddFile(), CalcGRFMD5Sum(), MD5File::CheckMD5(), GraphicsSet::CheckMD5(), FioCheckFileExists(), FioOpenFile(), GetFileTitle(), MidiFile::LoadFile(), Squirrel::LoadFile(), TextfileWindow::LoadTextfile(), IniFile::OpenFile(), ReadRawLanguageStrings(), MidiFile::ReadSMFHeader(), CrashLog::WriteCrashLog(), and MidiFile::WriteSMF().

◆ FioGetFilename()

const char* FioGetFilename ( uint8  slot)

Get the filename associated with a slot.

Parameters
slotIndex of queried file.
Returns
Name of the file.

Definition at line 69 of file fileio.cpp.

References _fio, and Fio::shortnames.

Referenced by WarnCorruptSprite().

◆ FioGetPos()

size_t FioGetPos ( )

Get position in the current file.

Returns
Position in the file.

Definition at line 59 of file fileio.cpp.

References _fio, Fio::buffer_end, and Fio::pos.

Referenced by FioReadBlock(), FioSeekTo(), GetGRFContainerVersion(), LoadNextSprite(), and ReadGRFSpriteOffsets().

◆ FioOpenFile()

void FioOpenFile ( int  slot,
const std::string &  filename,
Subdirectory  subdir 
)

Open a slotted file.

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

Definition at line 196 of file fileio.cpp.

References _fio, Fio::filenames, FioCloseFile(), FioFOpenFile(), Fio::handles, and usererror().

Referenced by GetMusicCatEntryData(), GetMusicCatEntryName(), LoadGrfFile(), and LoadGrfFileIndexed().

◆ FioReadBlock()

void FioReadBlock ( void *  ptr,
size_t  size 
)

Read a block.

Parameters
ptrDestination buffer.
sizeNumber of bytes to read.

Definition at line 161 of file fileio.cpp.

References _fio, Fio::cur_fh, FioGetPos(), FioSeekTo(), and Fio::pos.

Referenced by GetMusicCatEntryData(), and GetMusicCatEntryName().

◆ FioReadByte()

◆ FioReadDword()

uint32 FioReadDword ( )

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

Returns
Read word.

Definition at line 150 of file fileio.cpp.

References FioReadWord().

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

◆ FioReadWord()

uint16 FioReadWord ( )

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

Returns
Read word.

Definition at line 140 of file fileio.cpp.

References FioReadByte().

Referenced by FioReadDword(), GetGRFContainerVersion(), ImportGRFSound(), and LoadNextSprite().

◆ FioSeekTo()

void FioSeekTo ( 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 79 of file fileio.cpp.

References _fio, Fio::buffer_end, Fio::buffer_start, Fio::cur_fh, DEBUG, Fio::filename, FIO_BUFFER_SIZE, FioGetPos(), and Fio::pos.

Referenced by FioReadBlock(), FioSeekToFile(), GetGRFContainerVersion(), GetMusicCatEntryData(), GetMusicCatEntryName(), and ReadGRFSpriteOffsets().

◆ FioSeekToFile()

void FioSeekToFile ( uint8  slot,
size_t  pos 
)

Switch to a different file and seek to a position.

Parameters
slotSlot number of the new file.
posNew absolute position in the new file.

Definition at line 94 of file fileio.cpp.

References _fio, Fio::cur_fh, Fio::filename, Fio::filenames, FioSeekTo(), and Fio::handles.

◆ FiosGetScreenshotDir()

const char* FiosGetScreenshotDir ( )

Get the directory for screenshots.

Returns
path to screenshots

Definition at line 628 of file fios.cpp.

◆ FioSkipBytes()

void FioSkipBytes ( int  n)

Skip n bytes ahead in the file.

Parameters
nNumber of bytes to skip reading.

Definition at line 124 of file fileio.cpp.

References _fio, Fio::buffer_end, and FioReadByte().

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

◆ IsValidSearchPath()

bool IsValidSearchPath ( Searchpath  sp)

Checks whether the given search path is a valid search path.

Parameters
spthe search path to check
Returns
true if the search path is valid

Definition at line 255 of file fileio.cpp.

References _searchpaths.

◆ ReadFileToMem()

std::unique_ptr<char> ReadFileToMem ( const std::string &  filename,
size_t &  lenp,
size_t  maxsize 
)

Load a file into memory.

Parameters
filenameName of the file to load.
[out]lenpLength of loaded data.
maxsizeMaximum size to load.
Returns
Pointer to new memory containing the loaded data, or nullptr if loading failed.
Note
If maxsize less than the length of the file, loading fails.

Definition at line 1262 of file fileio.cpp.

Referenced by ReadLanguagePack().

◆ SanitizeFilename()

void SanitizeFilename ( char *  filename)

Sanitizes a filename, i.e.

removes all illegal characters from it.

Parameters
filenamethe "\0" terminated filename

Definition at line 1240 of file fileio.cpp.

◆ ttd_opendir()

static DIR* ttd_opendir ( const char *  path)
inlinestatic

A wrapper around opendir() which will convert the string from OPENTTD encoding to that of the filesystem.

For all purposes this function behaves the same as the original opendir function

Parameters
pathstring to open directory of
Returns
DIR pointer

Definition at line 133 of file fileio_func.h.

Referenced by GetLanguageList().