tron@2186: /* $Id$ */ tron@2186: 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" 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: truelight@0: typedef struct { 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 */ truelight@0: } Fio; truelight@0: truelight@0: static Fio _fio; truelight@0: truelight@0: // Get current position in file tron@1093: uint32 FioGetPos(void) 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: truelight@0: // 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: tron@1093: byte FioReadByte(void) 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: tron@1093: uint16 FioReadWord(void) truelight@0: { truelight@0: byte b = FioReadByte(); truelight@0: return (FioReadByte() << 8) | b; truelight@0: } truelight@0: tron@1093: uint32 FioReadDword(void) 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: darkvater@1036: void FioCloseAll(void) 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: Darkvater@4201: bool FioCheckFileExists(const char *filename) dominik@1198: { Darkvater@4202: FILE *f = FioFOpenFile(filename); Darkvater@4202: if (f == NULL) return false; dominik@1198: Darkvater@4202: fclose(f); Darkvater@4202: return true; dominik@1198: } dominik@1198: 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: bjarni@2736: FILE *FioFOpenFile(const char *filename) bjarni@2736: { bjarni@2736: FILE *f; bjarni@2736: char buf[MAX_PATH]; bjarni@2736: Darkvater@5296: snprintf(buf, lengthof(buf), "%s%s", _paths.data_dir, filename); bjarni@2736: bjarni@2736: f = fopen(buf, "rb"); bjarni@2736: #if !defined(WIN32) bjarni@2736: if (f == NULL) { Darkvater@5296: strtolower(buf + strlen(_paths.data_dir) - 1); bjarni@2736: f = fopen(buf, "rb"); bjarni@2736: bjarni@2736: #if defined SECOND_DATA_DIR bjarni@2736: // 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: truelight@0: void FioOpenFile(int slot, const char *filename) truelight@0: { truelight@6218: FILE *f; tron@915: truelight@6218: #if defined(LIMITED_FDS) truelight@6218: FioFreeHandle(); truelight@6218: #endif /* LIMITED_FDS */ truelight@6218: f = FioFOpenFile(filename); Darkvater@5296: if (f == NULL) error("Cannot open file '%s%s'", _paths.data_dir, filename); truelight@193: darkvater@1039: FioCloseFile(slot); // if file was opened before, close it truelight@0: _fio.handles[slot] = f; truelight@6218: #if defined(LIMITED_FDS) truelight@6218: _fio.filename[slot] = filename; truelight@6218: _fio.usage_count[slot] = 0; truelight@6218: _fio.open_handles++; truelight@6218: #endif /* LIMITED_FDS */ truelight@0: FioSeekToFile(slot << 24); truelight@0: }