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

123
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
r = da->mtime < db->mtime ? -1 : 1;
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;
return r;
@ -81,7 +84,7 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
_fios_path = _fios_save_path;
// Parent directory, only if not in root already.
if (_fios_path[1] != 0) {
if (_fios_path[1] != '\0') {
fios = FiosAlloc();
fios->type = FIOS_TYPE_PARENT;
fios->mtime = 0;
@ -91,15 +94,15 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
// Show subdirectories first
dir = opendir(_fios_path[0] ? _fios_path : "/");
if (dir != NULL) {
while ((dirent = readdir(dir))) {
snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
if (stat(filename, &sb) || !S_ISDIR(sb.st_mode))
continue;
if (dirent->d_name[0] != '.') {
while ((dirent = readdir(dir)) != NULL) {
snprintf(filename, lengthof(filename), "%s/%s",
_fios_path, dirent->d_name);
if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
dirent->d_name[0] != '.') {
fios = FiosAlloc();
fios->mtime = 0;
fios->type = FIOS_TYPE_DIR;
fios->title[0] = 0;
fios->title[0] = '\0';
sprintf(fios->name, "%s/ (Directory)", dirent->d_name);
}
}
@ -123,29 +126,31 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
* .SV1 Transport Tycoon Deluxe (Patch) saved 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) {
while ((dirent = readdir(dir))) {
char *t;
snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
if (stat(filename, &sb) || S_ISDIR(sb.st_mode))
continue;
snprintf(filename, lengthof(filename), "%s/%s",
_fios_path, dirent->d_name);
if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue;
t = strrchr(dirent->d_name, '.');
if (t && !strcasecmp(t, ".sav")) { // OpenTTD
*t = 0; // cut extension
if (t != NULL && strcasecmp(t, ".sav") == 0) { // OpenTTD
*t = '\0'; // cut extension
fios = FiosAlloc();
fios->type = FIOS_TYPE_FILE;
fios->mtime = sb.st_mtime;
fios->title[0] = 0;
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name));
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
int ext = 0; // start of savegame extensions in _old_extensions[]
if (t && ((ext++, !strcasecmp(t, ".ss1")) || (ext++, !strcasecmp(t, ".sv1"))
|| (ext++, !strcasecmp(t, ".sv2"))) ) { // TTDLX(Patch)
*t = 0; // cut extension
if (t != NULL && (
(ext++, strcasecmp(t, ".ss1") == 0) ||
(ext++, strcasecmp(t, ".sv1") == 0) ||
(ext++, strcasecmp(t, ".sv2") == 0)
)) { // TTDLX(Patch)
*t = '\0'; // cut extension
fios = FiosAlloc();
fios->old_extension = ext - 1;
fios->type = FIOS_TYPE_OLDFILE;
@ -183,15 +188,15 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
// Show subdirectories first
dir = opendir(_fios_path[0] ? _fios_path : "/");
if (dir != NULL) {
while ((dirent = readdir(dir))) {
snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
if (stat(filename, &sb) || !S_ISDIR(sb.st_mode))
continue;
if (dirent->d_name[0] != '.') {
while ((dirent = readdir(dir)) != NULL) {
snprintf(filename, lengthof(filename), "%s/%s",
_fios_path, dirent->d_name);
if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) &&
dirent->d_name[0] != '.') {
fios = FiosAlloc();
fios->mtime = 0;
fios->mtime = '\0';
fios->type = FIOS_TYPE_DIR;
fios->title[0] = 0;
fios->title[0] = '\0';
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 : "/");
if (dir != NULL) {
while ((dirent = readdir(dir))) {
while ((dirent = readdir(dir)) != NULL) {
char *t;
snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
if (stat(filename, &sb) || S_ISDIR(sb.st_mode))
continue;
if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue;
t = strrchr(dirent->d_name, '.');
if (t && !strcasecmp(t, ".scn")) { // OpenTTD
*t = 0; // cut extension
if (t != NULL && strcasecmp(t, ".scn") != 0) { // OpenTTD
*t = '\0'; // cut extension
fios = FiosAlloc();
fios->type = FIOS_TYPE_SCENARIO;
fios->mtime = sb.st_mtime;
fios->title[0] = 0;
fios->title[0] = '\0';
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3);
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) {
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO ||
mode == SLD_NEW_GAME) {
int ext = 3; // start of scenario extensions in _old_extensions[]
if (t && ((ext++, !strcasecmp(t, ".sv0")) || (ext++, !strcasecmp(t, ".ss0"))) ) {// TTDLX(Patch)
*t = 0; // cut extension
if (t != NULL && (
(ext++, strcasecmp(t, ".sv0") == 0) ||
(ext++, strcasecmp(t, ".ss0") == 0)
)) {// TTDLX(Patch)
*t = '\0'; // cut extension
fios = FiosAlloc();
fios->old_extension = ext-1;
fios->type = FIOS_TYPE_OLD_SCENARIO;
@ -264,15 +271,14 @@ char *FiosBrowseTo(const FiosItem *item)
switch (item->type) {
case FIOS_TYPE_PARENT:
s = strrchr(path, '/');
if (s != NULL) *s = 0;
if (s != NULL) *s = '\0';
break;
case FIOS_TYPE_DIR:
s = strchr(item->name, '/');
if (s) *s = 0;
while (*path) path++;
*path++ = '/';
strcpy(path, item->name);
if (s != NULL) *s = '\0';
strcat(path, "/");
strcat(path, item->name);
break;
case FIOS_TYPE_FILE:
@ -280,7 +286,8 @@ char *FiosBrowseTo(const FiosItem *item)
return str_buffr;
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",
_fios_path, item->name, _old_extensions[item->old_extension]);
return str_buffr;
case FIOS_TYPE_SCENARIO:
@ -288,7 +295,8 @@ char *FiosBrowseTo(const FiosItem *item)
return str_buffr;
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",
path, item->name, _old_extensions[item->old_extension]);
return str_buffr;
}
@ -300,19 +308,17 @@ char *FiosBrowseTo(const FiosItem *item)
// string describing the path.
StringID FiosGetDescText(const char **path)
{
*path = _fios_path[0] ? _fios_path : "/";
*path = _fios_path[0] != '\0' ? _fios_path : "/";
#if defined(__linux__)
{
struct statvfs s;
if (statvfs(*path, &s) == 0)
{
if (statvfs(*path, &s) == 0) {
uint64 tot = (uint64)s.f_bsize * s.f_bavail;
SetDParam(0, (uint32)(tot >> 20));
return STR_4005_BYTES_FREE;
}
else
} else
return STR_4006_UNABLE_TO_READ_DRIVE;
}
#else
@ -400,9 +406,10 @@ int GetLanguageList(char **languages, int max)
dir = opendir(_path.lang_dir);
if (dir != NULL) {
while ((dirent = readdir(dir))) {
while ((dirent = readdir(dir)) != NULL) {
char *t = strrchr(dirent->d_name, '.');
if (t && !strcmp(t, ".lng")) {
if (t != NULL && strcmp(t, ".lng") == 0) {
languages[num++] = strdup(dirent->d_name);
if (num == max) break;
}
@ -419,7 +426,7 @@ static void ChangeWorkingDirectory(char *exe)
{
char *s = strrchr(exe, '/');
if (s != NULL) {
*s = 0;
*s = '\0';
chdir(exe);
*s = '/';
}
@ -470,12 +477,11 @@ void DeterminePaths(void)
#if defined(USE_HOMEDIR)
{
char *homedir;
homedir = getenv("HOME");
const char *homedir = getenv("HOME");
if(!homedir) {
struct passwd *pw = getpwuid(getuid());
if (pw) homedir = pw->pw_dir;
if (homedir == NULL) {
const struct passwd *pw = getpwuid(getuid());
if (pw != NULL) homedir = pw->pw_dir;
}
_path.personal_dir = str_fmt("%s" PATHSEP "%s", homedir, PERSONAL_DIR);
@ -502,7 +508,7 @@ void DeterminePaths(void)
s = strchr(_path.personal_dir, 0);
// 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.autosave_dir = str_fmt("%s/autosave", _path.save_dir);
@ -528,4 +534,7 @@ void DeterminePaths(void)
mkdir(_path.scenario_dir, 0755);
}
bool InsertTextBufferClipboard(Textbuf *tb) {return false;}
bool InsertTextBufferClipboard(Textbuf *tb)
{
return false;
}