OpenTTD: src/fileio.cpp File Reference

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

Data Structures | Macros | Typedefs | Functions | Variables
fileio.cpp File Reference

Standard In/Out file operations. More...

`#include "stdafx.h"`
`#include "fileio_func.h"`
`#include "debug.h"`
`#include "fios.h"`
`#include "string_func.h"`
`#include "tar_type.h"`
`#include <unistd.h>`
`#include <pwd.h>`
`#include <sys/stat.h>`
`#include <algorithm>`
`#include "safeguards.h"`

Go to the source code of this file.

## Data Structures
struct | Fio
| Structure for keeping several open files with just one data buffer. More...

## Macros
#define | FIO_BUFFER_SIZE 512
| Size of the Fio data buffer.

## Typedefs
typedef std::map< std::string, std::string > | TarLinkList

## Functions
size_t | FioGetPos ()
| Get position in the current file.
const char * | FioGetFilename (uint8 slot)
| Get the filename associated with a slot.
void | FioSeekTo (size_t pos, int mode)
| Seek in the current file.
void | FioSeekToFile (uint8 slot, size_t pos)
| Switch to a different file and seek to a position.
byte | FioReadByte ()
| Read a byte from the file.
void | FioSkipBytes (int n)
| Skip n bytes ahead in the file.
uint16 | FioReadWord ()
| Read a word (16 bits) from the file (in low endian format).
uint32 | FioReadDword ()
| Read a double word (32 bits) from the file (in low endian format).
void | FioReadBlock (void *ptr, size_t size)
| Read a block.
static void | FioCloseFile (int slot)
| Close the file at the given slot number.
void | FioCloseAll ()
| Close all slotted open files.
void | FioOpenFile (int slot, const char *filename, Subdirectory subdir)
| Open a slotted file.
| assert_compile (lengthof(_subdirs)==NUM_SUBDIRS)
bool | FioCheckFileExists (const char *filename, Subdirectory subdir)
| Check whether the given file exists.
bool | FileExists (const char *filename)
| Test whether the given filename exists.
void | FioFCloseFile (FILE *f)
| Close a file in a safe way.
char * | FioGetFullPath (char *buf, const char *last, Searchpath sp, Subdirectory subdir, const char *filename)
char * | FioFindFullPath (char *buf, const char *last, Subdirectory subdir, const char *filename)
| Find a path to the filename in one of the search directories.
char * | FioAppendDirectory (char *buf, const char *last, Searchpath sp, Subdirectory subdir)
char * | FioGetDirectory (char *buf, const char *last, Subdirectory subdir)
static FILE * | FioFOpenFileSp (const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
FILE * | FioFOpenFileTar (TarFileListEntry *entry, size_t *filesize)
| Opens a file from inside a tar archive.
FILE * | FioFOpenFile (const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
| Opens a OpenTTD file somewhere in a personal or global directory.
static void | FioCreateDirectory (const char *name)
| Create a directory with the given name.
bool | AppendPathSeparator (char *buf, const char *last)
| Appends, if necessary, the path separator character to the end of the string.
char * | BuildWithFullPath (const char *dir)
| Allocates and files a variable with the full path based on the given directory.
const char * | FioTarFirstDir (const char *tarname, Subdirectory subdir)
| Find the first directory in a tar archive.
static void | TarAddLink (const std::string &srcParam, const std::string &destParam, Subdirectory subdir)
void | FioTarAddLink (const char *src, const char *dest, Subdirectory subdir)
static void | SimplifyFileName (char *name)
| Simplify filenames from tars.
bool | ExtractTar (const char *tar_filename, Subdirectory subdir)
| Extract the tar with the given filename in the directory where the tar resides.
static bool | ChangeWorkingDirectoryToExecutable (const char *exe)
| Changes the working directory to the path of the give executable.
bool | DoScanWorkingDirectory ()
| Whether we should scan the working directory.
void | DetermineBasePaths (const char *exe)
| Determine the base (personal dir and game data dir) paths.
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.
void | SanitizeFilename (char *filename)
| Sanitizes a filename, i.e.
void * | ReadFileToMem (const char *filename, size_t *lenp, size_t maxsize)
| Load a file into memory.
static bool | MatchesExtension (const char *extension, const char *filename)
| Helper to see whether a given filename matches the extension.
static uint | ScanPath (FileScanner *fs, const char *extension, const char *path, size_t basepath_length, bool recursive)
| Scan a single directory (and recursively its children) and add any graphics sets that are found.
static uint | ScanTar (FileScanner *fs, const char *extension, TarFileList::iterator tar)
| Scan the given tar and add graphics sets when it finds one.

## Variables
static Fio | _fio
| Fio instance.
static bool | _do_scan_working_directory = true
| Whether the working directory should be scanned.
char * | _config_file
| Configuration file of OpenTTD.
char * | _highscore_file
| The file to store the highscore data in.
static const char *const | _subdirs []
const char * | _searchpaths [NUM_SEARCHPATHS]
| The search paths OpenTTD could search through.
TarList | _tar_list [NUM_SUBDIRS]
TarFileList | _tar_filelist [NUM_SUBDIRS]
static TarLinkList | _tar_linklist [NUM_SUBDIRS]
| List of directory links.
const char * | _personal_dir
| custom directory for personal settings, saves, newgrf, etc.

## Detailed Description

Standard In/Out file operations.

Definition in file fileio.cpp.

## Macro Definition Documentation

#define FIO_BUFFER_SIZE 512

Size of the Fio data buffer.

Definition at line 38 of file fileio.cpp.

Referenced by FioReadByte(), and FioSeekTo().

## Function Documentation

bool AppendPathSeparator | ( | char * | buf,
|  | const char * | last
| ) |  |

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

  buf | string to append the separator to
  last | the last element of buf.

Returns
  true iff the operation succeeded

Definition at line 573 of file fileio.cpp.

References seprintf().

Referenced by BuildWithFullPath(), DetermineBasePaths(), DeterminePaths(), DoScanWorkingDirectory(), FileScanner::Scan(), and ScanPath().

char* BuildWithFullPath | ( | const char * | dir | ) |

Allocates and files a variable with the full path based on the given directory.

Parameters

  dir | the directory to base the path on

Returns
  the malloced full path

Definition at line 593 of file fileio.cpp.

References AppendPathSeparator(), strecat(), and strecpy().

static bool ChangeWorkingDirectoryToExecutable | ( | const char * | exe | ) |
| static

Changes the working directory to the path of the give executable.

For OSX application bundles '.app' is the required extension of the bundle, so when we crop the path to there, when can remove the name of the bundle in the same way we remove the name from the executable name.

Parameters

  exe | the path to the executable

Definition at line 1033 of file fileio.cpp.

References DEBUG, lastof, and strecpy().

Referenced by DetermineBasePaths().

void DetermineBasePaths | ( | const char * | exe | ) |

Determine the base (personal dir and game data dir) paths.

Parameters

  exe | the path to the executable

Definition at line 1092 of file fileio.cpp.

References _do_scan_working_directory, AppendPathSeparator(), ChangeWorkingDirectoryToExecutable(), DEBUG, DoScanWorkingDirectory(), free(), lastof, seprintf(), SP_APPLICATION_BUNDLE_DIR, SP_BINARY_DIR, SP_INSTALLATION_DIR, SP_PERSONAL_DIR, SP_SHARED_DIR, SP_WORKING_DIR, stredup(), and ValidateString().

Referenced by 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

  exe | the path from the current path to the executable

Definition at line 1197 of file fileio.cpp.

References _config_file, _do_scan_working_directory, _highscore_file, _log_file, _windows_file, AI_DIR, AI_LIBRARY_DIR, AppendPathSeparator(), AUTOSAVE_DIR, BASE_DIR, BASESET_DIR, DEBUG, DetermineBasePaths(), FileExists(), FioCreateDirectory(), FioFindFullPath(), FOR_ALL_SEARCHPATHS, free(), GAME_DIR, GAME_LIBRARY_DIR, HEIGHTMAP_DIR, IsValidSearchPath(), lastof, lengthof, NEWGRF_DIR, SAVE_DIR, SCENARIO_DIR, SCREENSHOT_DIR, seprintf(), SP_AUTODOWNLOAD_DIR, SP_BINARY_DIR, SP_INSTALLATION_DIR, SP_PERSONAL_DIR, SP_SHARED_DIR, SP_WORKING_DIR, str_fmt(), and stredup().

Referenced by openttd_main().

bool DoScanWorkingDirectory | ( |  | ) |

Whether we should scan the working directory.

It should not be scanned if it's the root or the home directory as in both cases a big data directory can cause huge amounts of unrelated files scanned. Furthermore there are nearly no use cases for the home/root directory to have OpenTTD directories.

Returns
  true if it should be scanned.

Definition at line 1071 of file fileio.cpp.

References AppendPathSeparator(), lastof, seprintf(), SP_PERSONAL_DIR, and SP_WORKING_DIR.

Referenced by DetermineBasePaths().

bool ExtractTar | ( | const char * | tar_filename,
|  | Subdirectory | subdir
| ) |  |

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

Parameters

  tar_filename | the name of the tar to extract.
  subdir | The sub directory the tar is in.

Returns
  false on failure.

Definition at line 950 of file fileio.cpp.

References DEBUG, FioCreateDirectory(), FioFOpenFileTar(), lastof, lengthof, min(), and strecpy().

Referenced by ClientNetworkContentSocketHandler::AfterDownload().

bool FileExists | ( | const char * | filename | ) |

Test whether the given filename exists.

Parameters

  filename | the file to test.

Returns
  true if and only if the file exists.

Definition at line 326 of file fileio.cpp.

References OTTD2FS().

Referenced by DeterminePaths(), FioFindFullPath(), ScriptInstance::LoadCompatibilityScripts(), and MakeScreenshotName().

bool FioCheckFileExists | ( | const char * | filename,
|  | Subdirectory | subdir
| ) |  |

Check whether the given file exists.

Parameters

  filename | the file to try for existence.
  subdir | the subdirectory to look in

Returns
  true if and only if the file can be opened

Definition at line 312 of file fileio.cpp.

References FioFCloseFile(), and FioFOpenFile().

Referenced by ScriptScanner::AddFile(), FillGRFDetails(), GetTextfile(), GRFLoadConfig(), LoadNewGRF(), and LoadTranslations().

void FioCloseAll | ( |  | ) |

Close all slotted open files.

Definition at line 213 of file fileio.cpp.

References FioCloseFile(), Fio::handles, and lengthof.

Referenced by ShutdownGame().

static void FioCloseFile | ( | int | slot | ) |
| inlinestatic

Close the file at the given slot number.

Parameters

  slot | File index to close.

Definition at line 197 of file fileio.cpp.

References free(), Fio::handles, and Fio::shortnames.

Referenced by FioCloseAll(), and FioOpenFile().

static void FioCreateDirectory | ( | const char * | name | ) |
| static

Create a directory with the given name.

Parameters

  name | the new name of the directory

Definition at line 543 of file fileio.cpp.

References lastof, OTTD2FS(), and strecpy().

Referenced by DeterminePaths(), and ExtractTar().

char* FioFindFullPath | ( | char * | buf,
|  | const char * | last,
|  | Subdirectory | subdir,
|  | const char * | filename
| ) |  |

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

Parameters

  buf | [out] Destination buffer for the path.
  last | End of the destination buffer.
  subdir | Subdirectory to try.
  filename | Filename to look for.

Returns
  buf containing the path if the path was found, else `NULL`.

Definition at line 364 of file fileio.cpp.

References FileExists(), FOR_ALL_SEARCHPATHS, NUM_SUBDIRS, and strtolower().

Referenced by DeterminePaths().

FILE* FioFOpenFile | ( | const char * | filename,
|  | const char * | mode,
|  | Subdirectory | subdir,
|  | size_t * | filesize
| ) |  |

Opens a OpenTTD file somewhere in a personal or global directory.

Parameters

  filename | Name of the file to open.
  subdir | Subdirectory to open.
  filename | Name of the file to open.

Returns
  File handle of the opened file, or `NULL` if the file is not available.

Definition at line 474 of file fileio.cpp.

References BASESET_DIR, FioFOpenFile(), FioFOpenFileTar(), FOR_ALL_SEARCHPATHS, lastof, NEWGRF_DIR, NO_DIRECTORY, NUM_SUBDIRS, OLD_DATA_DIR, OLD_GM_DIR, strecpy(), and strtolower().

Referenced by ScriptFileChecksumCreator::AddFile(), ScenarioScanner::AddFile(), CalcGRFMD5Sum(), MD5File::CheckMD5(), GraphicsSet::CheckMD5(), debug_print(), DisplaySplashImage(), FioCheckFileExists(), FioFOpenFile(), FioOpenFile(), GetFileTitle(), Squirrel::LoadFile(), TextfileWindow::LoadTextfile(), IniFile::OpenFile(), ReadHeightmapBMP(), ReadHeightmapPNG(), ReadRawLanguageStrings(), SaveOrLoad(), and CrashLog::WriteCrashLog().

FILE* FioFOpenFileTar | ( | TarFileListEntry * | entry,
|  | size_t * | filesize
| ) |  |

Opens a file from inside a tar archive.

Parameters

  entry | The entry to open.
  filesize | [out] If not `NULL`, size of the opened file.

Returns
  File handle of the opened file, or `NULL` if the file is not available.

Note
  The file is read from within the tar file, and may not return `EOF` after reading the whole file.

Definition at line 453 of file fileio.cpp.

Referenced by ExtractTar(), and FioFOpenFile().

const char* FioGetFilename | ( | uint8 | slot | ) |

Get the filename associated with a slot.

Parameters

  slot | Index of queried file.

Returns
  Name of the file.

Definition at line 78 of file fileio.cpp.

References Fio::shortnames.

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

size_t FioGetPos | ( |  | ) |

Get position in the current file.

Returns
  Position in the file.

Definition at line 68 of file fileio.cpp.

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

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

void FioOpenFile | ( | int | slot,
|  | const char * | filename,
|  | Subdirectory | subdir
| ) |  |

Open a slotted file.

Parameters

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

Definition at line 250 of file fileio.cpp.

References Fio::filenames, FioCloseFile(), FioFOpenFile(), FioSeekToFile(), Fio::handles, Fio::shortnames, stredup(), strtolower(), and usererror().

Referenced by LoadGrfFile(), LoadGrfFileIndexed(), and LoadNewGRFFile().

void FioReadBlock | ( | void * | ptr,
|  | size_t | size
| ) |  |

Read a block.

Parameters

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

Definition at line 187 of file fileio.cpp.

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

Referenced by LoadNewGRFSound(), and ReadRecolourSprite().

byte FioReadByte | ( |  | ) |

Read a byte from the file.

Returns
  Read byte.

Definition at line 133 of file fileio.cpp.

References Fio::buffer_end, Fio::buffer_start, Fio::cur_fh, FIO_BUFFER_SIZE, and Fio::pos.

Referenced by DecodeSingleSprite(), FioReadWord(), FioSkipBytes(), GetGRFContainerVersion(), LoadGrfFile(), LoadGrfFileIndexed(), LoadNewGRFFile(), LoadNewGRFSound(), LoadNextSprite(), and SkipSpriteData().

uint32 FioReadDword | ( |  | ) |

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

Returns
  Read word.

Definition at line 176 of file fileio.cpp.

References FioReadWord().

Referenced by ImportGRFSound(), LoadNewGRFFile(), LoadNewGRFSound(), LoadNextSprite(), and ReadGRFSpriteOffsets().

uint16 FioReadWord | ( |  | ) |

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

Returns
  Read word.

Definition at line 166 of file fileio.cpp.

References FioReadByte().

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

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

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

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

void FioSeekToFile | ( | uint8 | slot,
|  | size_t | pos
| ) |  |

Switch to a different file and seek to a position.

Parameters

  slot | Slot number of the new file.
  pos | New absolute position in the new file.

Definition at line 115 of file fileio.cpp.

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

Referenced by FioOpenFile(), and LoadNewGRFSound().

void FioSkipBytes | ( | int | n | ) |

Skip n bytes ahead in the file.

Parameters

  n | Number of bytes to skip reading.

Definition at line 150 of file fileio.cpp.

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

Referenced by LoadNewGRFFile(), LoadNewGRFSound(), LoadNextSprite(), ReadGRFSpriteOffsets(), and SkipSpriteData().

const char* FioTarFirstDir | ( | const char * | tarname,
|  | Subdirectory | subdir
| ) |  |

Find the first directory in a tar archive.

Parameters

  tarname | the name of the tar archive to look in.
  subdir | the subdirectory to look in.

Definition at line 618 of file fileio.cpp.

static bool MatchesExtension | ( | const char * | extension,
|  | const char * | filename
| ) |  |
| static

Helper to see whether a given filename matches the extension.

Parameters

  extension | The extension to look for.
  filename | The filename to look in for the extension.

Returns
  True iff the extension is NULL, or the filename ends with it.

Definition at line 1376 of file fileio.cpp.

Referenced by ScanPath(), and ScanTar().

void* ReadFileToMem | ( | const char * | filename,
|  | size_t * | lenp,
|  | size_t | maxsize
| ) |  |

Load a file into memory.

Parameters

  filename | Name of the file to load.
  lenp | [out] Length of loaded data.
  maxsize | Maximum size to load.

Returns
  Pointer to new memory containing the loaded data, or `NULL` if loading failed.

Note
  If maxsize less than the length of the file, loading fails.

Definition at line 1345 of file fileio.cpp.

References free().

Referenced by ReadLanguagePack().

void SanitizeFilename | ( | char * | filename | ) |

Sanitizes a filename, i.e.

removes all illegal characters from it.

Parameters

  filename | the "\0" terminated filename

Definition at line 1323 of file fileio.cpp.

Referenced by GenerateDefaultSaveName().

static uint ScanPath | ( | FileScanner * | fs,
|  | const char * | extension,
|  | const char * | path,
|  | size_t | basepath_length,
|  | bool | recursive
| ) |  |
| static

Scan a single directory (and recursively its children) and add any graphics sets that are found.

Parameters

  fs | the file scanner to add the files to
  extension | the extension of files to search for.
  path | full path we're currently at
  basepath_length | from where in the path are we 'based' on the search path
  recursive | whether to recursively search the sub directories

Definition at line 1393 of file fileio.cpp.

References FileScanner::AddFile(), AppendPathSeparator(), FS2OTTD(), lastof, MatchesExtension(), seprintf(), and ttd_opendir().

Referenced by FileScanner::Scan().

static uint ScanTar | ( | FileScanner * | fs,
|  | const char * | extension,
|  | TarFileList::iterator | tar
| ) |  |
| static

Scan the given tar and add graphics sets when it finds one.

Parameters

  fs | the file scanner to scan for
  extension | the extension of files to search for.
  tar | the tar to search in.

Definition at line 1435 of file fileio.cpp.

References FileScanner::AddFile(), and MatchesExtension().

Referenced by FileScanner::Scan().

static void SimplifyFileName | ( | char * | name | ) |
| static

Simplify filenames from tars.

Replace '/' by #PATHSEPCHAR, and force 'name' to lowercase.

Parameters

  name | Filename to process.

Definition at line 656 of file fileio.cpp.

References strtolower().

Referenced by TarScanner::AddFile().

## Variable Documentation

bool _do_scan_working_directory = true
| static

Whether the working directory should be scanned.

Definition at line 59 of file fileio.cpp.

Referenced by DetermineBasePaths(), DeterminePaths(), and FileScanner::Scan().

const char* _searchpaths[NUM_SEARCHPATHS]

The search paths OpenTTD could search through.

At least one of the slots has to be filled with a path. NULL paths tell that there is no such path for the current operating system.

Definition at line 299 of file fileio.cpp.

Referenced by IsValidSearchPath().

const char* const _subdirs[]
| static

Initial value:
{
"",
"save" PATHSEP,
"save" PATHSEP "autosave" PATHSEP,
"scenario" PATHSEP,
"scenario" PATHSEP "heightmap" PATHSEP,
"gm" PATHSEP,
"data" PATHSEP,
"baseset" PATHSEP,
"newgrf" PATHSEP,
"lang" PATHSEP,
"ai" PATHSEP,
"ai" PATHSEP "library" PATHSEP,
"game" PATHSEP,
"game" PATHSEP "library" PATHSEP,
"screenshot" PATHSEP,
}

Definition at line 280 of file fileio.cpp.