# fileio.cpp File Reference

Standard In/Out file operations. More...

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "fileio.h"`
`#include "variables.h"`
`#include "debug.h"`
`#include "fios.h"`
`#include "core/alloc_func.hpp"`
`#include "core/math_func.hpp"`
`#include "string_func.h"`
`#include <pwd.h>`
`#include <unistd.h>`
`#include <sys/stat.h>`

Go to the source code of this file.

## Data Structures
struct | Fio
## Defines
#define | FIO_BUFFER_SIZE 512
## Functions
uint32 | FioGetPos ()
const char * | FioGetFilename (uint8 slot)
void | FioSeekTo (uint32 pos, int mode)
void | FioSeekToFile (uint8 slot, uint32 pos)
byte | FioReadByte ()
void | FioSkipBytes (int n)
uint16 | FioReadWord ()
uint32 | FioReadDword ()
void | FioReadBlock (void *ptr, uint size)
static void | FioCloseFile (int slot)
void | FioCloseAll ()
void | FioOpenFile (int slot, const char *filename)
bool | FioCheckFileExists (const char *filename, Subdirectory subdir)
| Check whether the given file exists.
void | FioFCloseFile (FILE *f)
| Close a file in a safe way.
char * | FioGetFullPath (char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename)
char * | FioFindFullPath (char *buf, size_t buflen, Subdirectory subdir, const char *filename)
char * | FioAppendDirectory (char *buf, size_t buflen, Searchpath sp, Subdirectory subdir)
char * | FioGetDirectory (char *buf, size_t buflen, Subdirectory subdir)
FILE * | FioFOpenFileSp (const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
FILE * | FioFOpenFileTar (TarFileListEntry *entry, size_t *filesize)
FILE * | FioFOpenFile (const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
| Opens OpenTTD files somewhere in a personal or global directory.
void | FioCreateDirectory (const char *name)
| Create a directory with the given name.
void | AppendPathSeparator (char *buf, size_t buflen)
| 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.
static bool | TarListAddFile (const char *filename)
static int | ScanPathForTarFiles (const char *path, int basepath_length)
void | ScanForTarFiles ()
void | ChangeWorkingDirectory (const char *exe)
| Changes the working directory to the path of the give executable.
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)
## Variables
static Fio | _fio
const char * | _subdirs [NUM_SUBDIRS]
const char * | _searchpaths [NUM_SEARCHPATHS]
| The searchpaths OpenTTD could search through.
TarList | _tar_list
TarFileList | _tar_filelist
char * | _personal_dir
| custom directory for personal settings, saves, newgrf, etc.

## Detailed Description

Standard In/Out file operations.

Definition in file fileio.cpp.

## Function Documentation

void AppendPathSeparator | ( | char * | buf,
|  | size_t | buflen |
| ) |  |  |

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
  | buflen | the length of the buf

Definition at line 406 of file fileio.cpp.

Referenced by BuildWithFullPath(), and DetermineBasePaths().

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

References AppendPathSeparator(), and ttd_strlcat().

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

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

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

References AppendPathSeparator(), ChangeWorkingDirectory(), SP_APPLICATION_BUNDLE_DIR, SP_BINARY_DIR, SP_INSTALLATION_DIR, SP_PERSONAL_DIR, SP_SHARED_DIR, and SP_WORKING_DIR.

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

References AUTOSAVE_DIR, BASE_DIR, DetermineBasePaths(), FioCreateDirectory(), FOR_ALL_SEARCHPATHS, IsValidSearchPath(), SAVE_DIR, SP_BINARY_DIR, SP_INSTALLATION_DIR, SP_PERSONAL_DIR, SP_SHARED_DIR, and SP_WORKING_DIR.

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

Check whether the given file exists.

Parameters:

  | filename | the file to try for existance
  | subdir | the subdirectory to look in

Returns:
  true if and only if the file can be opened

Definition at line 228 of file fileio.cpp.

References FioFCloseFile(), and FioFOpenFile().

Referenced by DeterminePalette().

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

Create a directory with the given name.

Parameters:

  | name | the new name of the directory

Definition at line 379 of file fileio.cpp.

References OTTD2FS().

Referenced by DeterminePaths().

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

Sanitizes a filename, i.e.

removes all illegal characters from it.

Parameters:

  | filename | the "\0" terminated filename

Definition at line 774 of file fileio.cpp.

static bool TarListAddFile | ( | const char * | filename | ) | `[static]`

< Name of the file

< Size of the file, in ASCII

< Path of the file

Definition at line 442 of file fileio.cpp.

References Align(), and strtolower().

## Variable Documentation

const char* _searchpaths[NUM_SEARCHPATHS]

The searchpaths 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 218 of file fileio.cpp.

Referenced by IsValidSearchPath().

const char* _subdirs[NUM_SUBDIRS]

Initial value:

     {
      "",
      "save" PATHSEP,
      "save" PATHSEP "autosave" PATHSEP,
      "scenario" PATHSEP,
      "scenario" PATHSEP "heightmap" PATHSEP,
      "gm" PATHSEP,
      "data" PATHSEP,
      "lang" PATHSEP
    }

Definition at line 207 of file fileio.cpp.