src/fileio.cpp
author truelight
Thu, 13 Sep 2007 18:22:34 +0000
changeset 8066 7acd480e05c9
parent 7848 31b4a92bc171
child 8070 959633559d9f
permissions -rw-r--r--
(svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
-Note: on a side-note, this allows files bigger than 16+ MiB, needed for tar-support
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6527
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6505
diff changeset
     3
/** @file fileio.cpp Standard In/Out file operations */
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6218
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1198
diff changeset
     6
#include "openttd.h"
1093
e8d26c7dc42f (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
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     8
#include "functions.h"
4200
a45420ba0c23 (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
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2153
diff changeset
    10
#include "macros.h"
2153
91e89aa8c299 (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"
6218
89dc931b8d78 (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: 5835
diff changeset
    12
#include "debug.h"
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
    13
#include "fios.h"
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    14
#ifdef WIN32
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    15
#include <windows.h>
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    16
#else
6649
5598de323f01 (svn r9281) -Fix [FS#687]: compile failure when compiling with 'home directories' enabled (stepancheg).
rubidium
parents: 6646
diff changeset
    17
#include <pwd.h>
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
    18
#include <unistd.h>
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
    19
#include <sys/stat.h>
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
    20
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
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
/* FILE IO ROUTINES ******************************/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
/*************************************************/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
#define FIO_BUFFER_SIZE 512
6218
89dc931b8d78 (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: 5835
diff changeset
    27
#define MAX_HANDLES 64
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    29
struct Fio {
4203
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    30
	byte *buffer, *buffer_end;          ///< position pointer in local buffer and last valid byte of buffer
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    31
	uint32 pos;                         ///< current (system) position in file
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    32
	FILE *cur_fh;                       ///< current file handle
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 7371
diff changeset
    33
	const char *filename;               ///< current filename
6218
89dc931b8d78 (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: 5835
diff changeset
    34
	FILE *handles[MAX_HANDLES];         ///< array of file handles we can have open
4203
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    35
	byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 7371
diff changeset
    36
	const char *filenames[MAX_HANDLES]; ///< array of filenames we (should) have open
6218
89dc931b8d78 (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: 5835
diff changeset
    37
#if defined(LIMITED_FDS)
89dc931b8d78 (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: 5835
diff changeset
    38
	uint open_handles;                  ///< current amount of open handles
89dc931b8d78 (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: 5835
diff changeset
    39
	uint usage_count[MAX_HANDLES];      ///< count how many times this file has been opened
89dc931b8d78 (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: 5835
diff changeset
    40
#endif /* LIMITED_FDS */
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    41
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
static Fio _fio;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6218
diff changeset
    45
/* Get current position in file */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
diff changeset
    46
uint32 FioGetPos()
0
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
	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
    49
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 7371
diff changeset
    51
const char *FioGetFilename()
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 7371
diff changeset
    52
{
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 7371
diff changeset
    53
	return _fio.filename;
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 7371
diff changeset
    54
}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 7371
diff changeset
    55
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
void FioSeekTo(uint32 pos, int mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
	if (mode == SEEK_CUR) pos += FioGetPos();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
	_fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE;
4203
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    60
	_fio.pos = pos;
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    61
	fseek(_fio.cur_fh, _fio.pos, SEEK_SET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
6218
89dc931b8d78 (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: 5835
diff changeset
    64
#if defined(LIMITED_FDS)
89dc931b8d78 (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: 5835
diff changeset
    65
static void FioRestoreFile(int slot)
89dc931b8d78 (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: 5835
diff changeset
    66
{
89dc931b8d78 (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: 5835
diff changeset
    67
	/* Do we still have the file open, or should we reopen it? */
89dc931b8d78 (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: 5835
diff changeset
    68
	if (_fio.handles[slot] == NULL) {
7848
31b4a92bc171 (svn r10715) -Fix: limited-fs code was broken
truelight
parents: 7512
diff changeset
    69
		DEBUG(misc, 6, "Restoring file '%s' in slot '%d' from disk", _fio.filenames[slot], slot);
31b4a92bc171 (svn r10715) -Fix: limited-fs code was broken
truelight
parents: 7512
diff changeset
    70
		FioOpenFile(slot, _fio.filenames[slot]);
6218
89dc931b8d78 (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: 5835
diff changeset
    71
	}
89dc931b8d78 (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: 5835
diff changeset
    72
	_fio.usage_count[slot]++;
89dc931b8d78 (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: 5835
diff changeset
    73
}
89dc931b8d78 (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: 5835
diff changeset
    74
#endif /* LIMITED_FDS */
89dc931b8d78 (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: 5835
diff changeset
    75
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6218
diff changeset
    76
/* Seek to a file and a position */
8066
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
    77
void FioSeekToFile(uint8 slot, uint32 pos)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
{
6218
89dc931b8d78 (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: 5835
diff changeset
    79
	FILE *f;
89dc931b8d78 (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: 5835
diff changeset
    80
#if defined(LIMITED_FDS)
89dc931b8d78 (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: 5835
diff changeset
    81
	/* Make sure we have this file open */
8066
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
    82
	FioRestoreFile(slot);
6218
89dc931b8d78 (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: 5835
diff changeset
    83
#endif /* LIMITED_FDS */
8066
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
    84
	f = _fio.handles[slot];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    85
	assert(f != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
	_fio.cur_fh = f;
8066
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
    87
	_fio.filename = _fio.filenames[slot];
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
    88
	FioSeekTo(pos, SEEK_SET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
diff changeset
    91
byte FioReadByte()
0
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
	if (_fio.buffer == _fio.buffer_end) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
		_fio.pos += FIO_BUFFER_SIZE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    95
		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
    96
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
	return *_fio.buffer++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    98
}
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
void FioSkipBytes(int n)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
{
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2736
diff changeset
   102
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
		int m = min(_fio.buffer_end - _fio.buffer, n);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
		_fio.buffer += m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
		n -= m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
		if (n == 0) break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
		FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
		n--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
diff changeset
   112
uint16 FioReadWord()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
	byte b = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
	return (FioReadByte() << 8) | b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   116
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
diff changeset
   118
uint32 FioReadDword()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
	uint b = FioReadWord();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
	return (FioReadWord() << 16) | b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
}
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
void FioReadBlock(void *ptr, uint size)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
	FioSeekTo(FioGetPos(), SEEK_SET);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
	_fio.pos += size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
	fread(ptr, 1, size, _fio.cur_fh);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
1039
8736f759759b (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
static inline void FioCloseFile(int slot)
8736f759759b (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
   132
{
8736f759759b (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
	if (_fio.handles[slot] != NULL) {
1109
1bab892228cd (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1093
diff changeset
   134
		fclose(_fio.handles[slot]);
1039
8736f759759b (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
   135
		_fio.handles[slot] = NULL;
6218
89dc931b8d78 (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: 5835
diff changeset
   136
#if defined(LIMITED_FDS)
89dc931b8d78 (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: 5835
diff changeset
   137
		_fio.open_handles--;
89dc931b8d78 (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: 5835
diff changeset
   138
#endif /* LIMITED_FDS */
1039
8736f759759b (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
	}
8736f759759b (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
   140
}
8736f759759b (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
   141
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
diff changeset
   142
void FioCloseAll()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
1039
8736f759759b (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
   146
	for (i = 0; i != lengthof(_fio.handles); i++)
8736f759759b (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
   147
		FioCloseFile(i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   148
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
6218
89dc931b8d78 (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: 5835
diff changeset
   150
#if defined(LIMITED_FDS)
89dc931b8d78 (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: 5835
diff changeset
   151
static void FioFreeHandle()
89dc931b8d78 (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: 5835
diff changeset
   152
{
89dc931b8d78 (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: 5835
diff changeset
   153
	/* If we are about to open a file that will exceed the limit, close a file */
89dc931b8d78 (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: 5835
diff changeset
   154
	if (_fio.open_handles + 1 == LIMITED_FDS) {
89dc931b8d78 (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: 5835
diff changeset
   155
		uint i, count;
89dc931b8d78 (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: 5835
diff changeset
   156
		int slot;
89dc931b8d78 (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: 5835
diff changeset
   157
89dc931b8d78 (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: 5835
diff changeset
   158
		count = UINT_MAX;
89dc931b8d78 (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: 5835
diff changeset
   159
		slot = -1;
89dc931b8d78 (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: 5835
diff changeset
   160
		/* Find the file that is used the least */
89dc931b8d78 (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: 5835
diff changeset
   161
		for (i = 0; i < lengthof(_fio.handles); i++) {
89dc931b8d78 (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: 5835
diff changeset
   162
			if (_fio.handles[i] != NULL && _fio.usage_count[i] < count) {
89dc931b8d78 (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: 5835
diff changeset
   163
				count = _fio.usage_count[i];
89dc931b8d78 (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: 5835
diff changeset
   164
				slot  = i;
89dc931b8d78 (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: 5835
diff changeset
   165
			}
89dc931b8d78 (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: 5835
diff changeset
   166
		}
89dc931b8d78 (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: 5835
diff changeset
   167
		assert(slot != -1);
7848
31b4a92bc171 (svn r10715) -Fix: limited-fs code was broken
truelight
parents: 7512
diff changeset
   168
		DEBUG(misc, 6, "Closing filehandler '%s' in slot '%d' because of fd-limit", _fio.filenames[slot], slot);
6218
89dc931b8d78 (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: 5835
diff changeset
   169
		FioCloseFile(slot);
89dc931b8d78 (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: 5835
diff changeset
   170
	}
89dc931b8d78 (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: 5835
diff changeset
   171
}
89dc931b8d78 (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: 5835
diff changeset
   172
#endif /* LIMITED_FDS */
89dc931b8d78 (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: 5835
diff changeset
   173
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   174
void FioOpenFile(int slot, const char *filename)
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   175
{
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   176
	FILE *f;
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   177
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   178
#if defined(LIMITED_FDS)
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   179
	FioFreeHandle();
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   180
#endif /* LIMITED_FDS */
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   181
	f = FioFOpenFile(filename);
6920
ee15b916f758 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6649
diff changeset
   182
	if (f == NULL) error("Cannot open file '%s'", filename);
8066
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
   183
	uint32 pos = ftell(f);
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   184
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   185
	FioCloseFile(slot); // if file was opened before, close it
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   186
	_fio.handles[slot] = f;
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 7371
diff changeset
   187
	_fio.filenames[slot] = filename;
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   188
#if defined(LIMITED_FDS)
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   189
	_fio.usage_count[slot] = 0;
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   190
	_fio.open_handles++;
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   191
#endif /* LIMITED_FDS */
8066
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
   192
	FioSeekToFile(slot, pos);
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   193
}
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   194
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   195
const char *_subdirs[NUM_SUBDIRS] = {
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   196
	"",
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   197
	"save" PATHSEP,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   198
	"save" PATHSEP "autosave" PATHSEP,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   199
	"scenario" PATHSEP,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   200
	"scenario" PATHSEP "heightmap" PATHSEP,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   201
	"gm" PATHSEP,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   202
	"data" PATHSEP,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   203
	"lang" PATHSEP
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   204
};
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   205
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   206
const char *_searchpaths[NUM_SEARCHPATHS];
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   207
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   208
/**
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   209
 * Check whether the given file exists
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   210
 * @param filename the file to try for existance
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   211
 * @param subdir the subdirectory to look in
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   212
 * @return true if and only if the file can be opened
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   213
 */
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   214
bool FioCheckFileExists(const char *filename, Subdirectory subdir)
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   215
{
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   216
	FILE *f = FioFOpenFile(filename, "rb", subdir);
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   217
	if (f == NULL) return false;
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   218
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   219
	fclose(f);
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   220
	return true;
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   221
}
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   222
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   223
char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename)
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   224
{
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   225
	assert(subdir < NUM_SUBDIRS);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   226
	assert(sp < NUM_SEARCHPATHS);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   227
7437
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   228
	snprintf(buf, buflen, "%s%s%s", _searchpaths[sp], _subdirs[subdir], filename);
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   229
	return buf;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   230
}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   231
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   232
char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   233
{
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   234
	Searchpath sp;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   235
	assert(subdir < NUM_SUBDIRS);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   236
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   237
	FOR_ALL_SEARCHPATHS(sp) {
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   238
		FioGetFullPath(buf, buflen, sp, subdir, filename);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   239
		if (FileExists(buf)) break;
6920
ee15b916f758 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6649
diff changeset
   240
	}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   241
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   242
	return buf;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   243
}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   244
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   245
char *FioAppendDirectory(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   246
{
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   247
	assert(subdir < NUM_SUBDIRS);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   248
	assert(sp < NUM_SEARCHPATHS);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   249
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   250
	snprintf(buf, buflen, "%s%s", _searchpaths[sp], _subdirs[subdir]);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   251
	return buf;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   252
}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   253
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   254
char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   255
{
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   256
	Searchpath sp;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   257
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   258
	/* Find and return the first valid directory */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   259
	FOR_ALL_SEARCHPATHS(sp) {
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   260
		char *ret = FioAppendDirectory(buf, buflen, sp, subdir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   261
		if (FileExists(buf)) return ret;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   262
	}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   263
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   264
	/* Could not find the directory, fall back to a base path */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   265
	ttd_strlcpy(buf, _personal_dir, buflen);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   266
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   267
	return buf;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   268
}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   269
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   270
FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   271
{
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   272
#if defined(WIN32) && defined(UNICODE)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   273
	/* fopen is implemented as a define with ellipses for
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   274
	 * Unicode support (prepend an L). As we are not sending
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   275
	 * a string, but a variable, it 'renames' the variable,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   276
	 * so make that variable to makes it compile happily */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   277
	wchar_t Lmode[5];
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   278
	MultiByteToWideChar(CP_ACP, 0, mode, -1, Lmode, lengthof(Lmode));
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   279
#endif
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   280
	FILE *f = NULL;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   281
	char buf[MAX_PATH];
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   282
7431
f340111a55ec (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 7425
diff changeset
   283
	if (subdir == NO_DIRECTORY) {
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   284
		ttd_strlcpy(buf, filename, lengthof(buf));
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   285
	} else {
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   286
		snprintf(buf, lengthof(buf), "%s%s%s", _searchpaths[sp], _subdirs[subdir], filename);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   287
	}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   288
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   289
	f = fopen(buf, mode);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   290
#if !defined(WIN32)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   291
	if (f == NULL) {
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   292
		strtolower(buf + strlen(_searchpaths[sp]) - 1);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   293
		f = fopen(buf, mode);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   294
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   295
#endif
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   296
	return f;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   297
}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   298
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   299
/** Opens OpenTTD files somewhere in a personal or global directory */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   300
FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   301
{
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   302
	FILE *f = NULL;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   303
	Searchpath sp;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   304
7431
f340111a55ec (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 7425
diff changeset
   305
	assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   306
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   307
	FOR_ALL_SEARCHPATHS(sp) {
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   308
		f = FioFOpenFileSp(filename, mode, sp, subdir);
7431
f340111a55ec (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 7425
diff changeset
   309
		if (f != NULL || subdir == NO_DIRECTORY) break;
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   310
	}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   311
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   312
	return f;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   313
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   314
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   315
/**
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   316
 * Create a directory with the given name
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   317
 * @param name the new name of the directory
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   318
 */
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   319
void FioCreateDirectory(const char *name)
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   320
{
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   321
#if defined(WIN32) || defined(WINCE)
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   322
	CreateDirectory(OTTD2FS(name), NULL);
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   323
#elif defined(OS2) && !defined(__INNOTEK_LIBC__)
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   324
	mkdir(OTTD2FS(name));
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   325
#else
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   326
	mkdir(OTTD2FS(name), 0755);
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   327
#endif
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   328
}
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   329
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   330
/**
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   331
 * Appends, if necessary, the path separator character to the end of the string.
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   332
 * It does not add the path separator to zero-sized strings.
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   333
 * @param buf    string to append the separator to
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   334
 * @param buflen the length of the buf
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   335
 */
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   336
void AppendPathSeparator(char *buf, size_t buflen)
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   337
{
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   338
	size_t s = strlen(buf);
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   339
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   340
	/* Length of string + path separator + '\0' */
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   341
	if (s != 0 && buf[s - 1] != PATHSEPCHAR && s + 2 < buflen) {
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   342
		buf[s]     = PATHSEPCHAR;
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   343
		buf[s + 1] = '\0';
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   344
	}
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   345
}
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   346
7330
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   347
/**
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   348
 * Allocates and files a variable with the full path
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   349
 * based on the given directory.
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   350
 * @param dir the directory to base the path on
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   351
 * @return the malloced full path
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   352
 */
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   353
char *BuildWithFullPath(const char *dir)
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   354
{
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   355
	char *dest = MallocT<char>(MAX_PATH);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   356
	ttd_strlcpy(dest, dir, MAX_PATH);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   357
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   358
	/* Check if absolute or relative path */
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   359
	const char *s = strchr(dest, PATHSEPCHAR);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   360
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   361
	/* Add absolute path */
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   362
	if (s == NULL || dest != s) {
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   363
		getcwd(dest, MAX_PATH);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   364
		AppendPathSeparator(dest, MAX_PATH);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   365
		ttd_strlcat(dest, dir, MAX_PATH);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   366
	}
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   367
	AppendPathSeparator(dest, MAX_PATH);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   368
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   369
	return dest;
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   370
}
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   371
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   372
#if defined(WIN32) || defined(WINCE)
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   373
/**
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   374
 * Determine the base (personal dir and game data dir) paths
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   375
 * @param exe the path from the current path to the executable
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   376
 * @note defined in the OS related files (os2.cpp, win32.cpp, unix.cpp etc)
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   377
 */
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   378
extern void DetermineBasePaths(const char *exe);
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   379
#else /* defined(WIN32) || defined(WINCE) */
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   380
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   381
/**
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   382
 * Changes the working directory to the path of the give executable.
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   383
 * For OSX application bundles '.app' is the required extension of the bundle,
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   384
 * so when we crop the path to there, when can remove the name of the bundle
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   385
 * in the same way we remove the name from the executable name.
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   386
 * @param exe the path to the executable
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   387
 */
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   388
void ChangeWorkingDirectory(const char *exe)
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   389
{
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   390
#ifdef WITH_COCOA
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   391
	char *app_bundle = strchr(exe, '.');
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   392
	while (app_bundle != NULL && strncasecmp(app_bundle, ".app", 4) != 0) app_bundle = strchr(&app_bundle[1], '.');
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   393
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   394
	if (app_bundle != NULL) app_bundle[0] = '\0';
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   395
#endif /* WITH_COCOA */
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   396
	char *s = strrchr(exe, PATHSEPCHAR);
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   397
	if (s != NULL) {
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   398
		*s = '\0';
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   399
		chdir(exe);
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   400
		*s = PATHSEPCHAR;
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   401
	}
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   402
#ifdef WITH_COCOA
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   403
	if (app_bundle != NULL) app_bundle[0] = '.';
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   404
#endif /* WITH_COCOA */
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   405
}
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   406
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   407
/**
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   408
 * Determine the base (personal dir and game data dir) paths
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   409
 * @param exe the path to the executable
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   410
 */
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   411
void DetermineBasePaths(const char *exe)
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   412
{
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   413
	char tmp[MAX_PATH];
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   414
#if defined(__MORPHOS__) || defined(__AMIGA__) || !defined(WITH_PERSONAL_DIR)
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   415
	_searchpaths[SP_PERSONAL_DIR] = NULL;
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   416
#else
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   417
	const char *homedir = getenv("HOME");
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   418
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   419
	if (homedir == NULL) {
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   420
		const struct passwd *pw = getpwuid(getuid());
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   421
		homedir = (pw == NULL) ? "" : pw->pw_dir;
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   422
	}
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   423
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   424
	snprintf(tmp, MAX_PATH, "%s" PATHSEP "%s", homedir, PERSONAL_DIR);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   425
	AppendPathSeparator(tmp, MAX_PATH);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   426
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   427
	_searchpaths[SP_PERSONAL_DIR] = strdup(tmp);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   428
#endif
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   429
	_searchpaths[SP_SHARED_DIR] = NULL;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   430
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   431
#if defined(__MORPHOS__) || defined(__AMIGA__)
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   432
	_searchpaths[SP_WORKING_DIR] = NULL;
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   433
#else
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   434
	getcwd(tmp, MAX_PATH);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   435
	AppendPathSeparator(tmp, MAX_PATH);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   436
	_searchpaths[SP_WORKING_DIR] = strdup(tmp);
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   437
#endif
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   438
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   439
	/* Change the working directory to that one of the executable */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   440
	ChangeWorkingDirectory((char*)exe);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   441
	getcwd(tmp, MAX_PATH);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   442
	AppendPathSeparator(tmp, MAX_PATH);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   443
	_searchpaths[SP_BINARY_DIR] = strdup(tmp);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   444
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   445
#if defined(__MORPHOS__) || defined(__AMIGA__)
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   446
	_searchpaths[SP_INSTALLATION_DIR] = NULL;
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   447
#else
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   448
	snprintf(tmp, MAX_PATH, "%s", GLOBAL_DATA_DIR);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   449
	AppendPathSeparator(tmp, MAX_PATH);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   450
	_searchpaths[SP_INSTALLATION_DIR] = strdup(tmp);
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   451
#endif
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   452
#ifdef WITH_COCOA
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   453
extern void cocoaSetApplicationBundleDir();
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   454
	cocoaSetApplicationBundleDir();
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   455
#else
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   456
	_searchpaths[SP_APPLICATION_BUNDLE_DIR] = NULL;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   457
#endif
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   458
}
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   459
#endif /* defined(WIN32) || defined(WINCE) */
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   460
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   461
char *_personal_dir;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   462
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   463
/**
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   464
 * Acquire the base paths (personal dir and game data dir),
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   465
 * fill all other paths (save dir, autosave dir etc) and
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   466
 * make the save and scenario directories.
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   467
 * @param exe the path from the current path to the executable
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   468
 */
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   469
void DeterminePaths(const char *exe)
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   470
{
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   471
	DetermineBasePaths(exe);
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   472
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   473
	Searchpath sp;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   474
	FOR_ALL_SEARCHPATHS(sp) DEBUG(misc, 4, "%s added as search path", _searchpaths[sp]);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   475
7437
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   476
	if (_config_file != NULL) {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   477
		_personal_dir = strdup(_config_file);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   478
		char *end = strrchr(_personal_dir , PATHSEPCHAR);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   479
		if (end == NULL) {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   480
			_personal_dir[0] = '\0';
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   481
		} else {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   482
			end[1] = '\0';
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   483
		}
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   484
	} else {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   485
		char personal_dir[MAX_PATH];
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   486
		FioFindFullPath(personal_dir, lengthof(personal_dir), BASE_DIR, "openttd.cfg");
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   487
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   488
		if (FileExists(personal_dir)) {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   489
			char *end = strrchr(personal_dir, PATHSEPCHAR);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   490
			if (end != NULL) end[1] = '\0';
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   491
			_personal_dir = strdup(personal_dir);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   492
			_config_file = str_fmt("%sopenttd.cfg", _personal_dir);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   493
		} else {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   494
			static const Searchpath new_openttd_cfg_order[] = {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   495
					SP_PERSONAL_DIR, SP_BINARY_DIR, SP_WORKING_DIR, SP_SHARED_DIR, SP_INSTALLATION_DIR
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   496
				};
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   497
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   498
			for (uint i = 0; i < lengthof(new_openttd_cfg_order); i++) {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   499
				if (IsValidSearchPath(new_openttd_cfg_order[i])) {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   500
					_personal_dir = strdup(_searchpaths[new_openttd_cfg_order[i]]);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   501
					_config_file = str_fmt("%sopenttd.cfg", _personal_dir);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   502
					break;
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   503
				}
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   504
			}
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   505
		}
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   506
	}
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   507
7437
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   508
	DEBUG(misc, 3, "%s found as personal directory", _personal_dir);
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   509
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   510
	_highscore_file = str_fmt("%shs.dat", _personal_dir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   511
	_log_file = str_fmt("%sopenttd.log",  _personal_dir);
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   512
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   513
	char *save_dir     = str_fmt("%s%s", _personal_dir, FioGetSubdirectory(SAVE_DIR));
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   514
	char *autosave_dir = str_fmt("%s%s", _personal_dir, FioGetSubdirectory(AUTOSAVE_DIR));
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   515
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   516
	/* Make the necessary folders */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   517
	FioCreateDirectory(_personal_dir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   518
	FioCreateDirectory(save_dir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   519
	FioCreateDirectory(autosave_dir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   520
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   521
	free(save_dir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   522
	free(autosave_dir);
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   523
}
7371
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   524
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   525
/**
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   526
 * Sanitizes a filename, i.e. removes all illegal characters from it.
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   527
 * @param filename the "\0" terminated filename
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   528
 */
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   529
void SanitizeFilename(char *filename)
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   530
{
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   531
	for (; *filename != '\0'; filename++) {
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   532
		switch (*filename) {
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   533
			/* The following characters are not allowed in filenames
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   534
			 * on at least one of the supported operating systems: */
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   535
			case ':': case '\\': case '*': case '?': case '/':
7512
7889a88184c0 (svn r10272) -Fix (FS#916): remove more invalid characters from savegame names
glx
parents: 7486
diff changeset
   536
			case '<': case '>': case '|': case '"':
7371
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   537
				*filename = '_';
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   538
				break;
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   539
		}
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   540
	}
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   541
}