src/fileio.cpp
author Tero Marttila <terom@fixme.fi>
Tue, 22 Jul 2008 21:51:14 +0300
changeset 11180 982e9f814f97
parent 11179 fa96e29d7187
permissions -rw-r--r--
scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6527
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6505
diff changeset
     3
/** @file fileio.cpp Standard In/Out file operations */
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6218
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1198
diff changeset
     6
#include "openttd.h"
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1039
diff changeset
     7
#include "fileio.h"
2153
91e89aa8c299 (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"
6218
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
     9
#include "debug.h"
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
    10
#include "fios.h"
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8609
diff changeset
    11
#include "core/alloc_func.hpp"
8627
448ebf3a8291 (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: 8626
diff changeset
    12
#include "core/math_func.hpp"
8710
52015340050c (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8627
diff changeset
    13
#include "string_func.h"
11061
f5806d84e7a9 (svn r13618) -Codechange: move tar-specific declarations to separate file
smatz
parents: 10839
diff changeset
    14
#include "tar_type.h"
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    15
#ifdef WIN32
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    16
#include <windows.h>
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
    17
#else
6649
5598de323f01 (svn r9281) -Fix [FS#687]: compile failure when compiling with 'home directories' enabled (stepancheg).
rubidium
parents: 6646
diff changeset
    18
#include <pwd.h>
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
    19
#include <unistd.h>
8085
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
    20
#endif
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
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
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
    29
struct Fio {
8301
47e8f031a6ab (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 8124
diff changeset
    30
	byte *buffer, *buffer_end;             ///< position pointer in local buffer and last valid byte of buffer
10465
0c68afe3d725 (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 10421
diff changeset
    31
	size_t pos;                            ///< current (system) position in file
8301
47e8f031a6ab (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 8124
diff changeset
    32
	FILE *cur_fh;                          ///< current file handle
47e8f031a6ab (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 8124
diff changeset
    33
	const char *filename;                  ///< current filename
47e8f031a6ab (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 8124
diff changeset
    34
	FILE *handles[MAX_FILE_SLOTS];         ///< array of file handles we can have open
47e8f031a6ab (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 8124
diff changeset
    35
	byte buffer_start[FIO_BUFFER_SIZE];    ///< local buffer when read from file
47e8f031a6ab (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 8124
diff changeset
    36
	const char *filenames[MAX_FILE_SLOTS]; ///< array of filenames we (should) have open
8870
461ed7760525 (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: 8769
diff changeset
    37
	char *shortnames[MAX_FILE_SLOTS];///< array of short names for spriteloader's use
6218
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    38
#if defined(LIMITED_FDS)
8301
47e8f031a6ab (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 8124
diff changeset
    39
	uint open_handles;                     ///< current amount of open handles
47e8f031a6ab (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 8124
diff changeset
    40
	uint usage_count[MAX_FILE_SLOTS];      ///< count how many times this file has been opened
6218
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    41
#endif /* LIMITED_FDS */
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
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
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6218
diff changeset
    46
/* Get current position in file */
10751
ebd94f2d6385 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 10465
diff changeset
    47
size_t FioGetPos()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
{
10421
1daeaead8993 (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 10418
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
8870
461ed7760525 (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: 8769
diff changeset
    52
const char *FioGetFilename(uint8 slot)
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 7371
diff changeset
    53
{
8870
461ed7760525 (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: 8769
diff changeset
    54
	return _fio.shortnames[slot];
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 7371
diff changeset
    55
}
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 7371
diff changeset
    56
10751
ebd94f2d6385 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 10465
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
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    61
	_fio.pos = pos;
790879e123dd (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
6218
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    65
#if defined(LIMITED_FDS)
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    66
static void FioRestoreFile(int slot)
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    67
{
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    68
	/* Do we still have the file open, or should we reopen it? */
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    69
	if (_fio.handles[slot] == NULL) {
7848
31b4a92bc171 (svn r10715) -Fix: limited-fs code was broken
truelight
parents: 7512
diff changeset
    70
		DEBUG(misc, 6, "Restoring file '%s' in slot '%d' from disk", _fio.filenames[slot], slot);
31b4a92bc171 (svn r10715) -Fix: limited-fs code was broken
truelight
parents: 7512
diff changeset
    71
		FioOpenFile(slot, _fio.filenames[slot]);
6218
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    72
	}
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    73
	_fio.usage_count[slot]++;
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    74
}
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    75
#endif /* LIMITED_FDS */
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    76
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6218
diff changeset
    77
/* Seek to a file and a position */
10753
428f22725618 (svn r13303) -Fix (r13301): 64bit gcc compilation was broken
smatz
parents: 10751
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
{
6218
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    80
	FILE *f;
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    81
#if defined(LIMITED_FDS)
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    82
	/* Make sure we have this file open */
8066
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
    83
	FioRestoreFile(slot);
6218
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    84
#endif /* LIMITED_FDS */
8066
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
    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;
8066
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
    88
	_fio.filename = _fio.filenames[slot];
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
    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
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
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) {
10418
3ac4a89469df (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 10401
diff changeset
    95
		_fio.buffer = _fio.buffer_start;
10421
1daeaead8993 (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 10418
diff changeset
    96
		size_t size = fread(_fio.buffer, 1, FIO_BUFFER_SIZE, _fio.cur_fh);
1daeaead8993 (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 10418
diff changeset
    97
		_fio.pos += size;
1daeaead8993 (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 10418
diff changeset
    98
		_fio.buffer_end = _fio.buffer_start + size;
1daeaead8993 (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 10418
diff changeset
    99
1daeaead8993 (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 10418
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
6a26eeda9679 (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
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
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
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
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
10465
0c68afe3d725 (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 10421
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);
10418
3ac4a89469df (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 10401
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
8736f759759b (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   135
static inline void FioCloseFile(int slot)
8736f759759b (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   136
{
8736f759759b (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   137
	if (_fio.handles[slot] != NULL) {
1109
1bab892228cd (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1093
diff changeset
   138
		fclose(_fio.handles[slot]);
8870
461ed7760525 (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: 8769
diff changeset
   139
461ed7760525 (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: 8769
diff changeset
   140
		free(_fio.shortnames[slot]);
461ed7760525 (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: 8769
diff changeset
   141
		_fio.shortnames[slot] = NULL;
461ed7760525 (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: 8769
diff changeset
   142
1039
8736f759759b (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   143
		_fio.handles[slot] = NULL;
6218
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   144
#if defined(LIMITED_FDS)
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   145
		_fio.open_handles--;
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   146
#endif /* LIMITED_FDS */
1039
8736f759759b (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   147
	}
8736f759759b (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   148
}
8736f759759b (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   149
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6527
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
8736f759759b (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   154
	for (i = 0; i != lengthof(_fio.handles); i++)
8736f759759b (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   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
6218
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   158
#if defined(LIMITED_FDS)
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   159
static void FioFreeHandle()
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   160
{
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   161
	/* If we are about to open a file that will exceed the limit, close a file */
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   162
	if (_fio.open_handles + 1 == LIMITED_FDS) {
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   163
		uint i, count;
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   164
		int slot;
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   165
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   166
		count = UINT_MAX;
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   167
		slot = -1;
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   168
		/* Find the file that is used the least */
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   169
		for (i = 0; i < lengthof(_fio.handles); i++) {
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   170
			if (_fio.handles[i] != NULL && _fio.usage_count[i] < count) {
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   171
				count = _fio.usage_count[i];
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   172
				slot  = i;
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   173
			}
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   174
		}
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   175
		assert(slot != -1);
7848
31b4a92bc171 (svn r10715) -Fix: limited-fs code was broken
truelight
parents: 7512
diff changeset
   176
		DEBUG(misc, 6, "Closing filehandler '%s' in slot '%d' because of fd-limit", _fio.filenames[slot], slot);
6218
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   177
		FioCloseFile(slot);
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   178
	}
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   179
}
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   180
#endif /* LIMITED_FDS */
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   181
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   182
void FioOpenFile(int slot, const char *filename)
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   183
{
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   184
	FILE *f;
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   185
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   186
#if defined(LIMITED_FDS)
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   187
	FioFreeHandle();
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   188
#endif /* LIMITED_FDS */
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   189
	f = FioFOpenFile(filename);
10839
a62547c31fdb (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: 10753
diff changeset
   190
	if (f == NULL) usererror("Cannot open file '%s'", filename);
8066
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
   191
	uint32 pos = ftell(f);
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   192
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   193
	FioCloseFile(slot); // if file was opened before, close it
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   194
	_fio.handles[slot] = f;
7392
a716551b0c7f (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight
parents: 7371
diff changeset
   195
	_fio.filenames[slot] = filename;
8870
461ed7760525 (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: 8769
diff changeset
   196
461ed7760525 (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: 8769
diff changeset
   197
	/* Store the filename without path and extension */
461ed7760525 (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: 8769
diff changeset
   198
	const char *t = strrchr(filename, PATHSEPCHAR);
461ed7760525 (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: 8769
diff changeset
   199
	_fio.shortnames[slot] = strdup(t == NULL ? filename : t);
461ed7760525 (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: 8769
diff changeset
   200
	char *t2 = strrchr(_fio.shortnames[slot], '.');
461ed7760525 (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: 8769
diff changeset
   201
	if (t2 != NULL) *t2 = '\0';
461ed7760525 (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: 8769
diff changeset
   202
	strtolower(_fio.shortnames[slot]);
461ed7760525 (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: 8769
diff changeset
   203
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   204
#if defined(LIMITED_FDS)
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   205
	_fio.usage_count[slot] = 0;
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   206
	_fio.open_handles++;
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   207
#endif /* LIMITED_FDS */
8066
7acd480e05c9 (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 7848
diff changeset
   208
	FioSeekToFile(slot, pos);
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   209
}
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   210
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   211
const char *_subdirs[NUM_SUBDIRS] = {
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   212
	"",
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   213
	"save" PATHSEP,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   214
	"save" PATHSEP "autosave" PATHSEP,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   215
	"scenario" PATHSEP,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   216
	"scenario" PATHSEP "heightmap" PATHSEP,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   217
	"gm" PATHSEP,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   218
	"data" PATHSEP,
11179
fa96e29d7187 error messages, typos/omissions, and it can now actually download the .tar into cache/<GRFID>-<MD5SUM>.tar
Tero Marttila <terom@fixme.fi>
parents: 11140
diff changeset
   219
	"lang" PATHSEP,
fa96e29d7187 error messages, typos/omissions, and it can now actually download the .tar into cache/<GRFID>-<MD5SUM>.tar
Tero Marttila <terom@fixme.fi>
parents: 11140
diff changeset
   220
    "cache" PATHSEP
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   221
};
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   222
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   223
const char *_searchpaths[NUM_SEARCHPATHS];
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   224
TarList _tar_list;
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   225
TarFileList _tar_filelist;
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   226
11139
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   227
typedef std::map<std::string, std::string> TarLinkList;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   228
static TarLinkList _tar_linklist; ///< List of directory links
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   229
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   230
/**
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   231
 * Check whether the given file exists
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   232
 * @param filename the file to try for existance
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   233
 * @param subdir the subdirectory to look in
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   234
 * @return true if and only if the file can be opened
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   235
 */
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   236
bool FioCheckFileExists(const char *filename, Subdirectory subdir)
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   237
{
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   238
	FILE *f = FioFOpenFile(filename, "rb", subdir);
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   239
	if (f == NULL) return false;
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   240
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   241
	FioFCloseFile(f);
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   242
	return true;
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   243
}
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   244
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   245
/**
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   246
 * Close a file in a safe way.
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   247
 */
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   248
void FioFCloseFile(FILE *f)
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   249
{
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   250
	fclose(f);
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   251
}
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   252
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   253
char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename)
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   254
{
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   255
	assert(subdir < NUM_SUBDIRS);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   256
	assert(sp < NUM_SEARCHPATHS);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   257
7437
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   258
	snprintf(buf, buflen, "%s%s%s", _searchpaths[sp], _subdirs[subdir], filename);
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   259
	return buf;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   260
}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   261
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   262
char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   263
{
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   264
	Searchpath sp;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   265
	assert(subdir < NUM_SUBDIRS);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   266
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   267
	FOR_ALL_SEARCHPATHS(sp) {
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   268
		FioGetFullPath(buf, buflen, sp, subdir, filename);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   269
		if (FileExists(buf)) break;
6920
ee15b916f758 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6649
diff changeset
   270
	}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   271
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   272
	return buf;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   273
}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   274
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   275
char *FioAppendDirectory(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   276
{
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   277
	assert(subdir < NUM_SUBDIRS);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   278
	assert(sp < NUM_SEARCHPATHS);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   279
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   280
	snprintf(buf, buflen, "%s%s", _searchpaths[sp], _subdirs[subdir]);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   281
	return buf;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   282
}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   283
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   284
char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   285
{
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   286
	Searchpath sp;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   287
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   288
	/* Find and return the first valid directory */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   289
	FOR_ALL_SEARCHPATHS(sp) {
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   290
		char *ret = FioAppendDirectory(buf, buflen, sp, subdir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   291
		if (FileExists(buf)) return ret;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   292
	}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   293
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   294
	/* Could not find the directory, fall back to a base path */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   295
	ttd_strlcpy(buf, _personal_dir, buflen);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   296
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   297
	return buf;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   298
}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   299
8070
959633559d9f (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: 8066
diff changeset
   300
FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   301
{
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   302
#if defined(WIN32) && defined(UNICODE)
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   303
	/* fopen is implemented as a define with ellipses for
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   304
	 * Unicode support (prepend an L). As we are not sending
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   305
	 * a string, but a variable, it 'renames' the variable,
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   306
	 * so make that variable to makes it compile happily */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   307
	wchar_t Lmode[5];
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   308
	MultiByteToWideChar(CP_ACP, 0, mode, -1, Lmode, lengthof(Lmode));
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   309
#endif
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   310
	FILE *f = NULL;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   311
	char buf[MAX_PATH];
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   312
7431
f340111a55ec (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 7425
diff changeset
   313
	if (subdir == NO_DIRECTORY) {
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   314
		ttd_strlcpy(buf, filename, lengthof(buf));
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   315
	} else {
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   316
		snprintf(buf, lengthof(buf), "%s%s%s", _searchpaths[sp], _subdirs[subdir], filename);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   317
	}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   318
8120
2ab4ad82fd44 (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: 8088
diff changeset
   319
#if defined(WIN32)
8124
beda591abd6c (svn r11159) -Fix (r11155): restore the ability to save in a non existing file
glx
parents: 8121
diff changeset
   320
	if (mode[0] == 'r' && GetFileAttributes(OTTD2FS(buf)) == INVALID_FILE_ATTRIBUTES) return NULL;
8120
2ab4ad82fd44 (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: 8088
diff changeset
   321
#endif
2ab4ad82fd44 (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: 8088
diff changeset
   322
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   323
	f = fopen(buf, mode);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   324
#if !defined(WIN32)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   325
	if (f == NULL) {
10401
9036e551960f (svn r12943) -Fix: reading/modifying invalid data under some circumstances.
rubidium
parents: 8870
diff changeset
   326
		strtolower(buf + ((subdir == NO_DIRECTORY) ? 0 : strlen(_searchpaths[sp]) - 1));
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   327
		f = fopen(buf, mode);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   328
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   329
#endif
8070
959633559d9f (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: 8066
diff changeset
   330
	if (f != NULL && filesize != NULL) {
959633559d9f (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: 8066
diff changeset
   331
		/* Find the size of the file */
959633559d9f (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: 8066
diff changeset
   332
		fseek(f, 0, SEEK_END);
959633559d9f (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: 8066
diff changeset
   333
		*filesize = ftell(f);
959633559d9f (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: 8066
diff changeset
   334
		fseek(f, 0, SEEK_SET);
959633559d9f (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: 8066
diff changeset
   335
	}
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   336
	return f;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   337
}
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   338
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   339
FILE *FioFOpenFileTar(TarFileListEntry *entry, size_t *filesize)
8077
92edb3688fdf (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: 8071
diff changeset
   340
{
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   341
	FILE *f = fopen(entry->tar->filename, "rb");
8077
92edb3688fdf (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: 8071
diff changeset
   342
	assert(f != NULL);
92edb3688fdf (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: 8071
diff changeset
   343
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   344
	fseek(f, entry->position, SEEK_SET);
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   345
	if (filesize != NULL) *filesize = entry->size;
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   346
	return f;
8077
92edb3688fdf (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: 8071
diff changeset
   347
}
92edb3688fdf (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: 8071
diff changeset
   348
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   349
/** Opens OpenTTD files somewhere in a personal or global directory */
8071
8b1f7fbbe3f2 (svn r11100) -Fix r11099: darn typos...
truelight
parents: 8070
diff changeset
   350
FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   351
{
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   352
	FILE *f = NULL;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   353
	Searchpath sp;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   354
7431
f340111a55ec (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 7425
diff changeset
   355
	assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   356
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   357
	FOR_ALL_SEARCHPATHS(sp) {
8071
8b1f7fbbe3f2 (svn r11100) -Fix r11099: darn typos...
truelight
parents: 8070
diff changeset
   358
		f = FioFOpenFileSp(filename, mode, sp, subdir, filesize);
7431
f340111a55ec (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium
parents: 7425
diff changeset
   359
		if (f != NULL || subdir == NO_DIRECTORY) break;
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   360
	}
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   361
8077
92edb3688fdf (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: 8071
diff changeset
   362
	/* We can only use .tar in case of data-dir, and read-mode */
92edb3688fdf (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: 8071
diff changeset
   363
	if (f == NULL && subdir == DATA_DIR && mode[0] == 'r') {
11139
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   364
		static const uint MAX_RESOLVED_LENGTH = 2 * (100 + 100 + 155) + 1; // Enough space to hold two filenames plus link. See 'TarHeader'.
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   365
		char resolved_name[MAX_RESOLVED_LENGTH];
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   366
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   367
		/* Filenames in tars are always forced to be lowercase */
11139
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   368
		strcpy(resolved_name, filename);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   369
		strtolower(resolved_name);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   370
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   371
		uint resolved_len = strlen(resolved_name);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   372
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   373
		/* Resolve ONE directory link */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   374
		for (TarLinkList::iterator link = _tar_linklist.begin(); link != _tar_linklist.end(); link++) {
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   375
			const std::string &src = link->first;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   376
			uint len = src.length();
11140
67fe23cbc27b (svn r13698) -Fix (r13697): MorphOS compilation.
frosch
parents: 11139
diff changeset
   377
			if (resolved_len >= len && resolved_name[len - 1] == PATHSEPCHAR && strncmp(src.c_str(), resolved_name, len) == 0) {
11139
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   378
				/* Apply link */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   379
				char resolved_name2[MAX_RESOLVED_LENGTH];
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   380
				const std::string &dest = link->second;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   381
				strcpy(resolved_name2, &(resolved_name[len]));
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   382
				strcpy(resolved_name, dest.c_str());
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   383
				strcpy(&(resolved_name[dest.length()]), resolved_name2);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   384
				break; // Only resolve one level
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   385
			}
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   386
		}
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   387
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   388
		TarFileList::iterator it = _tar_filelist.find(resolved_name);
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   389
		if (it != _tar_filelist.end()) {
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   390
			f = FioFOpenFileTar(&((*it).second), filesize);
8077
92edb3688fdf (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: 8071
diff changeset
   391
		}
92edb3688fdf (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: 8071
diff changeset
   392
	}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   393
8505
e2e4f0c44da9 (svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium
parents: 8423
diff changeset
   394
	/* Sometimes a full path is given. To support
e2e4f0c44da9 (svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium
parents: 8423
diff changeset
   395
	 * the 'subdirectory' must be 'removed'. */
e2e4f0c44da9 (svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium
parents: 8423
diff changeset
   396
	if (f == NULL && subdir != NO_DIRECTORY) {
e2e4f0c44da9 (svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium
parents: 8423
diff changeset
   397
		f = FioFOpenFile(filename, mode, NO_DIRECTORY, filesize);
e2e4f0c44da9 (svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium
parents: 8423
diff changeset
   398
	}
e2e4f0c44da9 (svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium
parents: 8423
diff changeset
   399
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   400
	return f;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   401
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   402
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   403
/**
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   404
 * Create a directory with the given name
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   405
 * @param name the new name of the directory
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   406
 */
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   407
void FioCreateDirectory(const char *name)
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   408
{
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   409
#if defined(WIN32) || defined(WINCE)
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   410
	CreateDirectory(OTTD2FS(name), NULL);
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   411
#elif defined(OS2) && !defined(__INNOTEK_LIBC__)
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   412
	mkdir(OTTD2FS(name));
8407
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   413
#elif defined(__MORPHOS__) || defined(__AMIGAOS__)
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   414
	char buf[MAX_PATH];
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   415
	ttd_strlcpy(buf, name, MAX_PATH);
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   416
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   417
	size_t len = strlen(name) - 1;
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   418
	if (buf[len] == '/') {
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   419
		buf[len] = '\0'; // Kill pathsep, so mkdir() will not fail
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   420
	}
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   421
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   422
	mkdir(OTTD2FS(buf), 0755);
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   423
#else
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   424
	mkdir(OTTD2FS(name), 0755);
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   425
#endif
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   426
}
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   427
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   428
/**
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   429
 * Appends, if necessary, the path separator character to the end of the string.
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   430
 * It does not add the path separator to zero-sized strings.
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   431
 * @param buf    string to append the separator to
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   432
 * @param buflen the length of the buf
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   433
 */
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   434
void AppendPathSeparator(char *buf, size_t buflen)
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   435
{
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   436
	size_t s = strlen(buf);
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   437
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   438
	/* Length of string + path separator + '\0' */
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   439
	if (s != 0 && buf[s - 1] != PATHSEPCHAR && s + 2 < buflen) {
6625
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   440
		buf[s]     = PATHSEPCHAR;
3348b52bd823 (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents: 6624
diff changeset
   441
		buf[s + 1] = '\0';
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   442
	}
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   443
}
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   444
7330
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   445
/**
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   446
 * Allocates and files a variable with the full path
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   447
 * based on the given directory.
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   448
 * @param dir the directory to base the path on
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   449
 * @return the malloced full path
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   450
 */
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   451
char *BuildWithFullPath(const char *dir)
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   452
{
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   453
	char *dest = MallocT<char>(MAX_PATH);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   454
	ttd_strlcpy(dest, dir, MAX_PATH);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   455
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   456
	/* Check if absolute or relative path */
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   457
	const char *s = strchr(dest, PATHSEPCHAR);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   458
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   459
	/* Add absolute path */
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   460
	if (s == NULL || dest != s) {
10418
3ac4a89469df (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 10401
diff changeset
   461
		if (getcwd(dest, MAX_PATH) == NULL) *dest = '\0';
7330
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   462
		AppendPathSeparator(dest, MAX_PATH);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   463
		ttd_strlcat(dest, dir, MAX_PATH);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   464
	}
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   465
	AppendPathSeparator(dest, MAX_PATH);
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   466
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   467
	return dest;
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   468
}
577e92774f23 (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium
parents: 7251
diff changeset
   469
11139
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   470
/**
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   471
 * Simplify filenames from tars.
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   472
 * Replace '/' by PATHSEPCHAR, and force 'name' to lowercase.
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   473
 * @param name Filename to process.
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   474
 */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   475
static void SimplifyFileName(char *name)
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   476
{
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   477
	/* Force lowercase */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   478
	strtolower(name);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   479
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   480
	/* Tar-files always have '/' path-seperator, but we want our PATHSEPCHAR */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   481
#if (PATHSEPCHAR != '/')
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   482
	for (char *n = name; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   483
#endif
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   484
}
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   485
11180
982e9f814f97 scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
Tero Marttila <terom@fixme.fi>
parents: 11179
diff changeset
   486
static bool TarListAddFile(const char *filename, Subdirectory subdir)
8077
92edb3688fdf (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: 8071
diff changeset
   487
{
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   488
	/* The TAR-header, repeated for every file */
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   489
	typedef struct TarHeader {
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   490
		char name[100];      ///< Name of the file
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   491
		char mode[8];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   492
		char uid[8];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   493
		char gid[8];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   494
		char size[12];       ///< Size of the file, in ASCII
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   495
		char mtime[12];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   496
		char chksum[8];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   497
		char typeflag;
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   498
		char linkname[100];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   499
		char magic[6];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   500
		char version[2];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   501
		char uname[32];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   502
		char gname[32];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   503
		char devmajor[8];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   504
		char devminor[8];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   505
		char prefix[155];    ///< Path of the file
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   506
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   507
		char unused[12];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   508
	} TarHeader;
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   509
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   510
	/* Check if we already seen this file */
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   511
	TarList::iterator it = _tar_list.find(filename);
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   512
	if (it != _tar_list.end()) return false;
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   513
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   514
	FILE *f = fopen(filename, "rb");
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   515
	assert(f != NULL);
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   516
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   517
	TarListEntry *tar_entry = MallocT<TarListEntry>(1);
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   518
	tar_entry->filename = strdup(filename);
11180
982e9f814f97 scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
Tero Marttila <terom@fixme.fi>
parents: 11179
diff changeset
   519
    tar_entry->subdir = subdir;
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   520
	_tar_list.insert(TarList::value_type(filename, tar_entry));
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   521
11139
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   522
	TarLinkList links; ///< Temporary list to collect links
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   523
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   524
	TarHeader th;
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   525
	char buf[sizeof(th.name) + 1], *end;
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   526
	char name[sizeof(th.prefix) + 1 + sizeof(th.name) + 1];
11139
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   527
	char link[sizeof(th.linkname) + 1];
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   528
	char dest[sizeof(th.prefix) + 1 + sizeof(th.name) + 1 + 1 + sizeof(th.linkname) + 1];
10465
0c68afe3d725 (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 10421
diff changeset
   529
	size_t num = 0, pos = 0;
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   530
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   531
	/* Make a char of 512 empty bytes */
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   532
	char empty[512];
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   533
	memset(&empty[0], 0, sizeof(empty));
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   534
11135
6c703d2da75b (svn r13693) -Fix (r11106, r11117): Do not rely on .tar files always ending with a block of zeros.
frosch
parents: 11061
diff changeset
   535
	for (;;) { // Note: feof() always returns 'false' after 'fseek()'. Cool, isn't it?
6c703d2da75b (svn r13693) -Fix (r11106, r11117): Do not rely on .tar files always ending with a block of zeros.
frosch
parents: 11061
diff changeset
   536
		size_t num_bytes_read = fread(&th, 1, 512, f);
6c703d2da75b (svn r13693) -Fix (r11106, r11117): Do not rely on .tar files always ending with a block of zeros.
frosch
parents: 11061
diff changeset
   537
		if (num_bytes_read != 512) break;
6c703d2da75b (svn r13693) -Fix (r11106, r11117): Do not rely on .tar files always ending with a block of zeros.
frosch
parents: 11061
diff changeset
   538
		pos += num_bytes_read;
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   539
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   540
		/* Check if we have the new tar-format (ustar) or the old one (a lot of zeros after 'link' field) */
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   541
		if (strncmp(th.magic, "ustar", 5) != 0 && memcmp(&th.magic, &empty[0], 512 - offsetof(TarHeader, magic)) != 0) {
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   542
			/* If we have only zeros in the block, it can be an end-of-file indicator */
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   543
			if (memcmp(&th, &empty[0], 512) == 0) continue;
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   544
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   545
			DEBUG(misc, 0, "The file '%s' isn't a valid tar-file", filename);
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   546
			return false;
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   547
		}
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   548
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   549
		name[0] = '\0';
10465
0c68afe3d725 (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 10421
diff changeset
   550
		size_t len = 0;
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   551
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   552
		/* The prefix contains the directory-name */
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   553
		if (th.prefix[0] != '\0') {
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   554
			memcpy(name, th.prefix, sizeof(th.prefix));
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   555
			name[sizeof(th.prefix)] = '\0';
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   556
			len = strlen(name);
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   557
			name[len] = PATHSEPCHAR;
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   558
			len++;
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   559
		}
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   560
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   561
		/* Copy the name of the file in a safe way at the end of 'name' */
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   562
		memcpy(&name[len], th.name, sizeof(th.name));
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   563
		name[len + sizeof(th.name)] = '\0';
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   564
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   565
		/* Calculate the size of the file.. for some strange reason this is stored as a string */
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   566
		memcpy(buf, th.size, sizeof(th.size));
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   567
		buf[sizeof(th.size)] = '\0';
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   568
		int skip = strtol(buf, &end, 8);
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   569
11139
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   570
		switch (th.typeflag) {
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   571
			case '\0':
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   572
			case '0': { // regular file
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   573
				/* Ignore empty files */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   574
				if (skip == 0) break;
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   575
11139
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   576
				if (strlen(name) == 0) break;
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   577
11139
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   578
				/* Store this entry in the list */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   579
				TarFileListEntry entry;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   580
				entry.tar      = tar_entry;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   581
				entry.size     = skip;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   582
				entry.position = pos;
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   583
11139
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   584
				/* Convert to lowercase and our PATHSEPCHAR */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   585
				SimplifyFileName(name);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   586
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   587
				DEBUG(misc, 6, "Found file in tar: %s (%d bytes, %d offset)", name, skip, pos);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   588
				if (_tar_filelist.insert(TarFileList::value_type(name, entry)).second) num++;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   589
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   590
				break;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   591
			}
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   592
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   593
			case '1': // hard links
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   594
			case '2': { // symbolic links
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   595
				/* Copy the destination of the link in a safe way at the end of 'linkname' */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   596
				memcpy(link, th.linkname, sizeof(th.linkname));
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   597
				link[sizeof(th.linkname)] = '\0';
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   598
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   599
				if (strlen(name) == 0 || strlen(link) == 0) break;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   600
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   601
				/* Convert to lowercase and our PATHSEPCHAR */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   602
				SimplifyFileName(name);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   603
				SimplifyFileName(link);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   604
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   605
				/* Only allow relative links */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   606
				if (link[0] == PATHSEPCHAR) {
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   607
					DEBUG(misc, 1, "Ignoring absolute link in tar: %s -> %s", name, link);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   608
					break;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   609
				}
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   610
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   611
				/* Process relative path.
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   612
				 * Note: The destination of links must not contain any directory-links. */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   613
				strcpy(dest, name);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   614
				char *destpos = strrchr(dest, PATHSEPCHAR);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   615
				if (destpos == NULL) destpos = dest;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   616
				*destpos = '\0';
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   617
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   618
				char *pos = link;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   619
				while (*pos != '\0') {
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   620
					char *next = strchr(link, PATHSEPCHAR);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   621
					if (next == NULL) next = pos + strlen(pos);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   622
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   623
					/* Skip '.' (current dir) */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   624
					if (next != pos + 1 || pos[0] != '.') {
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   625
						if (next == pos + 2 && pos[0] == '.' && pos[1] == '.') {
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   626
							/* level up */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   627
							if (dest[0] == '\0') {
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   628
								DEBUG(misc, 1, "Ignoring link pointing outside of data directory: %s -> %s", name, link);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   629
								break;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   630
							}
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   631
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   632
							/* Truncate 'dest' after last PATHSEPCHAR.
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   633
							 * This assumes, that the truncated part is a real directory and not a link */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   634
							destpos = strrchr(dest, PATHSEPCHAR);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   635
							if (destpos == NULL) destpos = dest;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   636
						} else {
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   637
							/* Append at end of 'dest' */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   638
							if (destpos != dest) *(destpos++) = PATHSEPCHAR;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   639
							strncpy(destpos, pos, next - pos);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   640
							destpos += next - pos;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   641
						}
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   642
						*destpos = '\0';
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   643
					}
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   644
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   645
					pos = next;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   646
				}
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   647
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   648
				/* Store links in temporary list */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   649
				DEBUG(misc, 6, "Found link in tar: %s -> %s", name, dest);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   650
				links.insert(TarLinkList::value_type(name, dest));
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   651
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   652
				break;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   653
			}
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   654
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   655
			default:
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   656
				/* Ignore other types */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   657
				break;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   658
		}
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   659
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   660
		/* Skip to the next block.. */
8423
8453e9a0f0b5 (svn r11480) -Codechange: Rename the function ALIGN fitting to the naming style
skidd13
parents: 8407
diff changeset
   661
		skip = Align(skip, 512);
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   662
		fseek(f, skip, SEEK_CUR);
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   663
		pos += skip;
8077
92edb3688fdf (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: 8071
diff changeset
   664
	}
92edb3688fdf (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: 8071
diff changeset
   665
8088
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   666
	DEBUG(misc, 1, "Found tar '%s' with %d new files", filename, num);
7c1a4dd586ae (svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight
parents: 8086
diff changeset
   667
	fclose(f);
8077
92edb3688fdf (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: 8071
diff changeset
   668
11139
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   669
	/* Resolve file links and store directory links.
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   670
	 * We restrict usage of links to two cases:
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   671
	 *  1) Links to directories:
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   672
	 *      Both the source path and the destination path must NOT contain any further links.
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   673
	 *      When resolving files at most one directory link is resolved.
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   674
	 *  2) Links to files:
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   675
	 *      The destination path must NOT contain any links.
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   676
	 *      The source path may contain one directory link.
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   677
	 */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   678
	for (TarLinkList::iterator link = links.begin(); link != links.end(); link++) {
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   679
		const std::string &src = link->first;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   680
		const std::string &dest = link->second;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   681
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   682
		TarFileList::iterator dest_file = _tar_filelist.find(dest);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   683
		if (dest_file != _tar_filelist.end()) {
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   684
			/* Link to file. Process the link like the destination file. */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   685
			_tar_filelist.insert(TarFileList::value_type(src, dest_file->second));
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   686
		} else {
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   687
			/* Destination file not found. Assume 'link to directory' */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   688
			/* Append PATHSEPCHAR to 'src' and 'dest' */
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   689
			const std::string src_path = src + PATHSEPCHAR;
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   690
			const std::string dst_path = (dest.length() == 0 ? "" : dest + PATHSEPCHAR);
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   691
			_tar_linklist.insert(TarLinkList::value_type(src_path, dst_path));
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   692
		}
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   693
	}
3a72578d7811 (svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch
parents: 11135
diff changeset
   694
8077
92edb3688fdf (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: 8071
diff changeset
   695
	return true;
92edb3688fdf (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: 8071
diff changeset
   696
}
92edb3688fdf (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: 8071
diff changeset
   697
11180
982e9f814f97 scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
Tero Marttila <terom@fixme.fi>
parents: 11179
diff changeset
   698
static int ScanPathForTarFiles(const char *path, size_t basepath_length, Subdirectory subdir)
8077
92edb3688fdf (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: 8071
diff changeset
   699
{
92edb3688fdf (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: 8071
diff changeset
   700
	extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
92edb3688fdf (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: 8071
diff changeset
   701
92edb3688fdf (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: 8071
diff changeset
   702
	uint num = 0;
92edb3688fdf (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: 8071
diff changeset
   703
	struct stat sb;
92edb3688fdf (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: 8071
diff changeset
   704
	struct dirent *dirent;
92edb3688fdf (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: 8071
diff changeset
   705
	DIR *dir;
92edb3688fdf (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: 8071
diff changeset
   706
92edb3688fdf (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: 8071
diff changeset
   707
	if (path == NULL || (dir = ttd_opendir(path)) == NULL) return 0;
92edb3688fdf (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: 8071
diff changeset
   708
92edb3688fdf (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: 8071
diff changeset
   709
	while ((dirent = readdir(dir)) != NULL) {
92edb3688fdf (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: 8071
diff changeset
   710
		const char *d_name = FS2OTTD(dirent->d_name);
92edb3688fdf (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: 8071
diff changeset
   711
		char filename[MAX_PATH];
92edb3688fdf (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: 8071
diff changeset
   712
92edb3688fdf (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: 8071
diff changeset
   713
		if (!FiosIsValidFile(path, dirent, &sb)) continue;
92edb3688fdf (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: 8071
diff changeset
   714
92edb3688fdf (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: 8071
diff changeset
   715
		snprintf(filename, lengthof(filename), "%s%s", path, d_name);
92edb3688fdf (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: 8071
diff changeset
   716
92edb3688fdf (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: 8071
diff changeset
   717
		if (sb.st_mode & S_IFDIR) {
92edb3688fdf (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: 8071
diff changeset
   718
			/* Directory */
92edb3688fdf (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: 8071
diff changeset
   719
			if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
92edb3688fdf (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: 8071
diff changeset
   720
			AppendPathSeparator(filename, lengthof(filename));
11180
982e9f814f97 scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
Tero Marttila <terom@fixme.fi>
parents: 11179
diff changeset
   721
			num += ScanPathForTarFiles(filename, basepath_length, subdir);
8077
92edb3688fdf (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: 8071
diff changeset
   722
		} else if (sb.st_mode & S_IFREG) {
92edb3688fdf (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: 8071
diff changeset
   723
			/* File */
92edb3688fdf (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: 8071
diff changeset
   724
			char *ext = strrchr(filename, '.');
92edb3688fdf (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: 8071
diff changeset
   725
92edb3688fdf (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: 8071
diff changeset
   726
			/* If no extension or extension isn't .tar, skip the file */
92edb3688fdf (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: 8071
diff changeset
   727
			if (ext == NULL) continue;
92edb3688fdf (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: 8071
diff changeset
   728
			if (strcasecmp(ext, ".tar") != 0) continue;
92edb3688fdf (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: 8071
diff changeset
   729
11180
982e9f814f97 scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
Tero Marttila <terom@fixme.fi>
parents: 11179
diff changeset
   730
			if (TarListAddFile(filename, subdir)) num++;
8077
92edb3688fdf (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: 8071
diff changeset
   731
		}
92edb3688fdf (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: 8071
diff changeset
   732
	}
92edb3688fdf (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: 8071
diff changeset
   733
92edb3688fdf (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: 8071
diff changeset
   734
	closedir(dir);
92edb3688fdf (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: 8071
diff changeset
   735
	return num;
92edb3688fdf (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: 8071
diff changeset
   736
}
92edb3688fdf (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: 8071
diff changeset
   737
8085
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   738
void ScanForTarFiles()
8077
92edb3688fdf (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: 8071
diff changeset
   739
{
92edb3688fdf (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: 8071
diff changeset
   740
	Searchpath sp;
92edb3688fdf (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: 8071
diff changeset
   741
	char path[MAX_PATH];
92edb3688fdf (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: 8071
diff changeset
   742
	uint num = 0;
92edb3688fdf (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: 8071
diff changeset
   743
92edb3688fdf (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: 8071
diff changeset
   744
	DEBUG(misc, 1, "Scanning for tars");
92edb3688fdf (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: 8071
diff changeset
   745
	FOR_ALL_SEARCHPATHS(sp) {
92edb3688fdf (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: 8071
diff changeset
   746
		FioAppendDirectory(path, MAX_PATH, sp, DATA_DIR);
11180
982e9f814f97 scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
Tero Marttila <terom@fixme.fi>
parents: 11179
diff changeset
   747
		num += ScanPathForTarFiles(path, strlen(path), DATA_DIR);
982e9f814f97 scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
Tero Marttila <terom@fixme.fi>
parents: 11179
diff changeset
   748
		
982e9f814f97 scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
Tero Marttila <terom@fixme.fi>
parents: 11179
diff changeset
   749
        FioAppendDirectory(path, MAX_PATH, sp, CACHE_DIR);
982e9f814f97 scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
Tero Marttila <terom@fixme.fi>
parents: 11179
diff changeset
   750
		num += ScanPathForTarFiles(path, strlen(path), CACHE_DIR);
8077
92edb3688fdf (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: 8071
diff changeset
   751
	}
92edb3688fdf (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: 8071
diff changeset
   752
	DEBUG(misc, 1, "Scan complete, found %d files", num);
92edb3688fdf (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: 8071
diff changeset
   753
}
92edb3688fdf (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: 8071
diff changeset
   754
8085
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   755
#if defined(WIN32) || defined(WINCE)
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   756
/**
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   757
 * Determine the base (personal dir and game data dir) paths
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   758
 * @param exe the path from the current path to the executable
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   759
 * @note defined in the OS related files (os2.cpp, win32.cpp, unix.cpp etc)
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   760
 */
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   761
extern void DetermineBasePaths(const char *exe);
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   762
#else /* defined(WIN32) || defined(WINCE) */
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   763
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   764
/**
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   765
 * Changes the working directory to the path of the give executable.
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   766
 * For OSX application bundles '.app' is the required extension of the bundle,
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   767
 * so when we crop the path to there, when can remove the name of the bundle
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   768
 * in the same way we remove the name from the executable name.
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   769
 * @param exe the path to the executable
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   770
 */
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   771
void ChangeWorkingDirectory(const char *exe)
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   772
{
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   773
#ifdef WITH_COCOA
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   774
	char *app_bundle = strchr(exe, '.');
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   775
	while (app_bundle != NULL && strncasecmp(app_bundle, ".app", 4) != 0) app_bundle = strchr(&app_bundle[1], '.');
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   776
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   777
	if (app_bundle != NULL) app_bundle[0] = '\0';
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   778
#endif /* WITH_COCOA */
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   779
	char *s = strrchr(exe, PATHSEPCHAR);
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   780
	if (s != NULL) {
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   781
		*s = '\0';
10418
3ac4a89469df (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 10401
diff changeset
   782
		if (chdir(exe) != 0) DEBUG(misc, 0, "Directory with the binary does not exist?");
8085
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   783
		*s = PATHSEPCHAR;
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   784
	}
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   785
#ifdef WITH_COCOA
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   786
	if (app_bundle != NULL) app_bundle[0] = '.';
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   787
#endif /* WITH_COCOA */
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   788
}
698efbcb33de (svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx
parents: 8080
diff changeset
   789
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   790
/**
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   791
 * Determine the base (personal dir and game data dir) paths
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   792
 * @param exe the path to the executable
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   793
 */
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   794
void DetermineBasePaths(const char *exe)
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   795
{
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   796
	char tmp[MAX_PATH];
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   797
#if defined(__MORPHOS__) || defined(__AMIGA__) || !defined(WITH_PERSONAL_DIR)
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   798
	_searchpaths[SP_PERSONAL_DIR] = NULL;
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   799
#else
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   800
	const char *homedir = getenv("HOME");
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   801
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   802
	if (homedir == NULL) {
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   803
		const struct passwd *pw = getpwuid(getuid());
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   804
		homedir = (pw == NULL) ? "" : pw->pw_dir;
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   805
	}
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   806
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   807
	snprintf(tmp, MAX_PATH, "%s" PATHSEP "%s", homedir, PERSONAL_DIR);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   808
	AppendPathSeparator(tmp, MAX_PATH);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   809
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   810
	_searchpaths[SP_PERSONAL_DIR] = strdup(tmp);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   811
#endif
8769
ee288fa819f6 (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8710
diff changeset
   812
ee288fa819f6 (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8710
diff changeset
   813
#if defined(WITH_SHARED_DIR)
ee288fa819f6 (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8710
diff changeset
   814
	snprintf(tmp, MAX_PATH, "%s", SHARED_DIR);
ee288fa819f6 (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8710
diff changeset
   815
	AppendPathSeparator(tmp, MAX_PATH);
ee288fa819f6 (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8710
diff changeset
   816
	_searchpaths[SP_SHARED_DIR] = strdup(tmp);
ee288fa819f6 (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8710
diff changeset
   817
#else
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   818
	_searchpaths[SP_SHARED_DIR] = NULL;
8769
ee288fa819f6 (svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni
parents: 8710
diff changeset
   819
#endif
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   820
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   821
#if defined(__MORPHOS__) || defined(__AMIGA__)
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   822
	_searchpaths[SP_WORKING_DIR] = NULL;
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   823
#else
10418
3ac4a89469df (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 10401
diff changeset
   824
	if (getcwd(tmp, MAX_PATH) == NULL) *tmp = '\0';
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   825
	AppendPathSeparator(tmp, MAX_PATH);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   826
	_searchpaths[SP_WORKING_DIR] = strdup(tmp);
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   827
#endif
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   828
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   829
	/* Change the working directory to that one of the executable */
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   830
	ChangeWorkingDirectory((char*)exe);
10418
3ac4a89469df (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 10401
diff changeset
   831
	if (getcwd(tmp, MAX_PATH) == NULL) *tmp = '\0';
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   832
	AppendPathSeparator(tmp, MAX_PATH);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   833
	_searchpaths[SP_BINARY_DIR] = strdup(tmp);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   834
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   835
#if defined(__MORPHOS__) || defined(__AMIGA__)
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   836
	_searchpaths[SP_INSTALLATION_DIR] = NULL;
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   837
#else
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   838
	snprintf(tmp, MAX_PATH, "%s", GLOBAL_DATA_DIR);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   839
	AppendPathSeparator(tmp, MAX_PATH);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   840
	_searchpaths[SP_INSTALLATION_DIR] = strdup(tmp);
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7437
diff changeset
   841
#endif
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   842
#ifdef WITH_COCOA
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   843
extern void cocoaSetApplicationBundleDir();
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   844
	cocoaSetApplicationBundleDir();
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   845
#else
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   846
	_searchpaths[SP_APPLICATION_BUNDLE_DIR] = NULL;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   847
#endif
8077
92edb3688fdf (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: 8071
diff changeset
   848
92edb3688fdf (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: 8071
diff changeset
   849
	ScanForTarFiles();
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   850
}
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   851
#endif /* defined(WIN32) || defined(WINCE) */
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   852
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   853
char *_personal_dir;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   854
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   855
/**
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   856
 * Acquire the base paths (personal dir and game data dir),
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   857
 * fill all other paths (save dir, autosave dir etc) and
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   858
 * make the save and scenario directories.
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   859
 * @param exe the path from the current path to the executable
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   860
 */
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   861
void DeterminePaths(const char *exe)
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   862
{
6643
7fad81bde617 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents: 6625
diff changeset
   863
	DetermineBasePaths(exe);
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   864
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   865
	Searchpath sp;
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   866
	FOR_ALL_SEARCHPATHS(sp) DEBUG(misc, 4, "%s added as search path", _searchpaths[sp]);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   867
7437
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   868
	if (_config_file != NULL) {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   869
		_personal_dir = strdup(_config_file);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   870
		char *end = strrchr(_personal_dir , PATHSEPCHAR);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   871
		if (end == NULL) {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   872
			_personal_dir[0] = '\0';
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   873
		} else {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   874
			end[1] = '\0';
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   875
		}
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   876
	} else {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   877
		char personal_dir[MAX_PATH];
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   878
		FioFindFullPath(personal_dir, lengthof(personal_dir), BASE_DIR, "openttd.cfg");
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   879
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   880
		if (FileExists(personal_dir)) {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   881
			char *end = strrchr(personal_dir, PATHSEPCHAR);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   882
			if (end != NULL) end[1] = '\0';
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   883
			_personal_dir = strdup(personal_dir);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   884
			_config_file = str_fmt("%sopenttd.cfg", _personal_dir);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   885
		} else {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   886
			static const Searchpath new_openttd_cfg_order[] = {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   887
					SP_PERSONAL_DIR, SP_BINARY_DIR, SP_WORKING_DIR, SP_SHARED_DIR, SP_INSTALLATION_DIR
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   888
				};
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   889
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   890
			for (uint i = 0; i < lengthof(new_openttd_cfg_order); i++) {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   891
				if (IsValidSearchPath(new_openttd_cfg_order[i])) {
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   892
					_personal_dir = strdup(_searchpaths[new_openttd_cfg_order[i]]);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   893
					_config_file = str_fmt("%sopenttd.cfg", _personal_dir);
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   894
					break;
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   895
				}
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   896
			}
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   897
		}
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   898
	}
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   899
7437
3479a4c141b5 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium
parents: 7431
diff changeset
   900
	DEBUG(misc, 3, "%s found as personal directory", _personal_dir);
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   901
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   902
	_highscore_file = str_fmt("%shs.dat", _personal_dir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   903
	_log_file = str_fmt("%sopenttd.log",  _personal_dir);
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   904
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   905
	char *save_dir     = str_fmt("%s%s", _personal_dir, FioGetSubdirectory(SAVE_DIR));
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   906
	char *autosave_dir = str_fmt("%s%s", _personal_dir, FioGetSubdirectory(AUTOSAVE_DIR));
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   907
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   908
	/* Make the necessary folders */
8407
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   909
#if !defined(__MORPHOS__) && !defined(__AMIGA__) && defined(WITH_PERSONAL_DIR)
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   910
	FioCreateDirectory(_personal_dir);
8407
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   911
#endif
f2f6dae6b6d3 (svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium
parents: 8301
diff changeset
   912
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   913
	FioCreateDirectory(save_dir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   914
	FioCreateDirectory(autosave_dir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   915
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   916
	free(save_dir);
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7392
diff changeset
   917
	free(autosave_dir);
6624
554e5aee6c3f (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents: 6574
diff changeset
   918
}
7371
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   919
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   920
/**
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   921
 * Sanitizes a filename, i.e. removes all illegal characters from it.
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   922
 * @param filename the "\0" terminated filename
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   923
 */
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   924
void SanitizeFilename(char *filename)
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   925
{
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   926
	for (; *filename != '\0'; filename++) {
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   927
		switch (*filename) {
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   928
			/* The following characters are not allowed in filenames
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   929
			 * on at least one of the supported operating systems: */
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   930
			case ':': case '\\': case '*': case '?': case '/':
7512
7889a88184c0 (svn r10272) -Fix (FS#916): remove more invalid characters from savegame names
glx
parents: 7486
diff changeset
   931
			case '<': case '>': case '|': case '"':
7371
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   932
				*filename = '_';
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   933
				break;
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   934
		}
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   935
	}
b86b05214afe (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium
parents: 7330
diff changeset
   936
}
8627
448ebf3a8291 (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: 8626
diff changeset
   937
448ebf3a8291 (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: 8626
diff changeset
   938
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize)
448ebf3a8291 (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: 8626
diff changeset
   939
{
448ebf3a8291 (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: 8626
diff changeset
   940
	FILE *in;
448ebf3a8291 (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: 8626
diff changeset
   941
	byte *mem;
448ebf3a8291 (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: 8626
diff changeset
   942
	size_t len;
448ebf3a8291 (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: 8626
diff changeset
   943
448ebf3a8291 (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: 8626
diff changeset
   944
	in = fopen(filename, "rb");
448ebf3a8291 (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: 8626
diff changeset
   945
	if (in == NULL) return NULL;
448ebf3a8291 (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: 8626
diff changeset
   946
448ebf3a8291 (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: 8626
diff changeset
   947
	fseek(in, 0, SEEK_END);
448ebf3a8291 (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: 8626
diff changeset
   948
	len = ftell(in);
448ebf3a8291 (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: 8626
diff changeset
   949
	fseek(in, 0, SEEK_SET);
448ebf3a8291 (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: 8626
diff changeset
   950
	if (len > maxsize || (mem = MallocT<byte>(len + 1)) == NULL) {
448ebf3a8291 (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: 8626
diff changeset
   951
		fclose(in);
448ebf3a8291 (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: 8626
diff changeset
   952
		return NULL;
448ebf3a8291 (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: 8626
diff changeset
   953
	}
448ebf3a8291 (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: 8626
diff changeset
   954
	mem[len] = 0;
448ebf3a8291 (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: 8626
diff changeset
   955
	if (fread(mem, len, 1, in) != 1) {
448ebf3a8291 (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: 8626
diff changeset
   956
		fclose(in);
448ebf3a8291 (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: 8626
diff changeset
   957
		free(mem);
448ebf3a8291 (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: 8626
diff changeset
   958
		return NULL;
448ebf3a8291 (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: 8626
diff changeset
   959
	}
448ebf3a8291 (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: 8626
diff changeset
   960
	fclose(in);
448ebf3a8291 (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: 8626
diff changeset
   961
448ebf3a8291 (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: 8626
diff changeset
   962
	*lenp = len;
448ebf3a8291 (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: 8626
diff changeset
   963
	return mem;
448ebf3a8291 (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: 8626
diff changeset
   964
}