# HG changeset patch # User tron # Date 1110550494 0 # Node ID 6a31e3d7dfe30b85bb941c0906b22ee9bb05dd29 # Parent a2f36708c260370e043c3f517d4fec667360cab9 (svn r1990) Reduce the diff between the OS specific files with respect to file handling and fix some inconsitencies (I hope I didn't break the OS/2 part, couldn't test it, feedback is welcome) diff -r a2f36708c260 -r 6a31e3d7dfe3 os2.c --- a/os2.c Fri Mar 11 13:17:39 2005 +0000 +++ b/os2.c Fri Mar 11 14:14:54 2005 +0000 @@ -23,31 +23,39 @@ #include #endif +static inline int strcasecmp(const char* s1, const char* s2) +{ + return stricmp(s1, s2); +} + static char *_fios_path; static char *_fios_save_path; static char *_fios_scn_path; static FiosItem *_fios_items; static int _fios_count, _fios_alloc; -static FiosItem *FiosAlloc() +static FiosItem *FiosAlloc(void) { if (_fios_count == _fios_alloc) { _fios_alloc += 256; _fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem)); } - return &_fios_items[_fios_count++]; } -int compare_FiosItems (const void *a, const void *b) { - const FiosItem *da = (const FiosItem *) a; - const FiosItem *db = (const FiosItem *) b; +int compare_FiosItems(const void *a, const void *b) +{ + const FiosItem *da = (const FiosItem *)a; + const FiosItem *db = (const FiosItem *)b; int r; if (_savegame_sort_order < 2) // sort by date - r = da->mtime < db->mtime ? -1 : 1; + r = da->mtime < db->mtime ? -1 : 1; else - r = stricmp(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; @@ -62,9 +70,9 @@ return opendir(paths); } -static void append_path(char *out, char *path, char *file) +static void append_path(char *out, const char *path, const char *file) { - if ((path[2] == '\\') && (path[3] == 0)) + if (path[2] == '\\' && path[3] == '\0') sprintf(out, "%s%s", path, file); else sprintf(out, "%s\\%s", path, file); @@ -91,27 +99,27 @@ _fios_path = _fios_save_path; // Parent directory, only if not of the type C:\. - if (_fios_path[3] != 0) { + if (_fios_path[3] != '\0') { fios = FiosAlloc(); fios->type = FIOS_TYPE_PARENT; + fios->mtime = 0; strcpy(fios->title, ".. (Parent directory)"); } // Show subdirectories first dir = my_opendir(_fios_path, "*.*"); if (dir != NULL) { - while ((dirent = readdir(dir))) { + while ((dirent = readdir(dir)) != NULL) { append_path(filename, _fios_path, dirent->d_name); - if (!stat(filename, &sb)) { - if (S_ISDIR(sb.st_mode)) { - if (!(dirent->d_name[0] == '.' && (dirent->d_name[1] == 0 || (dirent->d_name[1] == '.' && dirent->d_name[2] == 0)))) - { - fios = FiosAlloc(); - fios->type = FIOS_TYPE_DIR; - strcpy(fios->name, dirent->d_name); - sprintf(fios->title, "%s\\ (Directory)", dirent->d_name); - } - } + if (!stat(filename, &sb) && S_ISDIR(sb.st_mode) && + strcmp(fd.cFileName, ".") != 0 && + strcmp(fd.cFileName, "..") != 0) { + fios = FiosAlloc(); + fios->type = FIOS_TYPE_DIR; + fios->mtime = 0; + ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); + snprintf(fios->title, lengthof(fios->title), + "%s\\ (Directory)", dirent->d_name); } } closedir(dir); @@ -128,38 +136,38 @@ // this is where to start sorting sort_start = _fios_count; - /* Show savegame files - * .SAV OpenTTD saved game - * .SS1 Transport Tycoon Deluxe preset game - * .SV1 Transport Tycoon Deluxe (Patch) saved game - * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game + /* Show savegame files + * .SAV OpenTTD saved game + * .SS1 Transport Tycoon Deluxe preset game + * .SV1 Transport Tycoon Deluxe (Patch) saved game + * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */ dir = my_opendir(_fios_path, "*.*"); if (dir != NULL) { - while ((dirent = readdir(dir))) { + while ((dirent = readdir(dir)) != NULL) { + char *t; + append_path(filename, _fios_path, dirent->d_name); - if (!stat(filename, &sb)) { - if (!S_ISDIR(sb.st_mode)) { - char *t = strrchr(dirent->d_name, '.'); - if (t && !stricmp(t, ".sav")) { // 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++, !stricmp(t, ".ss1")) || (ext++, !stricmp(t, ".sv1")) || (ext++, !stricmp(t, ".sv2"))) ) { // TTDLX(Patch) - *t = 0; // cut extension - 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); - } - } + if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue; + + t = strrchr(dirent->d_name, '.'); + if (t != NULL && strcasecmp(t, ".sav") == 0) { // OpenTTD + fios = FiosAlloc(); + fios->type = FIOS_TYPE_FILE; + fios->mtime = sb.st_mtime; + fios->title[0] = '\0'; + ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); + } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { + if (t != NULL && ( + strcasecmp(t, ".ss1") == 0 || + strcasecmp(t, ".sv1") == 0 || + strcasecmp(t, ".sv2") == 0 + )) { // TTDLX(Patch) + fios = FiosAlloc(); + fios->type = FIOS_TYPE_OLDFILE; + fios->mtime = sb.st_mtime; + ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); + GetOldSaveGameName(fios->title, filename); } } } @@ -170,15 +178,18 @@ // Drives { - unsigned save, disk, disk2, total; + uint save; + uint disk; + uint total; /* save original drive */ _dos_getdrive(&save); /* get available drive letters */ - for (disk = 1; disk < 27; ++disk) { + uint disk2; + _dos_setdrive(disk, &total); _dos_getdrive(&disk2); @@ -186,9 +197,7 @@ { fios = FiosAlloc(); fios->type = FIOS_TYPE_DRIVE; - fios->title[0] = disk + 'A'-1; - fios->title[1] = ':'; - fios->title[2] = 0; + sprintf(fios->title, "%c:", 'A' + disk - 1); } } @@ -218,28 +227,28 @@ _fios_path = _fios_scn_path; // Parent directory, only if not of the type C:\. - if (_fios_path[3] != 0 && mode != SLD_NEW_GAME) { + if (_fios_path[3] != '\0' && mode != SLD_NEW_GAME) { fios = FiosAlloc(); fios->type = FIOS_TYPE_PARENT; + fios->mtime = 0; strcpy(fios->title, ".. (Parent directory)"); } // Show subdirectories first dir = my_opendir(_fios_path, "*.*"); if (dir != NULL) { - while ((dirent = readdir(dir))) { + while ((dirent = readdir(dir)) != NULL) { append_path(filename, _fios_path, dirent->d_name); - if (!stat(filename, &sb)) { - if (S_ISDIR(sb.st_mode)) { - if (!(dirent->d_name[0] == '.' && (dirent->d_name[1] == 0 || (dirent->d_name[1] == '.' && dirent->d_name[2] == 0)))) - { - fios = FiosAlloc(); - fios->mtime = 0; - fios->type = FIOS_TYPE_DIR; - strcpy(fios->name, dirent->d_name); - sprintf(fios->title, "%s\\ (Directory)", dirent->d_name); - } - } + if (!stat(filename, &sb) && (S_ISDIR(sb.st_mode) + strcmp(dirent->d_name, ".") != 0 && + strcmp(dirent->d_name, "..") != 0 && + ) { + fios = FiosAlloc(); + fios->type = FIOS_TYPE_DIR; + fios->mtime = 0; + ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); + snprintf(fios->title, lengthof(fios->title), + "%s\\ (Directory)", dirent->d_name); } } closedir(dir); @@ -248,37 +257,37 @@ // this is where to start sorting sort_start = _fios_count; - /* Show scenario files - * .SCN OpenTTD style scenario file - * .SV0 Transport Tycoon Deluxe (Patch) scenario - * .SS0 Transport Tycoon Deluxe preset scenario + /* Show scenario files + * .SCN OpenTTD style scenario file + * .SV0 Transport Tycoon Deluxe (Patch) scenario + * .SS0 Transport Tycoon Deluxe preset scenario */ dir = my_opendir(_fios_path, "*.*"); if (dir != NULL) { - while ((dirent = readdir(dir))) { + while ((dirent = readdir(dir)) != NULL) { + char *t; + append_path(filename, _fios_path, dirent->d_name); - if (!stat(filename, &sb)) { - if (!S_ISDIR(sb.st_mode)) { - char *t = strrchr(dirent->d_name, '.'); - if (t && !stricmp(t, ".scn")) { // OpenTTD - *t = 0; // cut extension - fios = FiosAlloc(); - fios->type = FIOS_TYPE_SCENARIO; - fios->mtime = sb.st_mtime; - 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) { - int ext = 3; // start of scenario extensions in _old_extensions[] - if (t && ((ext++, !stricmp(t, ".sv0")) || (ext++, !stricmp(t, ".ss0"))) ) { // TTDLX(Patch) - *t = 0; // cut extension - fios = FiosAlloc(); - 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); - } - } + if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue; + + t = strrchr(dirent->d_name, '.'); + if (t != NULL && strcasecmp(t, ".scn") == 0) { // OpenTTD + fios = FiosAlloc(); + fios->type = FIOS_TYPE_SCENARIO; + fios->mtime = sb.st_mtime; + fios->title[0] = '\0'; + ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); + } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || + mode == SLD_NEW_GAME) { + if (t != NULL && ( + strcasecmp(t, ".sv0") == 0 || + strcasecmp(t, ".ss0") == 0 + )) { // TTDLX(Patch) + fios = FiosAlloc(); + fios->type = FIOS_TYPE_OLD_SCENARIO; + fios->mtime = sb.st_mtime; + GetOldScenarioGameName(fios->title, filename); + ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); } } } @@ -321,7 +330,7 @@ // Free the list of savegames -void FiosFreeSavegameList() +void FiosFreeSavegameList(void) { free(_fios_items); _fios_items = NULL; @@ -331,50 +340,35 @@ // Browse to char *FiosBrowseTo(const FiosItem *item) { - static char str_buffr[512]; char *path = _fios_path; char *s; - switch(item->type) { - case FIOS_TYPE_DRIVE: - sprintf(path, "%c:\\", item->title[0]); - break; - - case FIOS_TYPE_PARENT: - // Skip drive part - path += 3; - s = path; - while (*path) { - if (*path== '\\') - s = path; - path++; - } - *s = 0; - break; + switch (item->type) { + case FIOS_TYPE_DRIVE: + sprintf(path, "%c:\\", item->title[0]); + break; - case FIOS_TYPE_DIR: - // Scan to end - while (*++path); - // Add backslash? - if (path[-1] != '\\') *path++ = '\\'; - - strcpy(path, item->name); - break; + case FIOS_TYPE_PARENT: + s = strrchr(path, '\\'); + if (s != NULL) *s = '\0'; + break; - case FIOS_TYPE_FILE: - FiosMakeSavegameName(str_buffr, item->name); - return str_buffr; + case FIOS_TYPE_DIR: + s = strchr(item->name, '\\'); + if (s != NULL) *s = '\0'; + strcat(path, "\\"); + strcat(path, item->name); + break; - case FIOS_TYPE_OLDFILE: - sprintf(str_buffr, "%s\\%s.%s", _fios_path, item->name, _old_extensions[item->old_extension]); - return str_buffr; + case FIOS_TYPE_FILE: + case FIOS_TYPE_OLDFILE: + case FIOS_TYPE_SCENARIO: + case FIOS_TYPE_OLD_SCENARIO: { + static char str_buffr[512]; - case FIOS_TYPE_SCENARIO: - sprintf(str_buffr, "%s\\%s.scn", path, item->name); - return str_buffr; - case FIOS_TYPE_OLD_SCENARIO: - sprintf(str_buffr, "%s\\%s.%s", path, item->name, _old_extensions[item->old_extension]); - return str_buffr; + sprintf(str_buffr, "%s\\%s", path, item->name); + return str_buffr; + } } return NULL; @@ -389,7 +383,7 @@ char drive; *path = _fios_path; - drive = *path[0] - 'A'+1; + drive = *path[0] - 'A' + 1; _getdiskfree(drive, &free); @@ -399,7 +393,7 @@ 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); else sprintf(buf, "%s\\%s.sav", _fios_path, name); @@ -438,7 +432,7 @@ /* GetOSVersion returns the minimal required version of OS to be able to use that driver. Not needed for OS/2. */ -byte GetOSVersion() +byte GetOSVersion(void) { return 2; // any arbitrary number bigger then 0 } @@ -479,7 +473,7 @@ { char *s = strrchr(exe, '\\'); if (s != NULL) { - *s = 0; + *s = '\0'; chdir(exe); *s = '\\'; } @@ -534,21 +528,20 @@ { 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); #if defined SECOND_DATA_DIR - _path.second_data_dir = malloc( MAX_PATH ); - ttd_strlcpy( _path.second_data_dir, SECOND_DATA_DIR, MAX_PATH); + _path.second_data_dir = malloc(MAX_PATH); + ttd_strlcpy(_path.second_data_dir, SECOND_DATA_DIR, MAX_PATH); #endif #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); @@ -556,14 +549,14 @@ #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); // check if absolute or relative path s = strchr(_path.personal_dir, '\\'); // add absolute path - if (s==NULL || _path.personal_dir != s) { + if (s == NULL || _path.personal_dir != s) { getcwd(_path.personal_dir, MAX_PATH); s = strchr(_path.personal_dir, 0); *s++ = '\\'; @@ -575,7 +568,7 @@ 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); @@ -586,6 +579,9 @@ if (_config_file == NULL) _config_file = str_fmt("%sopenttd.cfg", _path.personal_dir); + _highscore_file = str_fmt("%shs.dat", _path.personal_dir); + _log_file = str_fmt("%sopenttd.log", _path.personal_dir); + #if defined CUSTOM_LANG_DIR // sets the search path for lng files to the custom one _path.lang_dir = malloc( MAX_PATH ); @@ -606,7 +602,7 @@ // Switches OpenTTD to a console app at run-time, instead of a PM app // Necessary to see stdout, etc -void OS2_SwitchToConsoleMode() +void OS2_SwitchToConsoleMode(void) { PPIB pib; PTIB tib; @@ -645,7 +641,7 @@ MidiSendCommand("play song from 0"); } -static void OS2MidiStopSong() +static void OS2MidiStopSong(void) { MidiSendCommand("close all"); } @@ -655,7 +651,7 @@ MidiSendCommand("set song audio volume %d", ((vol/127)*100)); } -static bool OS2MidiIsSongPlaying() +static bool OS2MidiIsSongPlaying(void) { char buf[16]; mciSendString("status song mode", buf, sizeof(buf), NULL, 0); @@ -667,7 +663,7 @@ return 0; } -static void OS2MidiStop() +static void OS2MidiStop(void) { MidiSendCommand("close all"); } @@ -681,4 +677,7 @@ OS2MidiSetVolume, }; -bool InsertTextBufferClipboard(Textbuf *tb) {return false;} +bool InsertTextBufferClipboard(Textbuf *tb) +{ + return false; +} diff -r a2f36708c260 -r 6a31e3d7dfe3 unix.c --- a/unix.c Fri Mar 11 13:17:39 2005 +0000 +++ b/unix.c Fri Mar 11 14:14:54 2005 +0000 @@ -45,13 +45,14 @@ return &_fios_items[_fios_count++]; } -int compare_FiosItems (const void *a, const void *b) { - const FiosItem *da = (const FiosItem *) a; - const FiosItem *db = (const FiosItem *) b; +int compare_FiosItems(const void *a, const void *b) +{ + const FiosItem *da = (const FiosItem *)a; + const FiosItem *db = (const FiosItem *)b; int r; if (_savegame_sort_order < 2) // sort by date - r = da->mtime < db->mtime ? -1 : 1; + r = da->mtime < db->mtime ? -1 : 1; else r = strcasecmp( da->title[0] != '\0' ? da->title : da->name, @@ -92,7 +93,7 @@ } // Show subdirectories first - dir = opendir(_fios_path[0] ? _fios_path : "/"); + dir = opendir(_fios_path[0] != '\0' ? _fios_path : "/"); if (dir != NULL) { while ((dirent = readdir(dir)) != NULL) { snprintf(filename, lengthof(filename), "%s/%s", @@ -100,10 +101,11 @@ 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'; - sprintf(fios->name, "%s/ (Directory)", dirent->d_name); + fios->mtime = 0; + ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); + snprintf(fios->title, lengthof(fios->title), + "%s/ (Directory)", dirent->d_name); } } closedir(dir); @@ -120,15 +122,15 @@ // this is where to start sorting sort_start = _fios_count; - /* Show savegame files - * .SAV OpenTTD saved game - * .SS1 Transport Tycoon Deluxe preset game - * .SV1 Transport Tycoon Deluxe (Patch) saved game - * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game + /* Show savegame files + * .SAV OpenTTD saved game + * .SS1 Transport Tycoon Deluxe preset game + * .SV1 Transport Tycoon Deluxe (Patch) saved game + * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */ dir = opendir(_fios_path[0] != '\0' ? _fios_path : "/"); if (dir != NULL) { - while ((dirent = readdir(dir))) { + while ((dirent = readdir(dir)) != NULL) { char *t; snprintf(filename, lengthof(filename), "%s/%s", @@ -137,25 +139,21 @@ t = strrchr(dirent->d_name, '.'); 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)); + fios->title[0] = '\0'; + ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { - int ext = 0; // start of savegame extensions in _old_extensions[] if (t != NULL && ( - (ext++, strcasecmp(t, ".ss1") == 0) || - (ext++, strcasecmp(t, ".sv1") == 0) || - (ext++, strcasecmp(t, ".sv2") == 0) + strcasecmp(t, ".ss1") == 0 || + strcasecmp(t, ".sv1") == 0 || + strcasecmp(t, ".sv2") == 0 )) { // TTDLX(Patch) - *t = '\0'; // cut extension 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)); + ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); GetOldSaveGameName(fios->title, filename); } } @@ -163,8 +161,8 @@ closedir(dir); } + qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); *num = _fios_count; - qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); return _fios_items; } @@ -185,6 +183,14 @@ } _fios_path = _fios_scn_path; + // Parent directory, only if not of the type C:\. + if (_fios_path[1] != '\0' && mode != SLD_NEW_GAME) { + fios = FiosAlloc(); + fios->type = FIOS_TYPE_PARENT; + fios->mtime = 0; + strcpy(fios->title, ".. (Parent directory)"); + } + // Show subdirectories first dir = opendir(_fios_path[0] ? _fios_path : "/"); if (dir != NULL) { @@ -194,10 +200,11 @@ 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'; - sprintf(fios->name, "%s/ (Directory)", dirent->d_name); + fios->mtime = 0; + ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); + snprintf(fios->title, lengthof(fios->title), + "%s/ (Directory)", dirent->d_name); } } closedir(dir); @@ -206,49 +213,46 @@ // this is where to start sorting sort_start = _fios_count; - /* Show scenario files - * .SCN OpenTTD style scenario file - * .SV0 Transport Tycoon Deluxe (Patch) scenario - * .SS0 Transport Tycoon Deluxe preset scenario + /* Show scenario files + * .SCN OpenTTD style scenario file + * .SV0 Transport Tycoon Deluxe (Patch) scenario + * .SS0 Transport Tycoon Deluxe preset scenario */ dir = opendir(_fios_path[0] ? _fios_path : "/"); if (dir != NULL) { while ((dirent = readdir(dir)) != NULL) { char *t; - snprintf(filename, MAX_PATH, "%s/%s", _fios_path, dirent->d_name); + 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 != NULL && strcasecmp(t, ".scn") != 0) { // OpenTTD - *t = '\0'; // cut extension + if (t != NULL && strcasecmp(t, ".scn") == 0) { // OpenTTD fios = FiosAlloc(); fios->type = FIOS_TYPE_SCENARIO; fios->mtime = sb.st_mtime; fios->title[0] = '\0'; - ttd_strlcpy(fios->name, dirent->d_name, sizeof(fios->name)-3); + ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); } 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 != NULL && ( - (ext++, strcasecmp(t, ".sv0") == 0) || - (ext++, strcasecmp(t, ".ss0") == 0) - )) {// TTDLX(Patch) - *t = '\0'; // cut extension + strcasecmp(t, ".sv0") == 0 || + strcasecmp(t, ".ss0") == 0 + )) { // TTDLX(Patch) fios = FiosAlloc(); - 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); + ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); } } } closedir(dir); } + qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); *num = _fios_count; - qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); return _fios_items; } @@ -264,7 +268,6 @@ // Browse to char *FiosBrowseTo(const FiosItem *item) { - static char str_buffr[512]; char *path = _fios_path; char *s; @@ -282,22 +285,14 @@ break; case FIOS_TYPE_FILE: - FiosMakeSavegameName(str_buffr, item->name); - return str_buffr; - case FIOS_TYPE_OLDFILE: - sprintf(str_buffr, "%s/%s.%s", - _fios_path, item->name, _old_extensions[item->old_extension]); - return str_buffr; + case FIOS_TYPE_SCENARIO: + case FIOS_TYPE_OLD_SCENARIO: { + static char str_buffr[512]; - case FIOS_TYPE_SCENARIO: - sprintf(str_buffr, "%s/%s.scn", path, item->name); + sprintf(str_buffr, "%s/%s", path, item->name); return str_buffr; - - case FIOS_TYPE_OLD_SCENARIO: - sprintf(str_buffr, "%s/%s.%s", - path, item->name, _old_extensions[item->old_extension]); - return str_buffr; + } } return NULL; diff -r a2f36708c260 -r 6a31e3d7dfe3 win32.c --- a/win32.c Fri Mar 11 13:17:39 2005 +0000 +++ b/win32.c Fri Mar 11 14:14:54 2005 +0000 @@ -1520,6 +1520,13 @@ } #endif +#ifndef __MINGW32__ +static inline int strcasecmp(const char* s1, const char* s2) +{ + return stricmp(s1, s2); +} +#endif + static char *_fios_path; static char *_fios_save_path; static char *_fios_scn_path; @@ -1544,15 +1551,16 @@ return FindFirstFile(paths, fd); } -int CDECL compare_FiosItems (const void *a, const void *b) { +int CDECL compare_FiosItems(const void *a, const void *b) +{ const FiosItem *da = (const FiosItem *)a; const FiosItem *db = (const FiosItem *)b; int r; if (_savegame_sort_order < 2) // sort by date - r = da->mtime < db->mtime ? -1 : 1; + r = da->mtime < db->mtime ? -1 : 1; else - r = stricmp( + r = strcasecmp( da->title[0] != '\0' ? da->title : da->name, db->title[0] != '\0' ? db->title : db->name ); @@ -1561,6 +1569,7 @@ return r; } + // Get a list of savegames FiosItem *FiosGetSavegameList(int *num, int mode) { @@ -1568,7 +1577,6 @@ HANDLE h; FiosItem *fios; int sort_start; - char buf[MAX_PATH]; if (_fios_save_path == NULL) { _fios_save_path = malloc(MAX_PATH); @@ -1581,13 +1589,13 @@ _fios_path = _fios_save_path; // Parent directory, only if not of the type C:\. - if (_fios_path[3] != 0) { + if (_fios_path[3] != '\0') { fios = FiosAlloc(); fios->type = FIOS_TYPE_PARENT; + fios->mtime = 0; strcpy(fios->title, ".. (Parent directory)"); } - // Show subdirectories first h = MyFindFirstFile(_fios_path, "*.*", &fd); if (h != INVALID_HANDLE_VALUE) { @@ -1597,8 +1605,10 @@ strcmp(fd.cFileName, "..") != 0) { fios = FiosAlloc(); fios->type = FIOS_TYPE_DIR; - strcpy(fios->name, fd.cFileName); - sprintf(fios->title, "\\%s (Directory)", fd.cFileName); + fios->mtime = 0; + ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); + snprintf(fios->title, lengthof(fios->title), + "%s\\ (Directory)", fd.cFileName); } } while (FindNextFile(h, &fd)); FindClose(h); @@ -1607,39 +1617,40 @@ // this is where to start sorting sort_start = _fios_count; - /* Show savegame files - * .SAV OpenTTD saved game - * .SS1 Transport Tycoon Deluxe preset game - * .SV1 Transport Tycoon Deluxe (Patch) saved game - * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game + /* Show savegame files + * .SAV OpenTTD saved game + * .SS1 Transport Tycoon Deluxe preset game + * .SV1 Transport Tycoon Deluxe (Patch) saved game + * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */ h = MyFindFirstFile(_fios_path, "*.*", &fd); if (h != INVALID_HANDLE_VALUE) { do { - if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { - char *t = strrchr(fd.cFileName, '.'); - if (t && !stricmp(t, ".SAV")) { // OpenTTD + char *t; + + if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; + + t = strrchr(fd.cFileName, '.'); + if (t != NULL && strcasecmp(t, ".sav") == 0) { // OpenTTD + fios = FiosAlloc(); + fios->type = FIOS_TYPE_FILE; + fios->mtime = *(uint64*)&fd.ftLastWriteTime; + fios->title[0] = '\0'; + ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); + } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { + if (t != NULL && ( + strcasecmp(t, ".ss1") == 0 || + strcasecmp(t, ".sv1") == 0 || + strcasecmp(t, ".sv2") == 0 + )) { // TTDLX(Patch) + char buf[MAX_PATH]; + fios = FiosAlloc(); + fios->type = FIOS_TYPE_OLDFILE; fios->mtime = *(uint64*)&fd.ftLastWriteTime; + ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); sprintf(buf, "%s\\%s", _fios_path, fd.cFileName); - fios->type = FIOS_TYPE_FILE; - fios->title[0] = 0; - ttd_strlcpy(fios->name, fd.cFileName, strlen(fd.cFileName)-3); - } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { - int ext = 0; // start of savegame extensions in _old_extensions[] - if (t != NULL && ( - (ext++, stricmp(t, ".SS1") == 0) || - (ext++, stricmp(t, ".SV1") == 0) || - (ext++, stricmp(t, ".SV2") == 0) - )) { // TTDLX(Patch) - fios = FiosAlloc(); - fios->old_extension = ext-1; - fios->mtime = *(uint64*)&fd.ftLastWriteTime; - sprintf(buf, "%s\\%s", _fios_path, fd.cFileName); - fios->type = FIOS_TYPE_OLDFILE; - GetOldSaveGameName(fios->title, buf); - ttd_strlcpy(fios->name, fd.cFileName, strlen(fd.cFileName)-3); - } + GetOldSaveGameName(fios->title, buf); } } } while (FindNextFile(h, &fd)); @@ -1651,18 +1662,17 @@ // Drives { char drives[256]; - char *s; + const char *s; + GetLogicalDriveStrings(sizeof(drives), drives); - s=drives; - while (*s) { + for (s = drives; *s != '\0';) { fios = FiosAlloc(); fios->type = FIOS_TYPE_DRIVE; - fios->title[0] = s[0]; - fios->title[1] = ':'; - fios->title[2] = 0; - while (*s++) {} + sprintf(fios->title, "%c:", s[0]); + while (*s++ != '\0') {} } } + *num = _fios_count; return _fios_items; } @@ -1674,7 +1684,6 @@ WIN32_FIND_DATA fd; HANDLE h; int sort_start; - char buf[MAX_PATH]; if (mode == SLD_NEW_GAME || _fios_scn_path == NULL) { if (_fios_scn_path == NULL) @@ -1688,6 +1697,7 @@ if (_fios_path[3] != '\0' && mode != SLD_NEW_GAME) { fios = FiosAlloc(); fios->type = FIOS_TYPE_PARENT; + fios->mtime = 0; strcpy(fios->title, ".. (Parent directory)"); } @@ -1700,8 +1710,10 @@ strcmp(fd.cFileName, "..") != 0) { fios = FiosAlloc(); fios->type = FIOS_TYPE_DIR; - strcpy(fios->name, fd.cFileName); - sprintf(fios->title, "\\%s (Directory)", fd.cFileName); + fios->mtime = 0; + ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); + snprintf(fios->title, lengthof(fios->title), + "%s\\ (Directory)", fd.cFileName); } } while (FindNextFile(h, &fd)); FindClose(h); @@ -1710,40 +1722,39 @@ // this is where to start sorting sort_start = _fios_count; - /* Show scenario files - * .SCN OpenTTD style scenario file - * .SV0 Transport Tycoon Deluxe (Patch) scenario - * .SS0 Transport Tycoon Deluxe preset scenario + /* Show scenario files + * .SCN OpenTTD style scenario file + * .SV0 Transport Tycoon Deluxe (Patch) scenario + * .SS0 Transport Tycoon Deluxe preset scenario */ h = MyFindFirstFile(_fios_scn_path, "*.*", &fd); if (h != INVALID_HANDLE_VALUE) { do { - if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { - char *t = strrchr(fd.cFileName, '.'); + char *t; - if (t != NULL && !stricmp(t, ".SCN")) { // OpenTTD + if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue; + + t = strrchr(fd.cFileName, '.'); + if (t != NULL && strcasecmp(t, ".scn") == 0) { // OpenTTD + fios = FiosAlloc(); + fios->type = FIOS_TYPE_SCENARIO; + fios->mtime = *(uint64*)&fd.ftLastWriteTime; + fios->title[0] = '\0'; + ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); + } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || + mode == SLD_NEW_GAME) { + if (t != NULL && ( + strcasecmp(t, ".sv0") == 0 || + strcasecmp(t, ".ss0") == 0 + )) { // TTDLX(Patch) + char buf[MAX_PATH]; + fios = FiosAlloc(); + fios->type = FIOS_TYPE_OLD_SCENARIO; fios->mtime = *(uint64*)&fd.ftLastWriteTime; sprintf(buf, "%s\\%s", _fios_path, fd.cFileName); - fios->type = FIOS_TYPE_SCENARIO; - fios->title[0] = '\0'; - ttd_strlcpy(fios->name, fd.cFileName, strlen(fd.cFileName)-3); - } 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 != NULL && ( - (ext++, stricmp(t, ".SV0") == 0) || - (ext++, stricmp(t, ".SS0") == 0) - )) { // TTDLX(Patch) - fios = FiosAlloc(); - fios->old_extension = ext-1; - fios->mtime = *(uint64*)&fd.ftLastWriteTime; - sprintf(buf, "%s\\%s", _fios_path, fd.cFileName); - fios->type = FIOS_TYPE_OLD_SCENARIO; - GetOldScenarioGameName(fios->title, buf); - ttd_strlcpy(fios->name, fd.cFileName, strlen(fd.cFileName)-3); - } + GetOldScenarioGameName(fios->title, buf); + ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); } } } while (FindNextFile(h, &fd)); @@ -1770,6 +1781,7 @@ return _fios_items; } + // Free the list of savegames void FiosFreeSavegameList(void) { @@ -1781,53 +1793,35 @@ // Browse to char *FiosBrowseTo(const FiosItem *item) { - static char str_buffr[512]; char *path = _fios_path; + char *s; switch (item->type) { - case FIOS_TYPE_DRIVE: - sprintf(path, "%c:\\", item->title[0]); - break; - - case FIOS_TYPE_PARENT: { - char *s; - - // Skip drive part - path += 3; - s = path; - for (; *path != '\0'; path++) { - if (*path== '\\') s = path; - } - *s = '\0'; - break; - } + case FIOS_TYPE_DRIVE: + sprintf(path, "%c:\\", item->title[0]); + break; - case FIOS_TYPE_DIR: - // Scan to end - while (*++path != '\0'); - // Add backslash? - if (path[-1] != '\\') *path++ = '\\'; - - strcpy(path, item->name); - break; - - case FIOS_TYPE_FILE: - FiosMakeSavegameName(str_buffr, item->name); - return str_buffr; + case FIOS_TYPE_PARENT: + s = strrchr(path, '\\'); + if (s != NULL) *s = '\0'; + break; - case FIOS_TYPE_OLDFILE: - sprintf(str_buffr, "%s\\%s.%s", - _fios_path, item->name, _old_extensions[item->old_extension]); - return str_buffr; + case FIOS_TYPE_DIR: + s = strchr(item->name, '\\'); + if (s != NULL) *s = '\0'; + strcat(path, "\\"); + strcat(path, item->name); + break; - case FIOS_TYPE_SCENARIO: - sprintf(str_buffr, "%s\\%s.scn", path, item->name); - return str_buffr; + case FIOS_TYPE_FILE: + case FIOS_TYPE_OLDFILE: + case FIOS_TYPE_SCENARIO: + case FIOS_TYPE_OLD_SCENARIO: { + static char str_buffr[512]; - case FIOS_TYPE_OLD_SCENARIO: - sprintf(str_buffr, "%s\\%s.%s", - path, item->name, _old_extensions[item->old_extension]); - return str_buffr; + sprintf(str_buffr, "%s\\%s", path, item->name); + return str_buffr; + } } return NULL;