OpenTTD Source: src/fileio_func.h File Reference

OpenTTD Source 15.0-beta1

Data Structures | Functions | Variables
fileio_func.h File Reference

Functions for Standard In/Out file operations. More...

`#include "core/enum_type.hpp"`
`#include "fileio_type.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...

## Functions
std::optional< FileHandle > | 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.
bool | FioCheckFileExists (const std::string &filename, Subdirectory subdir)
| Check whether the given file exists.
std::string | FioFindFullPath (Subdirectory subdir, const std::string &filename)
| Find a path to the filename in one of the search directories.
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.
bool | FioRemove (const std::string &filename)
| Remove a file.
const char * | FiosGetScreenshotDir ()
| Get the directory for screenshots.
void | SanitizeFilename (std::string &filename)
| Sanitizes a filename, i.e.
void | AppendPathSeparator (std::string &buf)
| Appends, if necessary, the path separator character to the end of the string.
void | DeterminePaths (const char *exe, bool only_local_path)
| 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.
std::unique_ptr< char[]> | ReadFileToMem (const std::string &filename, size_t &lenp, size_t maxsize)
| Load a file into memory.
bool | FileExists (const std::string &filename)
| Test whether the given filename exists.
bool | ExtractTar (const std::string &tar_filename, Subdirectory subdir)
| Extract the tar with the given filename in the directory where the tar resides.

## Variables
std::string | _personal_dir
| custom directory for personal settings, saves, newgrf, etc.
std::vector< Searchpath > | _valid_searchpaths

## 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

  buf | string to append the separator to

Returns
  true iff the operation succeeded

Definition at line 346 of file fileio.cpp.

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

## ◆ DeterminePaths()

void DeterminePaths | ( | const char * | exe,
|  | bool | only_local_path
| ) |  |

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
  only_local_path | Whether we shouldn't fill searchpaths with global folders.

Definition at line 878 of file fileio.cpp.

References _config_file, _do_scan_working_directory, _favs_file, _highscore_file, _log_file, _personal_dir, _private_file, _searchpaths, _secrets_file, _windows_file, AI_DIR, AI_LIBRARY_DIR, AppendPathSeparator(), AUTOSAVE_DIR, BASE_DIR, BASESET_DIR, Debug, DetermineBasePaths(), FioCreateDirectory(), FioFindFullPath(), GAME_DIR, GAME_LIBRARY_DIR, GetHomeDir(), HEIGHTMAP_DIR, IsValidSearchPath(), NEWGRF_DIR, SAVE_DIR, SCENARIO_DIR, SCREENSHOT_DIR, SOCIAL_INTEGRATION_DIR, SP_AUTODOWNLOAD_DIR, SP_BINARY_DIR, SP_INSTALLATION_DIR, SP_PERSONAL_DIR, SP_SHARED_DIR, and SP_WORKING_DIR.

Referenced by openttd_main().

## ◆ 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_filename | the name of the tar to extract.
  subdir | The sub directory the tar is in.

Returns
  false on failure.

Definition at line 590 of file fileio.cpp.

References Debug, FioCreateDirectory(), FioFOpenFileTar(), lengthof, and FileHandle::Open().

Referenced by ClientNetworkContentSocketHandler::AfterDownload().

## ◆ FileExists()

bool FileExists | ( | const std::string & | 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 132 of file fileio.cpp.

References OTTD2FS().

Referenced by DEF_CONSOLE_CMD(), FioFindFullPath(), MidiFile::GetSMFFile(), ScriptInstance::LoadCompatibilityScripts(), MakeScreenshotName(), and NetworkHTTPInitialize().

## ◆ FioCheckFileExists()

bool FioCheckFileExists | ( | const std::string & | 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 121 of file fileio.cpp.

References FioFOpenFile().

Referenced by ScriptScanner::AddFile(), FillGRFDetails(), FindGameManualFilePath(), GetMusicCatEntryData(), GetMusicCatEntryName(), GetTextfile(), GRFLoadConfig(), LoadNewGRF(), LoadTranslations(), TextfileWindow::NavigateToFile(), 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

  name | the new name of the directory

Definition at line 316 of file fileio.cpp.

References OTTD2FS().

Referenced by DeterminePaths(), ExtractTar(), and MidiFile::GetSMFFile().

## ◆ FioFindDirectory()

std::string FioFindDirectory | ( | Subdirectory | subdir | ) |

Definition at line 171 of file fileio.cpp.

## ◆ FioFindFullPath()

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

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

Parameters

  subdir | Subdirectory to try.
  filename | Filename to look for.

Returns
  String containing the path if the path was found, else an empty string.

Definition at line 144 of file fileio.cpp.

References _searchpaths, FileExists(), and NUM_SUBDIRS.

Referenced by VideoDriver_SDL_Base::CreateMainWindow(), DeterminePaths(), GetDefaultTruetypeFontFile(), MidiFile::GetSMFFile(), LoadFreeTypeFont(), DriverFactoryBase::MarkVideoDriverOperational(), and DriverFactoryBase::SelectDriverImpl().

## ◆ FioFOpenFile()

std::optional< FileHandle > 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

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

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

Definition at line 242 of file fileio.cpp.

References BASESET_DIR, FioFOpenFile(), FioFOpenFileTar(), NEWGRF_DIR, NO_DIRECTORY, NUM_SUBDIRS, OLD_DATA_DIR, and OLD_GM_DIR.

Referenced by _ValidateSignatureFile(), ScenarioScanner::AddFile(), ScriptFileChecksumCreator::AddFile(), CalcGRFMD5Sum(), CalculateHashV1(), GraphicsSet::CheckMD5(), MD5File::CheckMD5(), DebugPrint(), FioCheckFileExists(), FioFOpenFile(), GetFileTitle(), MidiFile::LoadFile(), Squirrel::LoadFile(), TextfileWindow::LoadTextfile(), LoadTownData(), IniFile::OpenFile(), RandomAccessFile::RandomAccessFile(), ReadHeightmapBMP(), ReadHeightmapPNG(), ReadRawLanguageStrings(), MidiFile::ReadSMFHeader(), SaveOrLoad(), DriverFactoryBase::SelectDriverImpl(), CrashLog::WriteCrashLog(), and MidiFile::WriteSMF().

## ◆ FioGetDirectory()

std::string FioGetDirectory | ( | Searchpath | sp,
|  | Subdirectory | subdir
| ) |  |

Definition at line 163 of file fileio.cpp.

## ◆ FioRemove()

bool FioRemove | ( | const std::string & | filename | ) |

Remove a file.

Parameters

  filename | Filename to remove.

Returns
  true iff the file was removed.

Definition at line 328 of file fileio.cpp.

References Debug, and OTTD2FS().

Referenced by ClientNetworkContentSocketHandler::AfterDownload(), FiosDelete(), DriverFactoryBase::MarkVideoDriverOperational(), and DriverFactoryBase::SelectDriverImpl().

## ◆ FiosGetScreenshotDir()

const char * FiosGetScreenshotDir | ( |  | ) |

Get the directory for screenshots.

Returns
  path to screenshots

Definition at line 602 of file fios.cpp.

References SCREENSHOT_DIR.

Referenced by NewGRFProfiler::GetOutputFilename(), and MakeScreenshotName().

## ◆ ReadFileToMem()

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

Load a file into memory.

Parameters

  | filename | Name of the file to load.
  [out] | lenp | Length of loaded data.
  | maxsize | Maximum 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 1025 of file fileio.cpp.

References FileHandle::Open().

Referenced by ReadLanguagePack().

## ◆ SanitizeFilename()

void SanitizeFilename | ( | std::string & | filename | ) |

Sanitizes a filename, i.e.

removes all illegal characters from it.

Parameters

  filename | the filename

Definition at line 1003 of file fileio.cpp.

Referenced by GenerateDefaultSaveName().

## Variable Documentation

## ◆ _personal_dir

std::string _personal_dir
| extern

custom directory for personal settings, saves, newgrf, etc.

Definition at line 869 of file fileio.cpp.

Referenced by CrashLog::CreateFileName(), DeterminePaths(), and MakeScreenshotName().

## ◆ _valid_searchpaths

std::vector<Searchpath> _valid_searchpaths
| extern

Definition at line 66 of file fileio.cpp.