Index: win32.c =================================================================== --- win32.c (revision 3311) +++ win32.c (working copy) @@ -611,69 +611,18 @@ } #endif -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(void) +FiosItem *FiosGetFileList(char *path, int *num, FiosItem *(callback)(char *, char *, int), int mode) { - if (_fios_count == _fios_alloc) { - _fios_alloc += 256; - _fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem)); - } - return &_fios_items[_fios_count++]; -} - -static HANDLE MyFindFirstFile(const char *path, const char *file, WIN32_FIND_DATA *fd) -{ - UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box - HANDLE h; - char paths[MAX_PATH]; - const char *s = strrchr(path, '\\'); - - snprintf(paths, sizeof(paths), "%s%s%s", path, (s[1] == '\0') ? "" : "\\", file); - h = FindFirstFile(paths, fd); - - SetErrorMode(sem); // restore previous setting - return h; -} - -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 & SORT_BY_NAME) { - r = strcasecmp(da->title, db->title); - } else { - r = da->mtime < db->mtime ? -1 : 1; - } - - if (_savegame_sort_order & SORT_DESCENDING) r = -r; - return r; -} - - -// Get a list of savegames -FiosItem *FiosGetSavegameList(int *num, int mode) -{ WIN32_FIND_DATA fd; HANDLE h; FiosItem *fios; int sort_start; + char filename[MAX_PATH]; - if (_fios_save_path == NULL) { - _fios_save_path = malloc(MAX_PATH); - strcpy(_fios_save_path, _path.save_dir); - } + _fios_path = path; - _fios_path = _fios_save_path; - // Parent directory, only if not of the type C:\. - if (_fios_path[3] != '\0') { + if (_fios_path[3] != '\0' && mode != SLD_NEW_GAME) { fios = FiosAlloc(); fios->type = FIOS_TYPE_PARENT; fios->mtime = 0; @@ -721,31 +670,14 @@ char *t; if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; + + sprintf(filename, "%s\\%s", _fios_path, fd.cFileName); - t = strrchr(fd.cFileName, '.'); - if (t == NULL) continue; - - if (strcasecmp(t, ".sav") == 0) { // OpenTTD - fios = FiosAlloc(); - fios->type = FIOS_TYPE_FILE; - fios->mtime = *(uint64*)&fd.ftLastWriteTime; - ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); - - *t = '\0'; // strip extension - ttd_strlcpy(fios->title, fd.cFileName, lengthof(fios->title)); - } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { - if (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); - GetOldSaveGameName(fios->title, buf); - } + fios = callback(fd.cFileName, filename, mode); + if(fios == NULL) + continue; + + fios->mtime = *(uint64*)&fd.ftLastWriteTime; } } while (FindNextFile(h, &fd)); FindClose(h); @@ -772,128 +704,6 @@ return _fios_items; } -// Get a list of scenarios -FiosItem *FiosGetScenarioList(int *num, int mode) -{ - FiosItem *fios; - WIN32_FIND_DATA fd; - HANDLE h; - int sort_start; - - if (_fios_scn_path == NULL) { - _fios_scn_path = malloc(MAX_PATH); - strcpy(_fios_scn_path, _path.scenario_dir); - } - - _fios_path = _fios_scn_path; - - // Parent directory, only if not of the type C:\. - 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 - h = MyFindFirstFile(_fios_scn_path, "*.*", &fd); - if (h != INVALID_HANDLE_VALUE && mode != SLD_NEW_GAME) { - do { - if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && - strcmp(fd.cFileName, ".") != 0 && - strcmp(fd.cFileName, "..") != 0) { - fios = FiosAlloc(); - fios->type = FIOS_TYPE_DIR; - 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); - } - - { - /* XXX ugly global variables ... */ - byte order = _savegame_sort_order; - _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING; - qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems); - _savegame_sort_order = order; - } - - // 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 - */ - h = MyFindFirstFile(_fios_scn_path, "*.*", &fd); - if (h != INVALID_HANDLE_VALUE) { - do { - char *t; - - if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue; - - t = strrchr(fd.cFileName, '.'); - if (t == NULL) continue; - - if (strcasecmp(t, ".scn") == 0) { // OpenTTD - fios = FiosAlloc(); - fios->type = FIOS_TYPE_SCENARIO; - fios->mtime = *(uint64*)&fd.ftLastWriteTime; - ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); - - *t = '\0'; // strip extension - ttd_strlcpy(fios->title, fd.cFileName, lengthof(fios->title)); - } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || - mode == SLD_NEW_GAME) { - if (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); - GetOldScenarioGameName(fios->title, buf); - ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); - } - } - } while (FindNextFile(h, &fd)); - FindClose(h); - } - - qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); - - // Drives - if (mode != SLD_NEW_GAME) { - char drives[256]; - const char *s; - - GetLogicalDriveStrings(sizeof(drives), drives); - for (s = drives; *s != '\0';) { - fios = FiosAlloc(); - fios->type = FIOS_TYPE_DRIVE; - sprintf(fios->name, "%c:", s[0]); - sprintf(fios->title, "%c:", s[0]); - while (*s++ != '\0') {} - } - } - - *num = _fios_count; - return _fios_items; -} - - -// Free the list of savegames -void FiosFreeSavegameList(void) -{ - free(_fios_items); - _fios_items = NULL; - _fios_alloc = _fios_count = 0; -} - // Browse to char *FiosBrowseTo(const FiosItem *item) { Index: os2.c =================================================================== --- os2.c (revision 3311) +++ os2.c (working copy) @@ -30,38 +30,6 @@ 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(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 r; - - if (_savegame_sort_order & SORT_BY_NAME) { - r = strcasecmp(da->title, db->title); - } else { - r = da->mtime < db->mtime ? -1 : 1; - } - - if (_savegame_sort_order & SORT_DESCENDING) r = -r; - return r; -} - - static void append_path(char *out, const char *path, const char *file) { if (path[2] == '\\' && path[3] == '\0') @@ -70,8 +38,8 @@ sprintf(out, "%s\\%s", path, file); } -// Get a list of savegames -FiosItem *FiosGetSavegameList(int *num, int mode) + +FiosItem *FiosGetFileList(char *path, int *num, FiosItem *(callback)(char *, char *, int), int mode) { FiosItem *fios; DIR *dir; @@ -80,15 +48,10 @@ int sort_start; char filename[MAX_PATH]; - if (_fios_save_path == NULL) { - _fios_save_path = malloc(MAX_PATH); - strcpy(_fios_save_path, _path.save_dir); - } + _fios_path = path; - _fios_path = _fios_save_path; - // Parent directory, only if not of the type C:\. - if (_fios_path[3] != '\0') { + if (_fios_path[3] != '\0' && mode != SLD_NEW_GAM) { fios = FiosAlloc(); fios->type = FIOS_TYPE_PARENT; fios->mtime = 0; @@ -139,28 +102,12 @@ append_path(filename, _fios_path, dirent->d_name); if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue; - t = strrchr(dirent->d_name, '.'); - if (t == NULL) continue; - if (strcasecmp(t, ".sav") == 0) { // OpenTTD - fios = FiosAlloc(); - fios->type = FIOS_TYPE_FILE; - fios->mtime = sb.st_mtime; - ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); - - *t = '\0'; // strip extension - ttd_strlcpy(fios->title, dirent->d_name, lengthof(fios->title)); - } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { - if (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); - } - } + fios = callback(dirent->d_name, filename, mode); + if(fios == NULL) + continue; + + fios->mtime = sb.st_mtime; } closedir(dir); } @@ -198,138 +145,6 @@ return _fios_items; } -// Get a list of scenarios -FiosItem *FiosGetScenarioList(int *num, int mode) -{ - FiosItem *fios; - DIR *dir; - struct dirent *dirent; - struct stat sb; - int sort_start; - char filename[MAX_PATH]; - - if (_fios_scn_path == NULL) { - _fios_scn_path = malloc(MAX_PATH); - strcpy(_fios_scn_path, _path.scenario_dir); - } - - _fios_path = _fios_scn_path; - - // Parent directory, only if not of the type C:\. - 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 = opendir(_fios_path); - if (dir != NULL) { - while ((dirent = readdir(dir)) != NULL) { - append_path(filename, _fios_path, 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); - } - - { - /* XXX ugly global variables ... */ - byte order = _savegame_sort_order; - _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING; - qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems); - _savegame_sort_order = order; - } - - // 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 - */ - dir = opendir(_fios_path); - if (dir != NULL) { - while ((dirent = readdir(dir)) != NULL) { - char *t; - - append_path(filename, _fios_path, dirent->d_name); - if (stat(filename, &sb) || S_ISDIR(sb.st_mode)) continue; - - t = strrchr(dirent->d_name, '.'); - if (t == NULL) continue; - - if (strcasecmp(t, ".scn") == 0) { // OpenTTD - fios = FiosAlloc(); - fios->type = FIOS_TYPE_SCENARIO; - fios->mtime = sb.st_mtime; - ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); - - *t = '\0'; // strip extension - ttd_strlcpy(fios->title, dirent->d_name, lengthof(fios->title)); - } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || - mode == SLD_NEW_GAME) { - if (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)); - } - } - } - closedir(dir); - } - - qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); - - // Drives - if (mode != SLD_NEW_GAME) { - unsigned save, disk, disk2, total; - - /* save original drive */ - _dos_getdrive(&save); - - /* get available drive letters */ - - for (disk = 1; disk < 27; ++disk) { - _dos_setdrive(disk, &total); - _dos_getdrive(&disk2); - - if (disk == disk2) { - fios = FiosAlloc(); - fios->type = FIOS_TYPE_DRIVE; - sprintf(fios->name, "%c:", 'A' + disk - 1); - sprintf(fios->title, "%c:", 'A' + disk - 1); - } - } - - _dos_setdrive(save, &total); - } - - *num = _fios_count; - return _fios_items; -} - - -// Free the list of savegames -void FiosFreeSavegameList(void) -{ - free(_fios_items); - _fios_items = NULL; - _fios_alloc = _fios_count = 0; -} - // Browse to char *FiosBrowseTo(const FiosItem *item) { Index: openttd.dsp =================================================================== --- openttd.dsp (revision 3311) +++ openttd.dsp (working copy) @@ -217,6 +217,10 @@ # End Source File # Begin Source File +SOURCE=.\fios.c +# End Source File +# Begin Source File + SOURCE=.\gfx.c # End Source File # Begin Source File Index: unix.c =================================================================== --- unix.c (revision 3311) +++ unix.c (working copy) @@ -8,6 +8,7 @@ #include "table/strings.h" #include "hal.h" #include "variables.h" +#include "fios.h" #include #include @@ -49,41 +50,8 @@ #endif #endif -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(void) +FiosItem *FiosGetFileList(char *path, int *num, FiosItem *(callback)(char *, char *, int), int mode) { - 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 r; - - if (_savegame_sort_order & SORT_BY_NAME) { - r = strcasecmp(da->title, db->title); - } else { - r = da->mtime < db->mtime ? -1 : 1; - } - - if (_savegame_sort_order & SORT_DESCENDING) r = -r; - return r; -} - - -// Get a list of savegames -FiosItem *FiosGetSavegameList(int *num, int mode) -{ FiosItem *fios; DIR *dir; struct dirent *dirent; @@ -91,15 +59,10 @@ int sort_start; char filename[MAX_PATH]; - if (_fios_save_path == NULL) { - _fios_save_path = malloc(MAX_PATH); - strcpy(_fios_save_path, _path.save_dir); - } + _fios_path = path; - _fios_path = _fios_save_path; - // Parent directory, only if not in root already. - if (_fios_path[1] != '\0') { + if (_fios_path[1] != '\0' && mode != SLD_NEW_GAME) { fios = FiosAlloc(); fios->type = FIOS_TYPE_PARENT; fios->mtime = 0; @@ -137,43 +100,18 @@ // 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 - */ dir = opendir(_fios_path); if (dir != NULL) { while ((dirent = readdir(dir)) != NULL) { - char *t; - 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) continue; - - if (strcasecmp(t, ".sav") == 0) { // OpenTTD - fios = FiosAlloc(); - fios->type = FIOS_TYPE_FILE; - fios->mtime = sb.st_mtime; - ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); - - *t = '\0'; // strip extension - ttd_strlcpy(fios->title, dirent->d_name, lengthof(fios->title)); - } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { - if (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); - } - } + + fios = callback(dirent->d_name, filename, mode); + if(fios == NULL) + continue; + + fios->mtime = sb.st_mtime; } closedir(dir); } @@ -181,116 +119,9 @@ qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); *num = _fios_count; return _fios_items; -} -// Get a list of scenarios -// FIXME: Gross code duplication with FiosGetSavegameList() -FiosItem *FiosGetScenarioList(int *num, int mode) -{ - FiosItem *fios; - DIR *dir; - struct dirent *dirent; - struct stat sb; - int sort_start; - char filename[MAX_PATH]; - - if (_fios_scn_path == NULL) { - _fios_scn_path = malloc(MAX_PATH); - strcpy(_fios_scn_path, _path.scenario_dir); - } - - _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); - if (dir != NULL) { - 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->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); - } - - { - /* XXX ugly global variables ... */ - byte order = _savegame_sort_order; - _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING; - qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems); - _savegame_sort_order = order; - } - - // 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 - */ - dir = opendir(_fios_path); - if (dir != NULL) { - while ((dirent = readdir(dir)) != NULL) { - char *t; - - 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) continue; - - if (strcasecmp(t, ".scn") == 0) { // OpenTTD - fios = FiosAlloc(); - fios->type = FIOS_TYPE_SCENARIO; - fios->mtime = sb.st_mtime; - ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name)); - - *t = '\0'; // strip extension - ttd_strlcpy(fios->title, dirent->d_name, lengthof(fios->title)); - } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || - mode == SLD_NEW_GAME) { - if (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)); - } - } - } - closedir(dir); - } - - qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); - *num = _fios_count; - return _fios_items; } - -// Free the list of savegames -void FiosFreeSavegameList(void) -{ - free(_fios_items); - _fios_items = NULL; - _fios_alloc = _fios_count = 0; -} - // Browse to char *FiosBrowseTo(const FiosItem *item) { Index: Makefile =================================================================== --- Makefile (revision 3311) +++ Makefile (working copy) @@ -647,6 +648,7 @@ SRCS += engine.c SRCS += engine_gui.c SRCS += fileio.c +SRCS += fios.c SRCS += gfx.c SRCS += gfxinit.c SRCS += graph_gui.c Index: openttd.vcproj =================================================================== --- openttd.vcproj (revision 3311) +++ openttd.vcproj (working copy) @@ -222,6 +222,9 @@ RelativePath=".\fileio.c"> + + title, db->title); + } else { + r = da->mtime < db->mtime ? -1 : 1; + } + + if (_savegame_sort_order & SORT_DESCENDING) r = -r; + return r; +} + +// Free the list of savegames +void FiosFreeSavegameList(void) +{ + free(_fios_items); + _fios_items = NULL; + _fios_alloc = _fios_count = 0; +} + + + + +static FiosItem *FiosGetSavegameCallback(char *file, char *filename, int mode) +{ + char *t; + FiosItem *fios = NULL; + + t = strrchr(file, '.'); + if (t == NULL) + return NULL; + + if (strcasecmp(t, ".sav") == 0) { // OpenTTD + fios = FiosAlloc(); + fios->type = FIOS_TYPE_FILE; + ttd_strlcpy(fios->name, file, lengthof(fios->name)); + + *t = '\0'; // strip extension + ttd_strlcpy(fios->title, file, lengthof(fios->title)); + } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { + if (strcasecmp(t, ".ss1") == 0 || strcasecmp(t, ".sv1") == 0 || strcasecmp(t, ".sv2") == 0) { // TTDLX(Patch) + fios = FiosAlloc(); + fios->type = FIOS_TYPE_OLDFILE; + ttd_strlcpy(fios->name, file, lengthof(fios->name)); + GetOldSaveGameName(fios->title, filename); + } + } + + return fios; +} + +// Get a list of savegames +FiosItem *FiosGetSavegameList(int *num, int mode) +{ + if (_fios_save_path == NULL) { + _fios_save_path = malloc(MAX_PATH); + strcpy(_fios_save_path, _path.save_dir); + } + + return FiosGetFileList(_fios_save_path, num, FiosGetSavegameCallback, mode); +} + +static FiosItem *FiosGetScenarioCallback(char *file, char *filename, int mode) +{ + char *t; + FiosItem *fios = NULL; + + t = strrchr(file, '.'); + if (t == NULL) + return NULL; + + if (strcasecmp(t, ".scn") == 0) { // OpenTTD + fios = FiosAlloc(); + fios->type = FIOS_TYPE_SCENARIO; + ttd_strlcpy(fios->name, file, lengthof(fios->name)); + + *t = '\0'; // strip extension + ttd_strlcpy(fios->title, file, lengthof(fios->title)); + } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || mode == SLD_NEW_GAME) { + if (strcasecmp(t, ".sv0") == 0 || strcasecmp(t, ".ss0") == 0) { // TTDLX(Patch) + fios = FiosAlloc(); + fios->type = FIOS_TYPE_OLD_SCENARIO; + GetOldScenarioGameName(fios->title, filename); + ttd_strlcpy(fios->name, file, lengthof(fios->name)); + } + } + + return fios; +} + +// Get a list of scenarios +FiosItem *FiosGetScenarioList(int *num, int mode) +{ + if (_fios_scn_path == NULL) { + _fios_scn_path = malloc(MAX_PATH); + strcpy(_fios_scn_path, _path.scenario_dir); + } + + return FiosGetFileList(_fios_scn_path, num, FiosGetScenarioCallback, mode); +} + + + + Index: fios.h =================================================================== --- fios.h (revision 0) +++ fios.h (revision 0) @@ -0,0 +1,12 @@ + +#ifndef FIOS_H +#define FIOS_H + +extern char *_fios_path; +extern int _fios_count; +extern FiosItem *_fios_items; + +FiosItem *FiosGetFileList(char *path, int *num, FiosItem *(callback)(char *, char *, int), int mode); +FiosItem *FiosAlloc(void); + +#endif