annotate src/fileio.cpp @ 7334:e9bc6de8292f draft

(svn r10697) -Codechange: give a more sensible names to some of the unkX variables.
author rubidium <rubidium@openttd.org>
date Thu, 26 Jul 2007 15:37:19 +0000
parents 616e563a689c
children 28ae6d57097b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1 /* $Id$ */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2
6201
3b141366478a (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas <belugas@openttd.org>
parents: 6179
diff changeset
3 /** @file fileio.cpp Standard In/Out file operations */
6179
e3e61b92574b (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 5967
diff changeset
4
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
5 #include "stdafx.h"
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
6 #include "openttd.h"
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
7 #include "fileio.h"
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
8 #include "functions.h"
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
9 #include "string.h"
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
10 #include "macros.h"
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
11 #include "variables.h"
5967
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
12 #include "debug.h"
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
13 #include "fios.h"
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
14 #ifdef WIN32
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
15 #include <windows.h>
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
16 #else
6323
b6002af339c0 (svn r9281) -Fix [FS#687]: compile failure when compiling with 'home directories' enabled (stepancheg).
rubidium <rubidium@openttd.org>
parents: 6320
diff changeset
17 #include <pwd.h>
6317
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
18 #include <unistd.h>
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
19 #include <sys/stat.h>
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
20 #endif
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
21
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
22 /*************************************************/
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
23 /* FILE IO ROUTINES ******************************/
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
24 /*************************************************/
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
25
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
26 #define FIO_BUFFER_SIZE 512
5967
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
27 #define MAX_HANDLES 64
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
28
6248
0789677a15a0 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium <rubidium@openttd.org>
parents: 6247
diff changeset
29 struct Fio {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
30 byte *buffer, *buffer_end; ///< position pointer in local buffer and last valid byte of buffer
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
31 uint32 pos; ///< current (system) position in file
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
32 FILE *cur_fh; ///< current file handle
6896
547d97a47394 (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight <truelight@openttd.org>
parents: 6875
diff changeset
33 const char *filename; ///< current filename
5967
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
34 FILE *handles[MAX_HANDLES]; ///< array of file handles we can have open
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
35 byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
6896
547d97a47394 (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight <truelight@openttd.org>
parents: 6875
diff changeset
36 const char *filenames[MAX_HANDLES]; ///< array of filenames we (should) have open
5967
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
37 #if defined(LIMITED_FDS)
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
38 uint open_handles; ///< current amount of open handles
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
39 uint usage_count[MAX_HANDLES]; ///< count how many times this file has been opened
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
40 #endif /* LIMITED_FDS */
6248
0789677a15a0 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium <rubidium@openttd.org>
parents: 6247
diff changeset
41 };
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
42
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
43 static Fio _fio;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
44
6179
e3e61b92574b (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 5967
diff changeset
45 /* Get current position in file */
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6201
diff changeset
46 uint32 FioGetPos()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
47 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
48 return _fio.pos + (_fio.buffer - _fio.buffer_start) - FIO_BUFFER_SIZE;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
49 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
50
6896
547d97a47394 (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight <truelight@openttd.org>
parents: 6875
diff changeset
51 const char *FioGetFilename()
547d97a47394 (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight <truelight@openttd.org>
parents: 6875
diff changeset
52 {
547d97a47394 (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight <truelight@openttd.org>
parents: 6875
diff changeset
53 return _fio.filename;
547d97a47394 (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight <truelight@openttd.org>
parents: 6875
diff changeset
54 }
547d97a47394 (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight <truelight@openttd.org>
parents: 6875
diff changeset
55
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
56 void FioSeekTo(uint32 pos, int mode)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
57 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
58 if (mode == SEEK_CUR) pos += FioGetPos();
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
59 _fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
60 _fio.pos = pos;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
61 fseek(_fio.cur_fh, _fio.pos, SEEK_SET);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
62 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
63
5967
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
64 #if defined(LIMITED_FDS)
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
65 static void FioRestoreFile(int slot)
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
66 {
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
67 /* Do we still have the file open, or should we reopen it? */
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
68 if (_fio.handles[slot] == NULL) {
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
69 DEBUG(misc, 6, "Restoring file '%s' in slot '%d' from disk", _fio.filename[slot], slot);
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
70 FioOpenFile(slot, _fio.filename[slot]);
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
71 }
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
72 _fio.usage_count[slot]++;
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
73 }
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
74 #endif /* LIMITED_FDS */
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
75
6179
e3e61b92574b (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas <belugas@openttd.org>
parents: 5967
diff changeset
76 /* Seek to a file and a position */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
77 void FioSeekToFile(uint32 pos)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
78 {
5967
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
79 FILE *f;
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
80 #if defined(LIMITED_FDS)
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
81 /* Make sure we have this file open */
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
82 FioRestoreFile(pos >> 24);
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
83 #endif /* LIMITED_FDS */
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
84 f = _fio.handles[pos >> 24];
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
85 assert(f != NULL);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
86 _fio.cur_fh = f;
6896
547d97a47394 (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight <truelight@openttd.org>
parents: 6875
diff changeset
87 _fio.filename = _fio.filenames[pos >> 24];
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
88 FioSeekTo(GB(pos, 0, 24), SEEK_SET);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
89 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
90
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6201
diff changeset
91 byte FioReadByte()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
92 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
93 if (_fio.buffer == _fio.buffer_end) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
94 _fio.pos += FIO_BUFFER_SIZE;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
95 fread(_fio.buffer = _fio.buffer_start, 1, FIO_BUFFER_SIZE, _fio.cur_fh);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
96 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
97 return *_fio.buffer++;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
98 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
99
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
100 void FioSkipBytes(int n)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
101 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
102 for (;;) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
103 int m = min(_fio.buffer_end - _fio.buffer, n);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
104 _fio.buffer += m;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
105 n -= m;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
106 if (n == 0) break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
107 FioReadByte();
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
108 n--;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
109 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
110 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
111
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6201
diff changeset
112 uint16 FioReadWord()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
113 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
114 byte b = FioReadByte();
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
115 return (FioReadByte() << 8) | b;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
116 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
117
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6201
diff changeset
118 uint32 FioReadDword()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
119 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
120 uint b = FioReadWord();
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
121 return (FioReadWord() << 16) | b;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
122 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
123
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
124 void FioReadBlock(void *ptr, uint size)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
125 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
126 FioSeekTo(FioGetPos(), SEEK_SET);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
127 _fio.pos += size;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
128 fread(ptr, 1, size, _fio.cur_fh);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
129 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
130
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
131 static inline void FioCloseFile(int slot)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
132 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
133 if (_fio.handles[slot] != NULL) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
134 fclose(_fio.handles[slot]);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
135 _fio.handles[slot] = NULL;
5967
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
136 #if defined(LIMITED_FDS)
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
137 _fio.open_handles--;
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
138 #endif /* LIMITED_FDS */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
139 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
140 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
141
6247
57363e064324 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents: 6201
diff changeset
142 void FioCloseAll()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
143 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
144 int i;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
145
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
146 for (i = 0; i != lengthof(_fio.handles); i++)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
147 FioCloseFile(i);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
148 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
149
5967
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
150 #if defined(LIMITED_FDS)
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
151 static void FioFreeHandle()
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
152 {
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
153 /* If we are about to open a file that will exceed the limit, close a file */
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
154 if (_fio.open_handles + 1 == LIMITED_FDS) {
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
155 uint i, count;
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
156 int slot;
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
157
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
158 count = UINT_MAX;
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
159 slot = -1;
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
160 /* Find the file that is used the least */
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
161 for (i = 0; i < lengthof(_fio.handles); i++) {
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
162 if (_fio.handles[i] != NULL && _fio.usage_count[i] < count) {
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
163 count = _fio.usage_count[i];
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
164 slot = i;
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
165 }
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
166 }
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
167 assert(slot != -1);
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
168 DEBUG(misc, 6, "Closing filehandler '%s' in slot '%d' because of fd-limit", _fio.filename[slot], slot);
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
169 FioCloseFile(slot);
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
170 }
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
171 }
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
172 #endif /* LIMITED_FDS */
0516aee3881f (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 <truelight@openttd.org>
parents: 5584
diff changeset
173
6299
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
174 void FioOpenFile(int slot, const char *filename)
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
175 {
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
176 FILE *f;
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
177
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
178 #if defined(LIMITED_FDS)
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
179 FioFreeHandle();
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
180 #endif /* LIMITED_FDS */
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
181 f = FioFOpenFile(filename);
6424
d0c943520987 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium <rubidium@openttd.org>
parents: 6323
diff changeset
182 if (f == NULL) error("Cannot open file '%s'", filename);
6299
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
183
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
184 FioCloseFile(slot); // if file was opened before, close it
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
185 _fio.handles[slot] = f;
6896
547d97a47394 (svn r10143) -Add: store the filename of the grfs opened and allow easy access to the name
truelight <truelight@openttd.org>
parents: 6875
diff changeset
186 _fio.filenames[slot] = filename;
6299
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
187 #if defined(LIMITED_FDS)
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
188 _fio.usage_count[slot] = 0;
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
189 _fio.open_handles++;
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
190 #endif /* LIMITED_FDS */
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
191 FioSeekToFile(slot << 24);
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
192 }
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
193
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
194 const char *_subdirs[NUM_SUBDIRS] = {
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
195 "",
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
196 "save" PATHSEP,
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
197 "save" PATHSEP "autosave" PATHSEP,
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
198 "scenario" PATHSEP,
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
199 "scenario" PATHSEP "heightmap" PATHSEP,
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
200 "gm" PATHSEP,
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
201 "data" PATHSEP,
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
202 "lang" PATHSEP
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
203 };
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
204
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
205 const char *_searchpaths[NUM_SEARCHPATHS];
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
206
6299
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
207 /**
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
208 * Check whether the given file exists
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
209 * @param filename the file to try for existance
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
210 * @param subdir the subdirectory to look in
6299
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
211 * @return true if and only if the file can be opened
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
212 */
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
213 bool FioCheckFileExists(const char *filename, Subdirectory subdir)
6299
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
214 {
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
215 FILE *f = FioFOpenFile(filename, "rb", subdir);
6299
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
216 if (f == NULL) return false;
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
217
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
218 fclose(f);
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
219 return true;
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
220 }
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
221
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
222 char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename)
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
223 {
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
224 assert(subdir < NUM_SUBDIRS);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
225 assert(sp < NUM_SEARCHPATHS);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
226
6941
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
227 snprintf(buf, buflen, "%s%s%s", _searchpaths[sp], _subdirs[subdir], filename);
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
228 return buf;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
229 }
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
230
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
231 char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
232 {
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
233 Searchpath sp;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
234 assert(subdir < NUM_SUBDIRS);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
235
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
236 FOR_ALL_SEARCHPATHS(sp) {
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
237 FioGetFullPath(buf, buflen, sp, subdir, filename);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
238 if (FileExists(buf)) break;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
239 }
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
240
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
241 return buf;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
242 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
243
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
244 char *FioAppendDirectory(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir)
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
245 {
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
246 assert(subdir < NUM_SUBDIRS);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
247 assert(sp < NUM_SEARCHPATHS);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
248
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
249 snprintf(buf, buflen, "%s%s", _searchpaths[sp], _subdirs[subdir]);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
250 return buf;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
251 }
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
252
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
253 char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir)
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
254 {
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
255 Searchpath sp;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
256
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
257 /* Find and return the first valid directory */
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
258 FOR_ALL_SEARCHPATHS(sp) {
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
259 char *ret = FioAppendDirectory(buf, buflen, sp, subdir);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
260 if (FileExists(buf)) return ret;
6424
d0c943520987 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium <rubidium@openttd.org>
parents: 6323
diff changeset
261 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
262
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
263 /* Could not find the directory, fall back to a base path */
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
264 ttd_strlcpy(buf, _personal_dir, buflen);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
265
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
266 return buf;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
267 }
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
268
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
269 FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir)
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
270 {
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
271 #if defined(WIN32) && defined(UNICODE)
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
272 /* fopen is implemented as a define with ellipses for
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
273 * Unicode support (prepend an L). As we are not sending
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
274 * a string, but a variable, it 'renames' the variable,
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
275 * so make that variable to makes it compile happily */
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
276 wchar_t Lmode[5];
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
277 MultiByteToWideChar(CP_ACP, 0, mode, -1, Lmode, lengthof(Lmode));
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
278 #endif
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
279 FILE *f = NULL;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
280 char buf[MAX_PATH];
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
281
6935
cb7bc383a325 (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium <rubidium@openttd.org>
parents: 6929
diff changeset
282 if (subdir == NO_DIRECTORY) {
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
283 ttd_strlcpy(buf, filename, lengthof(buf));
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
284 } else {
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
285 snprintf(buf, lengthof(buf), "%s%s%s", _searchpaths[sp], _subdirs[subdir], filename);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
286 }
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
287
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
288 f = fopen(buf, mode);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
289 #if !defined(WIN32)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
290 if (f == NULL) {
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
291 strtolower(buf + strlen(_searchpaths[sp]) - 1);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
292 f = fopen(buf, mode);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
293 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
294 #endif
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
295 return f;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
296 }
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
297
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
298 /** Opens OpenTTD files somewhere in a personal or global directory */
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
299 FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir)
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
300 {
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
301 FILE *f = NULL;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
302 Searchpath sp;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
303
6935
cb7bc383a325 (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium <rubidium@openttd.org>
parents: 6929
diff changeset
304 assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
305
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
306 FOR_ALL_SEARCHPATHS(sp) {
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
307 f = FioFOpenFileSp(filename, mode, sp, subdir);
6935
cb7bc383a325 (svn r10188) -Codechange: make it a little easier to load a savegame from the console:
rubidium <rubidium@openttd.org>
parents: 6929
diff changeset
308 if (f != NULL || subdir == NO_DIRECTORY) break;
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
309 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
310
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
311 return f;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
312 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
313
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
314 /**
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
315 * Create a directory with the given name
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
316 * @param name the new name of the directory
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
317 */
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
318 void FioCreateDirectory(const char *name)
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
319 {
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
320 #if defined(WIN32) || defined(WINCE)
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
321 CreateDirectory(OTTD2FS(name), NULL);
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
322 #elif defined(OS2) && !defined(__INNOTEK_LIBC__)
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
323 mkdir(OTTD2FS(name));
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
324 #else
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
325 mkdir(OTTD2FS(name), 0755);
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
326 #endif
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
327 }
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
328
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
329 /**
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
330 * Appends, if necessary, the path separator character to the end of the string.
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
331 * It does not add the path separator to zero-sized strings.
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
332 * @param buf string to append the separator to
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
333 * @param buflen the length of the buf
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
334 */
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
335 void AppendPathSeparator(char *buf, size_t buflen)
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
336 {
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
337 size_t s = strlen(buf);
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
338
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
339 /* Length of string + path separator + '\0' */
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
340 if (s != 0 && buf[s - 1] != PATHSEPCHAR && s + 2 < buflen) {
6299
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
341 buf[s] = PATHSEPCHAR;
b77652d15a0a (svn r9130) -Codechange: move the fileio functions that do open a file into a Fio slot together.
rubidium <rubidium@openttd.org>
parents: 6298
diff changeset
342 buf[s + 1] = '\0';
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
343 }
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
344 }
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
345
6834
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
346 /**
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
347 * Allocates and files a variable with the full path
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
348 * based on the given directory.
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
349 * @param dir the directory to base the path on
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
350 * @return the malloced full path
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
351 */
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
352 char *BuildWithFullPath(const char *dir)
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
353 {
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
354 char *dest = MallocT<char>(MAX_PATH);
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
355 ttd_strlcpy(dest, dir, MAX_PATH);
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
356
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
357 /* Check if absolute or relative path */
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
358 const char *s = strchr(dest, PATHSEPCHAR);
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
359
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
360 /* Add absolute path */
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
361 if (s == NULL || dest != s) {
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
362 getcwd(dest, MAX_PATH);
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
363 AppendPathSeparator(dest, MAX_PATH);
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
364 ttd_strlcat(dest, dir, MAX_PATH);
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
365 }
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
366 AppendPathSeparator(dest, MAX_PATH);
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
367
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
368 return dest;
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
369 }
3f0cfca3411d (svn r10073) -Codechange: make the NewGRF paths in the config file without any full paths (again).
rubidium <rubidium@openttd.org>
parents: 6755
diff changeset
370
6317
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
371 #if defined(WIN32) || defined(WINCE)
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
372 /**
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
373 * Determine the base (personal dir and game data dir) paths
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
374 * @param exe the path from the current path to the executable
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
375 * @note defined in the OS related files (os2.cpp, win32.cpp, unix.cpp etc)
6317
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
376 */
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
377 extern void DetermineBasePaths(const char *exe);
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
378 #else /* defined(WIN32) || defined(WINCE) */
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
379
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
380 /**
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
381 * Changes the working directory to the path of the give executable.
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
382 * For OSX application bundles '.app' is the required extension of the bundle,
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
383 * so when we crop the path to there, when can remove the name of the bundle
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
384 * in the same way we remove the name from the executable name.
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
385 * @param exe the path to the executable
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
386 */
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
387 void ChangeWorkingDirectory(const char *exe)
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
388 {
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
389 #ifdef WITH_COCOA
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
390 char *app_bundle = strchr(exe, '.');
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
391 while (app_bundle != NULL && strncasecmp(app_bundle, ".app", 4) != 0) app_bundle = strchr(&app_bundle[1], '.');
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
392
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
393 if (app_bundle != NULL) app_bundle[0] = '\0';
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
394 #endif /* WITH_COCOA */
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
395 char *s = strrchr(exe, PATHSEPCHAR);
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
396 if (s != NULL) {
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
397 *s = '\0';
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
398 chdir(exe);
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
399 *s = PATHSEPCHAR;
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
400 }
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
401 #ifdef WITH_COCOA
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
402 if (app_bundle != NULL) app_bundle[0] = '.';
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
403 #endif /* WITH_COCOA */
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
404 }
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
405
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
406 /**
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
407 * Determine the base (personal dir and game data dir) paths
6317
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
408 * @param exe the path to the executable
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
409 */
6317
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
410 void DetermineBasePaths(const char *exe)
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
411 {
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
412 char tmp[MAX_PATH];
6990
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6941
diff changeset
413 #if defined(__MORPHOS__) || defined(__AMIGA__) || !defined(WITH_PERSONAL_DIR)
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6941
diff changeset
414 _searchpaths[SP_PERSONAL_DIR] = NULL;
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6941
diff changeset
415 #else
6317
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
416 const char *homedir = getenv("HOME");
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
417
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
418 if (homedir == NULL) {
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
419 const struct passwd *pw = getpwuid(getuid());
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
420 homedir = (pw == NULL) ? "" : pw->pw_dir;
6317
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
421 }
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
422
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
423 snprintf(tmp, MAX_PATH, "%s" PATHSEP "%s", homedir, PERSONAL_DIR);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
424 AppendPathSeparator(tmp, MAX_PATH);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
425
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
426 _searchpaths[SP_PERSONAL_DIR] = strdup(tmp);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
427 #endif
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
428 _searchpaths[SP_SHARED_DIR] = NULL;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
429
6990
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6941
diff changeset
430 #if defined(__MORPHOS__) || defined(__AMIGA__)
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6941
diff changeset
431 _searchpaths[SP_WORKING_DIR] = NULL;
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6941
diff changeset
432 #else
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
433 getcwd(tmp, MAX_PATH);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
434 AppendPathSeparator(tmp, MAX_PATH);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
435 _searchpaths[SP_WORKING_DIR] = strdup(tmp);
6990
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6941
diff changeset
436 #endif
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
437
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
438 /* Change the working directory to that one of the executable */
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
439 ChangeWorkingDirectory((char*)exe);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
440 getcwd(tmp, MAX_PATH);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
441 AppendPathSeparator(tmp, MAX_PATH);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
442 _searchpaths[SP_BINARY_DIR] = strdup(tmp);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
443
6990
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6941
diff changeset
444 #if defined(__MORPHOS__) || defined(__AMIGA__)
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6941
diff changeset
445 _searchpaths[SP_INSTALLATION_DIR] = NULL;
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6941
diff changeset
446 #else
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
447 snprintf(tmp, MAX_PATH, "%s", GLOBAL_DATA_DIR);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
448 AppendPathSeparator(tmp, MAX_PATH);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
449 _searchpaths[SP_INSTALLATION_DIR] = strdup(tmp);
6990
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6941
diff changeset
450 #endif
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
451 #ifdef WITH_COCOA
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
452 extern void cocoaSetApplicationBundleDir();
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
453 cocoaSetApplicationBundleDir();
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
454 #else
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
455 _searchpaths[SP_APPLICATION_BUNDLE_DIR] = NULL;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
456 #endif
6317
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
457 }
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
458 #endif /* defined(WIN32) || defined(WINCE) */
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
459
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
460 char *_personal_dir;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
461
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
462 /**
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
463 * Acquire the base paths (personal dir and game data dir),
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
464 * fill all other paths (save dir, autosave dir etc) and
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
465 * make the save and scenario directories.
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
466 * @param exe the path from the current path to the executable
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
467 */
6317
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
468 void DeterminePaths(const char *exe)
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
469 {
6317
d5d3f3379eb0 (svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents: 6299
diff changeset
470 DetermineBasePaths(exe);
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
471
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
472 Searchpath sp;
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
473 FOR_ALL_SEARCHPATHS(sp) DEBUG(misc, 4, "%s added as search path", _searchpaths[sp]);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
474
6941
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
475 if (_config_file != NULL) {
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
476 _personal_dir = strdup(_config_file);
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
477 char *end = strrchr(_personal_dir , PATHSEPCHAR);
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
478 if (end == NULL) {
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
479 _personal_dir[0] = '\0';
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
480 } else {
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
481 end[1] = '\0';
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
482 }
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
483 } else {
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
484 char personal_dir[MAX_PATH];
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
485 FioFindFullPath(personal_dir, lengthof(personal_dir), BASE_DIR, "openttd.cfg");
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
486
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
487 if (FileExists(personal_dir)) {
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
488 char *end = strrchr(personal_dir, PATHSEPCHAR);
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
489 if (end != NULL) end[1] = '\0';
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
490 _personal_dir = strdup(personal_dir);
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
491 _config_file = str_fmt("%sopenttd.cfg", _personal_dir);
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
492 } else {
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
493 static const Searchpath new_openttd_cfg_order[] = {
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
494 SP_PERSONAL_DIR, SP_BINARY_DIR, SP_WORKING_DIR, SP_SHARED_DIR, SP_INSTALLATION_DIR
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
495 };
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
496
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
497 for (uint i = 0; i < lengthof(new_openttd_cfg_order); i++) {
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
498 if (IsValidSearchPath(new_openttd_cfg_order[i])) {
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
499 _personal_dir = strdup(_searchpaths[new_openttd_cfg_order[i]]);
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
500 _config_file = str_fmt("%sopenttd.cfg", _personal_dir);
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
501 break;
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
502 }
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
503 }
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
504 }
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
505 }
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
506
6941
0d0ce2f153a9 (svn r10194) -Codechange: prefer the current working directory above the user's home directory when openttd.cfg exists in the current working directory, otherwise prefer the home directory.
rubidium <rubidium@openttd.org>
parents: 6935
diff changeset
507 DEBUG(misc, 3, "%s found as personal directory", _personal_dir);
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
508
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
509 _highscore_file = str_fmt("%shs.dat", _personal_dir);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
510 _log_file = str_fmt("%sopenttd.log", _personal_dir);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
511
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
512 char *save_dir = str_fmt("%s%s", _personal_dir, FioGetSubdirectory(SAVE_DIR));
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
513 char *autosave_dir = str_fmt("%s%s", _personal_dir, FioGetSubdirectory(AUTOSAVE_DIR));
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
514
6929
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
515 /* Make the necessary folders */
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
516 FioCreateDirectory(_personal_dir);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
517 FioCreateDirectory(save_dir);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
518 FioCreateDirectory(autosave_dir);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
519
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
520 free(save_dir);
bb43858bed10 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents: 6896
diff changeset
521 free(autosave_dir);
6298
bd24b660fbe8 (svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents: 6248
diff changeset
522 }
6875
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
523
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
524 /**
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
525 * Sanitizes a filename, i.e. removes all illegal characters from it.
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
526 * @param filename the "\0" terminated filename
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
527 */
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
528 void SanitizeFilename(char *filename)
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
529 {
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
530 for (; *filename != '\0'; filename++) {
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
531 switch (*filename) {
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
532 /* The following characters are not allowed in filenames
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
533 * on at least one of the supported operating systems: */
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
534 case ':': case '\\': case '*': case '?': case '/':
7016
616e563a689c (svn r10272) -Fix (FS#916): remove more invalid characters from savegame names
glx <glx@openttd.org>
parents: 6990
diff changeset
535 case '<': case '>': case '|': case '"':
6875
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
536 *filename = '_';
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
537 break;
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
538 }
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
539 }
5e797b77132a (svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
rubidium <rubidium@openttd.org>
parents: 6834
diff changeset
540 }