OpenTTD Source
12.0-beta2
|
#include "stdafx.h"
#include "fileio_func.h"
#include "spriteloader/spriteloader.hpp"
#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 <array>
#include <sstream>
#include "safeguards.h"
Go to the source code of this file.
Typedefs | |
typedef std::map< std::string, std::string > | TarLinkList |
Functions | |
static bool | IsValidSearchPath (Searchpath sp) |
Checks whether the given search path is a valid search path. More... | |
static void | FillValidSearchPaths (bool only_local_path) |
bool | FioCheckFileExists (const std::string &filename, Subdirectory subdir) |
Check whether the given file exists. More... | |
bool | FileExists (const std::string &filename) |
Test whether the given filename exists. More... | |
void | FioFCloseFile (FILE *f) |
Close a file in a safe way. | |
std::string | FioFindFullPath (Subdirectory subdir, const char *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) |
static FILE * | FioFOpenFileSp (const std::string &filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize) |
FILE * | FioFOpenFileTar (const TarFileListEntry &entry, size_t *filesize) |
Opens a file from inside a tar archive. More... | |
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. More... | |
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... | |
void | AppendPathSeparator (std::string &buf) |
Appends, if necessary, the path separator character to the end of the string. More... | |
static void | TarAddLink (const std::string &srcParam, const std::string &destParam, Subdirectory subdir) |
static void | SimplifyFileName (char *name) |
Simplify filenames from tars. 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... | |
static bool | ChangeWorkingDirectoryToExecutable (const char *exe) |
Changes the working directory to the path of the give executable. More... | |
bool | DoScanWorkingDirectory () |
Whether we should scan the working directory. More... | |
static std::string | GetHomeDir () |
Gets the home directory of the user. More... | |
void | DetermineBasePaths (const char *exe) |
Determine the base (personal dir and game data dir) paths. 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... | |
void | SanitizeFilename (char *filename) |
Sanitizes a filename, i.e. More... | |
std::unique_ptr< char[]> | ReadFileToMem (const std::string &filename, size_t &lenp, size_t maxsize) |
Load a file into memory. More... | |
static bool | MatchesExtension (const char *extension, const char *filename) |
Helper to see whether a given filename matches the extension. More... | |
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. More... | |
static uint | ScanTar (FileScanner *fs, const char *extension, const TarFileList::value_type &tar) |
Scan the given tar and add graphics sets when it finds one. More... | |
Variables | |
static bool | _do_scan_working_directory = true |
Whether the working directory should be scanned. | |
std::string | _config_file |
Configuration file of OpenTTD. | |
std::string | _highscore_file |
The file to store the highscore data in. | |
static const char *const | _subdirs [] |
std::array< std::string, NUM_SEARCHPATHS > | _searchpaths |
The search paths OpenTTD could search through. More... | |
std::vector< Searchpath > | _valid_searchpaths |
std::array< TarList, NUM_SUBDIRS > | _tar_list |
TarFileList | _tar_filelist [NUM_SUBDIRS] |
static TarLinkList | _tar_linklist [NUM_SUBDIRS] |
List of directory links. | |
std::string | _personal_dir |
custom directory for personal settings, saves, newgrf, etc. | |
Standard In/Out file operations
Definition in file fileio.cpp.
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.
buf | string to append the separator to |
Definition at line 361 of file fileio.cpp.
Referenced by FileScanner::Scan().
|
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 762 of file fileio.cpp.
void DetermineBasePaths | ( | const char * | exe | ) |
Determine the base (personal dir and game data dir) paths.
exe | the path to the executable |
Definition at line 838 of file fileio.cpp.
References GetHomeDir().
Referenced by 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.
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 966 of file fileio.cpp.
References DetermineBasePaths().
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 796 of file fileio.cpp.
References _searchpaths, and SP_WORKING_DIR.
bool ExtractTar | ( | const std::string & | tar_filename, |
Subdirectory | subdir | ||
) |
Extract the tar with the given filename in the directory where the tar resides.
tar_filename | the name of the tar to extract. |
subdir | The sub directory the tar is in. |
Definition at line 683 of file fileio.cpp.
bool FileExists | ( | const std::string & | filename | ) |
Test whether the given filename exists.
filename | the file to test. |
Definition at line 122 of file fileio.cpp.
References OTTD2FS().
bool FioCheckFileExists | ( | const std::string & | filename, |
Subdirectory | subdir | ||
) |
Check whether the given file exists.
filename | the file to try for existence. |
subdir | the subdirectory to look in |
Definition at line 108 of file fileio.cpp.
References FioFCloseFile(), and FioFOpenFile().
Referenced by FillGRFDetails(), GetMusicCatEntryData(), GetMusicCatEntryName(), GRFLoadConfig(), and SaveLoadWindow::OnTimeout().
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.
name | the new name of the directory |
Definition at line 331 of file fileio.cpp.
std::string FioFindFullPath | ( | Subdirectory | subdir, |
const char * | filename | ||
) |
Find a path to the filename in one of the search directories.
subdir | Subdirectory to try. |
filename | Filename to look for. |
Definition at line 141 of file fileio.cpp.
References NUM_SUBDIRS.
Referenced by MidiFile::GetSMFFile().
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.
filename | Name of the file to open. |
subdir | Subdirectory to open. |
nullptr
if the file is not available. Definition at line 245 of file fileio.cpp.
References NO_DIRECTORY, and NUM_SUBDIRS.
Referenced by ScriptFileChecksumCreator::AddFile(), ScenarioScanner::AddFile(), CalcGRFMD5Sum(), MD5File::CheckMD5(), GraphicsSet::CheckMD5(), FioCheckFileExists(), GetFileTitle(), MidiFile::LoadFile(), Squirrel::LoadFile(), TextfileWindow::LoadTextfile(), IniFile::OpenFile(), RandomAccessFile::RandomAccessFile(), ReadRawLanguageStrings(), MidiFile::ReadSMFHeader(), CrashLog::WriteCrashLog(), and MidiFile::WriteSMF().
FILE* FioFOpenFileTar | ( | const TarFileListEntry & | entry, |
size_t * | filesize | ||
) |
Opens a file from inside a tar archive.
entry | The entry to open. | |
[out] | filesize | If not nullptr , size of the opened file. |
nullptr
if the file is not available. EOF
after reading the whole file. Definition at line 225 of file fileio.cpp.
|
static |
Gets the home directory of the user.
May return an empty string in the unlikely scenario that the home directory cannot be found.
Definition at line 818 of file fileio.cpp.
Referenced by DetermineBasePaths().
|
static |
Checks whether the given search path is a valid search path.
sp | the search path to check |
Definition at line 77 of file fileio.cpp.
References _searchpaths.
|
static |
Helper to see whether a given filename matches the extension.
extension | The extension to look for. |
filename | The filename to look in for the extension. |
Definition at line 1138 of file fileio.cpp.
Referenced by ScanTar().
std::unique_ptr<char[]> ReadFileToMem | ( | const std::string & | filename, |
size_t & | lenp, | ||
size_t | maxsize | ||
) |
Load a file into memory.
filename | Name of the file to load. | |
[out] | lenp | Length of loaded data. |
maxsize | Maximum size to load. |
nullptr
if loading failed. Definition at line 1111 of file fileio.cpp.
Referenced by ReadLanguagePack().
void SanitizeFilename | ( | char * | filename | ) |
Sanitizes a filename, i.e.
removes all illegal characters from it.
filename | the "\0" terminated filename |
Definition at line 1089 of file fileio.cpp.
|
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 1155 of file fileio.cpp.
Referenced by FileScanner::Scan().
|
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 1197 of file fileio.cpp.
References FileScanner::AddFile(), and MatchesExtension().
|
static |
Simplify filenames from tars.
Replace '/' by #PATHSEPCHAR, and force 'name' to lowercase.
name | Filename to process. |
Definition at line 394 of file fileio.cpp.
References strtolower().
std::array<std::string, NUM_SEARCHPATHS> _searchpaths |
The search paths OpenTTD could search through.
At least one of the slots has to be filled with a path. An empty string tells that there is no such path for the current operating system.
Definition at line 56 of file fileio.cpp.
Referenced by DoScanWorkingDirectory(), and IsValidSearchPath().
|
static |
Definition at line 39 of file fileio.cpp.