src/fileio.cpp
author rubidium
Wed, 26 Nov 2008 13:12:45 +0000
changeset 10385 4f9838649c7f
parent 10384 6b408b83ac34
child 10405 fb6f4c4476a6
permissions -rw-r--r--
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
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"
10039
1f236afd6cd1 (svn r14199) -Codechange: split fileio.h into fileio_type.h and fileio_func.h so not everything that includes saveload.h needs to include everything else too.
rubidium
parents: 10036
diff changeset
     7
#include "fileio_func.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
     8
#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
     9
#include "debug.h"
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
    10
#include "fios.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8113
diff changeset
    11
#include "core/alloc_func.hpp"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
    12
#include "core/math_func.hpp"
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8131
diff changeset
    13
#include "string_func.h"
9581
0d8f232e1721 (svn r13618) -Codechange: move tar-specific declarations to separate file
smatz
parents: 9470
diff changeset
    14
#include "tar_type.h"
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    15
#ifdef WIN32
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    16
#include <windows.h>
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
    17
#else
6323
0246c62cb1cb (svn r9281) -Fix [FS#687]: compile failure when compiling with 'home directories' enabled (stepancheg).
rubidium
parents: 6320
diff changeset
    18
#include <pwd.h>
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
    19
#include <unistd.h>
7589
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
    20
#endif
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
    21
#include <sys/stat.h>
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
/*************************************************/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
/* FILE IO ROUTINES ******************************/
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    27
#define FIO_BUFFER_SIZE 512
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 {
7805
7ab20f94cc80 (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 7628
diff changeset
    30
	byte *buffer, *buffer_end;             ///< position pointer in local buffer and last valid byte of buffer
9146
dbe2317185eb (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 9103
diff changeset
    31
	size_t pos;                            ///< current (system) position in file
7805
7ab20f94cc80 (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 7628
diff changeset
    32
	FILE *cur_fh;                          ///< current file handle
7ab20f94cc80 (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 7628
diff changeset
    33
	const char *filename;                  ///< current filename
7ab20f94cc80 (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 7628
diff changeset
    34
	FILE *handles[MAX_FILE_SLOTS];         ///< array of file handles we can have open
7ab20f94cc80 (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 7628
diff changeset
    35
	byte buffer_start[FIO_BUFFER_SIZE];    ///< local buffer when read from file
7ab20f94cc80 (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 7628
diff changeset
    36
	const char *filenames[MAX_FILE_SLOTS]; ///< array of filenames we (should) have open
8374
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
    37
	char *shortnames[MAX_FILE_SLOTS];///< array of short names for spriteloader's use
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
    38
#if defined(LIMITED_FDS)
7805
7ab20f94cc80 (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 7628
diff changeset
    39
	uint open_handles;                     ///< current amount of open handles
7ab20f94cc80 (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 7628
diff changeset
    40
	uint usage_count[MAX_FILE_SLOTS];      ///< count how many times this file has been opened
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
    41
#endif /* LIMITED_FDS */
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
    42
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
static Fio _fio;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5967
diff changeset
    46
/* Get current position in file */
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9146
diff changeset
    47
size_t FioGetPos()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
{
9103
95b1766c87dd (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 9100
diff changeset
    49
	return _fio.pos + (_fio.buffer - _fio.buffer_end);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
8374
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
    52
const char *FioGetFilename(uint8 slot)
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    53
{
8374
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
    54
	return _fio.shortnames[slot];
6896
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    55
}
b96972ff7d4d (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 6875
diff changeset
    56
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9146
diff changeset
    57
void FioSeekTo(size_t pos, int mode)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
	if (mode == SEEK_CUR) pos += FioGetPos();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
	_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
    61
	_fio.pos = pos;
9f0dade554c1 (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    62
	fseek(_fio.cur_fh, _fio.pos, SEEK_SET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
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
    65
#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
    66
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
    67
{
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
	/* 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
    69
	if (_fio.handles[slot] == NULL) {
7352
958170ce0a74 (svn r10715) -Fix: limited-fs code was broken
truelight
parents: 7016
diff changeset
    70
		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
    71
		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
    72
	}
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
	_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
    74
}
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
#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
    76
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5967
diff changeset
    77
/* Seek to a file and a position */
9392
b72de1fe60e1 (svn r13303) -Fix (r13301): 64bit gcc compilation was broken
smatz
parents: 9390
diff changeset
    78
void FioSeekToFile(uint8 slot, size_t pos)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
{
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
    80
	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
    81
#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
    82
	/* 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
    83
	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
    84
#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
    85
	f = _fio.handles[slot];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
	assert(f != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
	_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
    88
	_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
    89
	FioSeekTo(pos, SEEK_SET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
    92
byte FioReadByte()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    93
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
	if (_fio.buffer == _fio.buffer_end) {
9100
3ad0412f8ddf (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 9084
diff changeset
    95
		_fio.buffer = _fio.buffer_start;
9103
95b1766c87dd (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 9100
diff changeset
    96
		size_t size = fread(_fio.buffer, 1, FIO_BUFFER_SIZE, _fio.cur_fh);
95b1766c87dd (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 9100
diff changeset
    97
		_fio.pos += size;
95b1766c87dd (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 9100
diff changeset
    98
		_fio.buffer_end = _fio.buffer_start + size;
95b1766c87dd (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 9100
diff changeset
    99
95b1766c87dd (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 9100
diff changeset
   100
		if (size == 0) return 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
	return *_fio.buffer++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
void FioSkipBytes(int n)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
{
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2736
diff changeset
   107
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
		int m = min(_fio.buffer_end - _fio.buffer, n);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
		_fio.buffer += m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
		n -= m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
		if (n == 0) break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
		FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
		n--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   116
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
   117
uint16 FioReadWord()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
	byte b = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
	return (FioReadByte() << 8) | b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
   123
uint32 FioReadDword()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
	uint b = FioReadWord();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
	return (FioReadWord() << 16) | b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
9146
dbe2317185eb (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 9103
diff changeset
   129
void FioReadBlock(void *ptr, size_t size)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
	FioSeekTo(FioGetPos(), SEEK_SET);
9100
3ad0412f8ddf (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 9084
diff changeset
   132
	_fio.pos += fread(ptr, 1, size, _fio.cur_fh);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
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
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
   136
{
309068e65b16 (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   137
	if (_fio.handles[slot] != NULL) {
1109
ecb98f43ba2c (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1093
diff changeset
   138
		fclose(_fio.handles[slot]);
8374
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
   139
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
   140
		free(_fio.shortnames[slot]);
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
   141
		_fio.shortnames[slot] = NULL;
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
   142
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
   143
		_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
   144
#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
   145
		_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
   146
#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
   147
	}
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
   148
}
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
   149
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6201
diff changeset
   150
void FioCloseAll()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
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
   154
	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
   155
		FioCloseFile(i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
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
   158
#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
   159
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
   160
{
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
	/* 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
   162
	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
   163
		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
   164
		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
   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
		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
   167
		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
   168
		/* 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
   169
		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
   170
			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
   171
				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
   172
				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
   173
			}
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
   174
		}
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
   175
		assert(slot != -1);
7352
958170ce0a74 (svn r10715) -Fix: limited-fs code was broken
truelight
parents: 7016
diff changeset
   176
		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
   177
		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
   178
	}
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
   179
}
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
   180
#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
   181
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   182
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
   183
{
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   184
	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
   185
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   186
#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
   187
	FioFreeHandle();
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   188
#endif /* LIMITED_FDS */
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   189
	f = FioFOpenFile(filename);
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9392
diff changeset
   190
	if (f == NULL) usererror("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
   191
	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
   192
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   193
	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
   194
	_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
   195
	_fio.filenames[slot] = filename;
8374
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
   196
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
   197
	/* Store the filename without path and extension */
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
   198
	const char *t = strrchr(filename, PATHSEPCHAR);
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
   199
	_fio.shortnames[slot] = strdup(t == NULL ? filename : t);
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
   200
	char *t2 = strrchr(_fio.shortnames[slot], '.');
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
   201
	if (t2 != NULL) *t2 = '\0';
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
   202
	strtolower(_fio.shortnames[slot]);
7a1b6c89cb89 (svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138
parents: 8273
diff changeset
   203
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   204
#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
   205
	_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
   206
	_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
   207
#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
   208
	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
   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
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   211
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
   212
	"",
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   213
	"save" PATHSEP,
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   214
	"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
   215
	"scenario" PATHSEP,
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   216
	"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
   217
	"gm" PATHSEP,
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   218
	"data" PATHSEP,
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   219
	"lang" PATHSEP
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   220
};
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   221
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   222
const char *_searchpaths[NUM_SEARCHPATHS];
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   223
TarList _tar_list;
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   224
TarFileList _tar_filelist;
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   225
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   226
typedef std::map<std::string, std::string> TarLinkList;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   227
static TarLinkList _tar_linklist; ///< List of directory links
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   228
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   229
/**
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   230
 * 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
   231
 * @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
   232
 * @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
   233
 * @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
   234
 */
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   235
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
   236
{
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   237
	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
   238
	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
   239
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   240
	FioFCloseFile(f);
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   241
	return true;
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   242
}
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   243
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   244
/**
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   245
 * Close a file in a safe way.
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   246
 */
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   247
void FioFCloseFile(FILE *f)
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   248
{
6299
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   249
	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
   250
}
a3757291fd8c (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6298
diff changeset
   251
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   252
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
   253
{
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   254
	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
   255
	assert(sp < NUM_SEARCHPATHS);
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   256
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
   257
	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
   258
	return buf;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   259
}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   260
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   261
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
   262
{
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   263
	Searchpath sp;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   264
	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
   265
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   266
	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
   267
		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
   268
		if (FileExists(buf)) break;
6424
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6323
diff changeset
   269
	}
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   270
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   271
	return buf;
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
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   274
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
   275
{
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   276
	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
   277
	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
   278
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   279
	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
   280
	return buf;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   281
}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   282
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   283
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
   284
{
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   285
	Searchpath sp;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   286
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   287
	/* 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
   288
	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
   289
		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
   290
		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
   291
	}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   292
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   293
	/* 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
   294
	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
   295
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   296
	return buf;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   297
}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   298
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
   299
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
   300
{
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   301
#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
   302
	/* 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
   303
	 * 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
   304
	 * 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
   305
	 * 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
   306
	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
   307
	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
   308
#endif
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   309
	FILE *f = NULL;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   310
	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
   311
6935
0a2a174d3f6a (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 6929
diff changeset
   312
	if (subdir == NO_DIRECTORY) {
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10238
diff changeset
   313
		strecpy(buf, filename, lastof(buf));
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   314
	} else {
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   315
		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
   316
	}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   317
7624
febff7f99ea1 (svn r11155) -Fix [FS#1253]: work around a nasty MS CRT 8 SP1 bug making it virtually impossible to debug 32bpp games under MSVC (http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=257606). Patch by boekabart.
rubidium
parents: 7592
diff changeset
   318
#if defined(WIN32)
7628
b723a5a8065f (svn r11159) -Fix (r11155): restore the ability to save in a non existing file
glx
parents: 7625
diff changeset
   319
	if (mode[0] == 'r' && GetFileAttributes(OTTD2FS(buf)) == INVALID_FILE_ATTRIBUTES) return NULL;
7624
febff7f99ea1 (svn r11155) -Fix [FS#1253]: work around a nasty MS CRT 8 SP1 bug making it virtually impossible to debug 32bpp games under MSVC (http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=257606). Patch by boekabart.
rubidium
parents: 7592
diff changeset
   320
#endif
febff7f99ea1 (svn r11155) -Fix [FS#1253]: work around a nasty MS CRT 8 SP1 bug making it virtually impossible to debug 32bpp games under MSVC (http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=257606). Patch by boekabart.
rubidium
parents: 7592
diff changeset
   321
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   322
	f = fopen(buf, mode);
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   323
#if !defined(WIN32)
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   324
	if (f == NULL) {
9084
55b2d2cb8b90 (svn r12943) -Fix: reading/modifying invalid data under some circumstances.
rubidium
parents: 8374
diff changeset
   325
		strtolower(buf + ((subdir == NO_DIRECTORY) ? 0 : strlen(_searchpaths[sp]) - 1));
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   326
		f = fopen(buf, mode);
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   327
	}
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   328
#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
   329
	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
   330
		/* 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
   331
		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
   332
		*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
   333
		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
   334
	}
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   335
	return f;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   336
}
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   337
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   338
FILE *FioFOpenFileTar(TarFileListEntry *entry, size_t *filesize)
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
   339
{
9702
e69c01e76845 (svn r13810) -Fix: small memory leak when tar files would be found.
rubidium
parents: 9635
diff changeset
   340
	FILE *f = fopen(entry->tar_filename, "rb");
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
   341
	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
   342
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   343
	fseek(f, entry->position, SEEK_SET);
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   344
	if (filesize != NULL) *filesize = entry->size;
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   345
	return f;
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
   346
}
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
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   348
/** Opens OpenTTD files somewhere in a personal or global directory */
7575
3f76c2453685 (svn r11100) -Fix r11099: darn typos...
truelight
parents: 7574
diff changeset
   349
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
   350
{
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   351
	FILE *f = NULL;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   352
	Searchpath sp;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   353
6935
0a2a174d3f6a (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 6929
diff changeset
   354
	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
   355
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   356
	FOR_ALL_SEARCHPATHS(sp) {
7575
3f76c2453685 (svn r11100) -Fix r11099: darn typos...
truelight
parents: 7574
diff changeset
   357
		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
   358
		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
   359
	}
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   360
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
   361
	/* 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
   362
	if (f == NULL && subdir == DATA_DIR && mode[0] == 'r') {
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   363
		static const uint MAX_RESOLVED_LENGTH = 2 * (100 + 100 + 155) + 1; // Enough space to hold two filenames plus link. See 'TarHeader'.
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   364
		char resolved_name[MAX_RESOLVED_LENGTH];
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   365
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   366
		/* Filenames in tars are always forced to be lowercase */
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   367
		strcpy(resolved_name, filename);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   368
		strtolower(resolved_name);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   369
9916
40b0040c754b (svn r14069) -Fix: silence MSVC 64-bits compile warnings.
rubidium
parents: 9702
diff changeset
   370
		size_t resolved_len = strlen(resolved_name);
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   371
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   372
		/* Resolve ONE directory link */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   373
		for (TarLinkList::iterator link = _tar_linklist.begin(); link != _tar_linklist.end(); link++) {
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   374
			const std::string &src = link->first;
9916
40b0040c754b (svn r14069) -Fix: silence MSVC 64-bits compile warnings.
rubidium
parents: 9702
diff changeset
   375
			size_t len = src.length();
9635
b00a9384a924 (svn r13698) -Fix (r13697): MorphOS compilation.
frosch
parents: 9634
diff changeset
   376
			if (resolved_len >= len && resolved_name[len - 1] == PATHSEPCHAR && strncmp(src.c_str(), resolved_name, len) == 0) {
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   377
				/* Apply link */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   378
				char resolved_name2[MAX_RESOLVED_LENGTH];
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   379
				const std::string &dest = link->second;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   380
				strcpy(resolved_name2, &(resolved_name[len]));
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   381
				strcpy(resolved_name, dest.c_str());
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   382
				strcpy(&(resolved_name[dest.length()]), resolved_name2);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   383
				break; // Only resolve one level
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   384
			}
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   385
		}
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   386
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   387
		TarFileList::iterator it = _tar_filelist.find(resolved_name);
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   388
		if (it != _tar_filelist.end()) {
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   389
			f = FioFOpenFileTar(&((*it).second), filesize);
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
   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
	}
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   392
8009
e38f46188aad (svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium
parents: 7927
diff changeset
   393
	/* Sometimes a full path is given. To support
e38f46188aad (svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium
parents: 7927
diff changeset
   394
	 * the 'subdirectory' must be 'removed'. */
e38f46188aad (svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium
parents: 7927
diff changeset
   395
	if (f == NULL && subdir != NO_DIRECTORY) {
e38f46188aad (svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium
parents: 7927
diff changeset
   396
		f = FioFOpenFile(filename, mode, NO_DIRECTORY, filesize);
e38f46188aad (svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium
parents: 7927
diff changeset
   397
	}
e38f46188aad (svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium
parents: 7927
diff changeset
   398
2736
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   399
	return f;
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   400
}
3d6487cbbb69 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   401
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   402
/**
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   403
 * Create a directory with the given name
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   404
 * @param name the new name of the directory
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   405
 */
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   406
void FioCreateDirectory(const char *name)
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   407
{
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   408
#if defined(WIN32) || defined(WINCE)
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   409
	CreateDirectory(OTTD2FS(name), NULL);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   410
#elif defined(OS2) && !defined(__INNOTEK_LIBC__)
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   411
	mkdir(OTTD2FS(name));
7911
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   412
#elif defined(__MORPHOS__) || defined(__AMIGAOS__)
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   413
	char buf[MAX_PATH];
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   414
	ttd_strlcpy(buf, name, MAX_PATH);
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   415
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   416
	size_t len = strlen(name) - 1;
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   417
	if (buf[len] == '/') {
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   418
		buf[len] = '\0'; // Kill pathsep, so mkdir() will not fail
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   419
	}
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   420
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   421
	mkdir(OTTD2FS(buf), 0755);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   422
#else
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   423
	mkdir(OTTD2FS(name), 0755);
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   424
#endif
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   425
}
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
/**
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   428
 * 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
   429
 * It does not add the path separator to zero-sized strings.
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   430
 * @param buf    string to append the separator to
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   431
 * @param buflen the length of the buf
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   432
 */
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   433
void AppendPathSeparator(char *buf, size_t buflen)
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   434
{
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   435
	size_t s = strlen(buf);
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
	/* Length of string + path separator + '\0' */
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   438
	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
   439
		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
   440
		buf[s + 1] = '\0';
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   441
	}
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   442
}
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   443
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   444
/**
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   445
 * 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
   446
 * 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
   447
 * @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
   448
 * @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
   449
 */
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   450
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
   451
{
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   452
	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
   453
	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
   454
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   455
	/* 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
   456
	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
   457
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   458
	/* 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
   459
	if (s == NULL || dest != s) {
9100
3ad0412f8ddf (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 9084
diff changeset
   460
		if (getcwd(dest, MAX_PATH) == NULL) *dest = '\0';
6834
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   461
		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
   462
		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
   463
	}
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   464
	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
   465
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   466
	return dest;
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   467
}
7a64c73d5f45 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 6755
diff changeset
   468
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   469
/**
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   470
 * Simplify filenames from tars.
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   471
 * Replace '/' by PATHSEPCHAR, and force 'name' to lowercase.
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   472
 * @param name Filename to process.
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   473
 */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   474
static void SimplifyFileName(char *name)
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   475
{
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   476
	/* Force lowercase */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   477
	strtolower(name);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   478
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   479
	/* Tar-files always have '/' path-seperator, but we want our PATHSEPCHAR */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   480
#if (PATHSEPCHAR != '/')
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   481
	for (char *n = name; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   482
#endif
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   483
}
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   484
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
   485
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
   486
{
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   487
	/* The TAR-header, repeated for every file */
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   488
	typedef struct TarHeader {
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   489
		char name[100];      ///< Name of the file
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   490
		char mode[8];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   491
		char uid[8];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   492
		char gid[8];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   493
		char size[12];       ///< Size of the file, in ASCII
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   494
		char mtime[12];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   495
		char chksum[8];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   496
		char typeflag;
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   497
		char linkname[100];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   498
		char magic[6];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   499
		char version[2];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   500
		char uname[32];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   501
		char gname[32];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   502
		char devmajor[8];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   503
		char devminor[8];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   504
		char prefix[155];    ///< Path of the file
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   505
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   506
		char unused[12];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   507
	} TarHeader;
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   508
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   509
	/* Check if we already seen this file */
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   510
	TarList::iterator it = _tar_list.find(filename);
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   511
	if (it != _tar_list.end()) return false;
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   512
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   513
	FILE *f = fopen(filename, "rb");
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   514
	assert(f != NULL);
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   515
9702
e69c01e76845 (svn r13810) -Fix: small memory leak when tar files would be found.
rubidium
parents: 9635
diff changeset
   516
	const char *dupped_filename = strdup(filename);
e69c01e76845 (svn r13810) -Fix: small memory leak when tar files would be found.
rubidium
parents: 9635
diff changeset
   517
	_tar_list[filename].filename = dupped_filename;
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   518
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   519
	TarLinkList links; ///< Temporary list to collect links
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   520
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   521
	TarHeader th;
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   522
	char buf[sizeof(th.name) + 1], *end;
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   523
	char name[sizeof(th.prefix) + 1 + sizeof(th.name) + 1];
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   524
	char link[sizeof(th.linkname) + 1];
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   525
	char dest[sizeof(th.prefix) + 1 + sizeof(th.name) + 1 + 1 + sizeof(th.linkname) + 1];
9146
dbe2317185eb (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 9103
diff changeset
   526
	size_t num = 0, pos = 0;
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   527
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   528
	/* Make a char of 512 empty bytes */
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   529
	char empty[512];
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   530
	memset(&empty[0], 0, sizeof(empty));
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   531
9630
4336aabc35ce (svn r13693) -Fix (r11106, r11117): Do not rely on .tar files always ending with a block of zeros.
frosch
parents: 9581
diff changeset
   532
	for (;;) { // Note: feof() always returns 'false' after 'fseek()'. Cool, isn't it?
4336aabc35ce (svn r13693) -Fix (r11106, r11117): Do not rely on .tar files always ending with a block of zeros.
frosch
parents: 9581
diff changeset
   533
		size_t num_bytes_read = fread(&th, 1, 512, f);
4336aabc35ce (svn r13693) -Fix (r11106, r11117): Do not rely on .tar files always ending with a block of zeros.
frosch
parents: 9581
diff changeset
   534
		if (num_bytes_read != 512) break;
4336aabc35ce (svn r13693) -Fix (r11106, r11117): Do not rely on .tar files always ending with a block of zeros.
frosch
parents: 9581
diff changeset
   535
		pos += num_bytes_read;
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   536
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   537
		/* Check if we have the new tar-format (ustar) or the old one (a lot of zeros after 'link' field) */
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   538
		if (strncmp(th.magic, "ustar", 5) != 0 && memcmp(&th.magic, &empty[0], 512 - offsetof(TarHeader, magic)) != 0) {
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   539
			/* If we have only zeros in the block, it can be an end-of-file indicator */
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   540
			if (memcmp(&th, &empty[0], 512) == 0) continue;
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   541
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   542
			DEBUG(misc, 0, "The file '%s' isn't a valid tar-file", filename);
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   543
			return false;
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   544
		}
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   545
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   546
		name[0] = '\0';
9146
dbe2317185eb (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 9103
diff changeset
   547
		size_t len = 0;
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   548
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   549
		/* The prefix contains the directory-name */
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   550
		if (th.prefix[0] != '\0') {
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   551
			memcpy(name, th.prefix, sizeof(th.prefix));
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   552
			name[sizeof(th.prefix)] = '\0';
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   553
			len = strlen(name);
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   554
			name[len] = PATHSEPCHAR;
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   555
			len++;
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   556
		}
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   557
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   558
		/* Copy the name of the file in a safe way at the end of 'name' */
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   559
		memcpy(&name[len], th.name, sizeof(th.name));
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   560
		name[len + sizeof(th.name)] = '\0';
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   561
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   562
		/* Calculate the size of the file.. for some strange reason this is stored as a string */
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   563
		memcpy(buf, th.size, sizeof(th.size));
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   564
		buf[sizeof(th.size)] = '\0';
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   565
		int skip = strtol(buf, &end, 8);
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   566
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   567
		switch (th.typeflag) {
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   568
			case '\0':
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   569
			case '0': { // regular file
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   570
				/* Ignore empty files */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   571
				if (skip == 0) break;
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   572
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   573
				if (strlen(name) == 0) break;
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   574
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   575
				/* Store this entry in the list */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   576
				TarFileListEntry entry;
9702
e69c01e76845 (svn r13810) -Fix: small memory leak when tar files would be found.
rubidium
parents: 9635
diff changeset
   577
				entry.tar_filename = dupped_filename;
e69c01e76845 (svn r13810) -Fix: small memory leak when tar files would be found.
rubidium
parents: 9635
diff changeset
   578
				entry.size         = skip;
e69c01e76845 (svn r13810) -Fix: small memory leak when tar files would be found.
rubidium
parents: 9635
diff changeset
   579
				entry.position     = pos;
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   580
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   581
				/* Convert to lowercase and our PATHSEPCHAR */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   582
				SimplifyFileName(name);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   583
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   584
				DEBUG(misc, 6, "Found file in tar: %s (%d bytes, %d offset)", name, skip, pos);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   585
				if (_tar_filelist.insert(TarFileList::value_type(name, entry)).second) num++;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   586
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   587
				break;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   588
			}
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   589
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   590
			case '1': // hard links
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   591
			case '2': { // symbolic links
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   592
				/* Copy the destination of the link in a safe way at the end of 'linkname' */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   593
				memcpy(link, th.linkname, sizeof(th.linkname));
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   594
				link[sizeof(th.linkname)] = '\0';
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   595
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   596
				if (strlen(name) == 0 || strlen(link) == 0) break;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   597
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   598
				/* Convert to lowercase and our PATHSEPCHAR */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   599
				SimplifyFileName(name);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   600
				SimplifyFileName(link);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   601
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   602
				/* Only allow relative links */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   603
				if (link[0] == PATHSEPCHAR) {
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   604
					DEBUG(misc, 1, "Ignoring absolute link in tar: %s -> %s", name, link);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   605
					break;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   606
				}
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   607
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   608
				/* Process relative path.
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   609
				 * Note: The destination of links must not contain any directory-links. */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   610
				strcpy(dest, name);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   611
				char *destpos = strrchr(dest, PATHSEPCHAR);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   612
				if (destpos == NULL) destpos = dest;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   613
				*destpos = '\0';
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   614
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   615
				char *pos = link;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   616
				while (*pos != '\0') {
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   617
					char *next = strchr(link, PATHSEPCHAR);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   618
					if (next == NULL) next = pos + strlen(pos);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   619
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   620
					/* Skip '.' (current dir) */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   621
					if (next != pos + 1 || pos[0] != '.') {
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   622
						if (next == pos + 2 && pos[0] == '.' && pos[1] == '.') {
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   623
							/* level up */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   624
							if (dest[0] == '\0') {
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   625
								DEBUG(misc, 1, "Ignoring link pointing outside of data directory: %s -> %s", name, link);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   626
								break;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   627
							}
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   628
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   629
							/* Truncate 'dest' after last PATHSEPCHAR.
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   630
							 * This assumes, that the truncated part is a real directory and not a link */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   631
							destpos = strrchr(dest, PATHSEPCHAR);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   632
							if (destpos == NULL) destpos = dest;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   633
						} else {
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   634
							/* Append at end of 'dest' */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   635
							if (destpos != dest) *(destpos++) = PATHSEPCHAR;
10201
4c181fbe8f2a (svn r14414) -Fix: replace instances of strncpy with strecpy as strncpy doesn't guarantee the resulting string is '\0'-terminated.
rubidium
parents: 10039
diff changeset
   636
							strncpy(destpos, pos, next - pos); // Safe as we do '\0'-termination ourselves
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   637
							destpos += next - pos;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   638
						}
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   639
						*destpos = '\0';
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   640
					}
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   641
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   642
					pos = next;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   643
				}
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   644
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   645
				/* Store links in temporary list */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   646
				DEBUG(misc, 6, "Found link in tar: %s -> %s", name, dest);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   647
				links.insert(TarLinkList::value_type(name, dest));
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   648
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   649
				break;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   650
			}
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   651
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   652
			default:
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   653
				/* Ignore other types */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   654
				break;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   655
		}
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   656
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   657
		/* Skip to the next block.. */
7927
3a3289a049f9 (svn r11480) -Codechange: Rename the function ALIGN fitting to the naming style
skidd13
parents: 7911
diff changeset
   658
		skip = Align(skip, 512);
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   659
		fseek(f, skip, SEEK_CUR);
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   660
		pos += skip;
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
   661
	}
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
   662
7592
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   663
	DEBUG(misc, 1, "Found tar '%s' with %d new files", filename, num);
792daecf7c77 (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 7590
diff changeset
   664
	fclose(f);
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
   665
9634
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   666
	/* Resolve file links and store directory links.
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   667
	 * We restrict usage of links to two cases:
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   668
	 *  1) Links to directories:
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   669
	 *      Both the source path and the destination path must NOT contain any further links.
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   670
	 *      When resolving files at most one directory link is resolved.
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   671
	 *  2) Links to files:
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   672
	 *      The destination path must NOT contain any links.
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   673
	 *      The source path may contain one directory link.
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   674
	 */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   675
	for (TarLinkList::iterator link = links.begin(); link != links.end(); link++) {
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   676
		const std::string &src = link->first;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   677
		const std::string &dest = link->second;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   678
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   679
		TarFileList::iterator dest_file = _tar_filelist.find(dest);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   680
		if (dest_file != _tar_filelist.end()) {
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   681
			/* Link to file. Process the link like the destination file. */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   682
			_tar_filelist.insert(TarFileList::value_type(src, dest_file->second));
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   683
		} else {
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   684
			/* Destination file not found. Assume 'link to directory' */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   685
			/* Append PATHSEPCHAR to 'src' and 'dest' */
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   686
			const std::string src_path = src + PATHSEPCHAR;
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   687
			const std::string dst_path = (dest.length() == 0 ? "" : dest + PATHSEPCHAR);
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   688
			_tar_linklist.insert(TarLinkList::value_type(src_path, dst_path));
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   689
		}
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   690
	}
af4f8efdf70e (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 9630
diff changeset
   691
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
   692
	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
   693
}
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
   694
9146
dbe2317185eb (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 9103
diff changeset
   695
static int ScanPathForTarFiles(const char *path, size_t basepath_length)
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
   696
{
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
   697
	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
   698
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
   699
	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
   700
	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
   701
	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
   702
	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
   703
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
   704
	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
   705
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
   706
	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
   707
		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
   708
		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
   709
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
   710
		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
   711
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
   712
		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
   713
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
   714
		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
   715
			/* 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
   716
			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
   717
			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
   718
			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
   719
		} 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
   720
			/* 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
   721
			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
   722
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
   723
			/* 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
   724
			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
   725
			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
   726
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
   727
			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
   728
		}
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
   729
	}
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
   730
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
   731
	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
   732
	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
   733
}
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
   734
7589
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   735
void ScanForTarFiles()
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
   736
{
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
   737
	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
   738
	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
   739
	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
   740
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
   741
	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
   742
	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
   743
		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
   744
		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
   745
	}
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
   746
	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
   747
}
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
   748
7589
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   749
#if defined(WIN32) || defined(WINCE)
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   750
/**
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   751
 * Determine the base (personal dir and game data dir) paths
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   752
 * @param exe the path from the current path to the executable
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   753
 * @note defined in the OS related files (os2.cpp, win32.cpp, unix.cpp etc)
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   754
 */
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   755
extern void DetermineBasePaths(const char *exe);
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   756
#else /* defined(WIN32) || defined(WINCE) */
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   757
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   758
/**
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   759
 * Changes the working directory to the path of the give executable.
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   760
 * For OSX application bundles '.app' is the required extension of the bundle,
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   761
 * so when we crop the path to there, when can remove the name of the bundle
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   762
 * in the same way we remove the name from the executable name.
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   763
 * @param exe the path to the executable
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   764
 */
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   765
void ChangeWorkingDirectory(const char *exe)
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   766
{
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   767
#ifdef WITH_COCOA
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   768
	char *app_bundle = strchr(exe, '.');
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   769
	while (app_bundle != NULL && strncasecmp(app_bundle, ".app", 4) != 0) app_bundle = strchr(&app_bundle[1], '.');
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   770
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   771
	if (app_bundle != NULL) app_bundle[0] = '\0';
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   772
#endif /* WITH_COCOA */
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   773
	char *s = strrchr(exe, PATHSEPCHAR);
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   774
	if (s != NULL) {
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   775
		*s = '\0';
10385
4f9838649c7f (svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents: 10384
diff changeset
   776
#if defined(__DJGPP__)
4f9838649c7f (svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents: 10384
diff changeset
   777
		/* If we want to go to the root, we can't use cd C:, but we must use '/' */
4f9838649c7f (svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents: 10384
diff changeset
   778
		if (s[-1] == ':') chdir("/");
4f9838649c7f (svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents: 10384
diff changeset
   779
#endif
9100
3ad0412f8ddf (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 9084
diff changeset
   780
		if (chdir(exe) != 0) DEBUG(misc, 0, "Directory with the binary does not exist?");
7589
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   781
		*s = PATHSEPCHAR;
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   782
	}
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   783
#ifdef WITH_COCOA
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   784
	if (app_bundle != NULL) app_bundle[0] = '.';
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   785
#endif /* WITH_COCOA */
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   786
}
f989c4d11269 (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 7584
diff changeset
   787
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   788
/**
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   789
 * 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
   790
 * @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
   791
 */
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   792
void DetermineBasePaths(const char *exe)
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   793
{
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   794
	char tmp[MAX_PATH];
10385
4f9838649c7f (svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents: 10384
diff changeset
   795
#if defined(__MORPHOS__) || defined(__AMIGA__) || defined(DOS) || !defined(WITH_PERSONAL_DIR)
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
   796
	_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
   797
#else
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   798
	const char *homedir = getenv("HOME");
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   799
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   800
	if (homedir == NULL) {
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   801
		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
   802
		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
   803
	}
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   804
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   805
	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
   806
	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
   807
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   808
	_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
   809
#endif
8273
1d95cbda404b (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8214
diff changeset
   810
1d95cbda404b (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8214
diff changeset
   811
#if defined(WITH_SHARED_DIR)
1d95cbda404b (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8214
diff changeset
   812
	snprintf(tmp, MAX_PATH, "%s", SHARED_DIR);
1d95cbda404b (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8214
diff changeset
   813
	AppendPathSeparator(tmp, MAX_PATH);
1d95cbda404b (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8214
diff changeset
   814
	_searchpaths[SP_SHARED_DIR] = strdup(tmp);
1d95cbda404b (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8214
diff changeset
   815
#else
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   816
	_searchpaths[SP_SHARED_DIR] = NULL;
8273
1d95cbda404b (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8214
diff changeset
   817
#endif
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   818
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
   819
#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
   820
	_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
   821
#else
9100
3ad0412f8ddf (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 9084
diff changeset
   822
	if (getcwd(tmp, MAX_PATH) == NULL) *tmp = '\0';
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   823
	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
   824
	_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
   825
#endif
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   826
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   827
	/* 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
   828
	ChangeWorkingDirectory((char*)exe);
9100
3ad0412f8ddf (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 9084
diff changeset
   829
	if (getcwd(tmp, MAX_PATH) == NULL) *tmp = '\0';
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   830
	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
   831
	_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
   832
10385
4f9838649c7f (svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium
parents: 10384
diff changeset
   833
#if defined(__MORPHOS__) || defined(__AMIGA__) || defined(DOS)
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
   834
	_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
   835
#else
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   836
	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
   837
	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
   838
	_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
   839
#endif
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   840
#ifdef WITH_COCOA
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   841
extern void cocoaSetApplicationBundleDir();
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   842
	cocoaSetApplicationBundleDir();
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   843
#else
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   844
	_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
   845
#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
   846
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
   847
	ScanForTarFiles();
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   848
}
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   849
#endif /* defined(WIN32) || defined(WINCE) */
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   850
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   851
char *_personal_dir;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   852
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   853
/**
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   854
 * Acquire the base paths (personal dir and game data dir),
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   855
 * fill all other paths (save dir, autosave dir etc) and
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   856
 * 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
   857
 * @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
   858
 */
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   859
void DeterminePaths(const char *exe)
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   860
{
6317
70f4e9e52eb1 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6299
diff changeset
   861
	DetermineBasePaths(exe);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   862
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   863
	Searchpath sp;
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   864
	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
   865
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
   866
	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
   867
		_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
   868
		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
   869
		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
   870
			_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
   871
		} 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
   872
			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
   873
		}
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
   874
	} 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
   875
		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
   876
		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
   877
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
   878
		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
   879
			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
   880
			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
   881
			_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
   882
			_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
   883
		} 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
   884
			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
   885
					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
   886
				};
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
   887
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
   888
			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
   889
				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
   890
					_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
   891
					_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
   892
					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
   893
				}
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
   894
			}
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
   895
		}
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   896
	}
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   897
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
   898
	DEBUG(misc, 3, "%s found as personal directory", _personal_dir);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   899
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   900
	_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
   901
	_log_file = str_fmt("%sopenttd.log",  _personal_dir);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   902
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   903
	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
   904
	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
   905
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   906
	/* Make the necessary folders */
7911
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   907
#if !defined(__MORPHOS__) && !defined(__AMIGA__) && defined(WITH_PERSONAL_DIR)
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   908
	FioCreateDirectory(_personal_dir);
7911
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   909
#endif
48610042ac18 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 7805
diff changeset
   910
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   911
	FioCreateDirectory(save_dir);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   912
	FioCreateDirectory(autosave_dir);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   913
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   914
	free(save_dir);
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6896
diff changeset
   915
	free(autosave_dir);
6298
01c80746f308 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6248
diff changeset
   916
}
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
   917
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
   918
/**
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
   919
 * 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
   920
 * @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
   921
 */
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
   922
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
   923
{
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
   924
	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
   925
		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
   926
			/* 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
   927
			 * 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
   928
			case ':': case '\\': case '*': case '?': case '/':
7016
65ca64810a94 (svn r10272) -Fix (FS#916): remove more invalid characters from savegame names
glx
parents: 6990
diff changeset
   929
			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
   930
				*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
   931
				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
   932
		}
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
   933
	}
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
   934
}
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   935
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   936
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize)
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   937
{
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   938
	FILE *in;
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   939
	byte *mem;
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   940
	size_t len;
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   941
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   942
	in = fopen(filename, "rb");
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   943
	if (in == NULL) return NULL;
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   944
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   945
	fseek(in, 0, SEEK_END);
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   946
	len = ftell(in);
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   947
	fseek(in, 0, SEEK_SET);
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   948
	if (len > maxsize || (mem = MallocT<byte>(len + 1)) == NULL) {
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   949
		fclose(in);
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   950
		return NULL;
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   951
	}
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   952
	mem[len] = 0;
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   953
	if (fread(mem, len, 1, in) != 1) {
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   954
		fclose(in);
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   955
		free(mem);
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   956
		return NULL;
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   957
	}
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   958
	fclose(in);
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   959
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   960
	*lenp = len;
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   961
	return mem;
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
   962
}
10036
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   963
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   964
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   965
/**
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   966
 * Scan a single directory (and recursively it's children) and add
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   967
 * any graphics sets that are found.
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   968
 * @param extension       the extension of files to search for.
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   969
 * @param path            full path we're currently at
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   970
 * @param basepath_length from where in the path are we 'based' on the search path
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   971
 */
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   972
static uint ScanPath(FileScanner *fs, const char *extension, const char *path, size_t basepath_length)
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   973
{
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   974
	extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   975
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   976
	uint num = 0;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   977
	struct stat sb;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   978
	struct dirent *dirent;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   979
	DIR *dir;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   980
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   981
	if (path == NULL || (dir = ttd_opendir(path)) == NULL) return 0;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   982
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   983
	while ((dirent = readdir(dir)) != NULL) {
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   984
		const char *d_name = FS2OTTD(dirent->d_name);
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   985
		char filename[MAX_PATH];
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   986
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   987
		if (!FiosIsValidFile(path, dirent, &sb)) continue;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   988
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   989
		snprintf(filename, lengthof(filename), "%s%s", path, d_name);
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   990
10384
6b408b83ac34 (svn r14635) -Change: use S_ISDIR/S_ISREG as x & S_IFREG always results false on platforms when S_IFREG is 0x0000.
rubidium
parents: 10310
diff changeset
   991
		if (S_ISDIR(sb.st_mode)) {
10036
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   992
			/* Directory */
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   993
			if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   994
			AppendPathSeparator(filename, lengthof(filename));
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   995
			num += ScanPath(fs, extension, filename, basepath_length);
10384
6b408b83ac34 (svn r14635) -Change: use S_ISDIR/S_ISREG as x & S_IFREG always results false on platforms when S_IFREG is 0x0000.
rubidium
parents: 10310
diff changeset
   996
		} else if (S_ISREG(sb.st_mode)) {
10036
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   997
			/* File */
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   998
			char *ext = strrchr(filename, '.');
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
   999
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1000
			/* If no extension or extension isn't .grf, skip the file */
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1001
			if (ext == NULL) continue;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1002
			if (strcasecmp(ext, extension) != 0) continue;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1003
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1004
			if (fs->AddFile(filename, basepath_length)) num++;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1005
		}
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1006
	}
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1007
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1008
	closedir(dir);
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1009
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1010
	return num;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1011
}
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1012
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1013
/**
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1014
 * Scan the given tar and add graphics sets when it finds one.
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1015
 * @param extension the extension of files to search for.
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1016
 * @param tar       the tar to search in.
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1017
 */
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1018
static uint ScanTar(FileScanner *fs, const char *extension, TarFileList::iterator tar)
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1019
{
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1020
	uint num = 0;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1021
	const char *filename = (*tar).first.c_str();
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1022
	const char *ext = strrchr(filename, '.');
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1023
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1024
	/* If no extension or extension isn't .grf, skip the file */
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1025
	if (ext == NULL) return false;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1026
	if (strcasecmp(ext, extension) != 0) return false;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1027
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1028
	if (fs->AddFile(filename, 0)) num++;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1029
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1030
	return num;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1031
}
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1032
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1033
/**
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1034
 * Scan for files with the given extention in the given search path.
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1035
 * @param extension the extension of files to search for.
10238
5076964d81a1 (svn r14466) -Doc: remove some obsolete parameters, fix a few parameter names in comments and add a little more doxygen documentation.
rubidium
parents: 10201
diff changeset
  1036
 * @param sd        the sub directory to search in.
10036
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1037
 * @param tars      whether to search in the tars too.
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1038
 * @return the number of found files, i.e. the number of times that
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1039
 *         AddFile returned true.
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1040
 */
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1041
uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars)
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1042
{
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1043
	Searchpath sp;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1044
	char path[MAX_PATH];
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1045
	TarFileList::iterator tar;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1046
	uint num = 0;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1047
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1048
	FOR_ALL_SEARCHPATHS(sp) {
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1049
		FioAppendDirectory(path, MAX_PATH, sp, sd);
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1050
		num += ScanPath(this, extension, path, strlen(path));
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1051
	}
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1052
	FOR_ALL_TARS(tar) {
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1053
		num += ScanTar(this, extension, tar);
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1054
	}
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1055
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1056
	return num;
5c28389c3eeb (svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium
parents: 9916
diff changeset
  1057
}