(svn r1953) Codechange: Tidyup, reduced ridiculous indentation levels, some sprintf()s replced by snprintf()s.

This commit is contained in:
pasky 2005-03-07 00:39:41 +00:00
parent 43fbe3b2da
commit 1c41c61305

140
unix.c
View File

@ -85,24 +85,22 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
fios = FiosAlloc(); fios = FiosAlloc();
fios->type = FIOS_TYPE_PARENT; fios->type = FIOS_TYPE_PARENT;
fios->mtime = 0; fios->mtime = 0;
sprintf(fios->title, ".. (Parent directory)"); strcpy(fios->title, ".. (Parent directory)");
} }
// 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))) {
sprintf (filename, "%s/%s", _fios_path, dirent->d_name); snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
if (!stat(filename, &sb)) { if (stat(filename, &sb) || !S_ISDIR(sb.st_mode))
if (S_ISDIR(sb.st_mode)) { continue;
if (dirent->d_name[0] != '.') { if (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);
}
}
} }
} }
closedir(dir); closedir(dir);
@ -128,29 +126,32 @@ FiosItem *FiosGetSavegameList(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))) {
sprintf (filename, "%s/%s", _fios_path, dirent->d_name); char *t;
if (!stat(filename, &sb)) {
if (!S_ISDIR(sb.st_mode)) { snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
char *t = strrchr(dirent->d_name, '.'); if (stat(filename, &sb) || S_ISDIR(sb.st_mode))
if (t && !strcasecmp(t, ".sav")) { // OpenTTD continue;
*t = 0; // cut extension
fios = FiosAlloc(); t = strrchr(dirent->d_name, '.');
fios->type = FIOS_TYPE_FILE; if (t && !strcasecmp(t, ".sav")) { // OpenTTD
fios->mtime = sb.st_mtime; *t = 0; // cut extension
fios->title[0] = 0; fios = FiosAlloc();
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)); fios->type = FIOS_TYPE_FILE;
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { fios->mtime = sb.st_mtime;
int ext = 0; // start of savegame extensions in _old_extensions[] fios->title[0] = 0;
if (t && ((ext++, !strcasecmp(t, ".ss1")) || (ext++, !strcasecmp(t, ".sv1")) || (ext++, !strcasecmp(t, ".sv2"))) ) { // TTDLX(Patch) ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name));
*t = 0; // cut extension
fios = FiosAlloc(); } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
fios->old_extension = ext-1; int ext = 0; // start of savegame extensions in _old_extensions[]
fios->type = FIOS_TYPE_OLDFILE; if (t && ((ext++, !strcasecmp(t, ".ss1")) || (ext++, !strcasecmp(t, ".sv1"))
fios->mtime = sb.st_mtime; || (ext++, !strcasecmp(t, ".sv2"))) ) { // TTDLX(Patch)
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)); *t = 0; // cut extension
GetOldSaveGameName(fios->title, filename); fios = FiosAlloc();
} fios->old_extension = ext-1;
} fios->type = FIOS_TYPE_OLDFILE;
fios->mtime = sb.st_mtime;
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name));
GetOldSaveGameName(fios->title, filename);
} }
} }
} }
@ -163,6 +164,7 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
} }
// Get a list of scenarios // Get a list of scenarios
// FIXME: Gross code duplication with FiosGetSavegameList()
FiosItem *FiosGetScenarioList(int *num, int mode) FiosItem *FiosGetScenarioList(int *num, int mode)
{ {
FiosItem *fios; FiosItem *fios;
@ -182,17 +184,15 @@ 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))) {
sprintf (filename, "%s/%s", _fios_path, dirent->d_name); snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
if (!stat(filename, &sb)) { if (stat(filename, &sb) || !S_ISDIR(sb.st_mode))
if (S_ISDIR(sb.st_mode)) { continue;
if (dirent->d_name[0] != '.') { if (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);
}
}
} }
} }
closedir(dir); closedir(dir);
@ -209,29 +209,31 @@ 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))) {
sprintf (filename, "%s/%s", _fios_path, dirent->d_name); char *t;
if (!stat(filename, &sb)) {
if (!S_ISDIR(sb.st_mode)) { snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name);
char *t = strrchr(dirent->d_name, '.'); if (stat(filename, &sb) || S_ISDIR(sb.st_mode))
if (t && !strcasecmp(t, ".scn")) { // OpenTTD continue;
*t = 0; // cut extension
fios = FiosAlloc(); t = strrchr(dirent->d_name, '.');
fios->type = FIOS_TYPE_SCENARIO; if (t && !strcasecmp(t, ".scn")) { // OpenTTD
fios->mtime = sb.st_mtime; *t = 0; // cut extension
fios->title[0] = 0; fios = FiosAlloc();
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3); fios->type = FIOS_TYPE_SCENARIO;
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) { fios->mtime = sb.st_mtime;
int ext = 3; // start of scenario extensions in _old_extensions[] fios->title[0] = 0;
if (t && ((ext++, !strcasecmp(t, ".sv0")) || (ext++, !strcasecmp(t, ".ss0"))) ) { // TTDLX(Patch) ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3);
*t = 0; // cut extension
fios = FiosAlloc(); } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) {
fios->old_extension = ext-1; int ext = 3; // start of scenario extensions in _old_extensions[]
fios->type = FIOS_TYPE_OLD_SCENARIO; if (t && ((ext++, !strcasecmp(t, ".sv0")) || (ext++, !strcasecmp(t, ".ss0"))) ) {// TTDLX(Patch)
fios->mtime = sb.st_mtime; *t = 0; // cut extension
GetOldScenarioGameName(fios->title, filename); fios = FiosAlloc();
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3); fios->old_extension = ext-1;
} fios->type = FIOS_TYPE_OLD_SCENARIO;
} fios->mtime = sb.st_mtime;
GetOldScenarioGameName(fios->title, filename);
ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3);
} }
} }
} }