Mercurial > hg > openttd
annotate src/fileio.cpp @ 20661:1e53dec875f8 draft
(svn r25617) -Fix [FS#5655] (r25377): crash when Ctrl+clicking the start date button in timetable window without any orders
author | rubidium <rubidium@openttd.org> |
---|---|
date | Wed, 17 Jul 2013 18:37:13 +0000 |
parents | 4bafd180b961 |
children |
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 |
12778
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12174
diff
changeset
|
3 /* |
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12174
diff
changeset
|
4 * This file is part of OpenTTD. |
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12174
diff
changeset
|
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. |
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12174
diff
changeset
|
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12174
diff
changeset
|
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>. |
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12174
diff
changeset
|
8 */ |
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12174
diff
changeset
|
9 |
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
|
10 /** @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
|
11 |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
12 #include "stdafx.h" |
10039
6301378f3e0f
(svn r14199) -Codechange: split fileio.h into fileio_type.h and fileio_func.h so not everything that includes saveload.h needs to include everything else too.
rubidium <rubidium@openttd.org>
parents:
10036
diff
changeset
|
13 #include "fileio_func.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
|
14 #include "debug.h" |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
15 #include "fios.h" |
8214
6385dffc0b37
(svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium <rubidium@openttd.org>
parents:
8131
diff
changeset
|
16 #include "string_func.h" |
9581
9a3e1ee617ce
(svn r13618) -Codechange: move tar-specific declarations to separate file
smatz <smatz@openttd.org>
parents:
9470
diff
changeset
|
17 #include "tar_type.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
|
18 #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
|
19 #include <windows.h> |
17722
67faa494ca70
(svn r22502) -Fix (r22501): WIN32 compilation
glx <glx@openttd.org>
parents:
17721
diff
changeset
|
20 # define access _taccess |
14578
af0dd884a764
(svn r19151) -Codechange [FS#3625]: Move home directory to a better place in Haiku (jrepan)
rubidium <rubidium@openttd.org>
parents:
14443
diff
changeset
|
21 #elif defined(__HAIKU__) |
af0dd884a764
(svn r19151) -Codechange [FS#3625]: Move home directory to a better place in Haiku (jrepan)
rubidium <rubidium@openttd.org>
parents:
14443
diff
changeset
|
22 #include <Path.h> |
af0dd884a764
(svn r19151) -Codechange [FS#3625]: Move home directory to a better place in Haiku (jrepan)
rubidium <rubidium@openttd.org>
parents:
14443
diff
changeset
|
23 #include <storage/FindDirectory.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
|
24 #else |
14282
699f4b474064
(svn r18834) -Fix [FS#3538]: compilation failed on OpenBSD (matze)
rubidium <rubidium@openttd.org>
parents:
14068
diff
changeset
|
25 #include <unistd.h> |
6323
b6002af339c0
(svn r9281) -Fix [FS#687]: compile failure when compiling with 'home directories' enabled (stepancheg).
rubidium <rubidium@openttd.org>
parents:
6320
diff
changeset
|
26 #include <pwd.h> |
7589
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
27 #endif |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
28 #include <sys/stat.h> |
10779
454d306cd616
(svn r15112) -Fix (r15108): transform should include algorithm as that's where it's from. Furthermore not including it gives compile errors for some
rubidium <rubidium@openttd.org>
parents:
10778
diff
changeset
|
29 #include <algorithm> |
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 |
17685
75712bdb8713
(svn r22460) -Doc: Semantic documentation fixes, and doxygen additions (partly by planetmaker).
alberth <alberth@openttd.org>
parents:
16521
diff
changeset
|
31 /** Size of the #Fio data buffer. */ |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
32 #define FIO_BUFFER_SIZE 512 |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
33 |
17685
75712bdb8713
(svn r22460) -Doc: Semantic documentation fixes, and doxygen additions (partly by planetmaker).
alberth <alberth@openttd.org>
parents:
16521
diff
changeset
|
34 /** Structure for keeping several open files with just one data buffer. */ |
6248
0789677a15a0
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium <rubidium@openttd.org>
parents:
6247
diff
changeset
|
35 struct Fio { |
7805
9a2371156469
(svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium <rubidium@openttd.org>
parents:
7628
diff
changeset
|
36 byte *buffer, *buffer_end; ///< position pointer in local buffer and last valid byte of buffer |
9146
34811fd37a7e
(svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx <glx@openttd.org>
parents:
9103
diff
changeset
|
37 size_t pos; ///< current (system) position in file |
7805
9a2371156469
(svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium <rubidium@openttd.org>
parents:
7628
diff
changeset
|
38 FILE *cur_fh; ///< current file handle |
9a2371156469
(svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium <rubidium@openttd.org>
parents:
7628
diff
changeset
|
39 const char *filename; ///< current filename |
9a2371156469
(svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium <rubidium@openttd.org>
parents:
7628
diff
changeset
|
40 FILE *handles[MAX_FILE_SLOTS]; ///< array of file handles we can have open |
9a2371156469
(svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium <rubidium@openttd.org>
parents:
7628
diff
changeset
|
41 byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file |
9a2371156469
(svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium <rubidium@openttd.org>
parents:
7628
diff
changeset
|
42 const char *filenames[MAX_FILE_SLOTS]; ///< array of filenames we (should) have open |
15513
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
43 char *shortnames[MAX_FILE_SLOTS]; ///< array of short names for spriteloader's use |
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
|
44 #if defined(LIMITED_FDS) |
7805
9a2371156469
(svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium <rubidium@openttd.org>
parents:
7628
diff
changeset
|
45 uint open_handles; ///< current amount of open handles |
9a2371156469
(svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium <rubidium@openttd.org>
parents:
7628
diff
changeset
|
46 uint usage_count[MAX_FILE_SLOTS]; ///< count how many times this file has been opened |
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
|
47 #endif /* LIMITED_FDS */ |
6248
0789677a15a0
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium <rubidium@openttd.org>
parents:
6247
diff
changeset
|
48 }; |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
49 |
17685
75712bdb8713
(svn r22460) -Doc: Semantic documentation fixes, and doxygen additions (partly by planetmaker).
alberth <alberth@openttd.org>
parents:
16521
diff
changeset
|
50 static Fio _fio; ///< #Fio instance. |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
51 |
15513
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
52 /** Whether the working directory should be scanned. */ |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
53 static bool _do_scan_working_directory = true; |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
54 |
15529
152dcc71db0d
(svn r20188) -Codechange: unVARDEF the _[config|log|highscore]_file variables and move them to a more logical location
rubidium <rubidium@openttd.org>
parents:
15513
diff
changeset
|
55 extern char *_config_file; |
152dcc71db0d
(svn r20188) -Codechange: unVARDEF the _[config|log|highscore]_file variables and move them to a more logical location
rubidium <rubidium@openttd.org>
parents:
15513
diff
changeset
|
56 extern char *_highscore_file; |
152dcc71db0d
(svn r20188) -Codechange: unVARDEF the _[config|log|highscore]_file variables and move them to a more logical location
rubidium <rubidium@openttd.org>
parents:
15513
diff
changeset
|
57 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
58 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
59 * Get position in the current file. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
60 * @return Position in the file. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
61 */ |
9390
45c874394355
(svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium <rubidium@openttd.org>
parents:
9146
diff
changeset
|
62 size_t 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
|
63 { |
9103
c55ae436f215
(svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium <rubidium@openttd.org>
parents:
9100
diff
changeset
|
64 return _fio.pos + (_fio.buffer - _fio.buffer_end); |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
65 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
66 |
17685
75712bdb8713
(svn r22460) -Doc: Semantic documentation fixes, and doxygen additions (partly by planetmaker).
alberth <alberth@openttd.org>
parents:
16521
diff
changeset
|
67 /** |
75712bdb8713
(svn r22460) -Doc: Semantic documentation fixes, and doxygen additions (partly by planetmaker).
alberth <alberth@openttd.org>
parents:
16521
diff
changeset
|
68 * Get the filename associated with a slot. |
75712bdb8713
(svn r22460) -Doc: Semantic documentation fixes, and doxygen additions (partly by planetmaker).
alberth <alberth@openttd.org>
parents:
16521
diff
changeset
|
69 * @param slot Index of queried file. |
75712bdb8713
(svn r22460) -Doc: Semantic documentation fixes, and doxygen additions (partly by planetmaker).
alberth <alberth@openttd.org>
parents:
16521
diff
changeset
|
70 * @return Name of the file. |
75712bdb8713
(svn r22460) -Doc: Semantic documentation fixes, and doxygen additions (partly by planetmaker).
alberth <alberth@openttd.org>
parents:
16521
diff
changeset
|
71 */ |
8374
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
72 const char *FioGetFilename(uint8 slot) |
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
|
73 { |
8374
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
74 return _fio.shortnames[slot]; |
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
|
75 } |
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
|
76 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
77 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
78 * Seek in the current file. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
79 * @param pos New position. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
80 * @param mode Type of seek (\c SEEK_CUR means \a pos is relative to current position, \c SEEK_SET means \a pos is absolute). |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
81 */ |
9390
45c874394355
(svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium <rubidium@openttd.org>
parents:
9146
diff
changeset
|
82 void FioSeekTo(size_t pos, int 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
|
83 { |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
84 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
|
85 _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
|
86 _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
|
87 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
|
88 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
89 |
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
|
90 #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
|
91 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
|
92 { |
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
|
93 /* 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
|
94 if (_fio.handles[slot] == NULL) { |
7352
28ae6d57097b
(svn r10715) -Fix: limited-fs code was broken
truelight <truelight@openttd.org>
parents:
7016
diff
changeset
|
95 DEBUG(misc, 6, "Restoring file '%s' in slot '%d' from disk", _fio.filenames[slot], slot); |
28ae6d57097b
(svn r10715) -Fix: limited-fs code was broken
truelight <truelight@openttd.org>
parents:
7016
diff
changeset
|
96 FioOpenFile(slot, _fio.filenames[slot]); |
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
|
97 } |
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
|
98 _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
|
99 } |
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
|
100 #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
|
101 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
102 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
103 * Switch to a different file and seek to a position. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
104 * @param slot Slot number of the new file. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
105 * @param pos New absolute position in the new file. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
106 */ |
9392
8cc5852fb03f
(svn r13303) -Fix (r13301): 64bit gcc compilation was broken
smatz <smatz@openttd.org>
parents:
9390
diff
changeset
|
107 void FioSeekToFile(uint8 slot, size_t pos) |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
108 { |
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
|
109 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
|
110 #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
|
111 /* Make sure we have this file open */ |
7570
afe1137d97d8
(svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight <truelight@openttd.org>
parents:
7352
diff
changeset
|
112 FioRestoreFile(slot); |
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
|
113 #endif /* LIMITED_FDS */ |
7570
afe1137d97d8
(svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight <truelight@openttd.org>
parents:
7352
diff
changeset
|
114 f = _fio.handles[slot]; |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
115 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
|
116 _fio.cur_fh = f; |
7570
afe1137d97d8
(svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight <truelight@openttd.org>
parents:
7352
diff
changeset
|
117 _fio.filename = _fio.filenames[slot]; |
afe1137d97d8
(svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight <truelight@openttd.org>
parents:
7352
diff
changeset
|
118 FioSeekTo(pos, SEEK_SET); |
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 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
121 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
122 * Read a byte from the file. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
123 * @return Read byte. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
124 */ |
6247
57363e064324
(svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents:
6201
diff
changeset
|
125 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
|
126 { |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
127 if (_fio.buffer == _fio.buffer_end) { |
9100
2c5477e04d76
(svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium <rubidium@openttd.org>
parents:
9084
diff
changeset
|
128 _fio.buffer = _fio.buffer_start; |
9103
c55ae436f215
(svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium <rubidium@openttd.org>
parents:
9100
diff
changeset
|
129 size_t size = fread(_fio.buffer, 1, FIO_BUFFER_SIZE, _fio.cur_fh); |
c55ae436f215
(svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium <rubidium@openttd.org>
parents:
9100
diff
changeset
|
130 _fio.pos += size; |
c55ae436f215
(svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium <rubidium@openttd.org>
parents:
9100
diff
changeset
|
131 _fio.buffer_end = _fio.buffer_start + size; |
c55ae436f215
(svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium <rubidium@openttd.org>
parents:
9100
diff
changeset
|
132 |
c55ae436f215
(svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium <rubidium@openttd.org>
parents:
9100
diff
changeset
|
133 if (size == 0) return 0; |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
134 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
135 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
|
136 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
137 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
138 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
139 * Skip \a n bytes ahead in the file. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
140 * @param n Number of bytes to skip reading. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
141 */ |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
142 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
|
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 for (;;) { |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
145 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
|
146 _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
|
147 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
|
148 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
|
149 FioReadByte(); |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
150 n--; |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
151 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
152 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
153 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
154 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
155 * Read a word (16 bits) from the file (in low endian format). |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
156 * @return Read word. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
157 */ |
6247
57363e064324
(svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents:
6201
diff
changeset
|
158 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
|
159 { |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
160 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
|
161 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
|
162 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
163 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
164 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
165 * Read a double word (32 bits) from the file (in low endian format). |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
166 * @return Read word. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
167 */ |
6247
57363e064324
(svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents:
6201
diff
changeset
|
168 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
|
169 { |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
170 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
|
171 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
|
172 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
173 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
174 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
175 * Read a block. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
176 * @param ptr Destination buffer. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
177 * @param size Number of bytes to read. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
178 */ |
9146
34811fd37a7e
(svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx <glx@openttd.org>
parents:
9103
diff
changeset
|
179 void FioReadBlock(void *ptr, size_t size) |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
180 { |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
181 FioSeekTo(FioGetPos(), SEEK_SET); |
9100
2c5477e04d76
(svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium <rubidium@openttd.org>
parents:
9084
diff
changeset
|
182 _fio.pos += fread(ptr, 1, size, _fio.cur_fh); |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
183 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
184 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
185 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
186 * Close the file at the given slot number. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
187 * @param slot File index to close. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
188 */ |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
189 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
|
190 { |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
191 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
|
192 fclose(_fio.handles[slot]); |
8374
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
193 |
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
194 free(_fio.shortnames[slot]); |
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
195 _fio.shortnames[slot] = NULL; |
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
196 |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
197 _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
|
198 #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
|
199 _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
|
200 #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
|
201 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
202 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
203 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
204 /** Close all slotted open files. */ |
6247
57363e064324
(svn r9050) -Codechange: Foo(void) -> Foo()
rubidium <rubidium@openttd.org>
parents:
6201
diff
changeset
|
205 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
|
206 { |
15552
683f2dbe75bb
(svn r20211) -Codechange: Indented code should have curly braces around it.
alberth <alberth@openttd.org>
parents:
15535
diff
changeset
|
207 for (int i = 0; i != lengthof(_fio.handles); i++) { |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
208 FioCloseFile(i); |
15552
683f2dbe75bb
(svn r20211) -Codechange: Indented code should have curly braces around it.
alberth <alberth@openttd.org>
parents:
15535
diff
changeset
|
209 } |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
210 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
211 |
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
|
212 #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
|
213 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
|
214 { |
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
|
215 /* 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
|
216 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
|
217 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
|
218 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
|
219 |
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
|
220 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
|
221 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
|
222 /* 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 } |
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
|
228 } |
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
|
229 assert(slot != -1); |
7352
28ae6d57097b
(svn r10715) -Fix: limited-fs code was broken
truelight <truelight@openttd.org>
parents:
7016
diff
changeset
|
230 DEBUG(misc, 6, "Closing filehandler '%s' in slot '%d' because of fd-limit", _fio.filenames[slot], slot); |
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
|
231 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
|
232 } |
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
|
233 } |
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
|
234 #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
|
235 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
236 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
237 * Open a slotted file. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
238 * @param slot Index to assign. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
239 * @param filename Name of the file at the disk. |
18016
09780d1c580a
(svn r22825) -Codechange: pass subdir to FioOpenFile
rubidium <rubidium@openttd.org>
parents:
18013
diff
changeset
|
240 * @param subdir The sub directory to search this file in. |
19943
f4a2b39f3f8a
(svn r24877) -Fix: some whitespace "errors"
rubidium <rubidium@openttd.org>
parents:
19871
diff
changeset
|
241 */ |
18016
09780d1c580a
(svn r22825) -Codechange: pass subdir to FioOpenFile
rubidium <rubidium@openttd.org>
parents:
18013
diff
changeset
|
242 void FioOpenFile(int slot, 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
|
243 { |
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
|
244 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
|
245 |
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
|
246 #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
|
247 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
|
248 #endif /* LIMITED_FDS */ |
18016
09780d1c580a
(svn r22825) -Codechange: pass subdir to FioOpenFile
rubidium <rubidium@openttd.org>
parents:
18013
diff
changeset
|
249 f = FioFOpenFile(filename, "rb", subdir); |
9470
47924a4b2ad4
(svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx <glx@openttd.org>
parents:
9392
diff
changeset
|
250 if (f == NULL) usererror("Cannot open file '%s'", filename); |
7570
afe1137d97d8
(svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight <truelight@openttd.org>
parents:
7352
diff
changeset
|
251 uint32 pos = ftell(f); |
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
|
252 |
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
|
253 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
|
254 _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
|
255 _fio.filenames[slot] = filename; |
8374
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
256 |
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
257 /* Store the filename without path and extension */ |
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
258 const char *t = strrchr(filename, PATHSEPCHAR); |
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
259 _fio.shortnames[slot] = strdup(t == NULL ? filename : t); |
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
260 char *t2 = strrchr(_fio.shortnames[slot], '.'); |
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
261 if (t2 != NULL) *t2 = '\0'; |
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
262 strtolower(_fio.shortnames[slot]); |
510b88dbb390
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
peter1138 <peter1138@openttd.org>
parents:
8273
diff
changeset
|
263 |
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
|
264 #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
|
265 _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
|
266 _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
|
267 #endif /* LIMITED_FDS */ |
7570
afe1137d97d8
(svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight <truelight@openttd.org>
parents:
7352
diff
changeset
|
268 FioSeekToFile(slot, pos); |
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
|
269 } |
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
|
270 |
18013
7a582ee01092
(svn r22822) -Codechange: make a distinction between base sets and newgrfs for their directory
rubidium <rubidium@openttd.org>
parents:
17914
diff
changeset
|
271 static const char * const _subdirs[] = { |
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
|
272 "", |
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 "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
|
274 "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
|
275 "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
|
276 "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
|
277 "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
|
278 "data" PATHSEP, |
18383
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
279 "baseset" PATHSEP, |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
280 "newgrf" PATHSEP, |
10696
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
281 "lang" PATHSEP, |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
282 "ai" PATHSEP, |
10749
e631ec1c5bef
(svn r15082) -Codechange: make the AI library path also a searchdir instead of manually manufacturing it into a searchdir.
rubidium <rubidium@openttd.org>
parents:
10696
diff
changeset
|
283 "ai" PATHSEP "library" PATHSEP, |
18757
8fb3d74f1cd1
(svn r23605) -Add: GAME_DIR and CONTENT_TYPE_GAME, and read gamescript from that directory
truebrain <truebrain@openttd.org>
parents:
18398
diff
changeset
|
284 "game" PATHSEP, |
18764
bd2a1a20a792
(svn r23612) -Add: allow importing libraries in the same way as AI does, only with GS prefix (and in game/library)
truebrain <truebrain@openttd.org>
parents:
18757
diff
changeset
|
285 "game" PATHSEP "library" PATHSEP, |
19871
723a91a42f5c
(svn r24804) -Add: Separate subdirectory for screenshots.
frosch <frosch@openttd.org>
parents:
19126
diff
changeset
|
286 "screenshot" PATHSEP, |
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
|
287 }; |
18013
7a582ee01092
(svn r22822) -Codechange: make a distinction between base sets and newgrfs for their directory
rubidium <rubidium@openttd.org>
parents:
17914
diff
changeset
|
288 assert_compile(lengthof(_subdirs) == NUM_SUBDIRS); |
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
|
289 |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
290 const char *_searchpaths[NUM_SEARCHPATHS]; |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
291 TarList _tar_list[NUM_SUBDIRS]; |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
292 TarFileList _tar_filelist[NUM_SUBDIRS]; |
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
|
293 |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
294 typedef std::map<std::string, std::string> TarLinkList; |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
295 static TarLinkList _tar_linklist[NUM_SUBDIRS]; ///< List of directory links |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
296 |
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
|
297 /** |
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
|
298 * Check whether the given file exists |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
299 * @param filename the file to try for existence. |
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
|
300 * @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
|
301 * @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
|
302 */ |
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
|
303 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
|
304 { |
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 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
|
306 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
|
307 |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
308 FioFCloseFile(f); |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
309 return true; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
310 } |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
311 |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
312 /** |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
313 * Test whether the given filename exists. |
17721
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
314 * @param filename the file to test. |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
315 * @return true if and only if the file exists. |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
316 */ |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
317 bool FileExists(const char *filename) |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
318 { |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
319 #if defined(WINCE) |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
320 /* There is always one platform that doesn't support basic commands... */ |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
321 HANDLE hand = CreateFile(OTTD2FS(filename), 0, 0, NULL, OPEN_EXISTING, 0, NULL); |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
322 if (hand == INVALID_HANDLE_VALUE) return 1; |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
323 CloseHandle(hand); |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
324 return 0; |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
325 #else |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
326 return access(OTTD2FS(filename), 0) == 0; |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
327 #endif |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
328 } |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
329 |
688b683f70b7
(svn r22501) -Codechange: Move FileExists to a better place.
alberth <alberth@openttd.org>
parents:
17688
diff
changeset
|
330 /** |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
331 * Close a file in a safe way. |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
332 */ |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
333 void FioFCloseFile(FILE *f) |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
334 { |
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
|
335 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
|
336 } |
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
|
337 |
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
|
338 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
|
339 { |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
340 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
|
341 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
|
342 |
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
|
343 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
|
344 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
|
345 } |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
346 |
17872
339c99ac8633
(svn r22668) -Codechange: FioFindFullPath tests already whether the file exists.
alberth <alberth@openttd.org>
parents:
17722
diff
changeset
|
347 /** |
339c99ac8633
(svn r22668) -Codechange: FioFindFullPath tests already whether the file exists.
alberth <alberth@openttd.org>
parents:
17722
diff
changeset
|
348 * Find a path to the filename in one of the search directories. |
339c99ac8633
(svn r22668) -Codechange: FioFindFullPath tests already whether the file exists.
alberth <alberth@openttd.org>
parents:
17722
diff
changeset
|
349 * @param buf [out] Destination buffer for the path. |
339c99ac8633
(svn r22668) -Codechange: FioFindFullPath tests already whether the file exists.
alberth <alberth@openttd.org>
parents:
17722
diff
changeset
|
350 * @param buflen Length of the destination buffer. |
339c99ac8633
(svn r22668) -Codechange: FioFindFullPath tests already whether the file exists.
alberth <alberth@openttd.org>
parents:
17722
diff
changeset
|
351 * @param subdir Subdirectory to try. |
339c99ac8633
(svn r22668) -Codechange: FioFindFullPath tests already whether the file exists.
alberth <alberth@openttd.org>
parents:
17722
diff
changeset
|
352 * @param filename Filename to look for. |
339c99ac8633
(svn r22668) -Codechange: FioFindFullPath tests already whether the file exists.
alberth <alberth@openttd.org>
parents:
17722
diff
changeset
|
353 * @return \a buf containing the path if the path was found, else \c NULL. |
339c99ac8633
(svn r22668) -Codechange: FioFindFullPath tests already whether the file exists.
alberth <alberth@openttd.org>
parents:
17722
diff
changeset
|
354 */ |
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
|
355 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
|
356 { |
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
|
357 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
|
358 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
|
359 |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
360 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
|
361 FioGetFullPath(buf, buflen, sp, subdir, filename); |
17872
339c99ac8633
(svn r22668) -Codechange: FioFindFullPath tests already whether the file exists.
alberth <alberth@openttd.org>
parents:
17722
diff
changeset
|
362 if (FileExists(buf)) return buf; |
14068
efd1182433ab
(svn r18611) -Fix (r18608): uppercase-to-lowercase conversion when opening a file wasn't applied when finding the full path of a file
rubidium <rubidium@openttd.org>
parents:
13513
diff
changeset
|
363 #if !defined(WIN32) |
efd1182433ab
(svn r18611) -Fix (r18608): uppercase-to-lowercase conversion when opening a file wasn't applied when finding the full path of a file
rubidium <rubidium@openttd.org>
parents:
13513
diff
changeset
|
364 /* Be, as opening files, aware that sometimes the filename |
efd1182433ab
(svn r18611) -Fix (r18608): uppercase-to-lowercase conversion when opening a file wasn't applied when finding the full path of a file
rubidium <rubidium@openttd.org>
parents:
13513
diff
changeset
|
365 * might be in uppercase when it is in lowercase on the |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
366 * disk. Of course Windows doesn't care about casing. */ |
17873
9c497cde37af
(svn r22669) -Codechange: For non-windows, only test for file existence again if strtolower actually changed the name.
alberth <alberth@openttd.org>
parents:
17872
diff
changeset
|
367 if (strtolower(buf + strlen(_searchpaths[sp]) - 1) && FileExists(buf)) return buf; |
14068
efd1182433ab
(svn r18611) -Fix (r18608): uppercase-to-lowercase conversion when opening a file wasn't applied when finding the full path of a file
rubidium <rubidium@openttd.org>
parents:
13513
diff
changeset
|
368 #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
|
369 } |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
370 |
17872
339c99ac8633
(svn r22668) -Codechange: FioFindFullPath tests already whether the file exists.
alberth <alberth@openttd.org>
parents:
17722
diff
changeset
|
371 return 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
|
372 } |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
373 |
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 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
|
375 { |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
376 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
|
377 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
|
378 |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
379 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
|
380 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
|
381 } |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
382 |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
383 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
|
384 { |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
385 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
|
386 |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
387 /* 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
|
388 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
|
389 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
|
390 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
|
391 } |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
392 |
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
|
393 /* 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
|
394 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
|
395 |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
396 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
|
397 } |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
398 |
13509
59959bee9f95
(svn r18028) -Codechange: unglobalise some functions
rubidium <rubidium@openttd.org>
parents:
13507
diff
changeset
|
399 static FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize) |
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
|
400 { |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
401 #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
|
402 /* 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
|
403 * 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
|
404 * 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
|
405 * 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
|
406 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
|
407 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
|
408 #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
|
409 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
|
410 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
|
411 |
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
|
412 if (subdir == NO_DIRECTORY) { |
10310
5cefbb1c3fd7
(svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13 <skidd13@openttd.org>
parents:
10238
diff
changeset
|
413 strecpy(buf, filename, lastof(buf)); |
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
|
414 } 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
|
415 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
|
416 } |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
417 |
7624
080bf9f8ace8
(svn r11155) -Fix [FS#1253]: work around a nasty MS CRT 8 SP1 bug making it virtually impossible to debug 32bpp games under MSVC (http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=257606). Patch by boekabart.
rubidium <rubidium@openttd.org>
parents:
7592
diff
changeset
|
418 #if defined(WIN32) |
7628
32f9c85de330
(svn r11159) -Fix (r11155): restore the ability to save in a non existing file
glx <glx@openttd.org>
parents:
7625
diff
changeset
|
419 if (mode[0] == 'r' && GetFileAttributes(OTTD2FS(buf)) == INVALID_FILE_ATTRIBUTES) return NULL; |
7624
080bf9f8ace8
(svn r11155) -Fix [FS#1253]: work around a nasty MS CRT 8 SP1 bug making it virtually impossible to debug 32bpp games under MSVC (http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=257606). Patch by boekabart.
rubidium <rubidium@openttd.org>
parents:
7592
diff
changeset
|
420 #endif |
080bf9f8ace8
(svn r11155) -Fix [FS#1253]: work around a nasty MS CRT 8 SP1 bug making it virtually impossible to debug 32bpp games under MSVC (http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=257606). Patch by boekabart.
rubidium <rubidium@openttd.org>
parents:
7592
diff
changeset
|
421 |
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
|
422 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
|
423 #if !defined(WIN32) |
17873
9c497cde37af
(svn r22669) -Codechange: For non-windows, only test for file existence again if strtolower actually changed the name.
alberth <alberth@openttd.org>
parents:
17872
diff
changeset
|
424 if (f == NULL && strtolower(buf + ((subdir == NO_DIRECTORY) ? 0 : strlen(_searchpaths[sp]) - 1))) { |
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
|
425 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
|
426 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
427 #endif |
7574
62a4ffc139c0
(svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight <truelight@openttd.org>
parents:
7570
diff
changeset
|
428 if (f != NULL && filesize != NULL) { |
62a4ffc139c0
(svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight <truelight@openttd.org>
parents:
7570
diff
changeset
|
429 /* Find the size of the file */ |
62a4ffc139c0
(svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight <truelight@openttd.org>
parents:
7570
diff
changeset
|
430 fseek(f, 0, SEEK_END); |
62a4ffc139c0
(svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight <truelight@openttd.org>
parents:
7570
diff
changeset
|
431 *filesize = ftell(f); |
62a4ffc139c0
(svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight <truelight@openttd.org>
parents:
7570
diff
changeset
|
432 fseek(f, 0, SEEK_SET); |
62a4ffc139c0
(svn r11099) -Codechange: allow on opening of a file via FioFOpenFile to request the size of the file, so we can keep that in mind
truelight <truelight@openttd.org>
parents:
7570
diff
changeset
|
433 } |
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
|
434 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
|
435 } |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
436 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
437 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
438 * Opens a file from inside a tar archive. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
439 * @param entry The entry to open. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
440 * @param filesize [out] If not \c NULL, size of the opened file. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
441 * @return File handle of the opened file, or \c NULL if the file is not available. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
442 * @note The file is read from within the tar file, and may not return \c EOF after reading the whole file. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
443 */ |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
444 FILE *FioFOpenFileTar(TarFileListEntry *entry, size_t *filesize) |
7581
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
445 { |
9702
ab8ad03c5ff9
(svn r13810) -Fix: small memory leak when tar files would be found.
rubidium <rubidium@openttd.org>
parents:
9635
diff
changeset
|
446 FILE *f = fopen(entry->tar_filename, "rb"); |
12174
1f14f0dd0ca0
(svn r16590) -Fix [FS#2967]: don't crash when tars/newgrfs are removed, just tell the file could be opened/found.
rubidium <rubidium@openttd.org>
parents:
12016
diff
changeset
|
447 if (f == NULL) return f; |
7581
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
448 |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
449 fseek(f, entry->position, SEEK_SET); |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
450 if (filesize != NULL) *filesize = entry->size; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
451 return f; |
7581
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
452 } |
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
453 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
454 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
455 * Opens a OpenTTD file somewhere in a personal or global directory. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
456 * @param filename Name of the file to open. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
457 * @param subdir Subdirectory to open. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
458 * @param filename Name of the file to open. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
459 * @return File handle of the opened file, or \c NULL if the file is not available. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
460 */ |
7575
83da0ffcda47
(svn r11100) -Fix r11099: darn typos...
truelight <truelight@openttd.org>
parents:
7574
diff
changeset
|
461 FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize) |
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
|
462 { |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
463 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
|
464 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
|
465 |
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
|
466 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
|
467 |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
468 FOR_ALL_SEARCHPATHS(sp) { |
7575
83da0ffcda47
(svn r11100) -Fix r11099: darn typos...
truelight <truelight@openttd.org>
parents:
7574
diff
changeset
|
469 f = FioFOpenFileSp(filename, mode, sp, subdir, filesize); |
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
|
470 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
|
471 } |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
472 |
7581
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
473 /* We can only use .tar in case of data-dir, and read-mode */ |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
474 if (f == NULL && mode[0] == 'r' && subdir != NO_DIRECTORY) { |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
475 static const uint MAX_RESOLVED_LENGTH = 2 * (100 + 100 + 155) + 1; // Enough space to hold two filenames plus link. See 'TarHeader'. |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
476 char resolved_name[MAX_RESOLVED_LENGTH]; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
477 |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
478 /* Filenames in tars are always forced to be lowercase */ |
11644
7980a47b17cb
(svn r16024) -Codechange: harden string copying on places where it's possible
rubidium <rubidium@openttd.org>
parents:
11392
diff
changeset
|
479 strecpy(resolved_name, filename, lastof(resolved_name)); |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
480 strtolower(resolved_name); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
481 |
9916
fa925e28f10e
(svn r14069) -Fix: silence MSVC 64-bits compile warnings.
rubidium <rubidium@openttd.org>
parents:
9702
diff
changeset
|
482 size_t resolved_len = strlen(resolved_name); |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
483 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
484 /* Resolve ONE directory link */ |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
485 for (TarLinkList::iterator link = _tar_linklist[subdir].begin(); link != _tar_linklist[subdir].end(); link++) { |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
486 const std::string &src = link->first; |
9916
fa925e28f10e
(svn r14069) -Fix: silence MSVC 64-bits compile warnings.
rubidium <rubidium@openttd.org>
parents:
9702
diff
changeset
|
487 size_t len = src.length(); |
9635
9ae0f1fbed30
(svn r13698) -Fix (r13697): MorphOS compilation.
frosch <frosch@openttd.org>
parents:
9634
diff
changeset
|
488 if (resolved_len >= len && resolved_name[len - 1] == PATHSEPCHAR && strncmp(src.c_str(), resolved_name, len) == 0) { |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
489 /* Apply link */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
490 char resolved_name2[MAX_RESOLVED_LENGTH]; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
491 const std::string &dest = link->second; |
11644
7980a47b17cb
(svn r16024) -Codechange: harden string copying on places where it's possible
rubidium <rubidium@openttd.org>
parents:
11392
diff
changeset
|
492 strecpy(resolved_name2, &(resolved_name[len]), lastof(resolved_name2)); |
7980a47b17cb
(svn r16024) -Codechange: harden string copying on places where it's possible
rubidium <rubidium@openttd.org>
parents:
11392
diff
changeset
|
493 strecpy(resolved_name, dest.c_str(), lastof(resolved_name)); |
7980a47b17cb
(svn r16024) -Codechange: harden string copying on places where it's possible
rubidium <rubidium@openttd.org>
parents:
11392
diff
changeset
|
494 strecpy(&(resolved_name[dest.length()]), resolved_name2, lastof(resolved_name)); |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
495 break; // Only resolve one level |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
496 } |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
497 } |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
498 |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
499 TarFileList::iterator it = _tar_filelist[subdir].find(resolved_name); |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
500 if (it != _tar_filelist[subdir].end()) { |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
501 f = FioFOpenFileTar(&((*it).second), filesize); |
7581
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
502 } |
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
503 } |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
504 |
8009
b4b40ab80c96
(svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium <rubidium@openttd.org>
parents:
7927
diff
changeset
|
505 /* Sometimes a full path is given. To support |
b4b40ab80c96
(svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium <rubidium@openttd.org>
parents:
7927
diff
changeset
|
506 * the 'subdirectory' must be 'removed'. */ |
b4b40ab80c96
(svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium <rubidium@openttd.org>
parents:
7927
diff
changeset
|
507 if (f == NULL && subdir != NO_DIRECTORY) { |
18383
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
508 switch (subdir) { |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
509 case BASESET_DIR: |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
510 f = FioFOpenFile(filename, mode, OLD_GM_DIR, filesize); |
18390
bf91535eb515
(svn r23226) -Fix (r23219): If you find a file, also use it.
frosch <frosch@openttd.org>
parents:
18389
diff
changeset
|
511 if (f != NULL) break; |
bf91535eb515
(svn r23226) -Fix (r23219): If you find a file, also use it.
frosch <frosch@openttd.org>
parents:
18389
diff
changeset
|
512 /* FALL THROUGH */ |
18383
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
513 case NEWGRF_DIR: |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
514 f = FioFOpenFile(filename, mode, OLD_DATA_DIR, filesize); |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
515 break; |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
516 |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
517 default: |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
518 f = FioFOpenFile(filename, mode, NO_DIRECTORY, filesize); |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
519 break; |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
520 } |
8009
b4b40ab80c96
(svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium <rubidium@openttd.org>
parents:
7927
diff
changeset
|
521 } |
b4b40ab80c96
(svn r11569) -Fix [FS#1480]: full paths sometimes did not work correctly.
rubidium <rubidium@openttd.org>
parents:
7927
diff
changeset
|
522 |
5584
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
523 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
|
524 } |
4b26bd55bd24
(svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff
changeset
|
525 |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
526 /** |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
527 * Create a directory with the given name |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
528 * @param name the new name of the directory |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
529 */ |
16001
3ea421d91675
(svn r20689) -Codechange: Make some global functions used in 1 .cpp file static in that file.
alberth <alberth@openttd.org>
parents:
15552
diff
changeset
|
530 static void FioCreateDirectory(const char *name) |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
531 { |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
532 #if defined(WIN32) || defined(WINCE) |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
533 CreateDirectory(OTTD2FS(name), NULL); |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
534 #elif defined(OS2) && !defined(__INNOTEK_LIBC__) |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
535 mkdir(OTTD2FS(name)); |
7911
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
536 #elif defined(__MORPHOS__) || defined(__AMIGAOS__) |
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
537 char buf[MAX_PATH]; |
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
538 ttd_strlcpy(buf, name, MAX_PATH); |
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
539 |
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
540 size_t len = strlen(name) - 1; |
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
541 if (buf[len] == '/') { |
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
542 buf[len] = '\0'; // Kill pathsep, so mkdir() will not fail |
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
543 } |
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
544 |
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
545 mkdir(OTTD2FS(buf), 0755); |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
546 #else |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
547 mkdir(OTTD2FS(name), 0755); |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
548 #endif |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
549 } |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
550 |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
551 /** |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
552 * 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
|
553 * 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
|
554 * @param buf string to append the separator to |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
555 * @param buflen the length of \a buf. |
15150
12944c945d4c
(svn r19779) -Change: add a return type to AppendPathSeparator and use that to determine whether we could append the path separator. If not, do not recurse into that directory as the path would exceed the maximum path length
rubidium <rubidium@openttd.org>
parents:
15148
diff
changeset
|
556 * @return true iff the operation succeeded |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
557 */ |
15150
12944c945d4c
(svn r19779) -Change: add a return type to AppendPathSeparator and use that to determine whether we could append the path separator. If not, do not recurse into that directory as the path would exceed the maximum path length
rubidium <rubidium@openttd.org>
parents:
15148
diff
changeset
|
558 bool AppendPathSeparator(char *buf, size_t buflen) |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
559 { |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
560 size_t s = strlen(buf); |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
561 |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
562 /* Length of string + path separator + '\0' */ |
15150
12944c945d4c
(svn r19779) -Change: add a return type to AppendPathSeparator and use that to determine whether we could append the path separator. If not, do not recurse into that directory as the path would exceed the maximum path length
rubidium <rubidium@openttd.org>
parents:
15148
diff
changeset
|
563 if (s != 0 && buf[s - 1] != PATHSEPCHAR) { |
12944c945d4c
(svn r19779) -Change: add a return type to AppendPathSeparator and use that to determine whether we could append the path separator. If not, do not recurse into that directory as the path would exceed the maximum path length
rubidium <rubidium@openttd.org>
parents:
15148
diff
changeset
|
564 if (s + 2 >= buflen) return false; |
12944c945d4c
(svn r19779) -Change: add a return type to AppendPathSeparator and use that to determine whether we could append the path separator. If not, do not recurse into that directory as the path would exceed the maximum path length
rubidium <rubidium@openttd.org>
parents:
15148
diff
changeset
|
565 |
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
|
566 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
|
567 buf[s + 1] = '\0'; |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
568 } |
15150
12944c945d4c
(svn r19779) -Change: add a return type to AppendPathSeparator and use that to determine whether we could append the path separator. If not, do not recurse into that directory as the path would exceed the maximum path length
rubidium <rubidium@openttd.org>
parents:
15148
diff
changeset
|
569 |
12944c945d4c
(svn r19779) -Change: add a return type to AppendPathSeparator and use that to determine whether we could append the path separator. If not, do not recurse into that directory as the path would exceed the maximum path length
rubidium <rubidium@openttd.org>
parents:
15148
diff
changeset
|
570 return true; |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
571 } |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
572 |
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
|
573 /** |
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
|
574 * 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
|
575 * 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
|
576 * @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
|
577 * @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
|
578 */ |
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
|
579 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
|
580 { |
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
|
581 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
|
582 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
|
583 |
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
|
584 /* 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
|
585 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
|
586 |
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
|
587 /* 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
|
588 if (s == NULL || dest != s) { |
9100
2c5477e04d76
(svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium <rubidium@openttd.org>
parents:
9084
diff
changeset
|
589 if (getcwd(dest, MAX_PATH) == NULL) *dest = '\0'; |
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
|
590 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
|
591 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
|
592 } |
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
|
593 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
|
594 |
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
|
595 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
|
596 } |
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
|
597 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
598 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
599 * Find the first directory in a tar archive. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
600 * @param tarname the name of the tar archive to look in. |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
601 * @param subdir the subdirectory to look in. |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
602 */ |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
603 const char *FioTarFirstDir(const char *tarname, Subdirectory subdir) |
10696
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
604 { |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
605 TarList::iterator it = _tar_list[subdir].find(tarname); |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
606 if (it == _tar_list[subdir].end()) return NULL; |
10696
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
607 return (*it).second.dirname; |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
608 } |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
609 |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
610 static void TarAddLink(const std::string &srcParam, const std::string &destParam, Subdirectory subdir) |
10696
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
611 { |
10775
34d3cf5ba5ec
(svn r15108) -Fix: TarAddLink didn't lowercase strings, breaking tar-loading when using uppercase files
truebrain <truebrain@openttd.org>
parents:
10762
diff
changeset
|
612 std::string src = srcParam; |
34d3cf5ba5ec
(svn r15108) -Fix: TarAddLink didn't lowercase strings, breaking tar-loading when using uppercase files
truebrain <truebrain@openttd.org>
parents:
10762
diff
changeset
|
613 std::string dest = destParam; |
34d3cf5ba5ec
(svn r15108) -Fix: TarAddLink didn't lowercase strings, breaking tar-loading when using uppercase files
truebrain <truebrain@openttd.org>
parents:
10762
diff
changeset
|
614 /* Tar internals assume lowercase */ |
34d3cf5ba5ec
(svn r15108) -Fix: TarAddLink didn't lowercase strings, breaking tar-loading when using uppercase files
truebrain <truebrain@openttd.org>
parents:
10762
diff
changeset
|
615 std::transform(src.begin(), src.end(), src.begin(), tolower); |
34d3cf5ba5ec
(svn r15108) -Fix: TarAddLink didn't lowercase strings, breaking tar-loading when using uppercase files
truebrain <truebrain@openttd.org>
parents:
10762
diff
changeset
|
616 std::transform(dest.begin(), dest.end(), dest.begin(), tolower); |
34d3cf5ba5ec
(svn r15108) -Fix: TarAddLink didn't lowercase strings, breaking tar-loading when using uppercase files
truebrain <truebrain@openttd.org>
parents:
10762
diff
changeset
|
617 |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
618 TarFileList::iterator dest_file = _tar_filelist[subdir].find(dest); |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
619 if (dest_file != _tar_filelist[subdir].end()) { |
10696
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
620 /* Link to file. Process the link like the destination file. */ |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
621 _tar_filelist[subdir].insert(TarFileList::value_type(src, dest_file->second)); |
10696
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
622 } else { |
11368
82c90cd591ab
(svn r15718) -Cleanup: apply some comment coding style on the rest of the sources too
rubidium <rubidium@openttd.org>
parents:
11292
diff
changeset
|
623 /* Destination file not found. Assume 'link to directory' |
82c90cd591ab
(svn r15718) -Cleanup: apply some comment coding style on the rest of the sources too
rubidium <rubidium@openttd.org>
parents:
11292
diff
changeset
|
624 * Append PATHSEPCHAR to 'src' and 'dest' if needed */ |
10696
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
625 const std::string src_path = ((*src.rbegin() == PATHSEPCHAR) ? src : src + PATHSEPCHAR); |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
626 const std::string dst_path = (dest.length() == 0 ? "" : ((*dest.rbegin() == PATHSEPCHAR) ? dest : dest + PATHSEPCHAR)); |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
627 _tar_linklist[subdir].insert(TarLinkList::value_type(src_path, dst_path)); |
10696
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
628 } |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
629 } |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
630 |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
631 void FioTarAddLink(const char *src, const char *dest, Subdirectory subdir) |
10696
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
632 { |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
633 TarAddLink(src, dest, subdir); |
10696
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
634 } |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
635 |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
636 /** |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
637 * Simplify filenames from tars. |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
638 * Replace '/' by #PATHSEPCHAR, and force 'name' to lowercase. |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
639 * @param name Filename to process. |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
640 */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
641 static void SimplifyFileName(char *name) |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
642 { |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
643 /* Force lowercase */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
644 strtolower(name); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
645 |
19968
0dba7f49118c
(svn r24900) -Fix [FS#5389]: Comments with typos (most fixes supplied by Eagle_rainbow)
planetmaker <planetmaker@openttd.org>
parents:
19943
diff
changeset
|
646 /* Tar-files always have '/' path-separator, but we want our PATHSEPCHAR */ |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
647 #if (PATHSEPCHAR != '/') |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
648 for (char *n = name; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
649 #endif |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
650 } |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
651 |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
652 /** |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
653 * Perform the scanning of a particular subdirectory. |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
654 * @param subdir The subdirectory to scan. |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
655 * @return The number of found tar files. |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
656 */ |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
657 uint TarScanner::DoScan(Subdirectory sd) |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
658 { |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
659 _tar_filelist[sd].clear(); |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
660 _tar_list[sd].clear(); |
18383
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
661 uint num = this->Scan(".tar", sd, false); |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
662 if (sd == BASESET_DIR || sd == NEWGRF_DIR) num += this->Scan(".tar", OLD_DATA_DIR, false); |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
663 return num; |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
664 } |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
665 |
18381
6f9fad8837de
(svn r23217) -Codechange: introduce the concept of scanning only in a limited set of sub directories
rubidium <rubidium@openttd.org>
parents:
18380
diff
changeset
|
666 /* static */ uint TarScanner::DoScan(TarScanner::Mode mode) |
16521
13a1aad8427e
(svn r21247) -Fix: make the tar scanner forget about old (possibly removed) tars
rubidium <rubidium@openttd.org>
parents:
16349
diff
changeset
|
667 { |
15148
236482e2e554
(svn r19777) -Change: use the file scanner to find the .tars
rubidium <rubidium@openttd.org>
parents:
15002
diff
changeset
|
668 DEBUG(misc, 1, "Scanning for tars"); |
236482e2e554
(svn r19777) -Change: use the file scanner to find the .tars
rubidium <rubidium@openttd.org>
parents:
15002
diff
changeset
|
669 TarScanner fs; |
18381
6f9fad8837de
(svn r23217) -Codechange: introduce the concept of scanning only in a limited set of sub directories
rubidium <rubidium@openttd.org>
parents:
18380
diff
changeset
|
670 uint num = 0; |
18383
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
671 if (mode & TarScanner::BASESET) { |
18382
ee500e57ab78
(svn r23218) -Codechange: prepare code for a separate base set directory
rubidium <rubidium@openttd.org>
parents:
18381
diff
changeset
|
672 num += fs.DoScan(BASESET_DIR); |
18383
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
673 } |
77f232478188
(svn r23219) -Change: different directories for basesets and newgrfs. So data to baseset or newgrf, and gm to baseset
rubidium <rubidium@openttd.org>
parents:
18382
diff
changeset
|
674 if (mode & TarScanner::NEWGRF) { |
18381
6f9fad8837de
(svn r23217) -Codechange: introduce the concept of scanning only in a limited set of sub directories
rubidium <rubidium@openttd.org>
parents:
18380
diff
changeset
|
675 num += fs.DoScan(NEWGRF_DIR); |
6f9fad8837de
(svn r23217) -Codechange: introduce the concept of scanning only in a limited set of sub directories
rubidium <rubidium@openttd.org>
parents:
18380
diff
changeset
|
676 } |
6f9fad8837de
(svn r23217) -Codechange: introduce the concept of scanning only in a limited set of sub directories
rubidium <rubidium@openttd.org>
parents:
18380
diff
changeset
|
677 if (mode & TarScanner::AI) { |
6f9fad8837de
(svn r23217) -Codechange: introduce the concept of scanning only in a limited set of sub directories
rubidium <rubidium@openttd.org>
parents:
18380
diff
changeset
|
678 num += fs.DoScan(AI_DIR); |
6f9fad8837de
(svn r23217) -Codechange: introduce the concept of scanning only in a limited set of sub directories
rubidium <rubidium@openttd.org>
parents:
18380
diff
changeset
|
679 num += fs.DoScan(AI_LIBRARY_DIR); |
6f9fad8837de
(svn r23217) -Codechange: introduce the concept of scanning only in a limited set of sub directories
rubidium <rubidium@openttd.org>
parents:
18380
diff
changeset
|
680 } |
18757
8fb3d74f1cd1
(svn r23605) -Add: GAME_DIR and CONTENT_TYPE_GAME, and read gamescript from that directory
truebrain <truebrain@openttd.org>
parents:
18398
diff
changeset
|
681 if (mode & TarScanner::GAME) { |
8fb3d74f1cd1
(svn r23605) -Add: GAME_DIR and CONTENT_TYPE_GAME, and read gamescript from that directory
truebrain <truebrain@openttd.org>
parents:
18398
diff
changeset
|
682 num += fs.DoScan(GAME_DIR); |
18764
bd2a1a20a792
(svn r23612) -Add: allow importing libraries in the same way as AI does, only with GS prefix (and in game/library)
truebrain <truebrain@openttd.org>
parents:
18757
diff
changeset
|
683 num += fs.DoScan(GAME_LIBRARY_DIR); |
18757
8fb3d74f1cd1
(svn r23605) -Add: GAME_DIR and CONTENT_TYPE_GAME, and read gamescript from that directory
truebrain <truebrain@openttd.org>
parents:
18398
diff
changeset
|
684 } |
18381
6f9fad8837de
(svn r23217) -Codechange: introduce the concept of scanning only in a limited set of sub directories
rubidium <rubidium@openttd.org>
parents:
18380
diff
changeset
|
685 if (mode & TarScanner::SCENARIO) { |
6f9fad8837de
(svn r23217) -Codechange: introduce the concept of scanning only in a limited set of sub directories
rubidium <rubidium@openttd.org>
parents:
18380
diff
changeset
|
686 num += fs.DoScan(SCENARIO_DIR); |
19126
dde71b400db3
(svn r23983) -Fix [FS#5083]: tarred heightmaps wouldn't be found
rubidium <rubidium@openttd.org>
parents:
18764
diff
changeset
|
687 num += fs.DoScan(HEIGHTMAP_DIR); |
18381
6f9fad8837de
(svn r23217) -Codechange: introduce the concept of scanning only in a limited set of sub directories
rubidium <rubidium@openttd.org>
parents:
18380
diff
changeset
|
688 } |
15148
236482e2e554
(svn r19777) -Change: use the file scanner to find the .tars
rubidium <rubidium@openttd.org>
parents:
15002
diff
changeset
|
689 DEBUG(misc, 1, "Scan complete, found %d files", num); |
236482e2e554
(svn r19777) -Change: use the file scanner to find the .tars
rubidium <rubidium@openttd.org>
parents:
15002
diff
changeset
|
690 return num; |
236482e2e554
(svn r19777) -Change: use the file scanner to find the .tars
rubidium <rubidium@openttd.org>
parents:
15002
diff
changeset
|
691 } |
236482e2e554
(svn r19777) -Change: use the file scanner to find the .tars
rubidium <rubidium@openttd.org>
parents:
15002
diff
changeset
|
692 |
18398
78922d7138a3
(svn r23234) -Fix [FS#4840]: crash when after downloading content
rubidium <rubidium@openttd.org>
parents:
18392
diff
changeset
|
693 /** |
78922d7138a3
(svn r23234) -Fix [FS#4840]: crash when after downloading content
rubidium <rubidium@openttd.org>
parents:
18392
diff
changeset
|
694 * Add a single file to the scanned files of a tar, circumventing the scanning code. |
78922d7138a3
(svn r23234) -Fix [FS#4840]: crash when after downloading content
rubidium <rubidium@openttd.org>
parents:
18392
diff
changeset
|
695 * @param sd The sub directory the file is in. |
78922d7138a3
(svn r23234) -Fix [FS#4840]: crash when after downloading content
rubidium <rubidium@openttd.org>
parents:
18392
diff
changeset
|
696 * @param filename The name of the file to add. |
78922d7138a3
(svn r23234) -Fix [FS#4840]: crash when after downloading content
rubidium <rubidium@openttd.org>
parents:
18392
diff
changeset
|
697 * @return True if the additions went correctly. |
78922d7138a3
(svn r23234) -Fix [FS#4840]: crash when after downloading content
rubidium <rubidium@openttd.org>
parents:
18392
diff
changeset
|
698 */ |
78922d7138a3
(svn r23234) -Fix [FS#4840]: crash when after downloading content
rubidium <rubidium@openttd.org>
parents:
18392
diff
changeset
|
699 bool TarScanner::AddFile(Subdirectory sd, const char *filename) |
78922d7138a3
(svn r23234) -Fix [FS#4840]: crash when after downloading content
rubidium <rubidium@openttd.org>
parents:
18392
diff
changeset
|
700 { |
78922d7138a3
(svn r23234) -Fix [FS#4840]: crash when after downloading content
rubidium <rubidium@openttd.org>
parents:
18392
diff
changeset
|
701 this->subdir = sd; |
78922d7138a3
(svn r23234) -Fix [FS#4840]: crash when after downloading content
rubidium <rubidium@openttd.org>
parents:
18392
diff
changeset
|
702 return this->AddFile(filename, 0); |
78922d7138a3
(svn r23234) -Fix [FS#4840]: crash when after downloading content
rubidium <rubidium@openttd.org>
parents:
18392
diff
changeset
|
703 } |
78922d7138a3
(svn r23234) -Fix [FS#4840]: crash when after downloading content
rubidium <rubidium@openttd.org>
parents:
18392
diff
changeset
|
704 |
18083
7de5da015146
(svn r22904) -Codechange: add tar filename to file scanning
rubidium <rubidium@openttd.org>
parents:
18026
diff
changeset
|
705 bool TarScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename) |
7581
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
706 { |
18083
7de5da015146
(svn r22904) -Codechange: add tar filename to file scanning
rubidium <rubidium@openttd.org>
parents:
18026
diff
changeset
|
707 /* No tar within tar. */ |
7de5da015146
(svn r22904) -Codechange: add tar filename to file scanning
rubidium <rubidium@openttd.org>
parents:
18026
diff
changeset
|
708 assert(tar_filename == NULL); |
7de5da015146
(svn r22904) -Codechange: add tar filename to file scanning
rubidium <rubidium@openttd.org>
parents:
18026
diff
changeset
|
709 |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
710 /* The TAR-header, repeated for every file */ |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
711 typedef struct TarHeader { |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
712 char name[100]; ///< Name of the file |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
713 char mode[8]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
714 char uid[8]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
715 char gid[8]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
716 char size[12]; ///< Size of the file, in ASCII |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
717 char mtime[12]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
718 char chksum[8]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
719 char typeflag; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
720 char linkname[100]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
721 char magic[6]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
722 char version[2]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
723 char uname[32]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
724 char gname[32]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
725 char devmajor[8]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
726 char devminor[8]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
727 char prefix[155]; ///< Path of the file |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
728 |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
729 char unused[12]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
730 } TarHeader; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
731 |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
732 /* Check if we already seen this file */ |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
733 TarList::iterator it = _tar_list[this->subdir].find(filename); |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
734 if (it != _tar_list[this->subdir].end()) return false; |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
735 |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
736 FILE *f = fopen(filename, "rb"); |
14836
70a2afc34685
(svn r19427) -Codechange: gracefully handle the case where we can't open a .tar file.
rubidium <rubidium@openttd.org>
parents:
14681
diff
changeset
|
737 /* Although the file has been found there can be |
70a2afc34685
(svn r19427) -Codechange: gracefully handle the case where we can't open a .tar file.
rubidium <rubidium@openttd.org>
parents:
14681
diff
changeset
|
738 * a number of reasons we cannot open the file. |
70a2afc34685
(svn r19427) -Codechange: gracefully handle the case where we can't open a .tar file.
rubidium <rubidium@openttd.org>
parents:
14681
diff
changeset
|
739 * Most common case is when we simply have not |
70a2afc34685
(svn r19427) -Codechange: gracefully handle the case where we can't open a .tar file.
rubidium <rubidium@openttd.org>
parents:
14681
diff
changeset
|
740 * been given read access. */ |
70a2afc34685
(svn r19427) -Codechange: gracefully handle the case where we can't open a .tar file.
rubidium <rubidium@openttd.org>
parents:
14681
diff
changeset
|
741 if (f == NULL) return false; |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
742 |
9702
ab8ad03c5ff9
(svn r13810) -Fix: small memory leak when tar files would be found.
rubidium <rubidium@openttd.org>
parents:
9635
diff
changeset
|
743 const char *dupped_filename = strdup(filename); |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
744 _tar_list[this->subdir][filename].filename = dupped_filename; |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
745 _tar_list[this->subdir][filename].dirname = NULL; |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
746 |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
747 TarLinkList links; ///< Temporary list to collect links |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
748 |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
749 TarHeader th; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
750 char buf[sizeof(th.name) + 1], *end; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
751 char name[sizeof(th.prefix) + 1 + sizeof(th.name) + 1]; |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
752 char link[sizeof(th.linkname) + 1]; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
753 char dest[sizeof(th.prefix) + 1 + sizeof(th.name) + 1 + 1 + sizeof(th.linkname) + 1]; |
9146
34811fd37a7e
(svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx <glx@openttd.org>
parents:
9103
diff
changeset
|
754 size_t num = 0, pos = 0; |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
755 |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
756 /* Make a char of 512 empty bytes */ |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
757 char empty[512]; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
758 memset(&empty[0], 0, sizeof(empty)); |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
759 |
9630
2558f66e22b5
(svn r13693) -Fix (r11106, r11117): Do not rely on .tar files always ending with a block of zeros.
frosch <frosch@openttd.org>
parents:
9581
diff
changeset
|
760 for (;;) { // Note: feof() always returns 'false' after 'fseek()'. Cool, isn't it? |
2558f66e22b5
(svn r13693) -Fix (r11106, r11117): Do not rely on .tar files always ending with a block of zeros.
frosch <frosch@openttd.org>
parents:
9581
diff
changeset
|
761 size_t num_bytes_read = fread(&th, 1, 512, f); |
2558f66e22b5
(svn r13693) -Fix (r11106, r11117): Do not rely on .tar files always ending with a block of zeros.
frosch <frosch@openttd.org>
parents:
9581
diff
changeset
|
762 if (num_bytes_read != 512) break; |
2558f66e22b5
(svn r13693) -Fix (r11106, r11117): Do not rely on .tar files always ending with a block of zeros.
frosch <frosch@openttd.org>
parents:
9581
diff
changeset
|
763 pos += num_bytes_read; |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
764 |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
765 /* Check if we have the new tar-format (ustar) or the old one (a lot of zeros after 'link' field) */ |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
766 if (strncmp(th.magic, "ustar", 5) != 0 && memcmp(&th.magic, &empty[0], 512 - offsetof(TarHeader, magic)) != 0) { |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
767 /* If we have only zeros in the block, it can be an end-of-file indicator */ |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
768 if (memcmp(&th, &empty[0], 512) == 0) continue; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
769 |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
770 DEBUG(misc, 0, "The file '%s' isn't a valid tar-file", filename); |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
771 return false; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
772 } |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
773 |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
774 name[0] = '\0'; |
9146
34811fd37a7e
(svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx <glx@openttd.org>
parents:
9103
diff
changeset
|
775 size_t len = 0; |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
776 |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
777 /* The prefix contains the directory-name */ |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
778 if (th.prefix[0] != '\0') { |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
779 memcpy(name, th.prefix, sizeof(th.prefix)); |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
780 name[sizeof(th.prefix)] = '\0'; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
781 len = strlen(name); |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
782 name[len] = PATHSEPCHAR; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
783 len++; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
784 } |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
785 |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
786 /* Copy the name of the file in a safe way at the end of 'name' */ |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
787 memcpy(&name[len], th.name, sizeof(th.name)); |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
788 name[len + sizeof(th.name)] = '\0'; |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
789 |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
790 /* Calculate the size of the file.. for some strange reason this is stored as a string */ |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
791 memcpy(buf, th.size, sizeof(th.size)); |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
792 buf[sizeof(th.size)] = '\0'; |
11875
86fca539ec7a
(svn r16269) -Codechange: use gcc's ability to check parameters sent to printf-like functions
smatz <smatz@openttd.org>
parents:
11644
diff
changeset
|
793 size_t skip = strtoul(buf, &end, 8); |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
794 |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
795 switch (th.typeflag) { |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
796 case '\0': |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
797 case '0': { // regular file |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
798 /* Ignore empty files */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
799 if (skip == 0) break; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
800 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
801 if (strlen(name) == 0) break; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
802 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
803 /* Store this entry in the list */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
804 TarFileListEntry entry; |
9702
ab8ad03c5ff9
(svn r13810) -Fix: small memory leak when tar files would be found.
rubidium <rubidium@openttd.org>
parents:
9635
diff
changeset
|
805 entry.tar_filename = dupped_filename; |
ab8ad03c5ff9
(svn r13810) -Fix: small memory leak when tar files would be found.
rubidium <rubidium@openttd.org>
parents:
9635
diff
changeset
|
806 entry.size = skip; |
ab8ad03c5ff9
(svn r13810) -Fix: small memory leak when tar files would be found.
rubidium <rubidium@openttd.org>
parents:
9635
diff
changeset
|
807 entry.position = pos; |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
808 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
809 /* Convert to lowercase and our PATHSEPCHAR */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
810 SimplifyFileName(name); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
811 |
11875
86fca539ec7a
(svn r16269) -Codechange: use gcc's ability to check parameters sent to printf-like functions
smatz <smatz@openttd.org>
parents:
11644
diff
changeset
|
812 DEBUG(misc, 6, "Found file in tar: %s (" PRINTF_SIZE " bytes, " PRINTF_SIZE " offset)", name, skip, pos); |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
813 if (_tar_filelist[this->subdir].insert(TarFileList::value_type(name, entry)).second) num++; |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
814 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
815 break; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
816 } |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
817 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
818 case '1': // hard links |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
819 case '2': { // symbolic links |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
820 /* Copy the destination of the link in a safe way at the end of 'linkname' */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
821 memcpy(link, th.linkname, sizeof(th.linkname)); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
822 link[sizeof(th.linkname)] = '\0'; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
823 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
824 if (strlen(name) == 0 || strlen(link) == 0) break; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
825 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
826 /* Convert to lowercase and our PATHSEPCHAR */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
827 SimplifyFileName(name); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
828 SimplifyFileName(link); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
829 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
830 /* Only allow relative links */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
831 if (link[0] == PATHSEPCHAR) { |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
832 DEBUG(misc, 1, "Ignoring absolute link in tar: %s -> %s", name, link); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
833 break; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
834 } |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
835 |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
836 /* Process relative path. |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
837 * Note: The destination of links must not contain any directory-links. */ |
11644
7980a47b17cb
(svn r16024) -Codechange: harden string copying on places where it's possible
rubidium <rubidium@openttd.org>
parents:
11392
diff
changeset
|
838 strecpy(dest, name, lastof(dest)); |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
839 char *destpos = strrchr(dest, PATHSEPCHAR); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
840 if (destpos == NULL) destpos = dest; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
841 *destpos = '\0'; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
842 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
843 char *pos = link; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
844 while (*pos != '\0') { |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
845 char *next = strchr(link, PATHSEPCHAR); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
846 if (next == NULL) next = pos + strlen(pos); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
847 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
848 /* Skip '.' (current dir) */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
849 if (next != pos + 1 || pos[0] != '.') { |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
850 if (next == pos + 2 && pos[0] == '.' && pos[1] == '.') { |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
851 /* level up */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
852 if (dest[0] == '\0') { |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
853 DEBUG(misc, 1, "Ignoring link pointing outside of data directory: %s -> %s", name, link); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
854 break; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
855 } |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
856 |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
857 /* Truncate 'dest' after last PATHSEPCHAR. |
16349
b1fc0cfd2943
(svn r21060) -Doc: Improved wording of comments (mostly by __ln__)
alberth <alberth@openttd.org>
parents:
16001
diff
changeset
|
858 * This assumes that the truncated part is a real directory and not a link. */ |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
859 destpos = strrchr(dest, PATHSEPCHAR); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
860 if (destpos == NULL) destpos = dest; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
861 } else { |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
862 /* Append at end of 'dest' */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
863 if (destpos != dest) *(destpos++) = PATHSEPCHAR; |
10201
6b20fe326959
(svn r14414) -Fix: replace instances of strncpy with strecpy as strncpy doesn't guarantee the resulting string is '\0'-terminated.
rubidium <rubidium@openttd.org>
parents:
10039
diff
changeset
|
864 strncpy(destpos, pos, next - pos); // Safe as we do '\0'-termination ourselves |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
865 destpos += next - pos; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
866 } |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
867 *destpos = '\0'; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
868 } |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
869 |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
870 pos = next; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
871 } |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
872 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
873 /* Store links in temporary list */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
874 DEBUG(misc, 6, "Found link in tar: %s -> %s", name, dest); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
875 links.insert(TarLinkList::value_type(name, dest)); |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
876 |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
877 break; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
878 } |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
879 |
10696
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
880 case '5': // directory |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
881 /* Convert to lowercase and our PATHSEPCHAR */ |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
882 SimplifyFileName(name); |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
883 |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
884 /* Store the first directory name we detect */ |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
885 DEBUG(misc, 6, "Found dir in tar: %s", name); |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
886 if (_tar_list[this->subdir][filename].dirname == NULL) _tar_list[this->subdir][filename].dirname = strdup(name); |
10696
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
887 break; |
7edccbb67398
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents:
10405
diff
changeset
|
888 |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
889 default: |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
890 /* Ignore other types */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
891 break; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
892 } |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
893 |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
894 /* Skip to the next block.. */ |
7927
09dbacdca8de
(svn r11480) -Codechange: Rename the function ALIGN fitting to the naming style
skidd13 <skidd13@openttd.org>
parents:
7911
diff
changeset
|
895 skip = Align(skip, 512); |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
896 fseek(f, skip, SEEK_CUR); |
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
897 pos += skip; |
7581
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
898 } |
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
899 |
11875
86fca539ec7a
(svn r16269) -Codechange: use gcc's ability to check parameters sent to printf-like functions
smatz <smatz@openttd.org>
parents:
11644
diff
changeset
|
900 DEBUG(misc, 1, "Found tar '%s' with " PRINTF_SIZE " new files", filename, num); |
7592
eb177cee0aa4
(svn r11119) -Codechange: analyze .tar files upon loading, remembering their files and offsets, which speeds up .tar handling with a big factor
truelight <truelight@openttd.org>
parents:
7590
diff
changeset
|
901 fclose(f); |
7581
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
902 |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
903 /* Resolve file links and store directory links. |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
904 * We restrict usage of links to two cases: |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
905 * 1) Links to directories: |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
906 * Both the source path and the destination path must NOT contain any further links. |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
907 * When resolving files at most one directory link is resolved. |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
908 * 2) Links to files: |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
909 * The destination path must NOT contain any links. |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
910 * The source path may contain one directory link. |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
911 */ |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
912 for (TarLinkList::iterator link = links.begin(); link != links.end(); link++) { |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
913 const std::string &src = link->first; |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
914 const std::string &dest = link->second; |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
915 TarAddLink(src, dest, this->subdir); |
9634
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
916 } |
e37c6b4f25a2
(svn r13697) -Feature: Add some support for symbolic links in .tar files.
frosch <frosch@openttd.org>
parents:
9630
diff
changeset
|
917 |
7581
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
918 return true; |
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
919 } |
2572ccd87c4e
(svn r11106) -Add: added .tar support; you can pack all files in your data/ dir in how ever many .tar files you like, keeping the dir-structure equal to the unpacked version, and OpenTTD can handle them just like the files were unpacked
truelight <truelight@openttd.org>
parents:
7575
diff
changeset
|
920 |
14681
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
921 /** |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
922 * Extract the tar with the given filename in the directory |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
923 * where the tar resides. |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
924 * @param tar_filename the name of the tar to extract. |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
925 * @param subdir The sub directory the tar is in. |
14681
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
926 * @return false on failure. |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
927 */ |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
928 bool ExtractTar(const char *tar_filename, Subdirectory subdir) |
14681
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
929 { |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
930 TarList::iterator it = _tar_list[subdir].find(tar_filename); |
14681
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
931 /* We don't know the file. */ |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
932 if (it == _tar_list[subdir].end()) return false; |
14681
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
933 |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
934 const char *dirname = (*it).second.dirname; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
935 |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
936 /* The file doesn't have a sub directory! */ |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
937 if (dirname == NULL) return false; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
938 |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
939 char filename[MAX_PATH]; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
940 strecpy(filename, tar_filename, lastof(filename)); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
941 char *p = strrchr(filename, PATHSEPCHAR); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
942 /* The file's path does not have a separator? */ |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
943 if (p == NULL) return false; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
944 |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
945 p++; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
946 strecpy(p, dirname, lastof(filename)); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
947 DEBUG(misc, 8, "Extracting %s to directory %s", tar_filename, filename); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
948 FioCreateDirectory(filename); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
949 |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
950 for (TarFileList::iterator it2 = _tar_filelist[subdir].begin(); it2 != _tar_filelist[subdir].end(); it2++) { |
14681
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
951 if (strcmp((*it2).second.tar_filename, tar_filename) != 0) continue; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
952 |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
953 strecpy(p, (*it2).first.c_str(), lastof(filename)); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
954 |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
955 DEBUG(misc, 9, " extracting %s", filename); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
956 |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
957 /* First open the file in the .tar. */ |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
958 size_t to_copy = 0; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
959 FILE *in = FioFOpenFileTar(&(*it2).second, &to_copy); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
960 if (in == NULL) { |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
961 DEBUG(misc, 6, "Extracting %s failed; could not open %s", filename, tar_filename); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
962 return false; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
963 } |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
964 |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
965 /* Now open the 'output' file. */ |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
966 FILE *out = fopen(filename, "wb"); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
967 if (out == NULL) { |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
968 DEBUG(misc, 6, "Extracting %s failed; could not open %s", filename, filename); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
969 fclose(in); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
970 return false; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
971 } |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
972 |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
973 /* Now read from the tar and write it into the file. */ |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
974 char buffer[4096]; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
975 size_t read; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
976 for (; to_copy != 0; to_copy -= read) { |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
977 read = fread(buffer, 1, min(to_copy, lengthof(buffer)), in); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
978 if (read <= 0 || fwrite(buffer, 1, read, out) != read) break; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
979 } |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
980 |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
981 /* Close everything up. */ |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
982 fclose(in); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
983 fclose(out); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
984 |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
985 if (to_copy != 0) { |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
986 DEBUG(misc, 6, "Extracting %s failed; still %i bytes to copy", filename, (int)to_copy); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
987 return false; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
988 } |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
989 } |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
990 |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
991 DEBUG(misc, 9, " extraction successful"); |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
992 return true; |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
993 } |
86939658ba6b
(svn r19262) -Feature: finalise BaNaNaS support for music sets
rubidium <rubidium@openttd.org>
parents:
14669
diff
changeset
|
994 |
7589
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
995 #if defined(WIN32) || defined(WINCE) |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
996 /** |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
997 * Determine the base (personal dir and game data dir) paths |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
998 * @param exe the path from the current path to the executable |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
999 * @note defined in the OS related files (os2.cpp, win32.cpp, unix.cpp etc) |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1000 */ |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1001 extern void DetermineBasePaths(const char *exe); |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1002 #else /* defined(WIN32) || defined(WINCE) */ |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1003 |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1004 /** |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1005 * Changes the working directory to the path of the give executable. |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1006 * For OSX application bundles '.app' is the required extension of the bundle, |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1007 * so when we crop the path to there, when can remove the name of the bundle |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1008 * in the same way we remove the name from the executable name. |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1009 * @param exe the path to the executable |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1010 */ |
17688
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1011 static bool ChangeWorkingDirectoryToExecutable(const char *exe) |
7589
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1012 { |
17688
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1013 bool success = false; |
7589
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1014 #ifdef WITH_COCOA |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1015 char *app_bundle = strchr(exe, '.'); |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1016 while (app_bundle != NULL && strncasecmp(app_bundle, ".app", 4) != 0) app_bundle = strchr(&app_bundle[1], '.'); |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1017 |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1018 if (app_bundle != NULL) app_bundle[0] = '\0'; |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1019 #endif /* WITH_COCOA */ |
12016
d45475f68f60
(svn r16422) -Codechange: use const_cast for removing const and warn when const is (accidentally?) removed using C-style casts.
rubidium <rubidium@openttd.org>
parents:
12015
diff
changeset
|
1020 char *s = const_cast<char *>(strrchr(exe, PATHSEPCHAR)); |
7589
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1021 if (s != NULL) { |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1022 *s = '\0'; |
10385
9709c41adf56
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium <rubidium@openttd.org>
parents:
10384
diff
changeset
|
1023 #if defined(__DJGPP__) |
9709c41adf56
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium <rubidium@openttd.org>
parents:
10384
diff
changeset
|
1024 /* If we want to go to the root, we can't use cd C:, but we must use '/' */ |
9709c41adf56
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium <rubidium@openttd.org>
parents:
10384
diff
changeset
|
1025 if (s[-1] == ':') chdir("/"); |
9709c41adf56
(svn r14636) -Add: DOS port of OpenTTD, without network support though.
rubidium <rubidium@openttd.org>
parents:
10384
diff
changeset
|
1026 #endif |
17688
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1027 if (chdir(exe) != 0) { |
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1028 DEBUG(misc, 0, "Directory with the binary does not exist?"); |
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1029 } else { |
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1030 success = true; |
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1031 } |
7589
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1032 *s = PATHSEPCHAR; |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1033 } |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1034 #ifdef WITH_COCOA |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1035 if (app_bundle != NULL) app_bundle[0] = '.'; |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1036 #endif /* WITH_COCOA */ |
17688
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1037 return success; |
7589
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1038 } |
d4f0555f58e6
(svn r11116) -Fix (r11106): tar loader was not compiled for WIN32
glx <glx@openttd.org>
parents:
7584
diff
changeset
|
1039 |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
1040 /** |
15513
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1041 * Whether we should scan the working directory. |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1042 * It should not be scanned if it's the root or |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1043 * the home directory as in both cases a big data |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1044 * directory can cause huge amounts of unrelated |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1045 * files scanned. Furthermore there are nearly no |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1046 * use cases for the home/root directory to have |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1047 * OpenTTD directories. |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1048 * @return true if it should be scanned. |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1049 */ |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1050 bool DoScanWorkingDirectory() |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1051 { |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1052 /* No working directory, so nothing to do. */ |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1053 if (_searchpaths[SP_WORKING_DIR] == NULL) return false; |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1054 |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1055 /* Working directory is root, so do nothing. */ |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1056 if (strcmp(_searchpaths[SP_WORKING_DIR], PATHSEP) == 0) return false; |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1057 |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1058 /* No personal/home directory, so the working directory won't be that. */ |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1059 if (_searchpaths[SP_PERSONAL_DIR] == NULL) return true; |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1060 |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1061 char tmp[MAX_PATH]; |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1062 snprintf(tmp, lengthof(tmp), "%s%s", _searchpaths[SP_WORKING_DIR], PERSONAL_DIR); |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1063 AppendPathSeparator(tmp, MAX_PATH); |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1064 return strcmp(tmp, _searchpaths[SP_PERSONAL_DIR]) != 0; |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1065 } |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1066 |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1067 /** |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
1068 * 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
|
1069 * @param exe the path to the executable |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
1070 */ |
6317
d5d3f3379eb0
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents:
6299
diff
changeset
|
1071 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
|
1072 { |
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
|
1073 char tmp[MAX_PATH]; |
11392
22ae657143b3
(svn r15745) -Fix: Resolve compile error in fileio.cpp on OS/2 - base paths on OS/2 behave much like DOS
orudge <orudge@openttd.org>
parents:
11368
diff
changeset
|
1074 #if defined(__MORPHOS__) || defined(__AMIGA__) || defined(DOS) || defined(OS2) || !defined(WITH_PERSONAL_DIR) |
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
|
1075 _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
|
1076 #else |
14578
af0dd884a764
(svn r19151) -Codechange [FS#3625]: Move home directory to a better place in Haiku (jrepan)
rubidium <rubidium@openttd.org>
parents:
14443
diff
changeset
|
1077 #ifdef __HAIKU__ |
af0dd884a764
(svn r19151) -Codechange [FS#3625]: Move home directory to a better place in Haiku (jrepan)
rubidium <rubidium@openttd.org>
parents:
14443
diff
changeset
|
1078 BPath path; |
af0dd884a764
(svn r19151) -Codechange [FS#3625]: Move home directory to a better place in Haiku (jrepan)
rubidium <rubidium@openttd.org>
parents:
14443
diff
changeset
|
1079 find_directory(B_USER_SETTINGS_DIRECTORY, &path); |
af0dd884a764
(svn r19151) -Codechange [FS#3625]: Move home directory to a better place in Haiku (jrepan)
rubidium <rubidium@openttd.org>
parents:
14443
diff
changeset
|
1080 const char *homedir = path.Path(); |
af0dd884a764
(svn r19151) -Codechange [FS#3625]: Move home directory to a better place in Haiku (jrepan)
rubidium <rubidium@openttd.org>
parents:
14443
diff
changeset
|
1081 #else |
6317
d5d3f3379eb0
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents:
6299
diff
changeset
|
1082 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
|
1083 |
d5d3f3379eb0
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents:
6299
diff
changeset
|
1084 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
|
1085 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
|
1086 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
|
1087 } |
14578
af0dd884a764
(svn r19151) -Codechange [FS#3625]: Move home directory to a better place in Haiku (jrepan)
rubidium <rubidium@openttd.org>
parents:
14443
diff
changeset
|
1088 #endif |
6317
d5d3f3379eb0
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents:
6299
diff
changeset
|
1089 |
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
|
1090 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
|
1091 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
|
1092 |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
1093 _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
|
1094 #endif |
8273
5b296fce52ee
(svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni <bjarni@openttd.org>
parents:
8214
diff
changeset
|
1095 |
5b296fce52ee
(svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni <bjarni@openttd.org>
parents:
8214
diff
changeset
|
1096 #if defined(WITH_SHARED_DIR) |
5b296fce52ee
(svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni <bjarni@openttd.org>
parents:
8214
diff
changeset
|
1097 snprintf(tmp, MAX_PATH, "%s", SHARED_DIR); |
5b296fce52ee
(svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni <bjarni@openttd.org>
parents:
8214
diff
changeset
|
1098 AppendPathSeparator(tmp, MAX_PATH); |
5b296fce52ee
(svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni <bjarni@openttd.org>
parents:
8214
diff
changeset
|
1099 _searchpaths[SP_SHARED_DIR] = strdup(tmp); |
5b296fce52ee
(svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni <bjarni@openttd.org>
parents:
8214
diff
changeset
|
1100 #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
|
1101 _searchpaths[SP_SHARED_DIR] = NULL; |
8273
5b296fce52ee
(svn r11837) -Feature: [OSX] added "/Library/Application Support/OpenTTD" to the search paths (based on patch by pv2b)
bjarni <bjarni@openttd.org>
parents:
8214
diff
changeset
|
1102 #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
|
1103 |
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
|
1104 #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
|
1105 _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
|
1106 #else |
9100
2c5477e04d76
(svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium <rubidium@openttd.org>
parents:
9084
diff
changeset
|
1107 if (getcwd(tmp, MAX_PATH) == NULL) *tmp = '\0'; |
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
|
1108 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
|
1109 _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
|
1110 #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
|
1111 |
15513
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1112 _do_scan_working_directory = DoScanWorkingDirectory(); |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1113 |
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
|
1114 /* Change the working directory to that one of the executable */ |
17688
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1115 if (ChangeWorkingDirectoryToExecutable(exe)) { |
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1116 if (getcwd(tmp, MAX_PATH) == NULL) *tmp = '\0'; |
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1117 AppendPathSeparator(tmp, MAX_PATH); |
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1118 _searchpaths[SP_BINARY_DIR] = strdup(tmp); |
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1119 } else { |
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1120 _searchpaths[SP_BINARY_DIR] = NULL; |
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1121 } |
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
|
1122 |
13179
ac1b795b8da4
(svn r17686) -Fix [FS#3247]: make the -c <config file> location relative to the current directory instead of the directory of the binary
rubidium <rubidium@openttd.org>
parents:
13175
diff
changeset
|
1123 if (_searchpaths[SP_WORKING_DIR] != NULL) { |
ac1b795b8da4
(svn r17686) -Fix [FS#3247]: make the -c <config file> location relative to the current directory instead of the directory of the binary
rubidium <rubidium@openttd.org>
parents:
13175
diff
changeset
|
1124 /* Go back to the current working directory. */ |
17688
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1125 if (chdir(_searchpaths[SP_WORKING_DIR]) != 0) { |
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1126 DEBUG(misc, 0, "Failed to return to working directory!"); |
18cbda268559
(svn r22465) -Fix [FS#4613]: When determining the executable path failed, the working directory was used instead, circumventing the not-home-directory check.
frosch <frosch@openttd.org>
parents:
17685
diff
changeset
|
1127 } |
13179
ac1b795b8da4
(svn r17686) -Fix [FS#3247]: make the -c <config file> location relative to the current directory instead of the directory of the binary
rubidium <rubidium@openttd.org>
parents:
13175
diff
changeset
|
1128 } |
ac1b795b8da4
(svn r17686) -Fix [FS#3247]: make the -c <config file> location relative to the current directory instead of the directory of the binary
rubidium <rubidium@openttd.org>
parents:
13175
diff
changeset
|
1129 |
11392
22ae657143b3
(svn r15745) -Fix: Resolve compile error in fileio.cpp on OS/2 - base paths on OS/2 behave much like DOS
orudge <orudge@openttd.org>
parents:
11368
diff
changeset
|
1130 #if defined(__MORPHOS__) || defined(__AMIGA__) || defined(DOS) || defined(OS2) |
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
|
1131 _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
|
1132 #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
|
1133 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
|
1134 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
|
1135 _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
|
1136 #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
|
1137 #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
|
1138 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
|
1139 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
|
1140 #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
|
1141 _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
|
1142 #endif |
6317
d5d3f3379eb0
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents:
6299
diff
changeset
|
1143 } |
d5d3f3379eb0
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents:
6299
diff
changeset
|
1144 #endif /* defined(WIN32) || defined(WINCE) */ |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
1145 |
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
|
1146 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
|
1147 |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
1148 /** |
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
1149 * 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
|
1150 * 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
|
1151 * 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
|
1152 * @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
|
1153 */ |
6317
d5d3f3379eb0
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents:
6299
diff
changeset
|
1154 void DeterminePaths(const char *exe) |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
1155 { |
6317
d5d3f3379eb0
(svn r9266) -Codechange: unify the retrieval of the base paths a little more.
rubidium <rubidium@openttd.org>
parents:
6299
diff
changeset
|
1156 DetermineBasePaths(exe); |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
1157 |
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
|
1158 Searchpath sp; |
15513
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1159 FOR_ALL_SEARCHPATHS(sp) { |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1160 if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue; |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1161 DEBUG(misc, 4, "%s added as search path", _searchpaths[sp]); |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1162 } |
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
|
1163 |
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
|
1164 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
|
1165 _personal_dir = strdup(_config_file); |
13175
3f4cc2986131
(svn r17682) -Codechange: remove erroneous space before some commas
rubidium <rubidium@openttd.org>
parents:
13070
diff
changeset
|
1166 char *end = strrchr(_personal_dir, PATHSEPCHAR); |
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
|
1167 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
|
1168 _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
|
1169 } 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
|
1170 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
|
1171 } |
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
|
1172 } 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
|
1173 char personal_dir[MAX_PATH]; |
17872
339c99ac8633
(svn r22668) -Codechange: FioFindFullPath tests already whether the file exists.
alberth <alberth@openttd.org>
parents:
17722
diff
changeset
|
1174 if (FioFindFullPath(personal_dir, lengthof(personal_dir), BASE_DIR, "openttd.cfg") != NULL) { |
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
|
1175 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
|
1176 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
|
1177 _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
|
1178 _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
|
1179 } 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
|
1180 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
|
1181 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
|
1182 }; |
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
|
1183 |
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
|
1184 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
|
1185 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
|
1186 _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
|
1187 _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
|
1188 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
|
1189 } |
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
|
1190 } |
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
|
1191 } |
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
|
1192 } |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
1193 |
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
|
1194 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
|
1195 |
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
|
1196 _highscore_file = str_fmt("%shs.dat", _personal_dir); |
15412
70fe94392d25
(svn r20055) -Feature: save/load hotkeys to/from hotkeys.cfg
yexo <yexo@openttd.org>
parents:
15150
diff
changeset
|
1197 extern char *_hotkeys_file; |
70fe94392d25
(svn r20055) -Feature: save/load hotkeys to/from hotkeys.cfg
yexo <yexo@openttd.org>
parents:
15150
diff
changeset
|
1198 _hotkeys_file = str_fmt("%shotkeys.cfg", _personal_dir); |
20339
4bafd180b961
(svn r25291) -Add: WindowDesc ability to load and store information in a config file.
frosch <frosch@openttd.org>
parents:
19968
diff
changeset
|
1199 extern char *_windows_file; |
4bafd180b961
(svn r25291) -Add: WindowDesc ability to load and store information in a config file.
frosch <frosch@openttd.org>
parents:
19968
diff
changeset
|
1200 _windows_file = str_fmt("%swindows.cfg", _personal_dir); |
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
|
1201 |
bb43858bed10
(svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium <rubidium@openttd.org>
parents:
6896
diff
changeset
|
1202 /* Make the necessary folders */ |
7911
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
1203 #if !defined(__MORPHOS__) && !defined(__AMIGA__) && defined(WITH_PERSONAL_DIR) |
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
|
1204 FioCreateDirectory(_personal_dir); |
7911
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
1205 #endif |
6a9e47d1d4dd
(svn r11464) -Fix: some issue that made OpenTTD on MorphOS seriously unplayable.
rubidium <rubidium@openttd.org>
parents:
7805
diff
changeset
|
1206 |
11263
fd811b569e21
(svn r15612) -Codechange: unduplicate directory creation
rubidium <rubidium@openttd.org>
parents:
11261
diff
changeset
|
1207 static const Subdirectory default_subdirs[] = { |
19871
723a91a42f5c
(svn r24804) -Add: Separate subdirectory for screenshots.
frosch <frosch@openttd.org>
parents:
19126
diff
changeset
|
1208 SAVE_DIR, AUTOSAVE_DIR, SCENARIO_DIR, HEIGHTMAP_DIR, BASESET_DIR, NEWGRF_DIR, AI_DIR, AI_LIBRARY_DIR, GAME_DIR, GAME_LIBRARY_DIR, SCREENSHOT_DIR |
11263
fd811b569e21
(svn r15612) -Codechange: unduplicate directory creation
rubidium <rubidium@openttd.org>
parents:
11261
diff
changeset
|
1209 }; |
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
|
1210 |
11263
fd811b569e21
(svn r15612) -Codechange: unduplicate directory creation
rubidium <rubidium@openttd.org>
parents:
11261
diff
changeset
|
1211 for (uint i = 0; i < lengthof(default_subdirs); i++) { |
13507
54c2124042b7
(svn r18026) -Codechange: make a lookup table static that doesn't need to be 'exported'
rubidium <rubidium@openttd.org>
parents:
13179
diff
changeset
|
1212 char *dir = str_fmt("%s%s", _personal_dir, _subdirs[default_subdirs[i]]); |
11263
fd811b569e21
(svn r15612) -Codechange: unduplicate directory creation
rubidium <rubidium@openttd.org>
parents:
11261
diff
changeset
|
1213 FioCreateDirectory(dir); |
fd811b569e21
(svn r15612) -Codechange: unduplicate directory creation
rubidium <rubidium@openttd.org>
parents:
11261
diff
changeset
|
1214 free(dir); |
fd811b569e21
(svn r15612) -Codechange: unduplicate directory creation
rubidium <rubidium@openttd.org>
parents:
11261
diff
changeset
|
1215 } |
10780
2202e03eb494
(svn r15113) -Codechange: unify calling ScanForTarFiles when determining the paths.
rubidium <rubidium@openttd.org>
parents:
10779
diff
changeset
|
1216 |
10793
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1217 /* If we have network we make a directory for the autodownloading of content */ |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1218 _searchpaths[SP_AUTODOWNLOAD_DIR] = str_fmt("%s%s", _personal_dir, "content_download" PATHSEP); |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1219 #ifdef ENABLE_NETWORK |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1220 FioCreateDirectory(_searchpaths[SP_AUTODOWNLOAD_DIR]); |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1221 |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1222 /* Create the directory for each of the types of content */ |
18764
bd2a1a20a792
(svn r23612) -Add: allow importing libraries in the same way as AI does, only with GS prefix (and in game/library)
truebrain <truebrain@openttd.org>
parents:
18757
diff
changeset
|
1223 const Subdirectory dirs[] = { SCENARIO_DIR, HEIGHTMAP_DIR, BASESET_DIR, NEWGRF_DIR, AI_DIR, AI_LIBRARY_DIR, GAME_DIR, GAME_LIBRARY_DIR }; |
10793
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1224 for (uint i = 0; i < lengthof(dirs); i++) { |
13507
54c2124042b7
(svn r18026) -Codechange: make a lookup table static that doesn't need to be 'exported'
rubidium <rubidium@openttd.org>
parents:
13179
diff
changeset
|
1225 char *tmp = str_fmt("%s%s", _searchpaths[SP_AUTODOWNLOAD_DIR], _subdirs[dirs[i]]); |
10793
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1226 FioCreateDirectory(tmp); |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1227 free(tmp); |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1228 } |
15535
a3466ee74c31
(svn r20194) -Fix: compilation with network disabled failed
rubidium <rubidium@openttd.org>
parents:
15533
diff
changeset
|
1229 |
a3466ee74c31
(svn r20194) -Fix: compilation with network disabled failed
rubidium <rubidium@openttd.org>
parents:
15533
diff
changeset
|
1230 extern char *_log_file; |
a3466ee74c31
(svn r20194) -Fix: compilation with network disabled failed
rubidium <rubidium@openttd.org>
parents:
15533
diff
changeset
|
1231 _log_file = str_fmt("%sopenttd.log", _personal_dir); |
10793
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1232 #else /* ENABLE_NETWORK */ |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1233 /* If we don't have networking, we don't need to make the directory. But |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1234 * if it exists we keep it, otherwise remove it from the search paths. */ |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1235 if (!FileExists(_searchpaths[SP_AUTODOWNLOAD_DIR])) { |
18362
d7607ed4eef6
(svn r23198) -Codechange: introduce a free that takes const pointers so we don't need to cast to void/non-const before being able to free
rubidium <rubidium@openttd.org>
parents:
18083
diff
changeset
|
1236 free(_searchpaths[SP_AUTODOWNLOAD_DIR]); |
10793
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1237 _searchpaths[SP_AUTODOWNLOAD_DIR] = NULL; |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1238 } |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1239 #endif /* ENABLE_NETWORK */ |
6298
bd24b660fbe8
(svn r9129) -Codechange: unify parts of DeterminePaths.
rubidium <rubidium@openttd.org>
parents:
6248
diff
changeset
|
1240 } |
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
|
1241 |
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
|
1242 /** |
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
|
1243 * 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
|
1244 * @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
|
1245 */ |
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
|
1246 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
|
1247 { |
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
|
1248 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
|
1249 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
|
1250 /* 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
|
1251 * 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
|
1252 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
|
1253 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
|
1254 *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
|
1255 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
|
1256 } |
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
|
1257 } |
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
|
1258 } |
8131
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1259 |
17905
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
1260 /** |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
1261 * Load a file into memory. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
1262 * @param filename Name of the file to load. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
1263 * @param lenp [out] Length of loaded data. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
1264 * @param maxsize Maximum size to load. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
1265 * @return Pointer to new memory containing the loaded data, or \c NULL if loading failed. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
1266 * @note If \a maxsize less than the length of the file, loading fails. |
b1f6f7bb52fa
(svn r22709) -Doc: Doxyment fileio.cpp.
alberth <alberth@openttd.org>
parents:
17873
diff
changeset
|
1267 */ |
8131
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1268 void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize) |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1269 { |
11215
10653704cdaa
(svn r15568) -Cleanup: *allocT/AllocaM doesn't return NULL when allocating fails
smatz <smatz@openttd.org>
parents:
10960
diff
changeset
|
1270 FILE *in = fopen(filename, "rb"); |
8131
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1271 if (in == NULL) return NULL; |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1272 |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1273 fseek(in, 0, SEEK_END); |
11215
10653704cdaa
(svn r15568) -Cleanup: *allocT/AllocaM doesn't return NULL when allocating fails
smatz <smatz@openttd.org>
parents:
10960
diff
changeset
|
1274 size_t len = ftell(in); |
8131
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1275 fseek(in, 0, SEEK_SET); |
11215
10653704cdaa
(svn r15568) -Cleanup: *allocT/AllocaM doesn't return NULL when allocating fails
smatz <smatz@openttd.org>
parents:
10960
diff
changeset
|
1276 if (len > maxsize) { |
8131
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1277 fclose(in); |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1278 return NULL; |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1279 } |
11215
10653704cdaa
(svn r15568) -Cleanup: *allocT/AllocaM doesn't return NULL when allocating fails
smatz <smatz@openttd.org>
parents:
10960
diff
changeset
|
1280 byte *mem = MallocT<byte>(len + 1); |
8131
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1281 mem[len] = 0; |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1282 if (fread(mem, len, 1, in) != 1) { |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1283 fclose(in); |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1284 free(mem); |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1285 return NULL; |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1286 } |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1287 fclose(in); |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1288 |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1289 *lenp = len; |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1290 return mem; |
e300ac8001ae
(svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents:
8130
diff
changeset
|
1291 } |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1292 |
18025
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1293 /** |
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1294 * Helper to see whether a given filename matches the extension. |
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1295 * @param extension The extension to look for. |
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1296 * @param filename The filename to look in for the extension. |
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1297 * @return True iff the extension is NULL, or the filename ends with it. |
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1298 */ |
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1299 static bool MatchesExtension(const char *extension, const char *filename) |
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1300 { |
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1301 if (extension == NULL) return true; |
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1302 |
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1303 const char *ext = strrchr(filename, extension[0]); |
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1304 return ext != NULL && strcasecmp(ext, extension) == 0; |
7a87d9ed25a1
(svn r22834) -Codechange: unify some code, and extend it to work for other filenames that should end in a particular way
rubidium <rubidium@openttd.org>
parents:
18016
diff
changeset
|
1305 } |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1306 |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1307 /** |
15002
baf0e12ad2b4
(svn r19614) -Codechange: "it's" => "its" where appropriate
smatz <smatz@openttd.org>
parents:
14836
diff
changeset
|
1308 * Scan a single directory (and recursively its children) and add |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1309 * any graphics sets that are found. |
13070
1c4c4ae8d716
(svn r17570) -Fix: a number of Doxygen warnings about missing parameters, which were sometimes missing and sometimes just typos
rubidium <rubidium@openttd.org>
parents:
12778
diff
changeset
|
1310 * @param fs the file scanner to add the files to |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1311 * @param extension the extension of files to search for. |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1312 * @param path full path we're currently at |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1313 * @param basepath_length from where in the path are we 'based' on the search path |
13070
1c4c4ae8d716
(svn r17570) -Fix: a number of Doxygen warnings about missing parameters, which were sometimes missing and sometimes just typos
rubidium <rubidium@openttd.org>
parents:
12778
diff
changeset
|
1314 * @param recursive whether to recursively search the sub directories |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1315 */ |
11257
d83ad3b106a4
(svn r15606) -Codechange: make it possible to not recursively search with the file scanner (i.e. only search a single directory).
rubidium <rubidium@openttd.org>
parents:
11215
diff
changeset
|
1316 static uint ScanPath(FileScanner *fs, const char *extension, const char *path, size_t basepath_length, bool recursive) |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1317 { |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1318 extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb); |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1319 |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1320 uint num = 0; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1321 struct stat sb; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1322 struct dirent *dirent; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1323 DIR *dir; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1324 |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1325 if (path == NULL || (dir = ttd_opendir(path)) == NULL) return 0; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1326 |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1327 while ((dirent = readdir(dir)) != NULL) { |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1328 const char *d_name = FS2OTTD(dirent->d_name); |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1329 char filename[MAX_PATH]; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1330 |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1331 if (!FiosIsValidFile(path, dirent, &sb)) continue; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1332 |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1333 snprintf(filename, lengthof(filename), "%s%s", path, d_name); |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1334 |
10384
b0837d51a306
(svn r14635) -Change: use S_ISDIR/S_ISREG as x & S_IFREG always results false on platforms when S_IFREG is 0x0000.
rubidium <rubidium@openttd.org>
parents:
10310
diff
changeset
|
1335 if (S_ISDIR(sb.st_mode)) { |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1336 /* Directory */ |
11257
d83ad3b106a4
(svn r15606) -Codechange: make it possible to not recursively search with the file scanner (i.e. only search a single directory).
rubidium <rubidium@openttd.org>
parents:
11215
diff
changeset
|
1337 if (!recursive) continue; |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1338 if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue; |
15150
12944c945d4c
(svn r19779) -Change: add a return type to AppendPathSeparator and use that to determine whether we could append the path separator. If not, do not recurse into that directory as the path would exceed the maximum path length
rubidium <rubidium@openttd.org>
parents:
15148
diff
changeset
|
1339 if (!AppendPathSeparator(filename, lengthof(filename))) continue; |
11257
d83ad3b106a4
(svn r15606) -Codechange: make it possible to not recursively search with the file scanner (i.e. only search a single directory).
rubidium <rubidium@openttd.org>
parents:
11215
diff
changeset
|
1340 num += ScanPath(fs, extension, filename, basepath_length, recursive); |
10384
b0837d51a306
(svn r14635) -Change: use S_ISDIR/S_ISREG as x & S_IFREG always results false on platforms when S_IFREG is 0x0000.
rubidium <rubidium@openttd.org>
parents:
10310
diff
changeset
|
1341 } else if (S_ISREG(sb.st_mode)) { |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1342 /* File */ |
18083
7de5da015146
(svn r22904) -Codechange: add tar filename to file scanning
rubidium <rubidium@openttd.org>
parents:
18026
diff
changeset
|
1343 if (MatchesExtension(extension, filename) && fs->AddFile(filename, basepath_length, NULL)) num++; |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1344 } |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1345 } |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1346 |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1347 closedir(dir); |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1348 |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1349 return num; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1350 } |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1351 |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1352 /** |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1353 * Scan the given tar and add graphics sets when it finds one. |
13070
1c4c4ae8d716
(svn r17570) -Fix: a number of Doxygen warnings about missing parameters, which were sometimes missing and sometimes just typos
rubidium <rubidium@openttd.org>
parents:
12778
diff
changeset
|
1354 * @param fs the file scanner to scan for |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1355 * @param extension the extension of files to search for. |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1356 * @param tar the tar to search in. |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1357 */ |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1358 static uint ScanTar(FileScanner *fs, const char *extension, TarFileList::iterator tar) |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1359 { |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1360 uint num = 0; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1361 const char *filename = (*tar).first.c_str(); |
11258
dd1c7d2de523
(svn r15607) -Codechange: support searching files without filtering on extension.
rubidium <rubidium@openttd.org>
parents:
11257
diff
changeset
|
1362 |
18083
7de5da015146
(svn r22904) -Codechange: add tar filename to file scanning
rubidium <rubidium@openttd.org>
parents:
18026
diff
changeset
|
1363 if (MatchesExtension(extension, filename) && fs->AddFile(filename, 0, (*tar).second.tar_filename)) num++; |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1364 |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1365 return num; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1366 } |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1367 |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1368 /** |
19968
0dba7f49118c
(svn r24900) -Fix [FS#5389]: Comments with typos (most fixes supplied by Eagle_rainbow)
planetmaker <planetmaker@openttd.org>
parents:
19943
diff
changeset
|
1369 * Scan for files with the given extension in the given search path. |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1370 * @param extension the extension of files to search for. |
10238
12805d675e85
(svn r14466) -Doc: remove some obsolete parameters, fix a few parameter names in comments and add a little more doxygen documentation.
rubidium <rubidium@openttd.org>
parents:
10201
diff
changeset
|
1371 * @param sd the sub directory to search in. |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1372 * @param tars whether to search in the tars too. |
13070
1c4c4ae8d716
(svn r17570) -Fix: a number of Doxygen warnings about missing parameters, which were sometimes missing and sometimes just typos
rubidium <rubidium@openttd.org>
parents:
12778
diff
changeset
|
1373 * @param recursive whether to search recursively |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1374 * @return the number of found files, i.e. the number of times that |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1375 * AddFile returned true. |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1376 */ |
11257
d83ad3b106a4
(svn r15606) -Codechange: make it possible to not recursively search with the file scanner (i.e. only search a single directory).
rubidium <rubidium@openttd.org>
parents:
11215
diff
changeset
|
1377 uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool recursive) |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1378 { |
18026
eace4d79d513
(svn r22835) -Codechange: keep track of the subdirectory we are looking through in the file scanner
rubidium <rubidium@openttd.org>
parents:
18025
diff
changeset
|
1379 this->subdir = sd; |
eace4d79d513
(svn r22835) -Codechange: keep track of the subdirectory we are looking through in the file scanner
rubidium <rubidium@openttd.org>
parents:
18025
diff
changeset
|
1380 |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1381 Searchpath sp; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1382 char path[MAX_PATH]; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1383 TarFileList::iterator tar; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1384 uint num = 0; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1385 |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1386 FOR_ALL_SEARCHPATHS(sp) { |
15513
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1387 /* Don't search in the working directory */ |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1388 if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue; |
c4bd37c3a0c5
(svn r20166) -Fix [FS#3949]: do not scan /data and ~/data (if they happen to be your working directory). If it's the directory where your binary is located it will still scan them.
rubidium <rubidium@openttd.org>
parents:
15412
diff
changeset
|
1389 |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1390 FioAppendDirectory(path, MAX_PATH, sp, sd); |
11257
d83ad3b106a4
(svn r15606) -Codechange: make it possible to not recursively search with the file scanner (i.e. only search a single directory).
rubidium <rubidium@openttd.org>
parents:
11215
diff
changeset
|
1391 num += ScanPath(this, extension, path, strlen(path), recursive); |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1392 } |
10778
b2d1ef14b3d6
(svn r15111) -Fix: when scanning a path and saying it should ignore tars, it didn't do so
rubidium <rubidium@openttd.org>
parents:
10775
diff
changeset
|
1393 |
18380
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
1394 if (tars && sd != NO_DIRECTORY) { |
fec80cdeaab7
(svn r23216) -Codechange: introduce the concept of having different tar lists
rubidium <rubidium@openttd.org>
parents:
18362
diff
changeset
|
1395 FOR_ALL_TARS(tar, sd) { |
10778
b2d1ef14b3d6
(svn r15111) -Fix: when scanning a path and saying it should ignore tars, it didn't do so
rubidium <rubidium@openttd.org>
parents:
10775
diff
changeset
|
1396 num += ScanTar(this, extension, tar); |
b2d1ef14b3d6
(svn r15111) -Fix: when scanning a path and saying it should ignore tars, it didn't do so
rubidium <rubidium@openttd.org>
parents:
10775
diff
changeset
|
1397 } |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1398 } |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1399 |
18391
4cd52aa29256
(svn r23227) -Fix (r23219): The FileScanner should also consider the old directories.
frosch <frosch@openttd.org>
parents:
18390
diff
changeset
|
1400 switch (sd) { |
4cd52aa29256
(svn r23227) -Fix (r23219): The FileScanner should also consider the old directories.
frosch <frosch@openttd.org>
parents:
18390
diff
changeset
|
1401 case BASESET_DIR: |
4cd52aa29256
(svn r23227) -Fix (r23219): The FileScanner should also consider the old directories.
frosch <frosch@openttd.org>
parents:
18390
diff
changeset
|
1402 num += this->Scan(extension, OLD_GM_DIR, tars, recursive); |
18392
38b95a0d6307
(svn r23228) -Fix (r23227): FileScanner::Scan() still did not scan all required directories for basesets.
frosch <frosch@openttd.org>
parents:
18391
diff
changeset
|
1403 /* FALL THROUGH */ |
18391
4cd52aa29256
(svn r23227) -Fix (r23219): The FileScanner should also consider the old directories.
frosch <frosch@openttd.org>
parents:
18390
diff
changeset
|
1404 case NEWGRF_DIR: |
4cd52aa29256
(svn r23227) -Fix (r23219): The FileScanner should also consider the old directories.
frosch <frosch@openttd.org>
parents:
18390
diff
changeset
|
1405 num += this->Scan(extension, OLD_DATA_DIR, tars, recursive); |
4cd52aa29256
(svn r23227) -Fix (r23219): The FileScanner should also consider the old directories.
frosch <frosch@openttd.org>
parents:
18390
diff
changeset
|
1406 break; |
4cd52aa29256
(svn r23227) -Fix (r23219): The FileScanner should also consider the old directories.
frosch <frosch@openttd.org>
parents:
18390
diff
changeset
|
1407 |
4cd52aa29256
(svn r23227) -Fix (r23219): The FileScanner should also consider the old directories.
frosch <frosch@openttd.org>
parents:
18390
diff
changeset
|
1408 default: break; |
4cd52aa29256
(svn r23227) -Fix (r23219): The FileScanner should also consider the old directories.
frosch <frosch@openttd.org>
parents:
18390
diff
changeset
|
1409 } |
4cd52aa29256
(svn r23227) -Fix (r23219): The FileScanner should also consider the old directories.
frosch <frosch@openttd.org>
parents:
18390
diff
changeset
|
1410 |
10036
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1411 return num; |
4e6ccc5a0595
(svn r14196) -Codechange: make the searching for files with a specific extension extendable.
rubidium <rubidium@openttd.org>
parents:
9916
diff
changeset
|
1412 } |
10793
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1413 |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1414 /** |
19968
0dba7f49118c
(svn r24900) -Fix [FS#5389]: Comments with typos (most fixes supplied by Eagle_rainbow)
planetmaker <planetmaker@openttd.org>
parents:
19943
diff
changeset
|
1415 * Scan for files with the given extension in the given search path. |
10793
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1416 * @param extension the extension of files to search for. |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1417 * @param directory the sub directory to search in. |
13070
1c4c4ae8d716
(svn r17570) -Fix: a number of Doxygen warnings about missing parameters, which were sometimes missing and sometimes just typos
rubidium <rubidium@openttd.org>
parents:
12778
diff
changeset
|
1418 * @param recursive whether to search recursively |
10793
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1419 * @return the number of found files, i.e. the number of times that |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1420 * AddFile returned true. |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1421 */ |
11257
d83ad3b106a4
(svn r15606) -Codechange: make it possible to not recursively search with the file scanner (i.e. only search a single directory).
rubidium <rubidium@openttd.org>
parents:
11215
diff
changeset
|
1422 uint FileScanner::Scan(const char *extension, const char *directory, bool recursive) |
10793
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1423 { |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1424 char path[MAX_PATH]; |
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1425 strecpy(path, directory, lastof(path)); |
15150
12944c945d4c
(svn r19779) -Change: add a return type to AppendPathSeparator and use that to determine whether we could append the path separator. If not, do not recurse into that directory as the path would exceed the maximum path length
rubidium <rubidium@openttd.org>
parents:
15148
diff
changeset
|
1426 if (!AppendPathSeparator(path, lengthof(path))) return 0; |
11257
d83ad3b106a4
(svn r15606) -Codechange: make it possible to not recursively search with the file scanner (i.e. only search a single directory).
rubidium <rubidium@openttd.org>
parents:
11215
diff
changeset
|
1427 return ScanPath(this, extension, path, strlen(path), recursive); |
10793
0744d383a56b
(svn r15126) -Feature: downloading content from a central server (content.openttd.org) where authors can upload they NewGRFS/AI etc. This should make joining servers that use only NewGRFs that are distributed via this system easier as the players can download the NewGRFs from in the game. It should also make it easier to see whether there are updates for NewGRFs and make the necessary updates.
rubidium <rubidium@openttd.org>
parents:
10780
diff
changeset
|
1428 } |