1
0
Fork 0

(svn r1974) Cleanups, quite similar to those done to win32.c in r1972 (code duplication, anyone?)

release/0.4.5
tron 2005-03-09 17:45:51 +00:00
parent 0f3bb838d8
commit 95fafc7c25
1 changed files with 96 additions and 87 deletions

183
unix.c
View File

@ -53,7 +53,10 @@ int compare_FiosItems (const void *a, const void *b) {
if (_savegame_sort_order < 2) // sort by date if (_savegame_sort_order < 2) // sort by date
r = da->mtime < db->mtime ? -1 : 1; r = da->mtime < db->mtime ? -1 : 1;
else else
r = strcasecmp(da->title[0] ? da->title : da->name, db->title[0] ? db->title : db->name); r = strcasecmp(
da->title[0] != '\0' ? da->title : da->name,
db->title[0] != '\0' ? db->title : db->name
);
if (_savegame_sort_order & 1) r = -r; if (_savegame_sort_order & 1) r = -r;
return r; return r;
@ -75,13 +78,13 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
strcpy(_fios_save_path, _path.save_dir); strcpy(_fios_save_path, _path.save_dir);
} }
if(_game_mode==GM_EDITOR) if (_game_mode == GM_EDITOR)
_fios_path = _fios_scn_path; _fios_path = _fios_scn_path;
else else
_fios_path = _fios_save_path; _fios_path = _fios_save_path;
// Parent directory, only if not in root already. // Parent directory, only if not in root already.
if (_fios_path[1] != 0) { if (_fios_path[1] != '\0') {
fios = FiosAlloc(); fios = FiosAlloc();
fios->type = FIOS_TYPE_PARENT; fios->type = FIOS_TYPE_PARENT;
fios->mtime = 0; fios->mtime = 0;
@ -91,15 +94,15 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
// Show subdirectories first // Show subdirectories first
dir = opendir(_fios_path[0] ? _fios_path : "/"); dir = opendir(_fios_path[0] ? _fios_path : "/");
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir))) { while ((dirent = readdir(dir)) != NULL) {
snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name); snprintf(filename, lengthof(filename), "%s/%s",
if (stat(filename, &sb) || !S_ISDIR(sb.st_mode)) _fios_path, dirent->d_name);
continue; if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
if (dirent->d_name[0] != '.') { dirent->d_name[0] != '.') {
fios = FiosAlloc(); fios = FiosAlloc();
fios->mtime = 0; fios->mtime = 0;
fios->type = FIOS_TYPE_DIR; fios->type = FIOS_TYPE_DIR;
fios->title[0] = 0; fios->title[0] = '\0';
sprintf(fios->name, "%s/ (Directory)", dirent->d_name); sprintf(fios->name, "%s/ (Directory)", dirent->d_name);
} }
} }
@ -123,31 +126,33 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
* .SV1 Transport Tycoon Deluxe (Patch) saved game * .SV1 Transport Tycoon Deluxe (Patch) saved game
* .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game
*/ */
dir = opendir(_fios_path[0] ? _fios_path : "/"); dir = opendir(_fios_path[0] != '\0' ? _fios_path : "/");
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir))) { while ((dirent = readdir(dir))) {
char *t; char *t;
snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name); snprintf(filename, lengthof(filename), "%s/%s",
if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) _fios_path, dirent->d_name);
continue; if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue;
t = strrchr(dirent->d_name, '.'); t = strrchr(dirent->d_name, '.');
if (t && !strcasecmp(t, ".sav")) { // OpenTTD if (t != NULL && strcasecmp(t, ".sav") == 0) { // OpenTTD
*t = 0; // cut extension *t = '\0'; // cut extension
fios = FiosAlloc(); fios = FiosAlloc();
fios->type = FIOS_TYPE_FILE; fios->type = FIOS_TYPE_FILE;
fios->mtime = sb.st_mtime; fios->mtime = sb.st_mtime;
fios->title[0] = 0; fios->title[0] = 0;
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)); ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name));
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
int ext = 0; // start of savegame extensions in _old_extensions[] int ext = 0; // start of savegame extensions in _old_extensions[]
if (t && ((ext++, !strcasecmp(t, ".ss1")) || (ext++, !strcasecmp(t, ".sv1")) if (t != NULL && (
|| (ext++, !strcasecmp(t, ".sv2"))) ) { // TTDLX(Patch) (ext++, strcasecmp(t, ".ss1") == 0) ||
*t = 0; // cut extension (ext++, strcasecmp(t, ".sv1") == 0) ||
(ext++, strcasecmp(t, ".sv2") == 0)
)) { // TTDLX(Patch)
*t = '\0'; // cut extension
fios = FiosAlloc(); fios = FiosAlloc();
fios->old_extension = ext-1; fios->old_extension = ext - 1;
fios->type = FIOS_TYPE_OLDFILE; fios->type = FIOS_TYPE_OLDFILE;
fios->mtime = sb.st_mtime; fios->mtime = sb.st_mtime;
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)); ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name));
@ -183,15 +188,15 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
// Show subdirectories first // Show subdirectories first
dir = opendir(_fios_path[0] ? _fios_path : "/"); dir = opendir(_fios_path[0] ? _fios_path : "/");
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir))) { while ((dirent = readdir(dir)) != NULL) {
snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name); snprintf(filename, lengthof(filename), "%s/%s",
if (stat(filename, &sb) || !S_ISDIR(sb.st_mode)) _fios_path, dirent->d_name);
continue; if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
if (dirent->d_name[0] != '.') { dirent->d_name[0] != '.') {
fios = FiosAlloc(); fios = FiosAlloc();
fios->mtime = 0; fios->mtime = '\0';
fios->type = FIOS_TYPE_DIR; fios->type = FIOS_TYPE_DIR;
fios->title[0] = 0; fios->title[0] = '\0';
sprintf(fios->name, "%s/ (Directory)", dirent->d_name); sprintf(fios->name, "%s/ (Directory)", dirent->d_name);
} }
} }
@ -208,26 +213,28 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
*/ */
dir = opendir(_fios_path[0] ? _fios_path : "/"); dir = opendir(_fios_path[0] ? _fios_path : "/");
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir))) { while ((dirent = readdir(dir)) != NULL) {
char *t; char *t;
snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name); snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue;
continue;
t = strrchr(dirent->d_name, '.'); t = strrchr(dirent->d_name, '.');
if (t && !strcasecmp(t, ".scn")) { // OpenTTD if (t != NULL && strcasecmp(t, ".scn") != 0) { // OpenTTD
*t = 0; // cut extension *t = '\0'; // cut extension
fios = FiosAlloc(); fios = FiosAlloc();
fios->type = FIOS_TYPE_SCENARIO; fios->type = FIOS_TYPE_SCENARIO;
fios->mtime = sb.st_mtime; fios->mtime = sb.st_mtime;
fios->title[0] = 0; fios->title[0] = '\0';
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3); ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3);
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO ||
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) { mode == SLD_NEW_GAME) {
int ext = 3; // start of scenario extensions in _old_extensions[] int ext = 3; // start of scenario extensions in _old_extensions[]
if (t && ((ext++, !strcasecmp(t, ".sv0")) || (ext++, !strcasecmp(t, ".ss0"))) ) {// TTDLX(Patch) if (t != NULL && (
*t = 0; // cut extension (ext++, strcasecmp(t, ".sv0") == 0) ||
(ext++, strcasecmp(t, ".ss0") == 0)
)) {// TTDLX(Patch)
*t = '\0'; // cut extension
fios = FiosAlloc(); fios = FiosAlloc();
fios->old_extension = ext-1; fios->old_extension = ext-1;
fios->type = FIOS_TYPE_OLD_SCENARIO; fios->type = FIOS_TYPE_OLD_SCENARIO;
@ -261,35 +268,36 @@ char *FiosBrowseTo(const FiosItem *item)
char *path = _fios_path; char *path = _fios_path;
char *s; char *s;
switch(item->type) { switch (item->type) {
case FIOS_TYPE_PARENT: case FIOS_TYPE_PARENT:
s = strrchr(path, '/'); s = strrchr(path, '/');
if (s != NULL) *s = 0; if (s != NULL) *s = '\0';
break; break;
case FIOS_TYPE_DIR: case FIOS_TYPE_DIR:
s = strchr(item->name, '/'); s = strchr(item->name, '/');
if (s) *s = 0; if (s != NULL) *s = '\0';
while (*path) path++; strcat(path, "/");
*path++ = '/'; strcat(path, item->name);
strcpy(path, item->name); break;
break;
case FIOS_TYPE_FILE: case FIOS_TYPE_FILE:
FiosMakeSavegameName(str_buffr, item->name); FiosMakeSavegameName(str_buffr, item->name);
return str_buffr; return str_buffr;
case FIOS_TYPE_OLDFILE: case FIOS_TYPE_OLDFILE:
sprintf(str_buffr, "%s/%s.%s", _fios_path, item->name, _old_extensions[item->old_extension]); sprintf(str_buffr, "%s/%s.%s",
return str_buffr; _fios_path, item->name, _old_extensions[item->old_extension]);
return str_buffr;
case FIOS_TYPE_SCENARIO: case FIOS_TYPE_SCENARIO:
sprintf(str_buffr, "%s/%s.scn", path, item->name); sprintf(str_buffr, "%s/%s.scn", path, item->name);
return str_buffr; return str_buffr;
case FIOS_TYPE_OLD_SCENARIO: case FIOS_TYPE_OLD_SCENARIO:
sprintf(str_buffr, "%s/%s.%s", path, item->name, _old_extensions[item->old_extension]); sprintf(str_buffr, "%s/%s.%s",
return str_buffr; path, item->name, _old_extensions[item->old_extension]);
return str_buffr;
} }
return NULL; return NULL;
@ -300,20 +308,18 @@ char *FiosBrowseTo(const FiosItem *item)
// string describing the path. // string describing the path.
StringID FiosGetDescText(const char **path) StringID FiosGetDescText(const char **path)
{ {
*path = _fios_path[0] ? _fios_path : "/"; *path = _fios_path[0] != '\0' ? _fios_path : "/";
#if defined(__linux__) #if defined(__linux__)
{ {
struct statvfs s; struct statvfs s;
if (statvfs(*path, &s) == 0) if (statvfs(*path, &s) == 0) {
{ uint64 tot = (uint64)s.f_bsize * s.f_bavail;
uint64 tot = (uint64)s.f_bsize * s.f_bavail; SetDParam(0, (uint32)(tot >> 20));
SetDParam(0, (uint32)(tot >> 20)); return STR_4005_BYTES_FREE;
return STR_4005_BYTES_FREE; } else
} return STR_4006_UNABLE_TO_READ_DRIVE;
else
return STR_4006_UNABLE_TO_READ_DRIVE;
} }
#else #else
SetDParam(0, 0); SetDParam(0, 0);
@ -323,7 +329,7 @@ StringID FiosGetDescText(const char **path)
void FiosMakeSavegameName(char *buf, const char *name) void FiosMakeSavegameName(char *buf, const char *name)
{ {
if(_game_mode==GM_EDITOR) if (_game_mode == GM_EDITOR)
sprintf(buf, "%s/%s.scn", _fios_path, name); sprintf(buf, "%s/%s.scn", _fios_path, name);
else else
sprintf(buf, "%s/%s.sav", _fios_path, name); sprintf(buf, "%s/%s.sav", _fios_path, name);
@ -400,9 +406,10 @@ int GetLanguageList(char **languages, int max)
dir = opendir(_path.lang_dir); dir = opendir(_path.lang_dir);
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir))) { while ((dirent = readdir(dir)) != NULL) {
char *t = strrchr(dirent->d_name, '.'); char *t = strrchr(dirent->d_name, '.');
if (t && !strcmp(t, ".lng")) {
if (t != NULL && strcmp(t, ".lng") == 0) {
languages[num++] = strdup(dirent->d_name); languages[num++] = strdup(dirent->d_name);
if (num == max) break; if (num == max) break;
} }
@ -419,7 +426,7 @@ static void ChangeWorkingDirectory(char *exe)
{ {
char *s = strrchr(exe, '/'); char *s = strrchr(exe, '/');
if (s != NULL) { if (s != NULL) {
*s = 0; *s = '\0';
chdir(exe); chdir(exe);
*s = '/'; *s = '/';
} }
@ -461,21 +468,20 @@ void DeterminePaths(void)
{ {
char *s; char *s;
_path.game_data_dir = malloc( MAX_PATH ); _path.game_data_dir = malloc(MAX_PATH);
ttd_strlcpy(_path.game_data_dir, GAME_DATA_DIR, MAX_PATH); ttd_strlcpy(_path.game_data_dir, GAME_DATA_DIR, MAX_PATH);
#if defined SECOND_DATA_DIR #if defined SECOND_DATA_DIR
_path.second_data_dir = malloc( MAX_PATH ); _path.second_data_dir = malloc(MAX_PATH);
ttd_strlcpy( _path.second_data_dir, SECOND_DATA_DIR, MAX_PATH); ttd_strlcpy(_path.second_data_dir, SECOND_DATA_DIR, MAX_PATH);
#endif #endif
#if defined(USE_HOMEDIR) #if defined(USE_HOMEDIR)
{ {
char *homedir; const char *homedir = getenv("HOME");
homedir = getenv("HOME");
if(!homedir) { if (homedir == NULL) {
struct passwd *pw = getpwuid(getuid()); const struct passwd *pw = getpwuid(getuid());
if (pw) homedir = pw->pw_dir; if (pw != NULL) homedir = pw->pw_dir;
} }
_path.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR); _path.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
@ -483,14 +489,14 @@ void DeterminePaths(void)
#else /* not defined(USE_HOMEDIR) */ #else /* not defined(USE_HOMEDIR) */
_path.personal_dir = malloc( MAX_PATH ); _path.personal_dir = malloc(MAX_PATH);
ttd_strlcpy(_path.personal_dir, PERSONAL_DIR, MAX_PATH); ttd_strlcpy(_path.personal_dir, PERSONAL_DIR, MAX_PATH);
// check if absolute or relative path // check if absolute or relative path
s = strchr(_path.personal_dir, '/'); s = strchr(_path.personal_dir, '/');
// add absolute path // add absolute path
if (s==NULL || _path.personal_dir != s) { if (s == NULL || _path.personal_dir != s) {
getcwd(_path.personal_dir, MAX_PATH); getcwd(_path.personal_dir, MAX_PATH);
s = strchr(_path.personal_dir, 0); s = strchr(_path.personal_dir, 0);
*s++ = '/'; *s++ = '/';
@ -502,7 +508,7 @@ void DeterminePaths(void)
s = strchr(_path.personal_dir, 0); s = strchr(_path.personal_dir, 0);
// append a / ? // append a / ?
if (s[-1] != '/') { s[0] = '/'; s[1] = 0; } if (s[-1] != '/') strcpy(s, "/");
_path.save_dir = str_fmt("%ssave", _path.personal_dir); _path.save_dir = str_fmt("%ssave", _path.personal_dir);
_path.autosave_dir = str_fmt("%s/autosave", _path.save_dir); _path.autosave_dir = str_fmt("%s/autosave", _path.save_dir);
@ -528,4 +534,7 @@ void DeterminePaths(void)
mkdir(_path.scenario_dir, 0755); mkdir(_path.scenario_dir, 0755);
} }
bool InsertTextBufferClipboard(Textbuf *tb) {return false;} bool InsertTextBufferClipboard(Textbuf *tb)
{
return false;
}