author | miham |
Wed, 17 Aug 2005 12:30:07 +0000 | |
changeset 2349 | e6a55d70b372 |
parent 2338 | 8fadf1e5c5ea |
child 2356 | 43d1812406e7 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
0 | 3 |
#include "stdafx.h" |
1892
8717d92b32b1
(svn r2398) - CodeChange: forgot to update unix and os2 specific files as well.
Darkvater
parents:
1885
diff
changeset
|
4 |
#include "openttd.h" |
2163
b17b313113a0
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
5 |
#include "functions.h" |
1390
e7cdf3ce0fb6
(svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents:
1346
diff
changeset
|
6 |
#include "window.h" |
1317
3c90086ff34f
(svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
1093
diff
changeset
|
7 |
#include "string.h" |
507
04b5403aaf6b
(svn r815) Include strings.h only in the files which need it.
tron
parents:
437
diff
changeset
|
8 |
#include "table/strings.h" |
0 | 9 |
#include "hal.h" |
2159
f6284cf5fab0
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2099
diff
changeset
|
10 |
#include "variables.h" |
0 | 11 |
|
12 |
#include <dirent.h> |
|
13 |
#include <unistd.h> |
|
14 |
#include <sys/stat.h> |
|
15 |
#include <time.h> |
|
1509
795ba683e4f0
(svn r2013) Always ignore SIGPIPE, not only when SDL is used (ShadowJK)
tron
parents:
1508
diff
changeset
|
16 |
#include <signal.h> |
0 | 17 |
|
2323
eadae7d611e7
(svn r2849) Fix r2806: pwd.h is necessary, but only if USE_HOMEDIR is defined
tron
parents:
2285
diff
changeset
|
18 |
#ifdef USE_HOMEDIR |
eadae7d611e7
(svn r2849) Fix r2806: pwd.h is necessary, but only if USE_HOMEDIR is defined
tron
parents:
2285
diff
changeset
|
19 |
#include <pwd.h> |
eadae7d611e7
(svn r2849) Fix r2806: pwd.h is necessary, but only if USE_HOMEDIR is defined
tron
parents:
2285
diff
changeset
|
20 |
#endif |
eadae7d611e7
(svn r2849) Fix r2806: pwd.h is necessary, but only if USE_HOMEDIR is defined
tron
parents:
2285
diff
changeset
|
21 |
|
1850
15826a18b5ac
(svn r2356) Make check if statvfs() is availible a bit more sensible
tron
parents:
1687
diff
changeset
|
22 |
#if (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__) |
1597
ef6253448620
(svn r2101) statvfs() is availible on POSIX.1 conformant systems.
tron
parents:
1596
diff
changeset
|
23 |
#define HAS_STATVFS |
ef6253448620
(svn r2101) statvfs() is availible on POSIX.1 conformant systems.
tron
parents:
1596
diff
changeset
|
24 |
#endif |
ef6253448620
(svn r2101) statvfs() is availible on POSIX.1 conformant systems.
tron
parents:
1596
diff
changeset
|
25 |
|
ef6253448620
(svn r2101) statvfs() is availible on POSIX.1 conformant systems.
tron
parents:
1596
diff
changeset
|
26 |
#ifdef HAS_STATVFS |
0 | 27 |
#include <sys/statvfs.h> |
28 |
#endif |
|
29 |
||
30 |
||
31 |
#ifdef __MORPHOS__ |
|
32 |
#include <exec/types.h> |
|
33 |
ULONG __stack = (1024*1024)*2; // maybe not that much is needed actually ;) |
|
34 |
#endif /* __MORPHOS__ */ |
|
35 |
||
770
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
704
diff
changeset
|
36 |
#ifdef __AMIGA__ |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
704
diff
changeset
|
37 |
#warning add stack symbol to avoid that user needs to set stack manually (tokai) |
915 | 38 |
// ULONG __stack = |
770
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
704
diff
changeset
|
39 |
#endif |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
704
diff
changeset
|
40 |
|
2188
640a0a5f77ea
(svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
2186
diff
changeset
|
41 |
#if defined(__APPLE__) |
2217
7a5d829eca29
(svn r2735) -Fix: [OSX] fixed issue introduced in 2733 where dedicated servers on OSX included sdl.h even when WITH_SDL was not defined (oops)
bjarni
parents:
2215
diff
changeset
|
42 |
#if defined(WITH_SDL) |
7a5d829eca29
(svn r2735) -Fix: [OSX] fixed issue introduced in 2733 where dedicated servers on OSX included sdl.h even when WITH_SDL was not defined (oops)
bjarni
parents:
2215
diff
changeset
|
43 |
//the mac implementation needs this file included in the same file as main() |
7a5d829eca29
(svn r2735) -Fix: [OSX] fixed issue introduced in 2733 where dedicated servers on OSX included sdl.h even when WITH_SDL was not defined (oops)
bjarni
parents:
2215
diff
changeset
|
44 |
#include <SDL.h> |
7a5d829eca29
(svn r2735) -Fix: [OSX] fixed issue introduced in 2733 where dedicated servers on OSX included sdl.h even when WITH_SDL was not defined (oops)
bjarni
parents:
2215
diff
changeset
|
45 |
#endif |
2188
640a0a5f77ea
(svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
2186
diff
changeset
|
46 |
#endif |
640a0a5f77ea
(svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
2186
diff
changeset
|
47 |
|
0 | 48 |
static char *_fios_path; |
49 |
static char *_fios_save_path; |
|
50 |
static char *_fios_scn_path; |
|
51 |
static FiosItem *_fios_items; |
|
52 |
static int _fios_count, _fios_alloc; |
|
53 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1050
diff
changeset
|
54 |
static FiosItem *FiosAlloc(void) |
0 | 55 |
{ |
56 |
if (_fios_count == _fios_alloc) { |
|
57 |
_fios_alloc += 256; |
|
58 |
_fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem)); |
|
59 |
} |
|
60 |
return &_fios_items[_fios_count++]; |
|
61 |
} |
|
62 |
||
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
63 |
int compare_FiosItems(const void *a, const void *b) |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
64 |
{ |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
65 |
const FiosItem *da = (const FiosItem *)a; |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
66 |
const FiosItem *db = (const FiosItem *)b; |
0 | 67 |
int r; |
68 |
||
69 |
if (_savegame_sort_order < 2) // sort by date |
|
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
70 |
r = da->mtime < db->mtime ? -1 : 1; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
71 |
else |
2099
02c5a49863e9
(svn r2609) - Feature: remove extension from savegames/scenarios when browsing the folders.
Darkvater
parents:
2073
diff
changeset
|
72 |
r = strcasecmp(da->title, db->title); |
0 | 73 |
|
74 |
if (_savegame_sort_order & 1) r = -r; |
|
75 |
return r; |
|
76 |
} |
|
77 |
||
78 |
||
79 |
// Get a list of savegames |
|
80 |
FiosItem *FiosGetSavegameList(int *num, int mode) |
|
81 |
{ |
|
82 |
FiosItem *fios; |
|
83 |
DIR *dir; |
|
84 |
struct dirent *dirent; |
|
85 |
struct stat sb; |
|
86 |
int sort_start; |
|
87 |
char filename[MAX_PATH]; |
|
88 |
||
89 |
if (_fios_save_path == NULL) { |
|
90 |
_fios_save_path = malloc(MAX_PATH); |
|
91 |
strcpy(_fios_save_path, _path.save_dir); |
|
92 |
} |
|
93 |
||
2099
02c5a49863e9
(svn r2609) - Feature: remove extension from savegames/scenarios when browsing the folders.
Darkvater
parents:
2073
diff
changeset
|
94 |
_fios_path = _fios_save_path; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
95 |
|
0 | 96 |
// Parent directory, only if not in root already. |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
97 |
if (_fios_path[1] != '\0') { |
0 | 98 |
fios = FiosAlloc(); |
99 |
fios->type = FIOS_TYPE_PARENT; |
|
100 |
fios->mtime = 0; |
|
1572
cf4613ca991e
(svn r2076) Set the name for the parent directory to ".."
tron
parents:
1509
diff
changeset
|
101 |
strcpy(fios->name, ".."); |
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
102 |
strcpy(fios->title, ".. (Parent directory)"); |
0 | 103 |
} |
104 |
||
105 |
// Show subdirectories first |
|
2334
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
106 |
dir = opendir(_fios_path); |
0 | 107 |
if (dir != NULL) { |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
108 |
while ((dirent = readdir(dir)) != NULL) { |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
109 |
snprintf(filename, lengthof(filename), "%s/%s", |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
110 |
_fios_path, dirent->d_name); |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
111 |
if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) && |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
112 |
dirent->d_name[0] != '.') { |
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
113 |
fios = FiosAlloc(); |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
114 |
fios->type = FIOS_TYPE_DIR; |
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
115 |
fios->mtime = 0; |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
116 |
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
117 |
snprintf(fios->title, lengthof(fios->title), |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
118 |
"%s/ (Directory)", dirent->d_name); |
0 | 119 |
} |
120 |
} |
|
121 |
closedir(dir); |
|
122 |
} |
|
123 |
||
307
69da52d9b50f
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
124 |
{ |
69da52d9b50f
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
125 |
/* XXX ugly global variables ... */ |
69da52d9b50f
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
126 |
byte order = _savegame_sort_order; |
69da52d9b50f
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
127 |
_savegame_sort_order = 2; // sort ascending by name |
69da52d9b50f
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
128 |
qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems); |
69da52d9b50f
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
129 |
_savegame_sort_order = order; |
69da52d9b50f
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
130 |
} |
69da52d9b50f
(svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents:
268
diff
changeset
|
131 |
|
0 | 132 |
// this is where to start sorting |
133 |
sort_start = _fios_count; |
|
134 |
||
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
135 |
/* Show savegame files |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
136 |
* .SAV OpenTTD saved game |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
137 |
* .SS1 Transport Tycoon Deluxe preset game |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
138 |
* .SV1 Transport Tycoon Deluxe (Patch) saved game |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
139 |
* .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game |
0 | 140 |
*/ |
2334
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
141 |
dir = opendir(_fios_path); |
0 | 142 |
if (dir != NULL) { |
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
143 |
while ((dirent = readdir(dir)) != NULL) { |
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
144 |
char *t; |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
145 |
|
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
146 |
snprintf(filename, lengthof(filename), "%s/%s", |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
147 |
_fios_path, dirent->d_name); |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
148 |
if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue; |
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
149 |
|
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
150 |
t = strrchr(dirent->d_name, '.'); |
2338 | 151 |
if (t == NULL) continue; |
152 |
||
153 |
if (strcasecmp(t, ".sav") == 0) { // OpenTTD |
|
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
154 |
fios = FiosAlloc(); |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
155 |
fios->type = FIOS_TYPE_FILE; |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
156 |
fios->mtime = sb.st_mtime; |
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
157 |
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); |
2099
02c5a49863e9
(svn r2609) - Feature: remove extension from savegames/scenarios when browsing the folders.
Darkvater
parents:
2073
diff
changeset
|
158 |
|
02c5a49863e9
(svn r2609) - Feature: remove extension from savegames/scenarios when browsing the folders.
Darkvater
parents:
2073
diff
changeset
|
159 |
*t = '\0'; // strip extension |
02c5a49863e9
(svn r2609) - Feature: remove extension from savegames/scenarios when browsing the folders.
Darkvater
parents:
2073
diff
changeset
|
160 |
ttd_strlcpy(fios->title, dirent->d_name, lengthof(fios->title)); |
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
161 |
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { |
2338 | 162 |
if (strcasecmp(t, ".ss1") == 0 || |
163 |
strcasecmp(t, ".sv1") == 0 || |
|
164 |
strcasecmp(t, ".sv2") == 0) { // TTDLX(Patch) |
|
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
165 |
fios = FiosAlloc(); |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
166 |
fios->type = FIOS_TYPE_OLDFILE; |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
167 |
fios->mtime = sb.st_mtime; |
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
168 |
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); |
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
169 |
GetOldSaveGameName(fios->title, filename); |
0 | 170 |
} |
171 |
} |
|
172 |
} |
|
173 |
closedir(dir); |
|
174 |
} |
|
175 |
||
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
176 |
qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); |
0 | 177 |
*num = _fios_count; |
178 |
return _fios_items; |
|
179 |
} |
|
180 |
||
181 |
// Get a list of scenarios |
|
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
182 |
// FIXME: Gross code duplication with FiosGetSavegameList() |
0 | 183 |
FiosItem *FiosGetScenarioList(int *num, int mode) |
184 |
{ |
|
185 |
FiosItem *fios; |
|
186 |
DIR *dir; |
|
187 |
struct dirent *dirent; |
|
188 |
struct stat sb; |
|
189 |
int sort_start; |
|
190 |
char filename[MAX_PATH]; |
|
191 |
||
192 |
if (_fios_scn_path == NULL) { |
|
193 |
_fios_scn_path = malloc(MAX_PATH); |
|
194 |
strcpy(_fios_scn_path, _path.scenario_dir); |
|
195 |
} |
|
2334
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
196 |
|
0 | 197 |
_fios_path = _fios_scn_path; |
198 |
||
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
199 |
// Parent directory, only if not of the type C:\. |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
200 |
if (_fios_path[1] != '\0' && mode != SLD_NEW_GAME) { |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
201 |
fios = FiosAlloc(); |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
202 |
fios->type = FIOS_TYPE_PARENT; |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
203 |
fios->mtime = 0; |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
204 |
strcpy(fios->title, ".. (Parent directory)"); |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
205 |
} |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
206 |
|
0 | 207 |
// Show subdirectories first |
2334
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
208 |
dir = opendir(_fios_path); |
0 | 209 |
if (dir != NULL) { |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
210 |
while ((dirent = readdir(dir)) != NULL) { |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
211 |
snprintf(filename, lengthof(filename), "%s/%s", |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
212 |
_fios_path, dirent->d_name); |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
213 |
if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) && |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
214 |
dirent->d_name[0] != '.') { |
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
215 |
fios = FiosAlloc(); |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
216 |
fios->type = FIOS_TYPE_DIR; |
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
217 |
fios->mtime = 0; |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
218 |
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); |
2099
02c5a49863e9
(svn r2609) - Feature: remove extension from savegames/scenarios when browsing the folders.
Darkvater
parents:
2073
diff
changeset
|
219 |
snprintf(fios->title, lengthof(fios->title), "%s/ (Directory)", dirent->d_name); |
0 | 220 |
} |
221 |
} |
|
222 |
closedir(dir); |
|
223 |
} |
|
224 |
||
2334
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
225 |
{ |
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
226 |
/* XXX ugly global variables ... */ |
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
227 |
byte order = _savegame_sort_order; |
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
228 |
_savegame_sort_order = 2; // sort ascending by name |
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
229 |
qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems); |
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
230 |
_savegame_sort_order = order; |
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
231 |
} |
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
232 |
|
0 | 233 |
// this is where to start sorting |
234 |
sort_start = _fios_count; |
|
235 |
||
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
236 |
/* Show scenario files |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
237 |
* .SCN OpenTTD style scenario file |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
238 |
* .SV0 Transport Tycoon Deluxe (Patch) scenario |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
239 |
* .SS0 Transport Tycoon Deluxe preset scenario |
0 | 240 |
*/ |
2334
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
241 |
dir = opendir(_fios_path); |
0 | 242 |
if (dir != NULL) { |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
243 |
while ((dirent = readdir(dir)) != NULL) { |
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
244 |
char *t; |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
245 |
|
2099
02c5a49863e9
(svn r2609) - Feature: remove extension from savegames/scenarios when browsing the folders.
Darkvater
parents:
2073
diff
changeset
|
246 |
snprintf(filename, lengthof(filename), "%s/%s", _fios_path, dirent->d_name); |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
247 |
if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue; |
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
248 |
|
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
249 |
t = strrchr(dirent->d_name, '.'); |
2338 | 250 |
if (t == NULL) continue; |
251 |
||
252 |
if (strcasecmp(t, ".scn") == 0) { // OpenTTD |
|
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
253 |
fios = FiosAlloc(); |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
254 |
fios->type = FIOS_TYPE_SCENARIO; |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
255 |
fios->mtime = sb.st_mtime; |
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
256 |
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); |
2099
02c5a49863e9
(svn r2609) - Feature: remove extension from savegames/scenarios when browsing the folders.
Darkvater
parents:
2073
diff
changeset
|
257 |
|
2334
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
258 |
*t = '\0'; // strip extension |
2099
02c5a49863e9
(svn r2609) - Feature: remove extension from savegames/scenarios when browsing the folders.
Darkvater
parents:
2073
diff
changeset
|
259 |
ttd_strlcpy(fios->title, dirent->d_name, lengthof(fios->title)); |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
260 |
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
261 |
mode == SLD_NEW_GAME) { |
2338 | 262 |
if (strcasecmp(t, ".sv0") == 0 || |
263 |
strcasecmp(t, ".ss0") == 0) { // TTDLX(Patch) |
|
1449
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
264 |
fios = FiosAlloc(); |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
265 |
fios->type = FIOS_TYPE_OLD_SCENARIO; |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
266 |
fios->mtime = sb.st_mtime; |
df3ccdd9ee66
(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents:
1390
diff
changeset
|
267 |
GetOldScenarioGameName(fios->title, filename); |
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
268 |
ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); |
0 | 269 |
} |
270 |
} |
|
271 |
} |
|
272 |
closedir(dir); |
|
273 |
} |
|
274 |
||
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
275 |
qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); |
0 | 276 |
*num = _fios_count; |
277 |
return _fios_items; |
|
278 |
} |
|
279 |
||
280 |
||
281 |
// Free the list of savegames |
|
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1050
diff
changeset
|
282 |
void FiosFreeSavegameList(void) |
0 | 283 |
{ |
284 |
free(_fios_items); |
|
285 |
_fios_items = NULL; |
|
286 |
_fios_alloc = _fios_count = 0; |
|
287 |
} |
|
288 |
||
289 |
// Browse to |
|
290 |
char *FiosBrowseTo(const FiosItem *item) |
|
291 |
{ |
|
292 |
char *path = _fios_path; |
|
293 |
char *s; |
|
294 |
||
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
295 |
switch (item->type) { |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
296 |
case FIOS_TYPE_PARENT: |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
297 |
s = strrchr(path, '/'); |
2334
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
298 |
if (s != path) |
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
299 |
s[0] = '\0'; |
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
300 |
else |
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
301 |
s[1] = '\0'; |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
302 |
break; |
0 | 303 |
|
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
304 |
case FIOS_TYPE_DIR: |
2334
8986c80be5be
(svn r2860) Fix some issues in the savegame/scenario list code:
tron
parents:
2323
diff
changeset
|
305 |
if (path[1] != '\0') strcat(path, "/"); |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
306 |
strcat(path, item->name); |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
307 |
break; |
0 | 308 |
|
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
309 |
case FIOS_TYPE_FILE: |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
310 |
case FIOS_TYPE_OLDFILE: |
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
311 |
case FIOS_TYPE_SCENARIO: |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
312 |
case FIOS_TYPE_OLD_SCENARIO: { |
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
313 |
static char str_buffr[512]; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
314 |
|
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
315 |
sprintf(str_buffr, "%s/%s", path, item->name); |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
316 |
return str_buffr; |
1486
647d3a2be4fe
(svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome)
tron
parents:
1482
diff
changeset
|
317 |
} |
0 | 318 |
} |
319 |
||
320 |
return NULL; |
|
321 |
} |
|
322 |
||
1596
483007886b59
(svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents:
1572
diff
changeset
|
323 |
/** |
483007886b59
(svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents:
1572
diff
changeset
|
324 |
* Get descriptive texts. Returns the path and free space |
483007886b59
(svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents:
1572
diff
changeset
|
325 |
* left on the device |
483007886b59
(svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents:
1572
diff
changeset
|
326 |
* @param path string describing the path |
483007886b59
(svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents:
1572
diff
changeset
|
327 |
* @param tfs total free space in megabytes, optional (can be NULL) |
483007886b59
(svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents:
1572
diff
changeset
|
328 |
* @return StringID describing the path (free space or failure) |
483007886b59
(svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents:
1572
diff
changeset
|
329 |
*/ |
483007886b59
(svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents:
1572
diff
changeset
|
330 |
StringID FiosGetDescText(const char **path, uint32 *tot) |
0 | 331 |
{ |
1596
483007886b59
(svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents:
1572
diff
changeset
|
332 |
uint32 free = 0; |
2338 | 333 |
*path = _fios_path; |
0 | 334 |
|
1597
ef6253448620
(svn r2101) statvfs() is availible on POSIX.1 conformant systems.
tron
parents:
1596
diff
changeset
|
335 |
#ifdef HAS_STATVFS |
0 | 336 |
{ |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
337 |
struct statvfs s; |
0 | 338 |
|
1597
ef6253448620
(svn r2101) statvfs() is availible on POSIX.1 conformant systems.
tron
parents:
1596
diff
changeset
|
339 |
if (statvfs(*path, &s) == 0) { |
ef6253448620
(svn r2101) statvfs() is availible on POSIX.1 conformant systems.
tron
parents:
1596
diff
changeset
|
340 |
free = (uint64)s.f_frsize * s.f_bavail >> 20; |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
341 |
} else |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
342 |
return STR_4006_UNABLE_TO_READ_DRIVE; |
0 | 343 |
} |
1596
483007886b59
(svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents:
1572
diff
changeset
|
344 |
#endif |
483007886b59
(svn r2100) - Fix: [1024703]: Infinite access for A:\ (win32). Patch [1171208]. Only requery drive(s) if the user changes a directory, also surpress the OS error box that pops up on some windows machines. Tron + glx (and me)
darkvater
parents:
1572
diff
changeset
|
345 |
if (tot != NULL) *tot = free; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
346 |
return STR_4005_BYTES_FREE; |
0 | 347 |
} |
348 |
||
349 |
void FiosMakeSavegameName(char *buf, const char *name) |
|
350 |
{ |
|
1508
faae7585cae7
(svn r2012) When making a savegame name, don't append the extension, if it is already there
tron
parents:
1495
diff
changeset
|
351 |
const char* extension; |
faae7585cae7
(svn r2012) When making a savegame name, don't append the extension, if it is already there
tron
parents:
1495
diff
changeset
|
352 |
const char* period; |
faae7585cae7
(svn r2012) When making a savegame name, don't append the extension, if it is already there
tron
parents:
1495
diff
changeset
|
353 |
|
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
354 |
if (_game_mode == GM_EDITOR) |
1508
faae7585cae7
(svn r2012) When making a savegame name, don't append the extension, if it is already there
tron
parents:
1495
diff
changeset
|
355 |
extension = ".scn"; |
0 | 356 |
else |
1508
faae7585cae7
(svn r2012) When making a savegame name, don't append the extension, if it is already there
tron
parents:
1495
diff
changeset
|
357 |
extension = ".sav"; |
faae7585cae7
(svn r2012) When making a savegame name, don't append the extension, if it is already there
tron
parents:
1495
diff
changeset
|
358 |
|
faae7585cae7
(svn r2012) When making a savegame name, don't append the extension, if it is already there
tron
parents:
1495
diff
changeset
|
359 |
// Don't append the extension, if it is already there |
faae7585cae7
(svn r2012) When making a savegame name, don't append the extension, if it is already there
tron
parents:
1495
diff
changeset
|
360 |
period = strrchr(name, '.'); |
faae7585cae7
(svn r2012) When making a savegame name, don't append the extension, if it is already there
tron
parents:
1495
diff
changeset
|
361 |
if (period != NULL && strcasecmp(period, extension) == 0) extension = ""; |
faae7585cae7
(svn r2012) When making a savegame name, don't append the extension, if it is already there
tron
parents:
1495
diff
changeset
|
362 |
|
faae7585cae7
(svn r2012) When making a savegame name, don't append the extension, if it is already there
tron
parents:
1495
diff
changeset
|
363 |
sprintf(buf, "%s/%s%s", _fios_path, name, extension); |
0 | 364 |
} |
365 |
||
2255
fd5d5adee9d9
(svn r2775) Deleting a file can fail, display an error message when it happens
tron
parents:
2223
diff
changeset
|
366 |
bool FiosDelete(const char *name) |
0 | 367 |
{ |
1336
69391734ce23
(svn r1840) Repel str_buffr and use local buffers where possible
tron
parents:
1317
diff
changeset
|
368 |
char path[512]; |
69391734ce23
(svn r1840) Repel str_buffr and use local buffers where possible
tron
parents:
1317
diff
changeset
|
369 |
|
1495
a9e4e2b78877
(svn r1999) r1990 broke savegame deletion, fix that [1161729]
tron
parents:
1486
diff
changeset
|
370 |
snprintf(path, lengthof(path), "%s/%s", _fios_path, name); |
2255
fd5d5adee9d9
(svn r2775) Deleting a file can fail, display an error message when it happens
tron
parents:
2223
diff
changeset
|
371 |
return unlink(path) == 0; |
0 | 372 |
} |
373 |
||
374 |
bool FileExists(const char *filename) |
|
375 |
{ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
376 |
return access(filename, 0) == 0; |
0 | 377 |
} |
378 |
||
379 |
static int LanguageCompareFunc(const void *a, const void *b) |
|
380 |
{ |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
206
diff
changeset
|
381 |
return strcmp(*(const char* const *)a, *(const char* const *)b); |
0 | 382 |
} |
383 |
||
384 |
int GetLanguageList(char **languages, int max) |
|
385 |
{ |
|
386 |
DIR *dir; |
|
387 |
struct dirent *dirent; |
|
388 |
int num = 0; |
|
389 |
||
390 |
dir = opendir(_path.lang_dir); |
|
391 |
if (dir != NULL) { |
|
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
392 |
while ((dirent = readdir(dir)) != NULL) { |
0 | 393 |
char *t = strrchr(dirent->d_name, '.'); |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
394 |
|
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
395 |
if (t != NULL && strcmp(t, ".lng") == 0) { |
0 | 396 |
languages[num++] = strdup(dirent->d_name); |
397 |
if (num == max) break; |
|
398 |
} |
|
399 |
} |
|
400 |
closedir(dir); |
|
401 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
402 |
|
0 | 403 |
qsort(languages, num, sizeof(char*), LanguageCompareFunc); |
404 |
return num; |
|
405 |
} |
|
406 |
||
1050
958032fed209
(svn r1551) Silence a warning and simplify a preprocessor check
tron
parents:
1010
diff
changeset
|
407 |
#if defined(__BEOS__) || defined(__linux__) |
410 | 408 |
static void ChangeWorkingDirectory(char *exe) |
0 | 409 |
{ |
410 |
char *s = strrchr(exe, '/'); |
|
411 |
if (s != NULL) { |
|
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
412 |
*s = '\0'; |
0 | 413 |
chdir(exe); |
414 |
*s = '/'; |
|
415 |
} |
|
416 |
} |
|
1050
958032fed209
(svn r1551) Silence a warning and simplify a preprocessor check
tron
parents:
1010
diff
changeset
|
417 |
#endif |
0 | 418 |
|
419 |
void ShowInfo(const char *str) |
|
420 |
{ |
|
421 |
puts(str); |
|
422 |
} |
|
423 |
||
424 |
void ShowOSErrorBox(const char *buf) |
|
425 |
{ |
|
529
39d3b5a074c4
(svn r869) Mac: finally made asserts open the console, so people should be able to see what went wrong even if they opened the game in the GUI
bjarni
parents:
507
diff
changeset
|
426 |
#if defined(__APPLE__) |
2188
640a0a5f77ea
(svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
2186
diff
changeset
|
427 |
// this creates an NSAlertPanel with the contents of 'buf' |
640a0a5f77ea
(svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
2186
diff
changeset
|
428 |
// this is the native and nicest way to do this on OSX |
640a0a5f77ea
(svn r2703) - Feature: [OSX] Added a native alert window to show whatever error() needs to print (Tobin made this, while I fixed some issued in it)
bjarni
parents:
2186
diff
changeset
|
429 |
ShowMacDialog( buf, "See readme for more info\nMost likely you are missing files from the original TTD", "Quit" ); |
529
39d3b5a074c4
(svn r869) Mac: finally made asserts open the console, so people should be able to see what went wrong even if they opened the game in the GUI
bjarni
parents:
507
diff
changeset
|
430 |
#else |
39d3b5a074c4
(svn r869) Mac: finally made asserts open the console, so people should be able to see what went wrong even if they opened the game in the GUI
bjarni
parents:
507
diff
changeset
|
431 |
// all systems, but OSX |
0 | 432 |
fprintf(stderr, "\033[1;31mError: %s\033[0;39m\n", buf); |
433 |
#endif |
|
434 |
} |
|
435 |
||
436 |
int CDECL main(int argc, char* argv[]) |
|
437 |
{ |
|
438 |
// change the working directory to enable doubleclicking in UIs |
|
1050
958032fed209
(svn r1551) Silence a warning and simplify a preprocessor check
tron
parents:
1010
diff
changeset
|
439 |
#if defined(__BEOS__) || defined(__linux__) |
0 | 440 |
ChangeWorkingDirectory(argv[0]); |
441 |
#endif |
|
442 |
||
206 | 443 |
_random_seeds[0][1] = _random_seeds[0][0] = time(NULL); |
2073 | 444 |
SeedMT(_random_seeds[0][1]); |
0 | 445 |
|
1509
795ba683e4f0
(svn r2013) Always ignore SIGPIPE, not only when SDL is used (ShadowJK)
tron
parents:
1508
diff
changeset
|
446 |
signal(SIGPIPE, SIG_IGN); |
0 | 447 |
|
448 |
return ttd_main(argc, argv); |
|
449 |
} |
|
450 |
||
1093
4fdc46eaf423
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1050
diff
changeset
|
451 |
void DeterminePaths(void) |
0 | 452 |
{ |
453 |
char *s; |
|
454 |
||
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
455 |
_path.game_data_dir = malloc(MAX_PATH); |
561
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
456 |
ttd_strlcpy(_path.game_data_dir, GAME_DATA_DIR, MAX_PATH); |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
457 |
#if defined SECOND_DATA_DIR |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
458 |
_path.second_data_dir = malloc(MAX_PATH); |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
459 |
ttd_strlcpy(_path.second_data_dir, SECOND_DATA_DIR, MAX_PATH); |
561
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
460 |
#endif |
0 | 461 |
|
462 |
#if defined(USE_HOMEDIR) |
|
463 |
{ |
|
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
464 |
const char *homedir = getenv("HOME"); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
465 |
|
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
466 |
if (homedir == NULL) { |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
467 |
const struct passwd *pw = getpwuid(getuid()); |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
468 |
if (pw != NULL) homedir = pw->pw_dir; |
0 | 469 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
470 |
|
0 | 471 |
_path.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR); |
472 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
473 |
|
0 | 474 |
#else /* not defined(USE_HOMEDIR) */ |
475 |
||
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
476 |
_path.personal_dir = malloc(MAX_PATH); |
561
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
477 |
ttd_strlcpy(_path.personal_dir, PERSONAL_DIR, MAX_PATH); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
478 |
|
0 | 479 |
// check if absolute or relative path |
480 |
s = strchr(_path.personal_dir, '/'); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
481 |
|
0 | 482 |
// add absolute path |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
483 |
if (s == NULL || _path.personal_dir != s) { |
0 | 484 |
getcwd(_path.personal_dir, MAX_PATH); |
485 |
s = strchr(_path.personal_dir, 0); |
|
486 |
*s++ = '/'; |
|
561
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
487 |
ttd_strlcpy(s, PERSONAL_DIR, MAX_PATH); |
0 | 488 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
489 |
|
0 | 490 |
#endif /* defined(USE_HOMEDIR) */ |
491 |
||
492 |
s = strchr(_path.personal_dir, 0); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
493 |
|
0 | 494 |
// append a / ? |
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
495 |
if (s[-1] != '/') strcpy(s, "/"); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
496 |
|
0 | 497 |
_path.save_dir = str_fmt("%ssave", _path.personal_dir); |
498 |
_path.autosave_dir = str_fmt("%s/autosave", _path.save_dir); |
|
499 |
_path.scenario_dir = str_fmt("%sscenario", _path.personal_dir); |
|
500 |
_path.gm_dir = str_fmt("%sgm/", _path.game_data_dir); |
|
501 |
_path.data_dir = str_fmt("%sdata/", _path.game_data_dir); |
|
1482
80e315adc3bd
(svn r1986) - Fix: free _config_file when shutting down openttd. Strange that Valgrind didn't catch this, kudos to TrueLight
Darkvater
parents:
1470
diff
changeset
|
502 |
|
80e315adc3bd
(svn r1986) - Fix: free _config_file when shutting down openttd. Strange that Valgrind didn't catch this, kudos to TrueLight
Darkvater
parents:
1470
diff
changeset
|
503 |
if (_config_file == NULL) |
80e315adc3bd
(svn r1986) - Fix: free _config_file when shutting down openttd. Strange that Valgrind didn't catch this, kudos to TrueLight
Darkvater
parents:
1470
diff
changeset
|
504 |
_config_file = str_fmt("%sopenttd.cfg", _path.personal_dir); |
80e315adc3bd
(svn r1986) - Fix: free _config_file when shutting down openttd. Strange that Valgrind didn't catch this, kudos to TrueLight
Darkvater
parents:
1470
diff
changeset
|
505 |
|
983
4765bf636f6b
(svn r1479) -Added highscore chart (accessible from the difficulty window) with top5 companies for a given difficulty (select the difficulty in the menu)
darkvater
parents:
915
diff
changeset
|
506 |
_highscore_file = str_fmt("%shs.dat", _path.personal_dir); |
704
e843dd369938
(svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents:
569
diff
changeset
|
507 |
_log_file = str_fmt("%sopenttd.log", _path.personal_dir); |
915 | 508 |
|
561
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
509 |
#if defined CUSTOM_LANG_DIR |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
510 |
// sets the search path for lng files to the custom one |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
511 |
_path.lang_dir = malloc( MAX_PATH ); |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
512 |
ttd_strlcpy( _path.lang_dir, CUSTOM_LANG_DIR, MAX_PATH); |
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
513 |
#else |
0 | 514 |
_path.lang_dir = str_fmt("%slang/", _path.game_data_dir); |
561
9a0c469a251c
(svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents:
543
diff
changeset
|
515 |
#endif |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
516 |
|
238
a73a3e50c7f9
(svn r239) Fix: [ 1025361 ] PERSONAL_DIR is created at startup now
dominik
parents:
223
diff
changeset
|
517 |
// create necessary folders |
a73a3e50c7f9
(svn r239) Fix: [ 1025361 ] PERSONAL_DIR is created at startup now
dominik
parents:
223
diff
changeset
|
518 |
mkdir(_path.personal_dir, 0755); |
0 | 519 |
mkdir(_path.save_dir, 0755); |
520 |
mkdir(_path.autosave_dir, 0755); |
|
521 |
mkdir(_path.scenario_dir, 0755); |
|
522 |
} |
|
523 |
||
1470
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
524 |
bool InsertTextBufferClipboard(Textbuf *tb) |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
525 |
{ |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
526 |
return false; |
a8fd3e037531
(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents:
1449
diff
changeset
|
527 |
} |
1885
aef601001f5e
(svn r2391) - Feature: saving games happen in a seperate thread so you no longer will have to wait such a long time (especially handy on bigger maps and multiplayer games). The mouse also changes into the 'ZZZ' state :P. The thread on windows is currently given a little-bit-less-than-normal priority so it should not interfere that much with the gameplay; it will take a bit longer though. Upon the exit of the game any pending saves are waited upon.
Darkvater
parents:
1850
diff
changeset
|
528 |
|
2073 | 529 |
|
530 |
#ifdef ENABLE_NETWORK |
|
531 |
||
532 |
// multi os compatible sleep function |
|
533 |
||
534 |
#ifdef __AMIGA__ |
|
535 |
// usleep() implementation |
|
536 |
# include <devices/timer.h> |
|
537 |
# include <dos/dos.h> |
|
538 |
||
539 |
extern struct Device *TimerBase = NULL; |
|
540 |
extern struct MsgPort *TimerPort = NULL; |
|
541 |
extern struct timerequest *TimerRequest = NULL; |
|
542 |
#endif // __AMIGA__ |
|
543 |
||
544 |
void CSleep(int milliseconds) |
|
545 |
{ |
|
546 |
#if !defined(__BEOS__) && !defined(__AMIGA__) |
|
547 |
usleep(milliseconds * 1000); |
|
548 |
#endif |
|
549 |
#ifdef __BEOS__ |
|
550 |
snooze(milliseconds * 1000); |
|
551 |
#endif |
|
552 |
#if defined(__AMIGA__) |
|
553 |
{ |
|
554 |
ULONG signals; |
|
555 |
ULONG TimerSigBit = 1 << TimerPort->mp_SigBit; |
|
556 |
||
557 |
// send IORequest |
|
558 |
TimerRequest->tr_node.io_Command = TR_ADDREQUEST; |
|
559 |
TimerRequest->tr_time.tv_secs = (milliseconds * 1000) / 1000000; |
|
560 |
TimerRequest->tr_time.tv_micro = (milliseconds * 1000) % 1000000; |
|
561 |
SendIO((struct IORequest *)TimerRequest); |
|
562 |
||
563 |
if (!((signals = Wait(TimerSigBit | SIGBREAKF_CTRL_C)) & TimerSigBit) ) { |
|
564 |
AbortIO((struct IORequest *)TimerRequest); |
|
565 |
} |
|
566 |
WaitIO((struct IORequest *)TimerRequest); |
|
567 |
} |
|
568 |
#endif // __AMIGA__ |
|
569 |
} |
|
570 |
||
571 |
#endif /* ENABLE_NETWORK */ |
|
572 |