src/fileio.cpp
author celestar
Wed, 13 Jun 2007 12:05:56 +0000
branchgamebalance
changeset 9912 1ac8aac92385
parent 9908 0fa543611bbe
child 9913 e79cd19772dd
permissions -rw-r--r--
(svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6527
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6505
diff changeset
     3
/** @file fileio.cpp Standard In/Out file operations */
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6218
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1198
diff changeset
     6
#include "openttd.h"
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1039
diff changeset
     7
#include "fileio.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     8
#include "functions.h"
4200
a45420ba0c23 (svn r5684) - Codechange: create an strtolower() function that uses tolower() on a whole string and apply it in the places this was used.
Darkvater
parents: 4077
diff changeset
     9
#include "string.h"
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2153
diff changeset
    10
#include "macros.h"
2153
91e89aa8c299 (svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents: 1891
diff changeset
    11
#include "variables.h"
6218
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    12
#include "debug.h"
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
    13
#include "fios.h"
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
    14
#ifndef WIN32
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
    15
#include <pwd.h>
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
    16
#include <unistd.h>
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
    17
#include <sys/stat.h>
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
    18
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
/*************************************************/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
/* FILE IO ROUTINES ******************************/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
/*************************************************/
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
#define FIO_BUFFER_SIZE 512
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
    25
#define MAX_HANDLES 64
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
    27
struct Fio {
4203
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    28
	byte *buffer, *buffer_end;          ///< position pointer in local buffer and last valid byte of buffer
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    29
	uint32 pos;                         ///< current (system) position in file
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    30
	FILE *cur_fh;                       ///< current file handle
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
    31
	FILE *handles[MAX_HANDLES];         ///< array of file handles we can have open
4203
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    32
	byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
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
    33
#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
    34
	uint open_handles;                  ///< current amount of open handles
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    35
	const char *filename[MAX_HANDLES];  ///< array of filenames we (should) have open
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
    36
	uint usage_count[MAX_HANDLES];      ///< count how many times this file has been opened
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    37
#endif /* LIMITED_FDS */
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
    38
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
static Fio _fio;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6218
diff changeset
    42
/* Get current position in file */
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
    43
uint32 FioGetPos()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
	return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
void FioSeekTo(uint32 pos, int mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
	if (mode == SEEK_CUR) pos += FioGetPos();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
	_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
    52
	_fio.pos = pos;
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    53
	fseek(_fio.cur_fh, _fio.pos, SEEK_SET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
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
    56
#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
    57
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
    58
{
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
    59
	/* 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
    60
	if (_fio.handles[slot] == NULL) {
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
    61
		DEBUG(misc, 6, "Restoring file '%s' in slot '%d' from disk", _fio.filename[slot], 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
    62
		FioOpenFile(slot, _fio.filename[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
    63
	}
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    64
	_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
    65
}
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
#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
    67
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6218
diff changeset
    68
/* Seek to a file and a position */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
void FioSeekToFile(uint32 pos)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
{
6218
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
    71
	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
    72
#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
    73
	/* Make sure we have this file open */
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
	FioRestoreFile(pos >> 24);
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
	f = _fio.handles[pos >> 24];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
	assert(f != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
	_fio.cur_fh = f;
4203
790879e123dd (svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents: 4202
diff changeset
    79
	FioSeekTo(GB(pos, 0, 24), SEEK_SET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
    82
byte FioReadByte()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
	if (_fio.buffer == _fio.buffer_end) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    85
		_fio.pos += FIO_BUFFER_SIZE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
		fread(_fio.buffer = _fio.buffer_start, 1, FIO_BUFFER_SIZE, _fio.cur_fh);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
	return *_fio.buffer++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
void FioSkipBytes(int n)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
{
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2736
diff changeset
    93
	for (;;) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
		int m = min(_fio.buffer_end - _fio.buffer, n);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    95
		_fio.buffer += m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
		n -= m;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
		if (n == 0) break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    98
		FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
		n--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
	}
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
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   103
uint16 FioReadWord()
0
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
	byte b = FioReadByte();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
	return (FioReadByte() << 8) | b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   109
uint32 FioReadDword()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
	uint b = FioReadWord();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
	return (FioReadWord() << 16) | b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
void FioReadBlock(void *ptr, uint size)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   116
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
	FioSeekTo(FioGetPos(), SEEK_SET);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
	_fio.pos += size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
	fread(ptr, 1, size, _fio.cur_fh);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
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
   122
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
   123
{
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
   124
	if (_fio.handles[slot] != NULL) {
1109
1bab892228cd (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1093
diff changeset
   125
		fclose(_fio.handles[slot]);
1039
8736f759759b (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   126
		_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
   127
#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
   128
		_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
   129
#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
   130
	}
8736f759759b (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   131
}
8736f759759b (svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents: 1036
diff changeset
   132
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   133
void FioCloseAll()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
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
   137
	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
   138
		FioCloseFile(i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
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
   141
#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
   142
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
   143
{
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 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
   145
	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
   146
		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
   147
		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
   148
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
   149
		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
   150
		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
   151
		/* 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
   152
		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
   153
			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
   154
				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
   155
				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
   156
			}
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   157
		}
89dc931b8d78 (svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents: 5835
diff changeset
   158
		assert(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
   159
		DEBUG(misc, 6, "Closing filehandler '%s' in slot '%d' because of fd-limit", _fio.filename[slot], 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
   160
		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
   161
	}
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
}
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
#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
   164
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   165
void FioOpenFile(int slot, const char *filename)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   166
{
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   167
	FILE *f;
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   168
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   169
#if defined(LIMITED_FDS)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   170
	FioFreeHandle();
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   171
#endif /* LIMITED_FDS */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   172
	f = FioFOpenFile(filename);
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9895
diff changeset
   173
	if (f == NULL) error("Cannot open file '%s'", filename);
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   174
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   175
	FioCloseFile(slot); // if file was opened before, close it
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   176
	_fio.handles[slot] = f;
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   177
#if defined(LIMITED_FDS)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   178
	_fio.filename[slot] = filename;
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   179
	_fio.usage_count[slot] = 0;
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   180
	_fio.open_handles++;
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   181
#endif /* LIMITED_FDS */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   182
	FioSeekToFile(slot << 24);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   183
}
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   184
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   185
/**
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   186
 * Check whether the given file exists
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   187
 * @param filename the file to try for existance
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   188
 * @return true if and only if the file can be opened
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   189
 */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   190
bool FioCheckFileExists(const char *filename)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   191
{
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   192
	FILE *f = FioFOpenFile(filename);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   193
	if (f == NULL) return false;
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   194
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   195
	fclose(f);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   196
	return true;
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   197
}
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   198
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   199
/**
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   200
 * Opens the file with the given name
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   201
 * @param filename the file to open (in either data_dir or second_data_dir)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   202
 * @return the opened file or NULL when it failed.
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   203
 */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   204
FILE *FioFOpenFile(const char *filename)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   205
{
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   206
	FILE *f;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   207
	char buf[MAX_PATH];
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   208
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   209
	if (filename[0] == PATHSEPCHAR || filename[1] == ':') {
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   210
		ttd_strlcpy(buf, filename, lengthof(buf));
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   211
	} else {
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9895
diff changeset
   212
		snprintf(buf, lengthof(buf), "%s%s", _paths.data_dir, filename);
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9895
diff changeset
   213
	}
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   214
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   215
	f = fopen(buf, "rb");
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   216
#if !defined(WIN32)
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   217
	if (f == NULL) {
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9895
diff changeset
   218
		strtolower(strrchr(buf, PATHSEPCHAR));
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   219
		f = fopen(buf, "rb");
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   220
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   221
#if defined SECOND_DATA_DIR
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6218
diff changeset
   222
		/* tries in the 2nd data directory */
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   223
		if (f == NULL) {
5296
6a4aaa66eed3 (svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents: 5170
diff changeset
   224
			snprintf(buf, lengthof(buf), "%s%s", _paths.second_data_dir, filename);
6a4aaa66eed3 (svn r7449) -Codechange: Rename _path to _paths as it is technically more correct, but mainly because
Darkvater
parents: 5170
diff changeset
   225
			strtolower(buf + strlen(_paths.second_data_dir) - 1);
2736
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   226
			f = fopen(buf, "rb");
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   227
		}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   228
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   229
	}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   230
#endif
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   231
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   232
	return f;
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   233
}
1ea068235989 (svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents: 2548
diff changeset
   234
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   235
/**
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   236
 * Create a directory with the given name
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   237
 * @param name the new name of the directory
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   238
 */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   239
void FioCreateDirectory(const char *name)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   240
{
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   241
#if defined(WIN32) || defined(WINCE)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   242
	CreateDirectory(OTTD2FS(name), NULL);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   243
#elif defined(OS2) && !defined(__INNOTEK_LIBC__)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   244
	mkdir(OTTD2FS(name));
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   245
#else
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   246
	mkdir(OTTD2FS(name), 0755);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   247
#endif
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   248
}
915
013cb2d74800 (svn r1402) Trim trailing whitespace
tron
parents: 810
diff changeset
   249
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   250
/**
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   251
 * Appends, if necessary, the path separator character to the end of the string.
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   252
 * It does not add the path separator to zero-sized strings.
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   253
 * @param buf    string to append the separator to
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   254
 * @param buflen the length of the buf
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   255
 */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   256
void AppendPathSeparator(char *buf, size_t buflen)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   257
{
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   258
	size_t s = strlen(buf);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
   259
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   260
	/* Length of string + path separator + '\0' */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   261
	if (s != 0 && buf[s - 1] != PATHSEPCHAR && s + 2 < buflen) {
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   262
		buf[s]     = PATHSEPCHAR;
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   263
		buf[s + 1] = '\0';
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   264
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   265
}
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   266
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   267
/**
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   268
 * Allocates and files a variable with the full path
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   269
 * based on the given directory.
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   270
 * @param dir the directory to base the path on
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   271
 * @return the malloced full path
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   272
 */
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   273
char *BuildWithFullPath(const char *dir)
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   274
{
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   275
	char *dest = MallocT<char>(MAX_PATH);
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   276
	ttd_strlcpy(dest, dir, MAX_PATH);
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   277
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   278
	/* Check if absolute or relative path */
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   279
	const char *s = strchr(dest, PATHSEPCHAR);
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   280
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   281
	/* Add absolute path */
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   282
	if (s == NULL || dest != s) {
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   283
		getcwd(dest, MAX_PATH);
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   284
		AppendPathSeparator(dest, MAX_PATH);
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   285
		ttd_strlcat(dest, dir, MAX_PATH);
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   286
	}
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   287
	AppendPathSeparator(dest, MAX_PATH);
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   288
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   289
	return dest;
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   290
}
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   291
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   292
#if defined(WIN32) || defined(WINCE)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   293
/**
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   294
 * Determine the base (personal dir and game data dir) paths
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   295
 * @param exe the path to the executable
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   296
 */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   297
extern void DetermineBasePaths(const char *exe);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   298
#else /* defined(WIN32) || defined(WINCE) */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   299
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   300
/**
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   301
 * Changes the working directory to the path of the give executable.
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   302
 * For OSX application bundles '.app' is the required extension of the bundle,
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   303
 * so when we crop the path to there, when can remove the name of the bundle
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   304
 * in the same way we remove the name from the executable name.
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   305
 * @param exe the path to the executable
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   306
 */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   307
void ChangeWorkingDirectory(const char *exe)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   308
{
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   309
#ifdef WITH_COCOA
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   310
	char *app_bundle = strchr(exe, '.');
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   311
	while (app_bundle != NULL && strncasecmp(app_bundle, ".app", 4) != 0) app_bundle = strchr(&app_bundle[1], '.');
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   312
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   313
	if (app_bundle != NULL) app_bundle[0] = '\0';
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   314
#endif /* WITH_COCOA */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   315
	char *s = strrchr(exe, PATHSEPCHAR);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   316
	if (s != NULL) {
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   317
		*s = '\0';
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   318
		chdir(exe);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   319
		*s = PATHSEPCHAR;
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   320
	}
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   321
#ifdef WITH_COCOA
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   322
	if (app_bundle != NULL) app_bundle[0] = '.';
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   323
#endif /* WITH_COCOA */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   324
}
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   325
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   326
/**
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   327
 * Determine the base (personal dir and game data dir) paths
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   328
 * @param exe the path to the executable
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   329
 */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   330
void DetermineBasePaths(const char *exe)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   331
{
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   332
	/* Change the working directory to enable doubleclicking in UIs */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   333
	ChangeWorkingDirectory(exe);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   334
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   335
	_paths.game_data_dir = BuildWithFullPath(GAME_DATA_DIR);
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   336
#if defined(SECOND_DATA_DIR)
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   337
	_paths.second_data_dir = BuildWithFullPath(SECOND_DATA_DIR);
9908
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9895
diff changeset
   338
#else
0fa543611bbe (svn r10112) [gamebalance] -Sync: r9520:9620 from trunk
celestar
parents: 9895
diff changeset
   339
	_paths.second_data_dir = NULL;
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   340
#endif
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   341
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   342
#if defined(USE_HOMEDIR)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   343
	const char *homedir = getenv("HOME");
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   344
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   345
	if (homedir == NULL) {
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   346
		const struct passwd *pw = getpwuid(getuid());
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   347
		if (pw != NULL) homedir = pw->pw_dir;
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   348
	}
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   349
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   350
	_paths.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   351
	AppendPathSeparator(_paths.personal_dir, MAX_PATH);
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   352
#else /* not defined(USE_HOMEDIR) */
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   353
	_paths.personal_dir = BuildWithFullPath(PERSONAL_DIR);
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   354
#endif /* defined(USE_HOMEDIR) */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   355
}
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   356
#endif /* defined(WIN32) || defined(WINCE) */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   357
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   358
/**
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   359
 * Acquire the base paths (personal dir and game data dir),
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   360
 * fill all other paths (save dir, autosave dir etc) and
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   361
 * make the save and scenario directories.
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   362
 * @param exe the path to the executable
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   363
 * @todo for save_dir, autosave_dir, scenario_dir and heightmap_dir the
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   364
 *       assumption is that there is no path separator, however for gm_dir
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   365
 *       lang_dir and data_dir that assumption is made.
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   366
 *       This inconsistency should be resolved.
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   367
 */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   368
void DeterminePaths(const char *exe)
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   369
{
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   370
	DetermineBasePaths(exe);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   371
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   372
	_paths.save_dir      = str_fmt("%ssave" PATHSEP, _paths.personal_dir);
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   373
	_paths.autosave_dir  = str_fmt("%s" PATHSEP "autosave" PATHSEP, _paths.save_dir);
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   374
	_paths.scenario_dir  = str_fmt("%sscenario" PATHSEP, _paths.personal_dir);
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   375
	_paths.heightmap_dir = str_fmt("%s" PATHSEP "heightmap" PATHSEP, _paths.scenario_dir);
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   376
	_paths.gm_dir        = str_fmt("%sgm" PATHSEP, _paths.game_data_dir);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   377
	_paths.data_dir      = str_fmt("%sdata" PATHSEP, _paths.game_data_dir);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   378
#if defined(CUSTOM_LANG_DIR)
9912
1ac8aac92385 (svn r10136) [gamebalance] -Sync: r9900:10100 from trunk
celestar
parents: 9908
diff changeset
   379
	_paths.lang_dir = BuildWithFullPath(CUSTOM_LANG_DIR);
9895
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   380
#else
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   381
	_paths.lang_dir = str_fmt("%slang" PATHSEP, _paths.game_data_dir);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   382
#endif
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   383
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   384
	if (_config_file == NULL) {
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   385
		_config_file = str_fmt("%sopenttd.cfg", _paths.personal_dir);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   386
	}
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   387
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   388
	_highscore_file = str_fmt("%shs.dat", _paths.personal_dir);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   389
	_log_file = str_fmt("%sopenttd.log",  _paths.personal_dir);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   390
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   391
	/* Make (auto)save and scenario folder */
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   392
	FioCreateDirectory(_paths.save_dir);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   393
	FioCreateDirectory(_paths.autosave_dir);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   394
	FioCreateDirectory(_paths.scenario_dir);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   395
	FioCreateDirectory(_paths.heightmap_dir);
7bd07f43b0e3 (svn r9321) [gamebalance] -Sync: r9025:9314 from trunk
celestar
parents: 6527
diff changeset
   396
}