src/fileio.cpp
author truelight
Thu, 08 Feb 2007 23:46:25 +0000
changeset 5967 e8c5596608ad
parent 5584 1111b4d36e35
child 6179 d19b0137d8e4
permissions -rw-r--r--
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
The Fios keeps track how many times a file is opened, and tries to close up files which aren't used often.
The first platform using this is PSP. Patch based on the work of Turulo.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1198
diff changeset
     4
#include "openttd.h"
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1039
diff changeset
     5
#include "fileio.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     6
#include "functions.h"
4200
e6ba94085b81 (svn r5684) - Codechange: create an strtolower() function that uses tolower() on a whole string and apply it in the places this was used.
Darkvater
parents: 4077
diff changeset
     7
#include "string.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2153
diff changeset
     8
#include "macros.h"
2153
ecfc674410b4 (svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents: 1891
diff changeset
     9
#include "variables.h"
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    10
#include "debug.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
/*************************************************/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
/* FILE IO ROUTINES ******************************/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
/*************************************************/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#define FIO_BUFFER_SIZE 512
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    17
#define MAX_HANDLES 64
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
typedef struct {
4203
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    20
	byte *buffer, *buffer_end;          ///< position pointer in local buffer and last valid byte of buffer
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    21
	uint32 pos;                         ///< current (system) position in file
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    22
	FILE *cur_fh;                       ///< current file handle
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    23
	FILE *handles[MAX_HANDLES];         ///< array of file handles we can have open
4203
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    24
	byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    25
#if defined(LIMITED_FDS)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    26
	uint open_handles;                  ///< current amount of open handles
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    27
	const char *filename[MAX_HANDLES];  ///< array of filenames we (should) have open
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    28
	uint usage_count[MAX_HANDLES];      ///< count how many times this file has been opened
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    29
#endif /* LIMITED_FDS */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
} Fio;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
static Fio _fio;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
// Get current position in file
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1039
diff changeset
    35
uint32 FioGetPos(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
	return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
void FioSeekTo(uint32 pos, int mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
	if (mode == SEEK_CUR) pos += FioGetPos();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
	_fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE;
4203
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    44
	_fio.pos = pos;
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    45
	fseek(_fio.cur_fh, _fio.pos, SEEK_SET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    48
#if defined(LIMITED_FDS)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    49
static void FioRestoreFile(int slot)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    50
{
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    51
	/* Do we still have the file open, or should we reopen it? */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    52
	if (_fio.handles[slot] == NULL) {
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    53
		DEBUG(misc, 6, "Restoring file '%s' in slot '%d' from disk", _fio.filename[slot], slot);
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    54
		FioOpenFile(slot, _fio.filename[slot]);
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    55
	}
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    56
	_fio.usage_count[slot]++;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    57
}
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    58
#endif /* LIMITED_FDS */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    59
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
// Seek to a file and a position
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
void FioSeekToFile(uint32 pos)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
{
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    63
	FILE *f;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    64
#if defined(LIMITED_FDS)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    65
	/* Make sure we have this file open */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    66
	FioRestoreFile(pos >> 24);
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    67
#endif /* LIMITED_FDS */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    68
	f = _fio.handles[pos >> 24];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
	assert(f != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
	_fio.cur_fh = f;
4203
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    71
	FioSeekTo(GB(pos, 0, 24), SEEK_SET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1039
diff changeset
    74
byte FioReadByte(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
	if (_fio.buffer == _fio.buffer_end) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
		_fio.pos += FIO_BUFFER_SIZE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
		fread(_fio.buffer = _fio.buffer_start, 1, FIO_BUFFER_SIZE, _fio.cur_fh);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
	return *_fio.buffer++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
void FioSkipBytes(int n)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
{
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2736
diff changeset
    85
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
		int m = min(_fio.buffer_end - _fio.buffer, n);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
		_fio.buffer += m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
		n -= m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
		if (n == 0) break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
		FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
		n--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    93
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1039
diff changeset
    95
uint16 FioReadWord(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
	byte b = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    98
	return (FioReadByte() << 8) | b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1039
diff changeset
   101
uint32 FioReadDword(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
	uint b = FioReadWord();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
	return (FioReadWord() << 16) | b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
void FioReadBlock(void *ptr, uint size)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
	FioSeekTo(FioGetPos(), SEEK_SET);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
	_fio.pos += size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
	fread(ptr, 1, size, _fio.cur_fh);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
1039
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   114
static inline void FioCloseFile(int slot)
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   115
{
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   116
	if (_fio.handles[slot] != NULL) {
1109
ecb98f43ba2c (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1093
diff changeset
   117
		fclose(_fio.handles[slot]);
1039
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   118
		_fio.handles[slot] = NULL;
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   119
#if defined(LIMITED_FDS)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   120
		_fio.open_handles--;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   121
#endif /* LIMITED_FDS */
1039
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   122
	}
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   123
}
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   124
1036
8a82ce28b724 (svn r1537) -Fix: Close all and any open filehandles open at shutdown (tamlin)
darkvater
parents: 915
diff changeset
   125
void FioCloseAll(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
1039
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   129
	for (i = 0; i != lengthof(_fio.handles); i++)
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   130
		FioCloseFile(i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
4201
5c1b9b3043b1 (svn r5685) - Codechange: s/FiosCheckFileExists/FioCheckFileExists/ to unify the naming of these functions.
Darkvater
parents: 4200
diff changeset
   133
bool FioCheckFileExists(const char *filename)
1198
2ad7ac9e1d59 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1109
diff changeset
   134
{
4202
669c27034153 (svn r5686) - Codechange: Use only FioFOpenFile for opening files, use the other similar functions (FioCheckFileExists and FioOpenFile) as its caller.
Darkvater
parents: 4201
diff changeset
   135
	FILE *f = FioFOpenFile(filename);
669c27034153 (svn r5686) - Codechange: Use only FioFOpenFile for opening files, use the other similar functions (FioCheckFileExists and FioOpenFile) as its caller.
Darkvater
parents: 4201
diff changeset
   136
	if (f == NULL) return false;
1198
2ad7ac9e1d59 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1109
diff changeset
   137
4202
669c27034153 (svn r5686) - Codechange: Use only FioFOpenFile for opening files, use the other similar functions (FioCheckFileExists and FioOpenFile) as its caller.
Darkvater
parents: 4201
diff changeset
   138
	fclose(f);
669c27034153 (svn r5686) - Codechange: Use only FioFOpenFile for opening files, use the other similar functions (FioCheckFileExists and FioOpenFile) as its caller.
Darkvater
parents: 4201
diff changeset
   139
	return true;
1198
2ad7ac9e1d59 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1109
diff changeset
   140
}
2ad7ac9e1d59 (svn r1702) - Fix: [ 1110407 ] Game does not crash any more when a newgrf file doesn't exist
dominik
parents: 1109
diff changeset
   141
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   142
#if defined(LIMITED_FDS)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   143
static void FioFreeHandle()
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   144
{
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   145
	/* If we are about to open a file that will exceed the limit, close a file */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   146
	if (_fio.open_handles + 1 == LIMITED_FDS) {
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   147
		uint i, count;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   148
		int slot;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   149
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   150
		count = UINT_MAX;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   151
		slot = -1;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   152
		/* Find the file that is used the least */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   153
		for (i = 0; i < lengthof(_fio.handles); i++) {
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   154
			if (_fio.handles[i] != NULL && _fio.usage_count[i] < count) {
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   155
				count = _fio.usage_count[i];
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   156
				slot  = i;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   157
			}
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   158
		}
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   159
		assert(slot != -1);
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   160
		DEBUG(misc, 6, "Closing filehandler '%s' in slot '%d' because of fd-limit", _fio.filename[slot], slot);
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   161
		FioCloseFile(slot);
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   162
	}
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   163
}
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   164
#endif /* LIMITED_FDS */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   165
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   166
FILE *FioFOpenFile(const char *filename)
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   167
{
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   168
	FILE *f;
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   169
	char buf[MAX_PATH];
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   170
5296
e7acddfdd8a7 (svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents: 5170
diff changeset
   171
	snprintf(buf, lengthof(buf), "%s%s", _paths.data_dir, filename);
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   172
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   173
	f = fopen(buf, "rb");
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   174
#if !defined(WIN32)
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   175
	if (f == NULL) {
5296
e7acddfdd8a7 (svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents: 5170
diff changeset
   176
		strtolower(buf + strlen(_paths.data_dir) - 1);
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   177
		f = fopen(buf, "rb");
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   178
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   179
#if defined SECOND_DATA_DIR
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   180
		// tries in the 2nd data directory
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   181
		if (f == NULL) {
5296
e7acddfdd8a7 (svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents: 5170
diff changeset
   182
			snprintf(buf, lengthof(buf), "%s%s", _paths.second_data_dir, filename);
e7acddfdd8a7 (svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents: 5170
diff changeset
   183
			strtolower(buf + strlen(_paths.second_data_dir) - 1);
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   184
			f = fopen(buf, "rb");
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   185
		}
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   186
#endif
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   187
	}
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   188
#endif
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   189
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   190
	return f;
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   191
}
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   192
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
void FioOpenFile(int slot, const char *filename)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   194
{
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   195
	FILE *f;
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 810
diff changeset
   196
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   197
#if defined(LIMITED_FDS)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   198
	FioFreeHandle();
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   199
#endif /* LIMITED_FDS */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   200
	f = FioFOpenFile(filename);
5296
e7acddfdd8a7 (svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents: 5170
diff changeset
   201
	if (f == NULL) error("Cannot open file '%s%s'", _paths.data_dir, filename);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
   202
1039
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   203
	FioCloseFile(slot); // if file was opened before, close it
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
	_fio.handles[slot] = f;
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   205
#if defined(LIMITED_FDS)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   206
	_fio.filename[slot] = filename;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   207
	_fio.usage_count[slot] = 0;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   208
	_fio.open_handles++;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   209
#endif /* LIMITED_FDS */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
	FioSeekToFile(slot << 24);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
}