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 <pwd.h>
#include <sys/stat.h>
#include <algorithm>
Go to the source code of this file.
Data Structures | |
struct | Fio |
Structure for keeping several open files with just one data buffer. More... | |
Defines | |
#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 current position in file. | |
const char * | FioGetFilename (uint8 slot) |
Get the filename associated with a slot. | |
void | FioSeekTo (size_t pos, int mode) |
void | FioSeekToFile (uint8 slot, size_t pos) |
byte | FioReadByte () |
void | FioSkipBytes (int n) |
uint16 | FioReadWord () |
uint32 | FioReadDword () |
void | FioReadBlock (void *ptr, size_t 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. | |
bool | FileExists (const char *filename) |
Test whether the fiven filename 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) |
static 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. | |
static void | FioCreateDirectory (const char *name) |
Create a directory with the given name. | |
bool | 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. | |
const char * | FioTarFirstDir (const char *tarname) |
static void | TarAddLink (const std::string &srcParam, const std::string &destParam) |
void | FioTarAddLink (const char *src, const char *dest) |
static void | SimplifyFileName (char *name) |
Simplify filenames from tars. | |
bool | ExtractTar (const char *tar_filename) |
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) |
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 [NUM_SUBDIRS] |
const char * | _searchpaths [NUM_SEARCHPATHS] |
The search paths OpenTTD could search through. | |
TarList | _tar_list |
TarFileList | _tar_filelist |
static TarLinkList | _tar_linklist |
List of directory links. | |
char * | _personal_dir |
custom directory for personal settings, saves, newgrf, etc. |
Standard In/Out file operations.
Definition in file fileio.cpp.
#define FIO_BUFFER_SIZE 512 |
Size of the Fio data buffer.
Definition at line 38 of file fileio.cpp.
bool 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.
buf | string to append the separator to | |
buflen | the length of the buf |
Definition at line 483 of file fileio.cpp.
Referenced by BuildWithFullPath(), DetermineBasePaths(), DoScanWorkingDirectory(), FileScanner::Scan(), and ScanPath().
char* BuildWithFullPath | ( | const char * | dir | ) |
Allocates and files a variable with the full path based on the given directory.
dir | the directory to base the path on |
Definition at line 504 of file fileio.cpp.
References AppendPathSeparator(), ttd_strlcat(), and ttd_strlcpy().
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.
exe | the path to the executable |
Definition at line 889 of file fileio.cpp.
References DEBUG.
Referenced by DetermineBasePaths().
void DetermineBasePaths | ( | const char * | exe | ) |
Determine the base (personal dir and game data dir) paths.
exe | the path to the executable |
Definition at line 949 of file fileio.cpp.
References _do_scan_working_directory, AppendPathSeparator(), ChangeWorkingDirectoryToExecutable(), DEBUG, DoScanWorkingDirectory(), 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.
exe | the path from the current path to the executable |
Definition at line 1032 of file fileio.cpp.
References _config_file, _do_scan_working_directory, _highscore_file, _log_file, BASE_DIR, DEBUG, DetermineBasePaths(), TarScanner::DoScan(), FileExists(), FioCreateDirectory(), FOR_ALL_SEARCHPATHS, GM_DIR, HEIGHTMAP_DIR, IsValidSearchPath(), lengthof, SP_AUTODOWNLOAD_DIR, SP_INSTALLATION_DIR, SP_WORKING_DIR, and str_fmt().
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.
Definition at line 928 of file fileio.cpp.
References AppendPathSeparator(), lengthof, SP_PERSONAL_DIR, and SP_WORKING_DIR.
Referenced by DetermineBasePaths().
bool ExtractTar | ( | const char * | tar_filename | ) |
Extract the tar with the given filename in the directory where the tar resides.
tar_filename | the name of the tar to extract. |
Definition at line 806 of file fileio.cpp.
References DEBUG, FioCreateDirectory(), lastof, lengthof, min(), and strecpy().
Referenced by ClientNetworkContentSocketHandler::AfterDownload().
bool FileExists | ( | const char * | filename | ) |
Test whether the fiven filename exists.
filename | the file to test. |
Definition at line 273 of file fileio.cpp.
Referenced by DeterminePaths(), AIInstance::LoadCompatibilityScripts(), MakeScreenshotName(), and ScriptScanner::ScanScriptDir().
bool FioCheckFileExists | ( | const char * | filename, | |
Subdirectory | subdir | |||
) |
Check whether the given file exists.
filename | the file to try for existance | |
subdir | the subdirectory to look in |
Definition at line 259 of file fileio.cpp.
References FioFCloseFile(), and FioFOpenFile().
Referenced by FillGRFDetails(), and ScriptScanner::ScanDir().
static void FioCreateDirectory | ( | const char * | name | ) | [static] |
Create a directory with the given name.
name | the new name of the directory |
Definition at line 455 of file fileio.cpp.
References ttd_strlcpy().
Referenced by DeterminePaths(), and ExtractTar().
const char* FioGetFilename | ( | uint8 | slot | ) |
Get the filename associated with a slot.
slot | Index of queried file. |
Definition at line 75 of file fileio.cpp.
References Fio::shortnames.
Referenced by SpriteLoaderPNG::LoadSprite(), SpriteLoaderGrf::LoadSprite(), SpriteAlignerWindow::SetStringParameters(), and WarnCorruptSprite().
size_t FioGetPos | ( | ) |
Get current position in file.
Definition at line 65 of file fileio.cpp.
References Fio::buffer_end, and Fio::pos.
void SanitizeFilename | ( | char * | filename | ) |
Sanitizes a filename, i.e.
removes all illegal characters from it.
filename | the "\0" terminated filename |
Definition at line 1126 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.
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 1175 of file fileio.cpp.
References FileScanner::AddFile(), AppendPathSeparator(), FS2OTTD(), lengthof, 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.
fs | the file scanner to scan for | |
extension | the extension of files to search for. | |
tar | the tar to search in. |
Definition at line 1225 of file fileio.cpp.
References FileScanner::AddFile().
Referenced by FileScanner::Scan().
static void SimplifyFileName | ( | char * | name | ) | [static] |
Simplify filenames from tars.
Replace '/' by PATHSEPCHAR, and force 'name' to lowercase.
name | Filename to process. |
Definition at line 561 of file fileio.cpp.
References strtolower().
Referenced by TarScanner::AddFile().
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 246 of file fileio.cpp.
Referenced by IsValidSearchPath().
const char* const _subdirs[NUM_SUBDIRS] [static] |
{ "", "save" PATHSEP, "save" PATHSEP "autosave" PATHSEP, "scenario" PATHSEP, "scenario" PATHSEP "heightmap" PATHSEP, "gm" PATHSEP, "data" PATHSEP, "lang" PATHSEP, "ai" PATHSEP, "ai" PATHSEP "library" PATHSEP, }
Definition at line 233 of file fileio.cpp.