tron@2186: /* $Id$ */ tron@2186: belugas@6527: /** @file fileio.cpp Standard In/Out file operations */ belugas@6505: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1093: #include "fileio.h" tron@2163: #include "functions.h" Darkvater@4200: #include "string.h" tron@2159: #include "macros.h" tron@2153: #include "variables.h" truelight@6218: #include "debug.h" celestar@9895: #include "fios.h" celestar@9895: #ifndef WIN32 celestar@9895: #include celestar@9895: #include celestar@9895: #include celestar@9895: #endif truelight@0: truelight@0: /*************************************************/ truelight@0: /* FILE IO ROUTINES ******************************/ truelight@0: /*************************************************/ truelight@0: truelight@0: #define FIO_BUFFER_SIZE 512 truelight@6218: #define MAX_HANDLES 64 truelight@0: celestar@9895: struct Fio { Darkvater@4203: byte *buffer, *buffer_end; ///< position pointer in local buffer and last valid byte of buffer Darkvater@4203: uint32 pos; ///< current (system) position in file Darkvater@4203: FILE *cur_fh; ///< current file handle truelight@6218: FILE *handles[MAX_HANDLES]; ///< array of file handles we can have open Darkvater@4203: byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file truelight@6218: #if defined(LIMITED_FDS) truelight@6218: uint open_handles; ///< current amount of open handles truelight@6218: const char *filename[MAX_HANDLES]; ///< array of filenames we (should) have open truelight@6218: uint usage_count[MAX_HANDLES]; ///< count how many times this file has been opened truelight@6218: #endif /* LIMITED_FDS */ celestar@9895: }; truelight@0: truelight@0: static Fio _fio; truelight@0: belugas@6505: /* Get current position in file */ celestar@9895: uint32 FioGetPos() truelight@0: { truelight@0: return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE; truelight@0: } truelight@0: truelight@0: void FioSeekTo(uint32 pos, int mode) truelight@0: { truelight@0: if (mode == SEEK_CUR) pos += FioGetPos(); truelight@0: _fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE; Darkvater@4203: _fio.pos = pos; Darkvater@4203: fseek(_fio.cur_fh, _fio.pos, SEEK_SET); truelight@0: } truelight@0: truelight@6218: #if defined(LIMITED_FDS) truelight@6218: static void FioRestoreFile(int slot) truelight@6218: { truelight@6218: /* Do we still have the file open, or should we reopen it? */ truelight@6218: if (_fio.handles[slot] == NULL) { truelight@6218: DEBUG(misc, 6, "Restoring file '%s' in slot '%d' from disk", _fio.filename[slot], slot); truelight@6218: FioOpenFile(slot, _fio.filename[slot]); truelight@6218: } truelight@6218: _fio.usage_count[slot]++; truelight@6218: } truelight@6218: #endif /* LIMITED_FDS */ truelight@6218: belugas@6505: /* Seek to a file and a position */ truelight@0: void FioSeekToFile(uint32 pos) truelight@0: { truelight@6218: FILE *f; truelight@6218: #if defined(LIMITED_FDS) truelight@6218: /* Make sure we have this file open */ truelight@6218: FioRestoreFile(pos >> 24); truelight@6218: #endif /* LIMITED_FDS */ truelight@6218: f = _fio.handles[pos >> 24]; truelight@0: assert(f != NULL); truelight@0: _fio.cur_fh = f; Darkvater@4203: FioSeekTo(GB(pos, 0, 24), SEEK_SET); truelight@0: } truelight@0: celestar@9895: byte FioReadByte() truelight@0: { truelight@0: if (_fio.buffer == _fio.buffer_end) { truelight@0: _fio.pos += FIO_BUFFER_SIZE; truelight@0: fread(_fio.buffer = _fio.buffer_start, 1, FIO_BUFFER_SIZE, _fio.cur_fh); truelight@0: } truelight@0: return *_fio.buffer++; truelight@0: } truelight@0: truelight@0: void FioSkipBytes(int n) truelight@0: { tron@2952: for (;;) { truelight@0: int m = min(_fio.buffer_end - _fio.buffer, n); truelight@0: _fio.buffer += m; truelight@0: n -= m; truelight@0: if (n == 0) break; truelight@0: FioReadByte(); truelight@0: n--; truelight@0: } truelight@0: } truelight@0: celestar@9895: uint16 FioReadWord() truelight@0: { truelight@0: byte b = FioReadByte(); truelight@0: return (FioReadByte() << 8) | b; truelight@0: } truelight@0: celestar@9895: uint32 FioReadDword() truelight@0: { truelight@0: uint b = FioReadWord(); truelight@0: return (FioReadWord() << 16) | b; truelight@0: } truelight@0: truelight@0: void FioReadBlock(void *ptr, uint size) truelight@0: { truelight@0: FioSeekTo(FioGetPos(), SEEK_SET); truelight@0: _fio.pos += size; truelight@0: fread(ptr, 1, size, _fio.cur_fh); truelight@0: } truelight@0: darkvater@1039: static inline void FioCloseFile(int slot) darkvater@1039: { darkvater@1039: if (_fio.handles[slot] != NULL) { tron@1109: fclose(_fio.handles[slot]); darkvater@1039: _fio.handles[slot] = NULL; truelight@6218: #if defined(LIMITED_FDS) truelight@6218: _fio.open_handles--; truelight@6218: #endif /* LIMITED_FDS */ darkvater@1039: } darkvater@1039: } darkvater@1039: celestar@9895: void FioCloseAll() truelight@0: { truelight@0: int i; truelight@0: darkvater@1039: for (i = 0; i != lengthof(_fio.handles); i++) darkvater@1039: FioCloseFile(i); truelight@0: } truelight@0: truelight@6218: #if defined(LIMITED_FDS) truelight@6218: static void FioFreeHandle() truelight@6218: { truelight@6218: /* If we are about to open a file that will exceed the limit, close a file */ truelight@6218: if (_fio.open_handles + 1 == LIMITED_FDS) { truelight@6218: uint i, count; truelight@6218: int slot; truelight@6218: truelight@6218: count = UINT_MAX; truelight@6218: slot = -1; truelight@6218: /* Find the file that is used the least */ truelight@6218: for (i = 0; i < lengthof(_fio.handles); i++) { truelight@6218: if (_fio.handles[i] != NULL && _fio.usage_count[i] < count) { truelight@6218: count = _fio.usage_count[i]; truelight@6218: slot = i; truelight@6218: } truelight@6218: } truelight@6218: assert(slot != -1); truelight@6218: DEBUG(misc, 6, "Closing filehandler '%s' in slot '%d' because of fd-limit", _fio.filename[slot], slot); truelight@6218: FioCloseFile(slot); truelight@6218: } truelight@6218: } truelight@6218: #endif /* LIMITED_FDS */ truelight@6218: celestar@9895: void FioOpenFile(int slot, const char *filename) celestar@9895: { celestar@9895: FILE *f; celestar@9895: celestar@9895: #if defined(LIMITED_FDS) celestar@9895: FioFreeHandle(); celestar@9895: #endif /* LIMITED_FDS */ celestar@9895: f = FioFOpenFile(filename); celestar@9908: if (f == NULL) error("Cannot open file '%s'", filename); celestar@9895: celestar@9895: FioCloseFile(slot); // if file was opened before, close it celestar@9895: _fio.handles[slot] = f; celestar@9895: #if defined(LIMITED_FDS) celestar@9895: _fio.filename[slot] = filename; celestar@9895: _fio.usage_count[slot] = 0; celestar@9895: _fio.open_handles++; celestar@9895: #endif /* LIMITED_FDS */ celestar@9895: FioSeekToFile(slot << 24); celestar@9895: } celestar@9895: celestar@9895: /** celestar@9895: * Check whether the given file exists celestar@9895: * @param filename the file to try for existance celestar@9895: * @return true if and only if the file can be opened celestar@9895: */ celestar@9895: bool FioCheckFileExists(const char *filename) celestar@9895: { celestar@9895: FILE *f = FioFOpenFile(filename); celestar@9895: if (f == NULL) return false; celestar@9895: celestar@9895: fclose(f); celestar@9895: return true; celestar@9895: } celestar@9895: celestar@9895: /** celestar@9895: * Opens the file with the given name celestar@9895: * @param filename the file to open (in either data_dir or second_data_dir) celestar@9895: * @return the opened file or NULL when it failed. celestar@9895: */ bjarni@2736: FILE *FioFOpenFile(const char *filename) bjarni@2736: { bjarni@2736: FILE *f; bjarni@2736: char buf[MAX_PATH]; bjarni@2736: celestar@9912: if (filename[0] == PATHSEPCHAR || filename[1] == ':') { celestar@9912: ttd_strlcpy(buf, filename, lengthof(buf)); celestar@9912: } else { celestar@9908: snprintf(buf, lengthof(buf), "%s%s", _paths.data_dir, filename); celestar@9908: } bjarni@2736: bjarni@2736: f = fopen(buf, "rb"); bjarni@2736: #if !defined(WIN32) bjarni@2736: if (f == NULL) { celestar@9908: strtolower(strrchr(buf, PATHSEPCHAR)); bjarni@2736: f = fopen(buf, "rb"); bjarni@2736: bjarni@2736: #if defined SECOND_DATA_DIR belugas@6505: /* tries in the 2nd data directory */ bjarni@2736: if (f == NULL) { Darkvater@5296: snprintf(buf, lengthof(buf), "%s%s", _paths.second_data_dir, filename); Darkvater@5296: strtolower(buf + strlen(_paths.second_data_dir) - 1); bjarni@2736: f = fopen(buf, "rb"); bjarni@2736: } bjarni@2736: #endif bjarni@2736: } bjarni@2736: #endif bjarni@2736: bjarni@2736: return f; bjarni@2736: } bjarni@2736: celestar@9895: /** celestar@9895: * Create a directory with the given name celestar@9895: * @param name the new name of the directory celestar@9895: */ celestar@9895: void FioCreateDirectory(const char *name) truelight@0: { celestar@9895: #if defined(WIN32) || defined(WINCE) celestar@9895: CreateDirectory(OTTD2FS(name), NULL); celestar@9895: #elif defined(OS2) && !defined(__INNOTEK_LIBC__) celestar@9895: mkdir(OTTD2FS(name)); celestar@9895: #else celestar@9895: mkdir(OTTD2FS(name), 0755); celestar@9895: #endif celestar@9895: } tron@915: celestar@9895: /** celestar@9895: * Appends, if necessary, the path separator character to the end of the string. celestar@9895: * It does not add the path separator to zero-sized strings. celestar@9895: * @param buf string to append the separator to celestar@9895: * @param buflen the length of the buf celestar@9895: */ celestar@9895: void AppendPathSeparator(char *buf, size_t buflen) celestar@9895: { celestar@9895: size_t s = strlen(buf); truelight@193: celestar@9895: /* Length of string + path separator + '\0' */ celestar@9895: if (s != 0 && buf[s - 1] != PATHSEPCHAR && s + 2 < buflen) { celestar@9895: buf[s] = PATHSEPCHAR; celestar@9895: buf[s + 1] = '\0'; celestar@9895: } truelight@0: } celestar@9895: celestar@9912: /** celestar@9912: * Allocates and files a variable with the full path celestar@9912: * based on the given directory. celestar@9912: * @param dir the directory to base the path on celestar@9912: * @return the malloced full path celestar@9912: */ celestar@9912: char *BuildWithFullPath(const char *dir) celestar@9912: { celestar@9912: char *dest = MallocT(MAX_PATH); celestar@9912: ttd_strlcpy(dest, dir, MAX_PATH); celestar@9912: celestar@9912: /* Check if absolute or relative path */ celestar@9912: const char *s = strchr(dest, PATHSEPCHAR); celestar@9912: celestar@9912: /* Add absolute path */ celestar@9912: if (s == NULL || dest != s) { celestar@9912: getcwd(dest, MAX_PATH); celestar@9912: AppendPathSeparator(dest, MAX_PATH); celestar@9912: ttd_strlcat(dest, dir, MAX_PATH); celestar@9912: } celestar@9912: AppendPathSeparator(dest, MAX_PATH); celestar@9912: celestar@9912: return dest; celestar@9912: } celestar@9912: celestar@9895: #if defined(WIN32) || defined(WINCE) celestar@9895: /** celestar@9895: * Determine the base (personal dir and game data dir) paths celestar@9895: * @param exe the path to the executable celestar@9895: */ celestar@9895: extern void DetermineBasePaths(const char *exe); celestar@9895: #else /* defined(WIN32) || defined(WINCE) */ celestar@9895: celestar@9895: /** celestar@9895: * Changes the working directory to the path of the give executable. celestar@9895: * For OSX application bundles '.app' is the required extension of the bundle, celestar@9895: * so when we crop the path to there, when can remove the name of the bundle celestar@9895: * in the same way we remove the name from the executable name. celestar@9895: * @param exe the path to the executable celestar@9895: */ celestar@9895: void ChangeWorkingDirectory(const char *exe) celestar@9895: { celestar@9895: #ifdef WITH_COCOA celestar@9895: char *app_bundle = strchr(exe, '.'); celestar@9895: while (app_bundle != NULL && strncasecmp(app_bundle, ".app", 4) != 0) app_bundle = strchr(&app_bundle[1], '.'); celestar@9895: celestar@9895: if (app_bundle != NULL) app_bundle[0] = '\0'; celestar@9895: #endif /* WITH_COCOA */ celestar@9895: char *s = strrchr(exe, PATHSEPCHAR); celestar@9895: if (s != NULL) { celestar@9895: *s = '\0'; celestar@9895: chdir(exe); celestar@9895: *s = PATHSEPCHAR; celestar@9895: } celestar@9895: #ifdef WITH_COCOA celestar@9895: if (app_bundle != NULL) app_bundle[0] = '.'; celestar@9895: #endif /* WITH_COCOA */ celestar@9895: } celestar@9895: celestar@9895: /** celestar@9895: * Determine the base (personal dir and game data dir) paths celestar@9895: * @param exe the path to the executable celestar@9895: */ celestar@9895: void DetermineBasePaths(const char *exe) celestar@9895: { celestar@9895: /* Change the working directory to enable doubleclicking in UIs */ celestar@9895: ChangeWorkingDirectory(exe); celestar@9895: celestar@9912: _paths.game_data_dir = BuildWithFullPath(GAME_DATA_DIR); celestar@9895: #if defined(SECOND_DATA_DIR) celestar@9912: _paths.second_data_dir = BuildWithFullPath(SECOND_DATA_DIR); celestar@9908: #else celestar@9908: _paths.second_data_dir = NULL; celestar@9895: #endif celestar@9895: celestar@9895: #if defined(USE_HOMEDIR) celestar@9895: const char *homedir = getenv("HOME"); celestar@9895: celestar@9895: if (homedir == NULL) { celestar@9895: const struct passwd *pw = getpwuid(getuid()); celestar@9895: if (pw != NULL) homedir = pw->pw_dir; celestar@9895: } celestar@9895: celestar@9895: _paths.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR); celestar@9912: AppendPathSeparator(_paths.personal_dir, MAX_PATH); celestar@9895: #else /* not defined(USE_HOMEDIR) */ celestar@9912: _paths.personal_dir = BuildWithFullPath(PERSONAL_DIR); celestar@9895: #endif /* defined(USE_HOMEDIR) */ celestar@9895: } celestar@9895: #endif /* defined(WIN32) || defined(WINCE) */ celestar@9895: celestar@9895: /** celestar@9895: * Acquire the base paths (personal dir and game data dir), celestar@9895: * fill all other paths (save dir, autosave dir etc) and celestar@9895: * make the save and scenario directories. celestar@9895: * @param exe the path to the executable celestar@9895: * @todo for save_dir, autosave_dir, scenario_dir and heightmap_dir the celestar@9895: * assumption is that there is no path separator, however for gm_dir celestar@9895: * lang_dir and data_dir that assumption is made. celestar@9895: * This inconsistency should be resolved. celestar@9895: */ celestar@9895: void DeterminePaths(const char *exe) celestar@9895: { celestar@9895: DetermineBasePaths(exe); celestar@9895: celestar@9912: _paths.save_dir = str_fmt("%ssave" PATHSEP, _paths.personal_dir); celestar@9912: _paths.autosave_dir = str_fmt("%s" PATHSEP "autosave" PATHSEP, _paths.save_dir); celestar@9912: _paths.scenario_dir = str_fmt("%sscenario" PATHSEP, _paths.personal_dir); celestar@9912: _paths.heightmap_dir = str_fmt("%s" PATHSEP "heightmap" PATHSEP, _paths.scenario_dir); celestar@9895: _paths.gm_dir = str_fmt("%sgm" PATHSEP, _paths.game_data_dir); celestar@9895: _paths.data_dir = str_fmt("%sdata" PATHSEP, _paths.game_data_dir); celestar@9895: #if defined(CUSTOM_LANG_DIR) celestar@9912: _paths.lang_dir = BuildWithFullPath(CUSTOM_LANG_DIR); celestar@9895: #else celestar@9895: _paths.lang_dir = str_fmt("%slang" PATHSEP, _paths.game_data_dir); celestar@9895: #endif celestar@9895: celestar@9895: if (_config_file == NULL) { celestar@9895: _config_file = str_fmt("%sopenttd.cfg", _paths.personal_dir); celestar@9895: } celestar@9895: celestar@9895: _highscore_file = str_fmt("%shs.dat", _paths.personal_dir); celestar@9895: _log_file = str_fmt("%sopenttd.log", _paths.personal_dir); celestar@9895: celestar@9895: /* Make (auto)save and scenario folder */ celestar@9895: FioCreateDirectory(_paths.save_dir); celestar@9895: FioCreateDirectory(_paths.autosave_dir); celestar@9895: FioCreateDirectory(_paths.scenario_dir); celestar@9895: FioCreateDirectory(_paths.heightmap_dir); celestar@9895: }