unix.c
author Darkvater
Thu, 10 Mar 2005 21:44:17 +0000
changeset 1482 46a8146adecf
parent 1470 25c332b43280
child 1486 6a31e3d7dfe3
permissions -rw-r--r--
(svn r1986) - Fix: free _config_file when shutting down openttd. Strange that Valgrind didn't catch this, kudos to TrueLight
- Feature (small): new command-line option -c <config_file>. You can have OpenTTD load alternative config files with this setup, handy for servers, or for saves from other players if you don't want to rename your own config. This will have to do until all game-related settings are saved ingame. Couldn't find the SF patch, so credits to the guy that wrote it.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     1
#include "stdafx.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     2
#include "ttd.h"
1390
53a5713cf3f9 (svn r1894) - Codechange: cleaned up the console a bit, wholly unified handling of text with that of editboxes
Darkvater
parents: 1346
diff changeset
     3
#include "window.h"
1317
f382f1b439c7 (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
     4
#include "string.h"
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 437
diff changeset
     5
#include "table/strings.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     6
#include "hal.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     7
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     8
#include <dirent.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     9
#include <unistd.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include <sys/stat.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include <time.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include <pwd.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#if defined(__linux__)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include <sys/statvfs.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
#if defined(WITH_SDL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
#include <SDL.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
#ifdef __MORPHOS__
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
#include <exec/types.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
ULONG __stack = (1024*1024)*2; // maybe not that much is needed actually ;)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
#endif /* __MORPHOS__ */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    27
770
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 704
diff changeset
    28
#ifdef __AMIGA__
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 704
diff changeset
    29
#warning add stack symbol to avoid that user needs to set stack manually (tokai)
915
013cb2d74800 (svn r1402) Trim trailing whitespace
tron
parents: 826
diff changeset
    30
// ULONG __stack =
770
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 704
diff changeset
    31
#endif
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 704
diff changeset
    32
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
static char *_fios_path;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
static char *_fios_save_path;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
static char *_fios_scn_path;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
static FiosItem *_fios_items;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
static int _fios_count, _fios_alloc;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1050
diff changeset
    39
static FiosItem *FiosAlloc(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
	if (_fios_count == _fios_alloc) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
		_fios_alloc += 256;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
		_fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
	return &_fios_items[_fios_count++];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
int compare_FiosItems (const void *a, const void *b) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
	const FiosItem *da = (const FiosItem *) a;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
	const FiosItem *db = (const FiosItem *) b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
	int r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
	if (_savegame_sort_order < 2) // sort by date
164
0cbdf3c9bde1 (svn r165) -Feature: Option to sort vehicles in vehicle-list window by different criteria. Total independent sort for all types and players. Periodic resort of list every 10 TTD days. Thank you for your graphical inspiration follow and buxo (since none of you provided any code).
darkvater
parents: 0
diff changeset
    54
    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
    55
	else
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
    56
		r = strcasecmp(
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
    57
			da->title[0] != '\0' ? da->title : da->name,
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
    58
			db->title[0] != '\0' ? db->title : db->name
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
    59
		);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
	if (_savegame_sort_order & 1) r = -r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
	return r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    66
// Get a list of savegames
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
FiosItem *FiosGetSavegameList(int *num, int mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
	FiosItem *fios;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
	DIR *dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
	struct dirent *dirent;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
	struct stat sb;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
	int sort_start;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
	char filename[MAX_PATH];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
	if (_fios_save_path == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
		_fios_save_path = malloc(MAX_PATH);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
		strcpy(_fios_save_path, _path.save_dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
    81
	if (_game_mode == GM_EDITOR)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
		_fios_path = _fios_scn_path;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
	else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
		_fios_path = _fios_save_path;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
    85
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
	// Parent directory, only if not in root already.
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
    87
	if (_fios_path[1] != '\0') {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
		fios = FiosAlloc();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
		fios->type = FIOS_TYPE_PARENT;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
		fios->mtime = 0;
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
    91
		strcpy(fios->title, ".. (Parent directory)");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    93
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
	// Show subdirectories first
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    95
	dir = opendir(_fios_path[0] ? _fios_path : "/");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
	if (dir != NULL) {
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
    97
		while ((dirent = readdir(dir)) != NULL) {
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
    98
			snprintf(filename, lengthof(filename), "%s/%s",
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
    99
				_fios_path, dirent->d_name);
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   100
			if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   101
					dirent->d_name[0] != '.') {
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   102
				fios = FiosAlloc();
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   103
				fios->mtime = 0;
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   104
				fios->type = FIOS_TYPE_DIR;
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   105
				fios->title[0] = '\0';
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   106
				sprintf(fios->name, "%s/ (Directory)", dirent->d_name);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
		closedir(dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
307
d251be6c0d54 (svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents: 268
diff changeset
   112
	{
d251be6c0d54 (svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents: 268
diff changeset
   113
		/* XXX ugly global variables ... */
d251be6c0d54 (svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents: 268
diff changeset
   114
		byte order = _savegame_sort_order;
d251be6c0d54 (svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents: 268
diff changeset
   115
		_savegame_sort_order = 2; // sort ascending by name
d251be6c0d54 (svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents: 268
diff changeset
   116
		qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems);
d251be6c0d54 (svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents: 268
diff changeset
   117
		_savegame_sort_order = order;
d251be6c0d54 (svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents: 268
diff changeset
   118
	}
d251be6c0d54 (svn r313) -Feature/Fix: directories in *nix are now sorted alphabetically in ascending order (Tron).
darkvater
parents: 268
diff changeset
   119
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
	// this is where to start sorting
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
	sort_start = _fios_count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
	/*	Show savegame files
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
	 *	.SAV	OpenTTD saved game
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
	 *	.SS1	Transport Tycoon Deluxe preset game
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
	 *	.SV1	Transport Tycoon Deluxe (Patch) saved game
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
	 *	.SV2	Transport Tycoon Deluxe (Patch) saved 2-player game
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
	 */
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   129
	dir = opendir(_fios_path[0] != '\0' ? _fios_path : "/");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
	if (dir != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
		while ((dirent = readdir(dir))) {
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   132
			char *t;
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   133
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   134
			snprintf(filename, lengthof(filename), "%s/%s",
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   135
				_fios_path, dirent->d_name);
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   136
			if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue;
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   137
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   138
			t = strrchr(dirent->d_name, '.');
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   139
			if (t != NULL && strcasecmp(t, ".sav") == 0) { // OpenTTD
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   140
				*t = '\0'; // cut extension
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   141
				fios = FiosAlloc();
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   142
				fios->type = FIOS_TYPE_FILE;
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   143
				fios->mtime = sb.st_mtime;
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   144
				fios->title[0] = 0;
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   145
				ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name));
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   146
			} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   147
				int ext = 0; // start of savegame extensions in _old_extensions[]
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   148
				if (t != NULL && (
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   149
							(ext++, strcasecmp(t, ".ss1") == 0) ||
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   150
							(ext++, strcasecmp(t, ".sv1") == 0) ||
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   151
							(ext++, strcasecmp(t, ".sv2") == 0)
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   152
						)) { // TTDLX(Patch)
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   153
					*t = '\0'; // cut extension
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   154
					fios = FiosAlloc();
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   155
					fios->old_extension = ext - 1;
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   156
					fios->type = FIOS_TYPE_OLDFILE;
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   157
					fios->mtime = sb.st_mtime;
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   158
					ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name));
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   159
					GetOldSaveGameName(fios->title, filename);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
		closedir(dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
	*num = _fios_count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
	qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
	return _fios_items;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
// Get a list of scenarios
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   172
// FIXME: Gross code duplication with FiosGetSavegameList()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
FiosItem *FiosGetScenarioList(int *num, int mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   175
	FiosItem *fios;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
	DIR *dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
	struct dirent *dirent;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
	struct stat sb;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
	int sort_start;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
	char filename[MAX_PATH];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
	if (_fios_scn_path == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
		_fios_scn_path = malloc(MAX_PATH);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   184
		strcpy(_fios_scn_path, _path.scenario_dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
	_fios_path = _fios_scn_path;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   188
	// Show subdirectories first
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
	dir = opendir(_fios_path[0] ? _fios_path : "/");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   190
	if (dir != NULL) {
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   191
		while ((dirent = readdir(dir)) != NULL) {
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   192
			snprintf(filename, lengthof(filename), "%s/%s",
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   193
				_fios_path, dirent->d_name);
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   194
			if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   195
					dirent->d_name[0] != '.') {
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   196
				fios = FiosAlloc();
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   197
				fios->mtime = '\0';
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   198
				fios->type = FIOS_TYPE_DIR;
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   199
				fios->title[0] = '\0';
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   200
				sprintf(fios->name, "%s/ (Directory)", dirent->d_name);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
		closedir(dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
	// this is where to start sorting
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
	sort_start = _fios_count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   209
	/*	Show scenario files
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
	 *	.SCN	OpenTTD style scenario file
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
	 *	.SV0	Transport Tycoon Deluxe (Patch) scenario
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   212
	 *	.SS0	Transport Tycoon Deluxe preset scenario
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
	 */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
	dir = opendir(_fios_path[0] ? _fios_path : "/");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
	if (dir != NULL) {
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   216
		while ((dirent = readdir(dir)) != NULL) {
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   217
			char *t;
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   218
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   219
			snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   220
			if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue;
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   221
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   222
			t = strrchr(dirent->d_name, '.');
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   223
			if (t != NULL && strcasecmp(t, ".scn") != 0) { // OpenTTD
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   224
				*t = '\0'; // cut extension
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   225
				fios = FiosAlloc();
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   226
				fios->type = FIOS_TYPE_SCENARIO;
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   227
				fios->mtime = sb.st_mtime;
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   228
				fios->title[0] = '\0';
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   229
				ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3);
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   230
			} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO ||
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   231
					mode == SLD_NEW_GAME) {
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   232
				int ext = 3; // start of scenario extensions in _old_extensions[]
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   233
				if (t != NULL && (
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   234
							(ext++, strcasecmp(t, ".sv0") == 0) ||
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   235
							(ext++, strcasecmp(t, ".ss0") == 0)
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   236
						)) {// TTDLX(Patch)
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   237
					*t = '\0'; // cut extension
1449
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   238
					fios = FiosAlloc();
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   239
					fios->old_extension = ext-1;
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   240
					fios->type = FIOS_TYPE_OLD_SCENARIO;
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   241
					fios->mtime = sb.st_mtime;
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   242
					GetOldScenarioGameName(fios->title, filename);
71331c8f3f90 (svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.
pasky
parents: 1390
diff changeset
   243
					ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   244
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
		closedir(dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
	*num = _fios_count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   251
	qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
	return _fios_items;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   254
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
// Free the list of savegames
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1050
diff changeset
   257
void FiosFreeSavegameList(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   259
	free(_fios_items);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
	_fios_items = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
	_fios_alloc = _fios_count = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   263
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
// Browse to
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   265
char *FiosBrowseTo(const FiosItem *item)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   266
{
1346
6d940a8a8103 (svn r1850) Change the last consumers of str_buffr so they use local static buffers now and remove the global array str_buffr.
tron
parents: 1336
diff changeset
   267
	static char str_buffr[512];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   268
	char *path = _fios_path;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
	char *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   271
	switch (item->type) {
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   272
		case FIOS_TYPE_PARENT:
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   273
			s = strrchr(path, '/');
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   274
			if (s != NULL) *s = '\0';
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   275
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   277
		case FIOS_TYPE_DIR:
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   278
			s = strchr(item->name, '/');
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   279
			if (s != NULL) *s = '\0';
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   280
			strcat(path, "/");
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   281
			strcat(path, item->name);
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   282
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   284
		case FIOS_TYPE_FILE:
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   285
			FiosMakeSavegameName(str_buffr, item->name);
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   286
			return str_buffr;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   287
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   288
		case FIOS_TYPE_OLDFILE:
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   289
			sprintf(str_buffr, "%s/%s.%s",
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   290
				_fios_path, item->name, _old_extensions[item->old_extension]);
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   291
			return str_buffr;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   292
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   293
		case FIOS_TYPE_SCENARIO:
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   294
			sprintf(str_buffr, "%s/%s.scn", path, item->name);
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   295
			return str_buffr;
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   296
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   297
		case FIOS_TYPE_OLD_SCENARIO:
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   298
			sprintf(str_buffr, "%s/%s.%s",
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   299
				path, item->name, _old_extensions[item->old_extension]);
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   300
			return str_buffr;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   303
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
// Get descriptive texts.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
// Returns a path as well as a
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   308
//  string describing the path.
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
   309
StringID FiosGetDescText(const char **path)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
{
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   311
	*path = _fios_path[0] != '\0' ? _fios_path : "/";
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   313
#if defined(__linux__)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
	{
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   315
		struct statvfs s;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   317
		if (statvfs(*path, &s) == 0) {
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   318
			uint64 tot = (uint64)s.f_bsize * s.f_bavail;
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   319
			SetDParam(0, (uint32)(tot >> 20));
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   320
			return STR_4005_BYTES_FREE;
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   321
		} else
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   322
			return STR_4006_UNABLE_TO_READ_DRIVE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   324
#else
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 529
diff changeset
   325
	SetDParam(0, 0);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   326
	return STR_4005_BYTES_FREE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   327
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   328
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   330
void FiosMakeSavegameName(char *buf, const char *name)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
{
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   332
	if (_game_mode == GM_EDITOR)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
		sprintf(buf, "%s/%s.scn", _fios_path, name);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   334
	else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   335
		sprintf(buf, "%s/%s.sav", _fios_path, name);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   336
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   337
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   338
void FiosDelete(const char *name)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
{
1336
c9e6b766bf21 (svn r1840) Repel str_buffr and use local buffers where possible
tron
parents: 1317
diff changeset
   340
	char path[512];
c9e6b766bf21 (svn r1840) Repel str_buffr and use local buffers where possible
tron
parents: 1317
diff changeset
   341
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   342
	FiosMakeSavegameName(path, name);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   343
	unlink(path);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
const DriverDesc _video_driver_descs[] = {
179
003096efeb9d (svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents: 164
diff changeset
   347
	{"null",	"Null Video Driver",	&_null_video_driver,	0},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
#if defined(WITH_SDL)
179
003096efeb9d (svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents: 164
diff changeset
   349
	{ "sdl",	"SDL Video Driver",		&_sdl_video_driver,		1},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
#endif
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   351
	{ "dedicated", "Dedicated Video Driver", &_dedicated_video_driver, 0},
179
003096efeb9d (svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents: 164
diff changeset
   352
	{ NULL,		NULL,									NULL,									0}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
const DriverDesc _sound_driver_descs[] = {
179
003096efeb9d (svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents: 164
diff changeset
   356
	{"null",	"Null Sound Driver",	&_null_sound_driver,		0},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
#if defined(WITH_SDL)
179
003096efeb9d (svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents: 164
diff changeset
   358
	{ "sdl",	"SDL Sound Driver",		&_sdl_sound_driver,			1},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   359
#endif
179
003096efeb9d (svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents: 164
diff changeset
   360
	{ NULL,		NULL,									NULL,										0}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   361
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   362
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   363
#if defined(__APPLE__)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   364
#define EXTMIDI_PRI 2
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   365
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
#define EXTMIDI_PRI 0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   367
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
const DriverDesc _music_driver_descs[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   370
#ifndef __BEOS__
569
2e86ae517874 (svn r980) Fixed issues where MorphOS would get problems if AmigaOS would get a port, since MorphOS also have the flag __AMIGA__ defined (Tokai)
bjarni
parents: 561
diff changeset
   371
#if !defined(__MORPHOS__) && !defined(__AMIGA__)
2e86ae517874 (svn r980) Fixed issues where MorphOS would get problems if AmigaOS would get a port, since MorphOS also have the flag __AMIGA__ defined (Tokai)
bjarni
parents: 561
diff changeset
   372
// MorphOS and AmigaOS have no music support
179
003096efeb9d (svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents: 164
diff changeset
   373
	{"extmidi",	"External MIDI Driver",	&_extmidi_music_driver,	EXTMIDI_PRI},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
#ifdef __BEOS__
179
003096efeb9d (svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents: 164
diff changeset
   377
	{ "bemidi",	"BeOS MIDI Driver",			&_bemidi_music_driver,	1},
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   378
#endif
179
003096efeb9d (svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents: 164
diff changeset
   379
	{   "null",	"Null Music Driver",		&_null_music_driver,		1},
003096efeb9d (svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents: 164
diff changeset
   380
	{     NULL,	NULL,										NULL,										0}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
223
0e5cc5a65df6 (svn r224) -Fix: Music now finally works on WinXP. DirectMusic is now default for an OS >= WinNT4 (WinNT4, Win2k, WinXP), and MIDI driver for lower OS's (Win95, Win98, WinME, etc).
darkvater
parents: 222
diff changeset
   383
/* GetOSVersion returns the minimal required version of OS to be able to use that driver.
0e5cc5a65df6 (svn r224) -Fix: Music now finally works on WinXP. DirectMusic is now default for an OS >= WinNT4 (WinNT4, Win2k, WinXP), and MIDI driver for lower OS's (Win95, Win98, WinME, etc).
darkvater
parents: 222
diff changeset
   384
	 Not needed for *nix. */
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1050
diff changeset
   385
byte GetOSVersion(void)
223
0e5cc5a65df6 (svn r224) -Fix: Music now finally works on WinXP. DirectMusic is now default for an OS >= WinNT4 (WinNT4, Win2k, WinXP), and MIDI driver for lower OS's (Win95, Win98, WinME, etc).
darkvater
parents: 222
diff changeset
   386
{
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 770
diff changeset
   387
	return 2;  // any arbitrary number bigger than 0
268
00fabd0160bc (svn r274) Mac: restored extmusic to be default, which were accidently turned off by a windows music fix
bjarni
parents: 238
diff changeset
   388
				// numbers lower than 2 breaks default music selection on mac
223
0e5cc5a65df6 (svn r224) -Fix: Music now finally works on WinXP. DirectMusic is now default for an OS >= WinNT4 (WinNT4, Win2k, WinXP), and MIDI driver for lower OS's (Win95, Win98, WinME, etc).
darkvater
parents: 222
diff changeset
   389
}
0e5cc5a65df6 (svn r224) -Fix: Music now finally works on WinXP. DirectMusic is now default for an OS >= WinNT4 (WinNT4, Win2k, WinXP), and MIDI driver for lower OS's (Win95, Win98, WinME, etc).
darkvater
parents: 222
diff changeset
   390
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   391
bool FileExists(const char *filename)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
{
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   393
	return access(filename, 0) == 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   396
static int LanguageCompareFunc(const void *a, const void *b)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   397
{
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
   398
	return strcmp(*(const char* const *)a, *(const char* const *)b);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
int GetLanguageList(char **languages, int max)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
	DIR *dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
	struct dirent *dirent;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   405
	int num = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
	dir = opendir(_path.lang_dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
	if (dir != NULL) {
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   409
		while ((dirent = readdir(dir)) != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
			char *t = strrchr(dirent->d_name, '.');
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   411
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   412
			if (t != NULL && strcmp(t, ".lng") == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
				languages[num++] = strdup(dirent->d_name);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
				if (num == max) break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   416
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
		closedir(dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   419
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   420
	qsort(languages, num, sizeof(char*), LanguageCompareFunc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
	return num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   423
1050
e76fa5c5d835 (svn r1551) Silence a warning and simplify a preprocessor check
tron
parents: 1010
diff changeset
   424
#if defined(__BEOS__) || defined(__linux__)
410
8de2aaf20800 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 307
diff changeset
   425
static void ChangeWorkingDirectory(char *exe)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
	char *s = strrchr(exe, '/');
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
	if (s != NULL) {
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   429
		*s = '\0';
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
		chdir(exe);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
		*s = '/';
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
}
1050
e76fa5c5d835 (svn r1551) Silence a warning and simplify a preprocessor check
tron
parents: 1010
diff changeset
   434
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
void ShowInfo(const char *str)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
	puts(str);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   441
void ShowOSErrorBox(const char *buf)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   442
{
529
3b23d5073d32 (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
   443
#if defined(__APPLE__)
3b23d5073d32 (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
   444
	// this creates an error in the console and then opens the console.
3b23d5073d32 (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
   445
	// Colourcodes are not used in the console, so they are skipped here
3b23d5073d32 (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
   446
	fprintf(stderr, "Error: %s", buf);
561
e3b9689745ab (svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents: 543
diff changeset
   447
	system("/Applications/Utilities/Console.app/Contents/MacOS/Console &");
529
3b23d5073d32 (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
   448
#else
3b23d5073d32 (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
   449
	// all systems, but OSX
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
	fprintf(stderr, "\033[1;31mError: %s\033[0;39m\n", buf);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
int CDECL main(int argc, char* argv[])
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   456
	// change the working directory to enable doubleclicking in UIs
1050
e76fa5c5d835 (svn r1551) Silence a warning and simplify a preprocessor check
tron
parents: 1010
diff changeset
   457
#if defined(__BEOS__) || defined(__linux__)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
	ChangeWorkingDirectory(argv[0]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   460
206
7f8c26d8526b (svn r207) -Codechange: randomizer handling
signde
parents: 193
diff changeset
   461
	_random_seeds[0][1] = _random_seeds[0][0] = time(NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
	return ttd_main(argc, argv);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   465
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   466
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1050
diff changeset
   467
void DeterminePaths(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   468
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   469
	char *s;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   470
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   471
	_path.game_data_dir = malloc(MAX_PATH);
561
e3b9689745ab (svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents: 543
diff changeset
   472
	ttd_strlcpy(_path.game_data_dir, GAME_DATA_DIR, MAX_PATH);
e3b9689745ab (svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents: 543
diff changeset
   473
	#if defined SECOND_DATA_DIR
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   474
	_path.second_data_dir = malloc(MAX_PATH);
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   475
	ttd_strlcpy(_path.second_data_dir, SECOND_DATA_DIR, MAX_PATH);
561
e3b9689745ab (svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents: 543
diff changeset
   476
	#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   477
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   478
#if defined(USE_HOMEDIR)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   479
	{
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   480
		const char *homedir = getenv("HOME");
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   481
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   482
		if (homedir == NULL) {
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   483
			const struct passwd *pw = getpwuid(getuid());
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   484
			if (pw != NULL) homedir = pw->pw_dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   486
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   487
		_path.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   488
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   489
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
#else /* not defined(USE_HOMEDIR) */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   492
	_path.personal_dir = malloc(MAX_PATH);
561
e3b9689745ab (svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents: 543
diff changeset
   493
	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
   494
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
	// check if absolute or relative path
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   496
	s = strchr(_path.personal_dir, '/');
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   497
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
	// add absolute path
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   499
	if (s == NULL || _path.personal_dir != s) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
		getcwd(_path.personal_dir, MAX_PATH);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
		s = strchr(_path.personal_dir, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   502
		*s++ = '/';
561
e3b9689745ab (svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents: 543
diff changeset
   503
		ttd_strlcpy(s, PERSONAL_DIR, MAX_PATH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   504
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   505
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
#endif /* defined(USE_HOMEDIR) */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
	s = strchr(_path.personal_dir, 0);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   509
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   510
	// append a / ?
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   511
	if (s[-1] != '/') strcpy(s, "/");
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   512
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
	_path.save_dir = str_fmt("%ssave", _path.personal_dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
	_path.autosave_dir = str_fmt("%s/autosave", _path.save_dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   515
	_path.scenario_dir = str_fmt("%sscenario", _path.personal_dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   516
	_path.gm_dir = str_fmt("%sgm/", _path.game_data_dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   517
	_path.data_dir = str_fmt("%sdata/", _path.game_data_dir);
1482
46a8146adecf (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
   518
46a8146adecf (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
   519
	if (_config_file == NULL)
46a8146adecf (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
   520
		_config_file = str_fmt("%sopenttd.cfg", _path.personal_dir);
46a8146adecf (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
   521
983
1be852dcdd4c (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
   522
	_highscore_file = str_fmt("%shs.dat", _path.personal_dir);
704
a526dc96fbfc (svn r1154) -Add: [Network] Forked dedicated server (start openttd with -Df) (GeniusDex)
truelight
parents: 569
diff changeset
   523
	_log_file = str_fmt("%sopenttd.log", _path.personal_dir);
915
013cb2d74800 (svn r1402) Trim trailing whitespace
tron
parents: 826
diff changeset
   524
561
e3b9689745ab (svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents: 543
diff changeset
   525
#if defined CUSTOM_LANG_DIR
e3b9689745ab (svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents: 543
diff changeset
   526
	// sets the search path for lng files to the custom one
e3b9689745ab (svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents: 543
diff changeset
   527
	_path.lang_dir = malloc( MAX_PATH );
e3b9689745ab (svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents: 543
diff changeset
   528
	ttd_strlcpy( _path.lang_dir, CUSTOM_LANG_DIR, MAX_PATH);
e3b9689745ab (svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents: 543
diff changeset
   529
#else
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   530
	_path.lang_dir = str_fmt("%slang/", _path.game_data_dir);
561
e3b9689745ab (svn r970) Added 2nd data path for all non-windows OSes
bjarni
parents: 543
diff changeset
   531
#endif
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 179
diff changeset
   532
238
a73a3e50c7f9 (svn r239) Fix: [ 1025361 ] PERSONAL_DIR is created at startup now
dominik
parents: 223
diff changeset
   533
	// create necessary folders
a73a3e50c7f9 (svn r239) Fix: [ 1025361 ] PERSONAL_DIR is created at startup now
dominik
parents: 223
diff changeset
   534
	mkdir(_path.personal_dir, 0755);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
	mkdir(_path.save_dir, 0755);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
	mkdir(_path.autosave_dir, 0755);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   537
	mkdir(_path.scenario_dir, 0755);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
1470
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   540
bool InsertTextBufferClipboard(Textbuf *tb)
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   541
{
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   542
	return false;
25c332b43280 (svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)
tron
parents: 1449
diff changeset
   543
}