author | KUDr |
Fri, 20 Apr 2007 19:43:06 +0000 | |
changeset 6513 | 454347ca3dfb |
parent 6424 | d410d4c31d15 |
child 6755 | 7cca96e92189 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
6201
bee01dc45e39
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
6179
diff
changeset
|
3 |
/** @file fileio.cpp Standard In/Out file operations */ |
6179
d19b0137d8e4
(svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents:
5967
diff
changeset
|
4 |
|
0 | 5 |
#include "stdafx.h" |
1891
862800791170
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1198
diff
changeset
|
6 |
#include "openttd.h" |
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1039
diff
changeset
|
7 |
#include "fileio.h" |
2163
b17b313113a0
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
8 |
#include "functions.h" |
4200
e6ba94085b81
(svn r5684) - Codechange: create an strtolower() function that uses tolower() on a whole string and apply it in the places this was used.
Darkvater
parents:
4077
diff
changeset
|
9 |
#include "string.h" |
2159
f6284cf5fab0
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2153
diff
changeset
|
10 |
#include "macros.h" |
2153
ecfc674410b4
(svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents:
1891
diff
changeset
|
11 |
#include "variables.h" |
5967
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
12 |
#include "debug.h" |
6298
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
13 |
#include "fios.h" |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
14 |
#ifndef WIN32 |
6323
0246c62cb1cb
(svn r9281) -Fix [FS#687]: compile failure when compiling with 'home directories' enabled (stepancheg).
rubidium
parents:
6320
diff
changeset
|
15 |
#include <pwd.h> |
6317
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
16 |
#include <unistd.h> |
6298
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
17 |
#include <sys/stat.h> |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
18 |
#endif |
0 | 19 |
|
20 |
/*************************************************/ |
|
21 |
/* FILE IO ROUTINES ******************************/ |
|
22 |
/*************************************************/ |
|
23 |
||
24 |
#define FIO_BUFFER_SIZE 512 |
|
5967
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
25 |
#define MAX_HANDLES 64 |
0 | 26 |
|
6248
e4a2ed7e5613
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6247
diff
changeset
|
27 |
struct Fio { |
4203
9f0dade554c1
(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 |
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
29 |
uint32 pos; ///< current (system) position in file |
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
30 |
FILE *cur_fh; ///< current file handle |
5967
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
31 |
FILE *handles[MAX_HANDLES]; ///< array of file handles we can have open |
4203
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
32 |
byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file |
5967
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
33 |
#if defined(LIMITED_FDS) |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
34 |
uint open_handles; ///< current amount of open handles |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
35 |
const char *filename[MAX_HANDLES]; ///< array of filenames we (should) have open |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
36 |
uint usage_count[MAX_HANDLES]; ///< count how many times this file has been opened |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
37 |
#endif /* LIMITED_FDS */ |
6248
e4a2ed7e5613
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6247
diff
changeset
|
38 |
}; |
0 | 39 |
|
40 |
static Fio _fio; |
|
41 |
||
6179
d19b0137d8e4
(svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents:
5967
diff
changeset
|
42 |
/* Get current position in file */ |
6247 | 43 |
uint32 FioGetPos() |
0 | 44 |
{ |
45 |
return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE; |
|
46 |
} |
|
47 |
||
48 |
void FioSeekTo(uint32 pos, int mode) |
|
49 |
{ |
|
50 |
if (mode == SEEK_CUR) pos += FioGetPos(); |
|
51 |
_fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE; |
|
4203
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
52 |
_fio.pos = pos; |
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
53 |
fseek(_fio.cur_fh, _fio.pos, SEEK_SET); |
0 | 54 |
} |
55 |
||
5967
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
56 |
#if defined(LIMITED_FDS) |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
57 |
static void FioRestoreFile(int slot) |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
58 |
{ |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
59 |
/* Do we still have the file open, or should we reopen it? */ |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
60 |
if (_fio.handles[slot] == NULL) { |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
61 |
DEBUG(misc, 6, "Restoring file '%s' in slot '%d' from disk", _fio.filename[slot], slot); |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
62 |
FioOpenFile(slot, _fio.filename[slot]); |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
63 |
} |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
64 |
_fio.usage_count[slot]++; |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
65 |
} |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
66 |
#endif /* LIMITED_FDS */ |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
67 |
|
6179
d19b0137d8e4
(svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents:
5967
diff
changeset
|
68 |
/* Seek to a file and a position */ |
0 | 69 |
void FioSeekToFile(uint32 pos) |
70 |
{ |
|
5967
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
71 |
FILE *f; |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
72 |
#if defined(LIMITED_FDS) |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
73 |
/* Make sure we have this file open */ |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
74 |
FioRestoreFile(pos >> 24); |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
75 |
#endif /* LIMITED_FDS */ |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
76 |
f = _fio.handles[pos >> 24]; |
0 | 77 |
assert(f != NULL); |
78 |
_fio.cur_fh = f; |
|
4203
9f0dade554c1
(svn r5687) - Cleanup: Some cleanup and commentarizing.
Darkvater
parents:
4202
diff
changeset
|
79 |
FioSeekTo(GB(pos, 0, 24), SEEK_SET); |
0 | 80 |
} |
81 |
||
6247 | 82 |
byte FioReadByte() |
0 | 83 |
{ |
84 |
if (_fio.buffer == _fio.buffer_end) { |
|
85 |
_fio.pos += FIO_BUFFER_SIZE; |
|
86 |
fread(_fio.buffer = _fio.buffer_start, 1, FIO_BUFFER_SIZE, _fio.cur_fh); |
|
87 |
} |
|
88 |
return *_fio.buffer++; |
|
89 |
} |
|
90 |
||
91 |
void FioSkipBytes(int n) |
|
92 |
{ |
|
2952 | 93 |
for (;;) { |
0 | 94 |
int m = min(_fio.buffer_end - _fio.buffer, n); |
95 |
_fio.buffer += m; |
|
96 |
n -= m; |
|
97 |
if (n == 0) break; |
|
98 |
FioReadByte(); |
|
99 |
n--; |
|
100 |
} |
|
101 |
} |
|
102 |
||
6247 | 103 |
uint16 FioReadWord() |
0 | 104 |
{ |
105 |
byte b = FioReadByte(); |
|
106 |
return (FioReadByte() << 8) | b; |
|
107 |
} |
|
108 |
||
6247 | 109 |
uint32 FioReadDword() |
0 | 110 |
{ |
111 |
uint b = FioReadWord(); |
|
112 |
return (FioReadWord() << 16) | b; |
|
113 |
} |
|
114 |
||
115 |
void FioReadBlock(void *ptr, uint size) |
|
116 |
{ |
|
117 |
FioSeekTo(FioGetPos(), SEEK_SET); |
|
118 |
_fio.pos += size; |
|
119 |
fread(ptr, 1, size, _fio.cur_fh); |
|
120 |
} |
|
121 |
||
1039
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
122 |
static inline void FioCloseFile(int slot) |
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
123 |
{ |
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
124 |
if (_fio.handles[slot] != NULL) { |
1109
ecb98f43ba2c
(svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents:
1093
diff
changeset
|
125 |
fclose(_fio.handles[slot]); |
1039
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
126 |
_fio.handles[slot] = NULL; |
5967
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
127 |
#if defined(LIMITED_FDS) |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
128 |
_fio.open_handles--; |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
129 |
#endif /* LIMITED_FDS */ |
1039
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
130 |
} |
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
131 |
} |
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
132 |
|
6247 | 133 |
void FioCloseAll() |
0 | 134 |
{ |
135 |
int i; |
|
136 |
||
1039
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
137 |
for (i = 0; i != lengthof(_fio.handles); i++) |
309068e65b16
(svn r1540) -Fix: since grf files are reopened on every load/new game, close the old opened to fix stale filehandles (thx tamlin)
darkvater
parents:
1036
diff
changeset
|
138 |
FioCloseFile(i); |
0 | 139 |
} |
140 |
||
5967
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
141 |
#if defined(LIMITED_FDS) |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
142 |
static void FioFreeHandle() |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
143 |
{ |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
144 |
/* If we are about to open a file that will exceed the limit, close a file */ |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
145 |
if (_fio.open_handles + 1 == LIMITED_FDS) { |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
146 |
uint i, count; |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
147 |
int slot; |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
148 |
|
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
149 |
count = UINT_MAX; |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
150 |
slot = -1; |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
151 |
/* Find the file that is used the least */ |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
152 |
for (i = 0; i < lengthof(_fio.handles); i++) { |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
153 |
if (_fio.handles[i] != NULL && _fio.usage_count[i] < count) { |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
154 |
count = _fio.usage_count[i]; |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
155 |
slot = i; |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
156 |
} |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
157 |
} |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
158 |
assert(slot != -1); |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
159 |
DEBUG(misc, 6, "Closing filehandler '%s' in slot '%d' because of fd-limit", _fio.filename[slot], slot); |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
160 |
FioCloseFile(slot); |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
161 |
} |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
162 |
} |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
163 |
#endif /* LIMITED_FDS */ |
e8c5596608ad
(svn r8647) -Codechange: add a general way to handle platforms who can only have a limited amount of file-descripters open at any given time.
truelight
parents:
5584
diff
changeset
|
164 |
|
6299
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
165 |
void FioOpenFile(int slot, const char *filename) |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
166 |
{ |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
167 |
FILE *f; |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
168 |
|
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
169 |
#if defined(LIMITED_FDS) |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
170 |
FioFreeHandle(); |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
171 |
#endif /* LIMITED_FDS */ |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
172 |
f = FioFOpenFile(filename); |
6424
d410d4c31d15
(svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents:
6323
diff
changeset
|
173 |
if (f == NULL) error("Cannot open file '%s'", filename); |
6299
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
174 |
|
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
175 |
FioCloseFile(slot); // if file was opened before, close it |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
176 |
_fio.handles[slot] = f; |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
177 |
#if defined(LIMITED_FDS) |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
178 |
_fio.filename[slot] = filename; |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
179 |
_fio.usage_count[slot] = 0; |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
180 |
_fio.open_handles++; |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
181 |
#endif /* LIMITED_FDS */ |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
182 |
FioSeekToFile(slot << 24); |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
183 |
} |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
184 |
|
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
185 |
/** |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
186 |
* Check whether the given file exists |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
187 |
* @param filename the file to try for existance |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
188 |
* @return true if and only if the file can be opened |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
189 |
*/ |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
190 |
bool FioCheckFileExists(const char *filename) |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
191 |
{ |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
192 |
FILE *f = FioFOpenFile(filename); |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
193 |
if (f == NULL) return false; |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
194 |
|
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
195 |
fclose(f); |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
196 |
return true; |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
197 |
} |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
198 |
|
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
199 |
/** |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
200 |
* Opens the file with the given name |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
201 |
* @param filename the file to open (in either data_dir or second_data_dir) |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
202 |
* @return the opened file or NULL when it failed. |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
203 |
*/ |
2736
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
204 |
FILE *FioFOpenFile(const char *filename) |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
205 |
{ |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
206 |
FILE *f; |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
207 |
char buf[MAX_PATH]; |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
208 |
|
6424
d410d4c31d15
(svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents:
6323
diff
changeset
|
209 |
if (strrchr(filename, PATHSEPCHAR) == NULL) { |
d410d4c31d15
(svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents:
6323
diff
changeset
|
210 |
snprintf(buf, lengthof(buf), "%s%s", _paths.data_dir, filename); |
d410d4c31d15
(svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents:
6323
diff
changeset
|
211 |
} else { |
d410d4c31d15
(svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents:
6323
diff
changeset
|
212 |
ttd_strlcpy(buf, filename, lengthof(buf)); |
d410d4c31d15
(svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents:
6323
diff
changeset
|
213 |
} |
2736
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
214 |
|
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
215 |
f = fopen(buf, "rb"); |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
216 |
#if !defined(WIN32) |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
217 |
if (f == NULL) { |
6424
d410d4c31d15
(svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents:
6323
diff
changeset
|
218 |
strtolower(strrchr(buf, PATHSEPCHAR)); |
2736
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
219 |
f = fopen(buf, "rb"); |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
220 |
|
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
221 |
#if defined SECOND_DATA_DIR |
6179
d19b0137d8e4
(svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents:
5967
diff
changeset
|
222 |
/* tries in the 2nd data directory */ |
2736
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
223 |
if (f == NULL) { |
5296
e7acddfdd8a7
(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); |
e7acddfdd8a7
(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
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
226 |
f = fopen(buf, "rb"); |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
227 |
} |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
228 |
#endif |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
229 |
} |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
230 |
#endif |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
231 |
|
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
232 |
return f; |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
233 |
} |
3d6487cbbb69
(svn r3281) -Feature: [OSX] added native cocoa sound and video drivers (egladil)
bjarni
parents:
2548
diff
changeset
|
234 |
|
6298
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
235 |
/** |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
236 |
* Create a directory with the given name |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
237 |
* @param name the new name of the directory |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
238 |
*/ |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
239 |
void FioCreateDirectory(const char *name) |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
240 |
{ |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
241 |
#if defined(WIN32) || defined(WINCE) |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
242 |
CreateDirectory(OTTD2FS(name), NULL); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
243 |
#elif defined(OS2) && !defined(__INNOTEK_LIBC__) |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
244 |
mkdir(OTTD2FS(name)); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
245 |
#else |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
246 |
mkdir(OTTD2FS(name), 0755); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
247 |
#endif |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
248 |
} |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
249 |
|
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
250 |
/** |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
251 |
* Appends, if necessary, the path separator character to the end of the string. |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
252 |
* It does not add the path separator to zero-sized strings. |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
253 |
* @param buf string to append the separator to |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
254 |
* @param buflen the length of the buf |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
255 |
*/ |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
256 |
void AppendPathSeparator(char *buf, size_t buflen) |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
257 |
{ |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
258 |
size_t s = strlen(buf); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
259 |
|
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
260 |
/* Length of string + path separator + '\0' */ |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
261 |
if (s != 0 && buf[s - 1] != PATHSEPCHAR && s + 2 < buflen) { |
6299
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
262 |
buf[s] = PATHSEPCHAR; |
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
263 |
buf[s + 1] = '\0'; |
6298
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
264 |
} |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
265 |
} |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
266 |
|
6317
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
267 |
#if defined(WIN32) || defined(WINCE) |
6298
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
268 |
/** |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
269 |
* Determine the base (personal dir and game data dir) paths |
6317
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
270 |
* @param exe the path to the executable |
6298
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
271 |
*/ |
6317
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
272 |
extern void DetermineBasePaths(const char *exe); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
273 |
#else /* defined(WIN32) || defined(WINCE) */ |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
274 |
|
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
275 |
/** |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
276 |
* Changes the working directory to the path of the give executable. |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
277 |
* For OSX application bundles '.app' is the required extension of the bundle, |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
278 |
* so when we crop the path to there, when can remove the name of the bundle |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
279 |
* in the same way we remove the name from the executable name. |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
280 |
* @param exe the path to the executable |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
281 |
*/ |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
282 |
void ChangeWorkingDirectory(const char *exe) |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
283 |
{ |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
284 |
#ifdef WITH_COCOA |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
285 |
char *app_bundle = strchr(exe, '.'); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
286 |
while (app_bundle != NULL && strncasecmp(app_bundle, ".app", 4) != 0) app_bundle = strchr(&app_bundle[1], '.'); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
287 |
|
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
288 |
if (app_bundle != NULL) app_bundle[0] = '\0'; |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
289 |
#endif /* WITH_COCOA */ |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
290 |
char *s = strrchr(exe, PATHSEPCHAR); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
291 |
if (s != NULL) { |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
292 |
*s = '\0'; |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
293 |
chdir(exe); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
294 |
*s = PATHSEPCHAR; |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
295 |
} |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
296 |
#ifdef WITH_COCOA |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
297 |
if (app_bundle != NULL) app_bundle[0] = '.'; |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
298 |
#endif /* WITH_COCOA */ |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
299 |
} |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
300 |
|
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
301 |
/** |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
302 |
* Determine the base (personal dir and game data dir) paths |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
303 |
* @param exe the path to the executable |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
304 |
*/ |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
305 |
void DetermineBasePaths(const char *exe) |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
306 |
{ |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
307 |
/* Change the working directory to enable doubleclicking in UIs */ |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
308 |
ChangeWorkingDirectory(exe); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
309 |
|
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
310 |
_paths.game_data_dir = MallocT<char>(MAX_PATH); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
311 |
ttd_strlcpy(_paths.game_data_dir, GAME_DATA_DIR, MAX_PATH); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
312 |
#if defined(SECOND_DATA_DIR) |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
313 |
_paths.second_data_dir = MallocT<char>(MAX_PATH); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
314 |
ttd_strlcpy(_paths.second_data_dir, SECOND_DATA_DIR, MAX_PATH); |
6424
d410d4c31d15
(svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents:
6323
diff
changeset
|
315 |
#else |
d410d4c31d15
(svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents:
6323
diff
changeset
|
316 |
_paths.second_data_dir = NULL; |
6317
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
317 |
#endif |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
318 |
|
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
319 |
#if defined(USE_HOMEDIR) |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
320 |
const char *homedir = getenv("HOME"); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
321 |
|
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
322 |
if (homedir == NULL) { |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
323 |
const struct passwd *pw = getpwuid(getuid()); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
324 |
if (pw != NULL) homedir = pw->pw_dir; |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
325 |
} |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
326 |
|
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
327 |
_paths.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
328 |
#else /* not defined(USE_HOMEDIR) */ |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
329 |
_paths.personal_dir = MallocT<char>(MAX_PATH); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
330 |
ttd_strlcpy(_paths.personal_dir, PERSONAL_DIR, MAX_PATH); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
331 |
|
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
332 |
/* check if absolute or relative path */ |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
333 |
const char *s = strchr(_paths.personal_dir, PATHSEPCHAR); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
334 |
|
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
335 |
/* add absolute path */ |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
336 |
if (s == NULL || _paths.personal_dir != s) { |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
337 |
getcwd(_paths.personal_dir, MAX_PATH); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
338 |
AppendPathSeparator(_paths.personal_dir, MAX_PATH); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
339 |
ttd_strlcat(_paths.personal_dir, PERSONAL_DIR, MAX_PATH); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
340 |
} |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
341 |
#endif /* defined(USE_HOMEDIR) */ |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
342 |
|
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
343 |
AppendPathSeparator(_paths.personal_dir, MAX_PATH); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
344 |
AppendPathSeparator(_paths.game_data_dir, MAX_PATH); |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
345 |
} |
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
346 |
#endif /* defined(WIN32) || defined(WINCE) */ |
6298
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
347 |
|
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
348 |
/** |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
349 |
* Acquire the base paths (personal dir and game data dir), |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
350 |
* fill all other paths (save dir, autosave dir etc) and |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
351 |
* make the save and scenario directories. |
6317
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
352 |
* @param exe the path to the executable |
6298
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
353 |
* @todo for save_dir, autosave_dir, scenario_dir and heightmap_dir the |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
354 |
* assumption is that there is no path separator, however for gm_dir |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
355 |
* lang_dir and data_dir that assumption is made. |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
356 |
* This inconsistency should be resolved. |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
357 |
*/ |
6317
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
358 |
void DeterminePaths(const char *exe) |
6298
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
359 |
{ |
6317
70f4e9e52eb1
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium
parents:
6299
diff
changeset
|
360 |
DetermineBasePaths(exe); |
6298
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
361 |
|
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
362 |
_paths.save_dir = str_fmt("%ssave", _paths.personal_dir); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
363 |
_paths.autosave_dir = str_fmt("%s" PATHSEP "autosave", _paths.save_dir); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
364 |
_paths.scenario_dir = str_fmt("%sscenario", _paths.personal_dir); |
6299
a3757291fd8c
(svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium
parents:
6298
diff
changeset
|
365 |
_paths.heightmap_dir = str_fmt("%s" PATHSEP "heightmap", _paths.scenario_dir); |
6298
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
366 |
_paths.gm_dir = str_fmt("%sgm" PATHSEP, _paths.game_data_dir); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
367 |
_paths.data_dir = str_fmt("%sdata" PATHSEP, _paths.game_data_dir); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
368 |
#if defined(CUSTOM_LANG_DIR) |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
369 |
/* Sets the search path for lng files to the custom one */ |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
370 |
_paths.lang_dir = MallocT<char>(MAX_PATH); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
371 |
ttd_strlcpy(_paths.lang_dir, CUSTOM_LANG_DIR, MAX_PATH); |
6320
32d0f299e2fd
(svn r9271) -Codechange: make the language pack initialisation a little more clear and extendable (more language paths).
rubidium
parents:
6317
diff
changeset
|
372 |
AppendPathSeparator(_paths.lang_dir, MAX_PATH); |
6298
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
373 |
#else |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
374 |
_paths.lang_dir = str_fmt("%slang" PATHSEP, _paths.game_data_dir); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
375 |
#endif |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
376 |
|
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
377 |
if (_config_file == NULL) { |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
378 |
_config_file = str_fmt("%sopenttd.cfg", _paths.personal_dir); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
379 |
} |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
380 |
|
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
381 |
_highscore_file = str_fmt("%shs.dat", _paths.personal_dir); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
382 |
_log_file = str_fmt("%sopenttd.log", _paths.personal_dir); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
383 |
|
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
384 |
/* Make (auto)save and scenario folder */ |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
385 |
FioCreateDirectory(_paths.save_dir); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
386 |
FioCreateDirectory(_paths.autosave_dir); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
387 |
FioCreateDirectory(_paths.scenario_dir); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
388 |
FioCreateDirectory(_paths.heightmap_dir); |
01c80746f308
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium
parents:
6248
diff
changeset
|
389 |
} |