src/fileio.cpp
author truelight
Wed, 13 Jun 2007 16:21:11 +0000
changeset 6896 b96972ff7d4d
parent 6875 e517a59b78e5
child 6929 56470c1b8a66
permissions -rw-r--r--
(svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
-Codechange: store the SpriteID in the spritecache too
-Add: add a PNG loader for graphical files
-Documentation: added a document to explain the PNG format
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
6201
bee01dc45e39 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6179
diff changeset
     3
/** @file fileio.cpp Standard In/Out file operations */
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5967
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1198
diff changeset
     6
#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
     7
#include "fileio.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     8
#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
     9
#include "string.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2153
diff changeset
    10
#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
    11
#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
    12
#include "debug.h"
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
    13
#include "fios.h"
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
    14
#ifndef WIN32
6323
0246c62cb1cb (svn r9281) -Fix [FS#687]: compile failure when compiling with 'home directories' enabled (stepancheg).
rubidium
parents: 6320
diff changeset
    15
#include <pwd.h>
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
    16
#include <unistd.h>
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
    17
#include <sys/stat.h>
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
    18
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
/*************************************************/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
/* FILE IO ROUTINES ******************************/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
/*************************************************/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
#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
    25
#define MAX_HANDLES 64
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    27
struct Fio {
4203
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    28
	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
    29
	uint32 pos;                         ///< current (system) position in file
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    30
	FILE *cur_fh;                       ///< current file handle
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    31
	const char *filename;               ///< current filename
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
    32
	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
    33
	byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    34
	const char *filenames[MAX_HANDLES]; ///< array of filenames we (should) have open
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
    35
#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
    36
	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
    37
	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
    38
#endif /* LIMITED_FDS */
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    39
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
static Fio _fio;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5967
diff changeset
    43
/* Get current position in file */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
    44
uint32 FioGetPos()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
	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
    47
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    49
const char *FioGetFilename()
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    50
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    51
	return _fio.filename;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    52
}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    53
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
void FioSeekTo(uint32 pos, int mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
	if (mode == SEEK_CUR) pos += FioGetPos();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
	_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
    58
	_fio.pos = pos;
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    59
	fseek(_fio.cur_fh, _fio.pos, SEEK_SET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
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
    62
#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
    63
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
    64
{
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
	/* 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
    66
	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
    67
		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
    68
		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
    69
	}
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
    70
	_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
    71
}
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
    72
#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
    73
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5967
diff changeset
    74
/* Seek to a file and a position */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
void FioSeekToFile(uint32 pos)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
{
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
    77
	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
    78
#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
    79
	/* 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
    80
	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
    81
#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
    82
	f = _fio.handles[pos >> 24];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
	assert(f != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
	_fio.cur_fh = f;
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    85
	_fio.filename = _fio.filenames[pos >> 24];
4203
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    86
	FioSeekTo(GB(pos, 0, 24), SEEK_SET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
    89
byte FioReadByte()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
	if (_fio.buffer == _fio.buffer_end) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
		_fio.pos += FIO_BUFFER_SIZE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    93
		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
    94
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    95
	return *_fio.buffer++;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    98
void FioSkipBytes(int n)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
{
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2736
diff changeset
   100
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
		int m = min(_fio.buffer_end - _fio.buffer, n);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
		_fio.buffer += m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
		n -= m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
		if (n == 0) break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
		FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
		n--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
	}
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
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
   110
uint16 FioReadWord()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
	byte b = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
	return (FioReadByte() << 8) | b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
   116
uint32 FioReadDword()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
	uint b = FioReadWord();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
	return (FioReadWord() << 16) | b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
void FioReadBlock(void *ptr, uint size)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
	FioSeekTo(FioGetPos(), SEEK_SET);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
	_fio.pos += size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
	fread(ptr, 1, size, _fio.cur_fh);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
}
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
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
   130
{
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
   131
	if (_fio.handles[slot] != NULL) {
1109
ecb98f43ba2c (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1093
diff changeset
   132
		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
   133
		_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
   134
#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
   135
		_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
   136
#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
   137
	}
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
   138
}
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
   139
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
   140
void FioCloseAll()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
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
   144
	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
   145
		FioCloseFile(i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   146
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
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
   148
#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
   149
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
   150
{
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
	/* 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
   152
	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
   153
		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
   154
		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
   155
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
		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
   157
		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
   158
		/* 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
   159
		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
   160
			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
   161
				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
   162
				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
   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
		}
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
		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
   166
		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
   167
		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
   168
	}
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
   169
}
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
   170
#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
   171
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   172
void FioOpenFile(int slot, const char *filename)
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   173
{
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   174
	FILE *f;
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   175
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   176
#if defined(LIMITED_FDS)
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   177
	FioFreeHandle();
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   178
#endif /* LIMITED_FDS */
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   179
	f = FioFOpenFile(filename);
6424
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6323
diff changeset
   180
	if (f == NULL) error("Cannot open file '%s'", filename);
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   181
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   182
	FioCloseFile(slot); // if file was opened before, close it
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   183
	_fio.handles[slot] = f;
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
   184
	_fio.filenames[slot] = filename;
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   185
#if defined(LIMITED_FDS)
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   186
	_fio.usage_count[slot] = 0;
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   187
	_fio.open_handles++;
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   188
#endif /* LIMITED_FDS */
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   189
	FioSeekToFile(slot << 24);
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   190
}
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   191
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   192
/**
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   193
 * Check whether the given file exists
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   194
 * @param filename the file to try for existance
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   195
 * @return true if and only if the file can be opened
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   196
 */
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   197
bool FioCheckFileExists(const char *filename)
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   198
{
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   199
	FILE *f = FioFOpenFile(filename);
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   200
	if (f == NULL) return false;
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   201
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   202
	fclose(f);
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   203
	return true;
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   204
}
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   205
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   206
/**
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   207
 * Opens the file with the given name
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   208
 * @param filename the file to open (in either data_dir or second_data_dir)
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   209
 * @return the opened file or NULL when it failed.
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   210
 */
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   211
FILE *FioFOpenFile(const char *filename)
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   212
{
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   213
	FILE *f;
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   214
	char buf[MAX_PATH];
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   215
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   216
	if (filename[0] == PATHSEPCHAR || filename[1] == ':') {
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   217
		ttd_strlcpy(buf, filename, lengthof(buf));
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   218
	} else {
6424
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6323
diff changeset
   219
		snprintf(buf, lengthof(buf), "%s%s", _paths.data_dir, filename);
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6323
diff changeset
   220
	}
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   221
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   222
	f = fopen(buf, "rb");
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   223
#if !defined(WIN32)
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   224
	if (f == NULL) {
6424
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6323
diff changeset
   225
		strtolower(strrchr(buf, PATHSEPCHAR));
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   226
		f = fopen(buf, "rb");
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   227
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   228
#if defined SECOND_DATA_DIR
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5967
diff changeset
   229
		/* tries in the 2nd data directory */
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   230
		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
   231
			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
   232
			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
   233
			f = fopen(buf, "rb");
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   234
		}
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   235
#endif
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   236
	}
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   237
#endif
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   238
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   239
	return f;
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   240
}
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   241
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   242
/**
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   243
 * Create a directory with the given name
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   244
 * @param name the new name of the directory
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   245
 */
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   246
void FioCreateDirectory(const char *name)
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   247
{
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   248
#if defined(WIN32) || defined(WINCE)
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   249
	CreateDirectory(OTTD2FS(name), NULL);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   250
#elif defined(OS2) && !defined(__INNOTEK_LIBC__)
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   251
	mkdir(OTTD2FS(name));
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   252
#else
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   253
	mkdir(OTTD2FS(name), 0755);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   254
#endif
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   255
}
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   256
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   257
/**
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   258
 * Appends, if necessary, the path separator character to the end of the string.
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   259
 * It does not add the path separator to zero-sized strings.
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   260
 * @param buf    string to append the separator to
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   261
 * @param buflen the length of the buf
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   262
 */
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   263
void AppendPathSeparator(char *buf, size_t buflen)
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   264
{
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   265
	size_t s = strlen(buf);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   266
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   267
	/* Length of string + path separator + '\0' */
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   268
	if (s != 0 && buf[s - 1] != PATHSEPCHAR && s + 2 < buflen) {
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   269
		buf[s]     = PATHSEPCHAR;
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   270
		buf[s + 1] = '\0';
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   271
	}
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   272
}
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   273
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   274
/**
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   275
 * Allocates and files a variable with the full path
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   276
 * based on the given directory.
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   277
 * @param dir the directory to base the path on
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   278
 * @return the malloced full path
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   279
 */
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   280
char *BuildWithFullPath(const char *dir)
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   281
{
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   282
	char *dest = MallocT<char>(MAX_PATH);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   283
	ttd_strlcpy(dest, dir, MAX_PATH);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   284
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   285
	/* Check if absolute or relative path */
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   286
	const char *s = strchr(dest, PATHSEPCHAR);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   287
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   288
	/* Add absolute path */
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   289
	if (s == NULL || dest != s) {
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   290
		getcwd(dest, MAX_PATH);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   291
		AppendPathSeparator(dest, MAX_PATH);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   292
		ttd_strlcat(dest, dir, MAX_PATH);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   293
	}
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   294
	AppendPathSeparator(dest, MAX_PATH);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   295
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   296
	return dest;
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   297
}
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   298
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   299
#if defined(WIN32) || defined(WINCE)
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   300
/**
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   301
 * Determine the base (personal dir and game data dir) paths
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   302
 * @param exe the path to the executable
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   303
 */
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   304
extern void DetermineBasePaths(const char *exe);
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   305
#else /* defined(WIN32) || defined(WINCE) */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   306
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   307
/**
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   308
 * Changes the working directory to the path of the give executable.
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   309
 * For OSX application bundles '.app' is the required extension of the bundle,
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   310
 * so when we crop the path to there, when can remove the name of the bundle
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   311
 * in the same way we remove the name from the executable name.
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   312
 * @param exe the path to the executable
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   313
 */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   314
void ChangeWorkingDirectory(const char *exe)
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   315
{
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   316
#ifdef WITH_COCOA
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   317
	char *app_bundle = strchr(exe, '.');
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   318
	while (app_bundle != NULL && strncasecmp(app_bundle, ".app", 4) != 0) app_bundle = strchr(&app_bundle[1], '.');
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   319
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   320
	if (app_bundle != NULL) app_bundle[0] = '\0';
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   321
#endif /* WITH_COCOA */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   322
	char *s = strrchr(exe, PATHSEPCHAR);
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   323
	if (s != NULL) {
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   324
		*s = '\0';
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   325
		chdir(exe);
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   326
		*s = PATHSEPCHAR;
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   327
	}
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   328
#ifdef WITH_COCOA
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   329
	if (app_bundle != NULL) app_bundle[0] = '.';
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   330
#endif /* WITH_COCOA */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   331
}
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   332
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   333
/**
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   334
 * Determine the base (personal dir and game data dir) paths
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   335
 * @param exe the path to the executable
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   336
 */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   337
void DetermineBasePaths(const char *exe)
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   338
{
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   339
	/* Change the working directory to enable doubleclicking in UIs */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   340
	ChangeWorkingDirectory(exe);
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   341
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   342
	_paths.game_data_dir = BuildWithFullPath(GAME_DATA_DIR);
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   343
#if defined(SECOND_DATA_DIR)
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   344
	_paths.second_data_dir = BuildWithFullPath(SECOND_DATA_DIR);
6424
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6323
diff changeset
   345
#else
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6323
diff changeset
   346
	_paths.second_data_dir = NULL;
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   347
#endif
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   348
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   349
#if defined(USE_HOMEDIR)
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   350
	const char *homedir = getenv("HOME");
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   351
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   352
	if (homedir == NULL) {
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   353
		const struct passwd *pw = getpwuid(getuid());
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   354
		if (pw != NULL) homedir = pw->pw_dir;
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   355
	}
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   356
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   357
	_paths.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   358
	AppendPathSeparator(_paths.personal_dir, MAX_PATH);
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   359
#else /* not defined(USE_HOMEDIR) */
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   360
	_paths.personal_dir = BuildWithFullPath(PERSONAL_DIR);
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   361
#endif /* defined(USE_HOMEDIR) */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   362
}
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   363
#endif /* defined(WIN32) || defined(WINCE) */
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   364
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   365
/**
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   366
 * Acquire the base paths (personal dir and game data dir),
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   367
 * fill all other paths (save dir, autosave dir etc) and
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   368
 * make the save and scenario directories.
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   369
 * @param exe the path to the executable
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   370
 * @todo for save_dir, autosave_dir, scenario_dir and heightmap_dir the
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   371
 *       assumption is that there is no path separator, however for gm_dir
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   372
 *       lang_dir and data_dir that assumption is made.
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   373
 *       This inconsistency should be resolved.
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   374
 */
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   375
void DeterminePaths(const char *exe)
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   376
{
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   377
	DetermineBasePaths(exe);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   378
6755
7cca96e92189 (svn r9990) -Fix: MorphOS and AmigaOS do not like "//" in filenames as that means the same as "/../" in means in Unix.
rubidium
parents: 6424
diff changeset
   379
	_paths.save_dir      = str_fmt("%ssave" PATHSEP, _paths.personal_dir);
7cca96e92189 (svn r9990) -Fix: MorphOS and AmigaOS do not like "//" in filenames as that means the same as "/../" in means in Unix.
rubidium
parents: 6424
diff changeset
   380
	_paths.autosave_dir  = str_fmt("%s" PATHSEP "autosave" PATHSEP, _paths.save_dir);
7cca96e92189 (svn r9990) -Fix: MorphOS and AmigaOS do not like "//" in filenames as that means the same as "/../" in means in Unix.
rubidium
parents: 6424
diff changeset
   381
	_paths.scenario_dir  = str_fmt("%sscenario" PATHSEP, _paths.personal_dir);
7cca96e92189 (svn r9990) -Fix: MorphOS and AmigaOS do not like "//" in filenames as that means the same as "/../" in means in Unix.
rubidium
parents: 6424
diff changeset
   382
	_paths.heightmap_dir = str_fmt("%s" PATHSEP "heightmap" PATHSEP, _paths.scenario_dir);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   383
	_paths.gm_dir        = str_fmt("%sgm" PATHSEP, _paths.game_data_dir);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   384
	_paths.data_dir      = str_fmt("%sdata" PATHSEP, _paths.game_data_dir);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   385
#if defined(CUSTOM_LANG_DIR)
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   386
	_paths.lang_dir = BuildWithFullPath(CUSTOM_LANG_DIR);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   387
#else
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   388
	_paths.lang_dir = str_fmt("%slang" PATHSEP, _paths.game_data_dir);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   389
#endif
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   390
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   391
	if (_config_file == NULL) {
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   392
		_config_file = str_fmt("%sopenttd.cfg", _paths.personal_dir);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   393
	}
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   394
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   395
	_highscore_file = str_fmt("%shs.dat", _paths.personal_dir);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   396
	_log_file = str_fmt("%sopenttd.log",  _paths.personal_dir);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   397
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   398
	/* Make (auto)save and scenario folder */
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   399
	FioCreateDirectory(_paths.save_dir);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   400
	FioCreateDirectory(_paths.autosave_dir);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   401
	FioCreateDirectory(_paths.scenario_dir);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   402
	FioCreateDirectory(_paths.heightmap_dir);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   403
}
6875
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   404
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   405
/**
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   406
 * Sanitizes a filename, i.e. removes all illegal characters from it.
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   407
 * @param filename the "\0" terminated filename
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   408
 */
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   409
void SanitizeFilename(char *filename)
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   410
{
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   411
	for (; *filename != '\0'; filename++) {
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   412
		switch (*filename) {
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   413
			/* The following characters are not allowed in filenames
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   414
			 * on at least one of the supported operating systems: */
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   415
			case ':': case '\\': case '*': case '?': case '/':
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   416
				*filename = '_';
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   417
				break;
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   418
		}
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   419
	}
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   420
}