OpenTTD Source: src/fileio_func.h File Reference

OpenTTD Source 14.1

Data Structures | Functions | Variables
fileio_func.h File Reference
`#include "core/enum_type.hpp"`
`#include "fileio_type.h"`
`#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...

## Functions
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 std::string &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 (std::string &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, 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. 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...
DIR * | ttd_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.
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 378 of file fileio.cpp.

Referenced by 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 975 of file fileio.cpp.

References DetermineBasePaths().

## ◆ 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 687 of file fileio.cpp.

## ◆ 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 141 of file fileio.cpp.

References OTTD2FS().

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

  name | the new name of the directory

Definition at line 350 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 160 of file fileio.cpp.

References NUM_SUBDIRS.

Referenced by MidiFile::GetSMFFile(), DriverFactoryBase::MarkVideoDriverOperational(), and DriverFactoryBase::SelectDriverImpl().

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

  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 264 of file fileio.cpp.

References NO_DIRECTORY, and NUM_SUBDIRS.

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

## ◆ FiosGetScreenshotDir()

const char* FiosGetScreenshotDir | ( |  | ) |

Get the directory for screenshots.

Returns
  path to screenshots

Definition at line 597 of file fios.cpp.

Referenced by NewGRFProfiler::GetOutputFilename().

## ◆ 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 1120 of file fileio.cpp.

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 1098 of file fileio.cpp.

## ◆ ttd_opendir()

DIR* ttd_opendir | ( | const char * | path | ) |
| inline

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

  path | string to open directory of

Returns
  DIR pointer

Definition at line 111 of file fileio_func.h.

Referenced by FillLanguageList(), and ScanPath().