src/fileio.cpp
author truelight
Fri, 14 Sep 2007 22:25:00 +0000
changeset 7581 24dab79dca15
parent 7575 3f76c2453685
child 7584 8f066de3f874
permissions -rw-r--r--
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
-Note: useful for GRF-packs and 32bpp PNGs. Don't forget to keep the dir-structure alive for 32bpp PNGs!
-Note: file-loading-order: search-paths, .tar-files in the order found on disk (can be anything at all, don't depend on it.. use 'openttd -d1' to see which order they are added)
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6201
bee01dc45e39 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6179
diff changeset
     3
/** @file fileio.cpp Standard In/Out file operations */
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5967
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1198
diff changeset
     6
#include "openttd.h"
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1039
diff changeset
     7
#include "fileio.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     8
#include "functions.h"
4200
e6ba94085b81 (svn r5684) - Codechange: create an strtolower() function that uses tolower() on a whole string and apply it in the places this was used.
Darkvater
parents: 4077
diff changeset
     9
#include "string.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2153
diff changeset
    10
#include "macros.h"
2153
ecfc674410b4 (svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents: 1891
diff changeset
    11
#include "variables.h"
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    12
#include "debug.h"
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
    13
#include "fios.h"
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    14
#ifdef WIN32
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    15
#include <windows.h>
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    16
#else
6323
0246c62cb1cb (svn r9281) -Fix [FS#687]: compile failure when compiling with 'home directories' enabled (stepancheg).
rubidium
parents: 6320
diff changeset
    17
#include <pwd.h>
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
    18
#include <unistd.h>
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
    19
#include <sys/stat.h>
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
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
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    27
#define MAX_HANDLES 64
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    29
struct Fio {
4203
9f0dade554c1 (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
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    31
	uint32 pos;                         ///< current (system) position in file
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    32
	FILE *cur_fh;                       ///< current file handle
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    33
	const char *filename;               ///< current filename
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    34
	FILE *handles[MAX_HANDLES];         ///< array of file handles we can have open
4203
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    35
	byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    36
	const char *filenames[MAX_HANDLES]; ///< array of filenames we (should) have open
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    37
#if defined(LIMITED_FDS)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    38
	uint open_handles;                  ///< current amount of open handles
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    39
	uint usage_count[MAX_HANDLES];      ///< count how many times this file has been opened
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    40
#endif /* LIMITED_FDS */
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
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
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5967
diff changeset
    45
/* Get current position in file */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
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
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    51
const char *FioGetFilename()
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    52
{
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    53
	return _fio.filename;
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    54
}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
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
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    60
	_fio.pos = pos;
9f0dade554c1 (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
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    64
#if defined(LIMITED_FDS)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    65
static void FioRestoreFile(int slot)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    66
{
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    67
	/* Do we still have the file open, or should we reopen it? */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    68
	if (_fio.handles[slot] == NULL) {
7352
958170ce0a74 (svn r10715) -Fix: limited-fs code was broken
truelight
parents: 7016
diff changeset
    69
		DEBUG(misc, 6, "Restoring file '%s' in slot '%d' from disk", _fio.filenames[slot], slot);
958170ce0a74 (svn r10715) -Fix: limited-fs code was broken
truelight
parents: 7016
diff changeset
    70
		FioOpenFile(slot, _fio.filenames[slot]);
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    71
	}
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    72
	_fio.usage_count[slot]++;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    73
}
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    74
#endif /* LIMITED_FDS */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    75
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5967
diff changeset
    76
/* Seek to a file and a position */
7570
5d5d9b6af0ef (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: 7352
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
{
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    79
	FILE *f;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    80
#if defined(LIMITED_FDS)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    81
	/* Make sure we have this file open */
7570
5d5d9b6af0ef (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: 7352
diff changeset
    82
	FioRestoreFile(slot);
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
    83
#endif /* LIMITED_FDS */
7570
5d5d9b6af0ef (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: 7352
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;
7570
5d5d9b6af0ef (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: 7352
diff changeset
    87
	_fio.filename = _fio.filenames[slot];
5d5d9b6af0ef (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: 7352
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
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
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
58522ed8f0f1 (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
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
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
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
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
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   131
static inline void FioCloseFile(int slot)
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   132
{
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   133
	if (_fio.handles[slot] != NULL) {
1109
ecb98f43ba2c (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1093
diff changeset
   134
		fclose(_fio.handles[slot]);
1039
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   135
		_fio.handles[slot] = NULL;
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   136
#if defined(LIMITED_FDS)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   137
		_fio.open_handles--;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   138
#endif /* LIMITED_FDS */
1039
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   139
	}
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   140
}
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   141
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
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
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   146
	for (i = 0; i != lengthof(_fio.handles); i++)
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   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
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   150
#if defined(LIMITED_FDS)
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   151
static void FioFreeHandle()
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   152
{
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   153
	/* If we are about to open a file that will exceed the limit, close a file */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   154
	if (_fio.open_handles + 1 == LIMITED_FDS) {
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   155
		uint i, count;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   156
		int slot;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   157
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   158
		count = UINT_MAX;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   159
		slot = -1;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   160
		/* Find the file that is used the least */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   161
		for (i = 0; i < lengthof(_fio.handles); i++) {
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   162
			if (_fio.handles[i] != NULL && _fio.usage_count[i] < count) {
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   163
				count = _fio.usage_count[i];
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   164
				slot  = i;
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   165
			}
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   166
		}
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   167
		assert(slot != -1);
7352
958170ce0a74 (svn r10715) -Fix: limited-fs code was broken
truelight
parents: 7016
diff changeset
   168
		DEBUG(misc, 6, "Closing filehandler '%s' in slot '%d' because of fd-limit", _fio.filenames[slot], slot);
5967
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   169
		FioCloseFile(slot);
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   170
	}
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   171
}
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   172
#endif /* LIMITED_FDS */
e8c5596608ad (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5584
diff changeset
   173
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   174
void FioOpenFile(int slot, const char *filename)
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   175
{
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   176
	FILE *f;
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   177
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   178
#if defined(LIMITED_FDS)
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   179
	FioFreeHandle();
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   180
#endif /* LIMITED_FDS */
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   181
	f = FioFOpenFile(filename);
6424
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6323
diff changeset
   182
	if (f == NULL) error("Cannot open file '%s'", filename);
7570
5d5d9b6af0ef (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: 7352
diff changeset
   183
	uint32 pos = ftell(f);
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   184
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   185
	FioCloseFile(slot); // if file was opened before, close it
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   186
	_fio.handles[slot] = f;
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
   187
	_fio.filenames[slot] = filename;
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   188
#if defined(LIMITED_FDS)
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   189
	_fio.usage_count[slot] = 0;
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   190
	_fio.open_handles++;
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   191
#endif /* LIMITED_FDS */
7570
5d5d9b6af0ef (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: 7352
diff changeset
   192
	FioSeekToFile(slot, pos);
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   193
}
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   194
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   195
const char *_subdirs[NUM_SUBDIRS] = {
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   196
	"",
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   197
	"save" PATHSEP,
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   198
	"save" PATHSEP "autosave" PATHSEP,
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   199
	"scenario" PATHSEP,
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   200
	"scenario" PATHSEP "heightmap" PATHSEP,
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   201
	"gm" PATHSEP,
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   202
	"data" PATHSEP,
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   203
	"lang" PATHSEP
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   204
};
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   205
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   206
const char *_searchpaths[NUM_SEARCHPATHS];
7581
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   207
std::list<const char *> _tar_list;
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   208
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   209
/**
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   210
 * Check whether the given file exists
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   211
 * @param filename the file to try for existance
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   212
 * @param subdir the subdirectory to look in
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   213
 * @return true if and only if the file can be opened
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   214
 */
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   215
bool FioCheckFileExists(const char *filename, Subdirectory subdir)
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   216
{
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   217
	FILE *f = FioFOpenFile(filename, "rb", subdir);
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   218
	if (f == NULL) return false;
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   219
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   220
	fclose(f);
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   221
	return true;
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   222
}
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   223
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   224
char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename)
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   225
{
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   226
	assert(subdir < NUM_SUBDIRS);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   227
	assert(sp < NUM_SEARCHPATHS);
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   228
6941
72147014e54d (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: 6935
diff changeset
   229
	snprintf(buf, buflen, "%s%s%s", _searchpaths[sp], _subdirs[subdir], filename);
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   230
	return buf;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   231
}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   232
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   233
char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename)
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   234
{
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   235
	Searchpath sp;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   236
	assert(subdir < NUM_SUBDIRS);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   237
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   238
	FOR_ALL_SEARCHPATHS(sp) {
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   239
		FioGetFullPath(buf, buflen, sp, subdir, filename);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   240
		if (FileExists(buf)) break;
6424
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6323
diff changeset
   241
	}
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   242
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   243
	return buf;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   244
}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   245
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   246
char *FioAppendDirectory(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir)
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   247
{
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   248
	assert(subdir < NUM_SUBDIRS);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   249
	assert(sp < NUM_SEARCHPATHS);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   250
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   251
	snprintf(buf, buflen, "%s%s", _searchpaths[sp], _subdirs[subdir]);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   252
	return buf;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   253
}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   254
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   255
char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir)
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   256
{
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   257
	Searchpath sp;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   258
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   259
	/* Find and return the first valid directory */
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   260
	FOR_ALL_SEARCHPATHS(sp) {
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   261
		char *ret = FioAppendDirectory(buf, buflen, sp, subdir);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   262
		if (FileExists(buf)) return ret;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   263
	}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   264
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   265
	/* Could not find the directory, fall back to a base path */
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   266
	ttd_strlcpy(buf, _personal_dir, buflen);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   267
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   268
	return buf;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   269
}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   270
7574
40e7c5575a2a (svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight
parents: 7570
diff changeset
   271
FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   272
{
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   273
#if defined(WIN32) && defined(UNICODE)
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   274
	/* fopen is implemented as a define with ellipses for
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   275
	 * Unicode support (prepend an L). As we are not sending
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   276
	 * a string, but a variable, it 'renames' the variable,
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   277
	 * so make that variable to makes it compile happily */
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   278
	wchar_t Lmode[5];
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   279
	MultiByteToWideChar(CP_ACP, 0, mode, -1, Lmode, lengthof(Lmode));
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   280
#endif
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   281
	FILE *f = NULL;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   282
	char buf[MAX_PATH];
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   283
6935
0a2a174d3f6a (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 6929
diff changeset
   284
	if (subdir == NO_DIRECTORY) {
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   285
		ttd_strlcpy(buf, filename, lengthof(buf));
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   286
	} else {
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   287
		snprintf(buf, lengthof(buf), "%s%s%s", _searchpaths[sp], _subdirs[subdir], filename);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   288
	}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   289
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   290
	f = fopen(buf, mode);
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   291
#if !defined(WIN32)
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   292
	if (f == NULL) {
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   293
		strtolower(buf + strlen(_searchpaths[sp]) - 1);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   294
		f = fopen(buf, mode);
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   295
	}
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   296
#endif
7574
40e7c5575a2a (svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight
parents: 7570
diff changeset
   297
	if (f != NULL && filesize != NULL) {
40e7c5575a2a (svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight
parents: 7570
diff changeset
   298
		/* Find the size of the file */
40e7c5575a2a (svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight
parents: 7570
diff changeset
   299
		fseek(f, 0, SEEK_END);
40e7c5575a2a (svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight
parents: 7570
diff changeset
   300
		*filesize = ftell(f);
40e7c5575a2a (svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight
parents: 7570
diff changeset
   301
		fseek(f, 0, SEEK_SET);
40e7c5575a2a (svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight
parents: 7570
diff changeset
   302
	}
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   303
	return f;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   304
}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   305
7581
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   306
FILE *FioTarFileList(const char *tar, const char *mode, size_t *filesize, FioTarFileListCallback *callback, void *userdata)
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   307
{
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   308
	/* The TAR-header, repeated for every file */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   309
	typedef struct TarHeader {
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   310
		char name[100];      ///< Name of the file
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   311
		char mode[8];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   312
		char uid[8];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   313
		char gid[8];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   314
		char size[12];       ///< Size of the file, in ASCII
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   315
		char mtime[12];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   316
		char chksum[8];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   317
		char typeflag;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   318
		char linkname[100];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   319
		char magic[6];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   320
		char version[2];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   321
		char uname[32];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   322
		char gname[32];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   323
		char devmajor[8];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   324
		char devminor[8];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   325
		char prefix[155];    ///< Path of the file
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   326
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   327
		char unused[12];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   328
	} TarHeader;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   329
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   330
	assert(mode[0] == 'r'); // Only reading is supported
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   331
	assert(callback != NULL); // We need a callback, else this function doens't do much
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   332
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   333
#if defined(WIN32) && defined(UNICODE)
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   334
	/* fopen is implemented as a define with ellipses for
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   335
	 * Unicode support (prepend an L). As we are not sending
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   336
	 * a string, but a variable, it 'renames' the variable,
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   337
	 * so make that variable to makes it compile happily */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   338
	wchar_t Lmode[5];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   339
	MultiByteToWideChar(CP_ACP, 0, mode, -1, Lmode, lengthof(Lmode));
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   340
#endif
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   341
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   342
	FILE *f = fopen(tar, mode);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   343
	assert(f != NULL);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   344
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   345
	TarHeader th;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   346
	char buf[sizeof(th.name) + 1], *end;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   347
	char name[sizeof(th.prefix) + 1 + sizeof(th.name) + 1];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   348
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   349
	while (!feof(f)) {
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   350
		/* Read the header and make sure it is a valid one */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   351
		fread(&th, 1, 512, f);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   352
		if (strncmp(th.magic, "ustar", 5) != 0) return NULL;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   353
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   354
		name[0] = '\0';
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   355
		int len = 0;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   356
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   357
		/* The prefix contains the directory-name */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   358
		if (th.prefix[0] != '\0') {
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   359
			memcpy(name, th.prefix, sizeof(th.prefix));
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   360
			name[sizeof(th.prefix)] = '\0';
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   361
			len = strlen(name);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   362
			name[len] = PATHSEPCHAR;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   363
			len++;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   364
		}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   365
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   366
		/* Copy the name of the file in a safe way at the end of 'name' */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   367
		memcpy(&name[len], th.name, sizeof(th.name));
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   368
		name[len + sizeof(th.name)] = '\0';
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   369
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   370
		/* Calculate the size of the file.. for some strange reason this is stored as a string */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   371
		memcpy(buf, th.size, sizeof(th.size));
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   372
		buf[sizeof(th.size)] = '\0';
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   373
		int skip = strtol(buf, &end, 8);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   374
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   375
		/* Check in the callback if this is the file we want */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   376
		if (callback(name, skip, userdata)) {
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   377
			if (filesize != NULL) *filesize = skip;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   378
			return f;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   379
		}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   380
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   381
		/* Skip to the next block.. */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   382
		fseek(f, ALIGN(skip, 512), SEEK_CUR);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   383
	}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   384
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   385
	fclose(f);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   386
	return NULL;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   387
}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   388
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   389
bool FioFOpenFileTarFileListCallback(const char *filename, int size, void *search_filename)
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   390
{
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   391
	return strcasecmp(filename, (const char *)search_filename) == 0;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   392
}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   393
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   394
FILE *FioFOpenFileTar(const char *filename, const char *tar_filename, size_t *filesize)
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   395
{
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   396
	return FioTarFileList(tar_filename, "rb", filesize, FioFOpenFileTarFileListCallback, (void *)filename);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   397
}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   398
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   399
/** Opens OpenTTD files somewhere in a personal or global directory */
7575
3f76c2453685 (svn r11100) -Fix r11099: darn typos...
truelight
parents: 7574
diff changeset
   400
FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   401
{
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   402
	FILE *f = NULL;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   403
	Searchpath sp;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   404
6935
0a2a174d3f6a (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 6929
diff changeset
   405
	assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   406
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   407
	FOR_ALL_SEARCHPATHS(sp) {
7575
3f76c2453685 (svn r11100) -Fix r11099: darn typos...
truelight
parents: 7574
diff changeset
   408
		f = FioFOpenFileSp(filename, mode, sp, subdir, filesize);
6935
0a2a174d3f6a (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 6929
diff changeset
   409
		if (f != NULL || subdir == NO_DIRECTORY) break;
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   410
	}
7581
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   411
	/* We can only use .tar in case of data-dir, and read-mode */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   412
	if (f == NULL && subdir == DATA_DIR && mode[0] == 'r') {
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   413
		const char *tar;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   414
		FOR_ALL_TARS(tar) {
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   415
			f = FioFOpenFileTar(filename, tar, filesize);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   416
			if (f != NULL) break;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   417
		}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   418
	}
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   419
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   420
	return f;
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   421
}
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   422
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   423
/**
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   424
 * Create a directory with the given name
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   425
 * @param name the new name of the directory
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   426
 */
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   427
void FioCreateDirectory(const char *name)
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   428
{
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   429
#if defined(WIN32) || defined(WINCE)
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   430
	CreateDirectory(OTTD2FS(name), NULL);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   431
#elif defined(OS2) && !defined(__INNOTEK_LIBC__)
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   432
	mkdir(OTTD2FS(name));
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   433
#else
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   434
	mkdir(OTTD2FS(name), 0755);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   435
#endif
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   436
}
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   437
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   438
/**
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   439
 * Appends, if necessary, the path separator character to the end of the string.
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   440
 * It does not add the path separator to zero-sized strings.
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   441
 * @param buf    string to append the separator to
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   442
 * @param buflen the length of the buf
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   443
 */
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   444
void AppendPathSeparator(char *buf, size_t buflen)
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   445
{
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   446
	size_t s = strlen(buf);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   447
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   448
	/* Length of string + path separator + '\0' */
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   449
	if (s != 0 && buf[s - 1] != PATHSEPCHAR && s + 2 < buflen) {
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   450
		buf[s]     = PATHSEPCHAR;
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   451
		buf[s + 1] = '\0';
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   452
	}
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   453
}
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   454
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   455
/**
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   456
 * Allocates and files a variable with the full path
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   457
 * based on the given directory.
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   458
 * @param dir the directory to base the path on
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   459
 * @return the malloced full path
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   460
 */
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   461
char *BuildWithFullPath(const char *dir)
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   462
{
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   463
	char *dest = MallocT<char>(MAX_PATH);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   464
	ttd_strlcpy(dest, dir, MAX_PATH);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   465
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   466
	/* Check if absolute or relative path */
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   467
	const char *s = strchr(dest, PATHSEPCHAR);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   468
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   469
	/* Add absolute path */
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   470
	if (s == NULL || dest != s) {
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   471
		getcwd(dest, MAX_PATH);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   472
		AppendPathSeparator(dest, MAX_PATH);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   473
		ttd_strlcat(dest, dir, MAX_PATH);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   474
	}
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   475
	AppendPathSeparator(dest, MAX_PATH);
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   476
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   477
	return dest;
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   478
}
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   479
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   480
#if defined(WIN32) || defined(WINCE)
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   481
/**
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   482
 * Determine the base (personal dir and game data dir) paths
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   483
 * @param exe the path from the current path to the executable
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   484
 * @note defined in the OS related files (os2.cpp, win32.cpp, unix.cpp etc)
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   485
 */
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   486
extern void DetermineBasePaths(const char *exe);
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   487
#else /* defined(WIN32) || defined(WINCE) */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   488
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   489
/**
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   490
 * Changes the working directory to the path of the give executable.
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   491
 * For OSX application bundles '.app' is the required extension of the bundle,
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   492
 * so when we crop the path to there, when can remove the name of the bundle
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   493
 * in the same way we remove the name from the executable name.
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   494
 * @param exe the path to the executable
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   495
 */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   496
void ChangeWorkingDirectory(const char *exe)
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   497
{
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   498
#ifdef WITH_COCOA
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   499
	char *app_bundle = strchr(exe, '.');
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   500
	while (app_bundle != NULL && strncasecmp(app_bundle, ".app", 4) != 0) app_bundle = strchr(&app_bundle[1], '.');
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   501
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   502
	if (app_bundle != NULL) app_bundle[0] = '\0';
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   503
#endif /* WITH_COCOA */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   504
	char *s = strrchr(exe, PATHSEPCHAR);
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   505
	if (s != NULL) {
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   506
		*s = '\0';
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   507
		chdir(exe);
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   508
		*s = PATHSEPCHAR;
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   509
	}
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   510
#ifdef WITH_COCOA
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   511
	if (app_bundle != NULL) app_bundle[0] = '.';
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   512
#endif /* WITH_COCOA */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   513
}
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   514
7581
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   515
static bool TarListAddFile(const char *filename)
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   516
{
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   517
	/* See if we already have a tar by that name; useless to have double entries in our list */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   518
	const char *tar;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   519
	FOR_ALL_TARS(tar) {
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   520
		if (strcmp(tar, filename) == 0) return false;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   521
	}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   522
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   523
	DEBUG(misc, 1, "Found tar: %s", filename);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   524
	_tar_list.push_back(strdup(filename));
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   525
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   526
	return true;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   527
}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   528
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   529
static int ScanPathForTarFiles(const char *path, int basepath_length)
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   530
{
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   531
	extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   532
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   533
	uint num = 0;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   534
	struct stat sb;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   535
	struct dirent *dirent;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   536
	DIR *dir;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   537
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   538
	if (path == NULL || (dir = ttd_opendir(path)) == NULL) return 0;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   539
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   540
	while ((dirent = readdir(dir)) != NULL) {
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   541
		const char *d_name = FS2OTTD(dirent->d_name);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   542
		char filename[MAX_PATH];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   543
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   544
		if (!FiosIsValidFile(path, dirent, &sb)) continue;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   545
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   546
		snprintf(filename, lengthof(filename), "%s%s", path, d_name);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   547
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   548
		if (sb.st_mode & S_IFDIR) {
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   549
			/* Directory */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   550
			if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   551
			AppendPathSeparator(filename, lengthof(filename));
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   552
			num += ScanPathForTarFiles(filename, basepath_length);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   553
		} else if (sb.st_mode & S_IFREG) {
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   554
			/* File */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   555
			char *ext = strrchr(filename, '.');
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   556
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   557
			/* If no extension or extension isn't .tar, skip the file */
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   558
			if (ext == NULL) continue;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   559
			if (strcasecmp(ext, ".tar") != 0) continue;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   560
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   561
			if (TarListAddFile(filename)) num++;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   562
		}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   563
	}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   564
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   565
	closedir(dir);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   566
	return num;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   567
}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   568
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   569
static void ScanForTarFiles()
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   570
{
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   571
	Searchpath sp;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   572
	char path[MAX_PATH];
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   573
	uint num = 0;
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   574
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   575
	DEBUG(misc, 1, "Scanning for tars");
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   576
	FOR_ALL_SEARCHPATHS(sp) {
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   577
		FioAppendDirectory(path, MAX_PATH, sp, DATA_DIR);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   578
		num += ScanPathForTarFiles(path, strlen(path));
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   579
	}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   580
	DEBUG(misc, 1, "Scan complete, found %d files", num);
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   581
}
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   582
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   583
/**
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   584
 * Determine the base (personal dir and game data dir) paths
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   585
 * @param exe the path to the executable
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   586
 */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   587
void DetermineBasePaths(const char *exe)
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   588
{
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   589
	char tmp[MAX_PATH];
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6941
diff changeset
   590
#if defined(__MORPHOS__) || defined(__AMIGA__) || !defined(WITH_PERSONAL_DIR)
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6941
diff changeset
   591
	_searchpaths[SP_PERSONAL_DIR] = NULL;
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6941
diff changeset
   592
#else
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   593
	const char *homedir = getenv("HOME");
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   594
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   595
	if (homedir == NULL) {
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   596
		const struct passwd *pw = getpwuid(getuid());
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   597
		homedir = (pw == NULL) ? "" : pw->pw_dir;
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   598
	}
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   599
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   600
	snprintf(tmp, MAX_PATH, "%s" PATHSEP "%s", homedir, PERSONAL_DIR);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   601
	AppendPathSeparator(tmp, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   602
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   603
	_searchpaths[SP_PERSONAL_DIR] = strdup(tmp);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   604
#endif
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   605
	_searchpaths[SP_SHARED_DIR] = NULL;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   606
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6941
diff changeset
   607
#if defined(__MORPHOS__) || defined(__AMIGA__)
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6941
diff changeset
   608
	_searchpaths[SP_WORKING_DIR] = NULL;
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6941
diff changeset
   609
#else
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   610
	getcwd(tmp, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   611
	AppendPathSeparator(tmp, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   612
	_searchpaths[SP_WORKING_DIR] = strdup(tmp);
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6941
diff changeset
   613
#endif
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   614
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   615
	/* Change the working directory to that one of the executable */
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   616
	ChangeWorkingDirectory((char*)exe);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   617
	getcwd(tmp, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   618
	AppendPathSeparator(tmp, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   619
	_searchpaths[SP_BINARY_DIR] = strdup(tmp);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   620
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6941
diff changeset
   621
#if defined(__MORPHOS__) || defined(__AMIGA__)
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6941
diff changeset
   622
	_searchpaths[SP_INSTALLATION_DIR] = NULL;
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6941
diff changeset
   623
#else
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   624
	snprintf(tmp, MAX_PATH, "%s", GLOBAL_DATA_DIR);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   625
	AppendPathSeparator(tmp, MAX_PATH);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   626
	_searchpaths[SP_INSTALLATION_DIR] = strdup(tmp);
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6941
diff changeset
   627
#endif
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   628
#ifdef WITH_COCOA
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   629
extern void cocoaSetApplicationBundleDir();
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   630
	cocoaSetApplicationBundleDir();
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   631
#else
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   632
	_searchpaths[SP_APPLICATION_BUNDLE_DIR] = NULL;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   633
#endif
7581
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   634
24dab79dca15 (svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight
parents: 7575
diff changeset
   635
	ScanForTarFiles();
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   636
}
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   637
#endif /* defined(WIN32) || defined(WINCE) */
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   638
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   639
char *_personal_dir;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   640
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   641
/**
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   642
 * Acquire the base paths (personal dir and game data dir),
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   643
 * fill all other paths (save dir, autosave dir etc) and
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   644
 * make the save and scenario directories.
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   645
 * @param exe the path from the current path to the executable
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   646
 */
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   647
void DeterminePaths(const char *exe)
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   648
{
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   649
	DetermineBasePaths(exe);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   650
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   651
	Searchpath sp;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   652
	FOR_ALL_SEARCHPATHS(sp) DEBUG(misc, 4, "%s added as search path", _searchpaths[sp]);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   653
6941
72147014e54d (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: 6935
diff changeset
   654
	if (_config_file != NULL) {
72147014e54d (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: 6935
diff changeset
   655
		_personal_dir = strdup(_config_file);
72147014e54d (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: 6935
diff changeset
   656
		char *end = strrchr(_personal_dir , PATHSEPCHAR);
72147014e54d (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: 6935
diff changeset
   657
		if (end == NULL) {
72147014e54d (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: 6935
diff changeset
   658
			_personal_dir[0] = '\0';
72147014e54d (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: 6935
diff changeset
   659
		} else {
72147014e54d (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: 6935
diff changeset
   660
			end[1] = '\0';
72147014e54d (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: 6935
diff changeset
   661
		}
72147014e54d (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: 6935
diff changeset
   662
	} else {
72147014e54d (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: 6935
diff changeset
   663
		char personal_dir[MAX_PATH];
72147014e54d (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: 6935
diff changeset
   664
		FioFindFullPath(personal_dir, lengthof(personal_dir), BASE_DIR, "openttd.cfg");
72147014e54d (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: 6935
diff changeset
   665
72147014e54d (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: 6935
diff changeset
   666
		if (FileExists(personal_dir)) {
72147014e54d (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: 6935
diff changeset
   667
			char *end = strrchr(personal_dir, PATHSEPCHAR);
72147014e54d (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: 6935
diff changeset
   668
			if (end != NULL) end[1] = '\0';
72147014e54d (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: 6935
diff changeset
   669
			_personal_dir = strdup(personal_dir);
72147014e54d (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: 6935
diff changeset
   670
			_config_file = str_fmt("%sopenttd.cfg", _personal_dir);
72147014e54d (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: 6935
diff changeset
   671
		} else {
72147014e54d (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: 6935
diff changeset
   672
			static const Searchpath new_openttd_cfg_order[] = {
72147014e54d (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: 6935
diff changeset
   673
					SP_PERSONAL_DIR, SP_BINARY_DIR, SP_WORKING_DIR, SP_SHARED_DIR, SP_INSTALLATION_DIR
72147014e54d (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: 6935
diff changeset
   674
				};
72147014e54d (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: 6935
diff changeset
   675
72147014e54d (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: 6935
diff changeset
   676
			for (uint i = 0; i < lengthof(new_openttd_cfg_order); i++) {
72147014e54d (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: 6935
diff changeset
   677
				if (IsValidSearchPath(new_openttd_cfg_order[i])) {
72147014e54d (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: 6935
diff changeset
   678
					_personal_dir = strdup(_searchpaths[new_openttd_cfg_order[i]]);
72147014e54d (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: 6935
diff changeset
   679
					_config_file = str_fmt("%sopenttd.cfg", _personal_dir);
72147014e54d (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: 6935
diff changeset
   680
					break;
72147014e54d (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: 6935
diff changeset
   681
				}
72147014e54d (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: 6935
diff changeset
   682
			}
72147014e54d (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: 6935
diff changeset
   683
		}
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   684
	}
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   685
6941
72147014e54d (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: 6935
diff changeset
   686
	DEBUG(misc, 3, "%s found as personal directory", _personal_dir);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   687
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   688
	_highscore_file = str_fmt("%shs.dat", _personal_dir);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   689
	_log_file = str_fmt("%sopenttd.log",  _personal_dir);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   690
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   691
	char *save_dir     = str_fmt("%s%s", _personal_dir, FioGetSubdirectory(SAVE_DIR));
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   692
	char *autosave_dir = str_fmt("%s%s", _personal_dir, FioGetSubdirectory(AUTOSAVE_DIR));
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   693
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   694
	/* Make the necessary folders */
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   695
	FioCreateDirectory(_personal_dir);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   696
	FioCreateDirectory(save_dir);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   697
	FioCreateDirectory(autosave_dir);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   698
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   699
	free(save_dir);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   700
	free(autosave_dir);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   701
}
6875
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   702
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   703
/**
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   704
 * Sanitizes a filename, i.e. removes all illegal characters from it.
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   705
 * @param filename the "\0" terminated filename
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   706
 */
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   707
void SanitizeFilename(char *filename)
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   708
{
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   709
	for (; *filename != '\0'; filename++) {
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   710
		switch (*filename) {
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   711
			/* The following characters are not allowed in filenames
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   712
			 * on at least one of the supported operating systems: */
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   713
			case ':': case '\\': case '*': case '?': case '/':
7016
65ca64810a94 (svn r10272) -Fix (FS#916): remove more invalid characters from savegame names
glx
parents: 6990
diff changeset
   714
			case '<': case '>': case '|': case '"':
6875
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   715
				*filename = '_';
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   716
				break;
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   717
		}
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   718
	}
e517a59b78e5 (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 6834
diff changeset
   719
}