1
0
Fork 0

(svn r2860) Fix some issues in the savegame/scenario list code:

-Fix: Sort the directories when making a scenario list
-Fix: Sort the directories when making a savegame list on Windows
-Fix: On OS/2 show the trailing \ if the current directory is a root directory
-Regression: On OS/2 the savegame list showed the scenario directory or crashed (probably introduced in r2609)
The rest is diff reduction between the 3 variants
release/0.4.5
tron 2005-08-12 06:37:48 +00:00
parent 7e702e2d50
commit bdff0fa2d2
3 changed files with 67 additions and 46 deletions

53
os2.c
View File

@ -61,14 +61,6 @@ int compare_FiosItems(const void *a, const void *b)
} }
static DIR *my_opendir(char *path, char *file)
{
char paths[MAX_PATH];
append_path(paths, path, file);
return opendir(paths);
}
static void append_path(char *out, const char *path, const 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')
@ -92,7 +84,7 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
strcpy(_fios_save_path, _path.save_dir); strcpy(_fios_save_path, _path.save_dir);
} }
_fios_path = _fios_scn_path; _fios_path = _fios_save_path;
// Parent directory, only if not of the type C:\. // Parent directory, only if not of the type C:\.
if (_fios_path[3] != '\0') { if (_fios_path[3] != '\0') {
@ -104,7 +96,7 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
} }
// Show subdirectories first // Show subdirectories first
dir = my_opendir(_fios_path, "*.*"); dir = opendir(_fios_path);
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir)) != NULL) { while ((dirent = readdir(dir)) != NULL) {
append_path(filename, _fios_path, dirent->d_name); append_path(filename, _fios_path, dirent->d_name);
@ -138,7 +130,7 @@ 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 = my_opendir(_fios_path, "*.*"); dir = opendir(_fios_path);
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir)) != NULL) { while ((dirent = readdir(dir)) != NULL) {
char *t; char *t;
@ -184,15 +176,13 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
_dos_getdrive(&save); _dos_getdrive(&save);
/* get available drive letters */ /* get available drive letters */
for (disk = 1; disk < 27; ++disk) for (disk = 1; disk < 27; ++disk) {
{
uint disk2; uint disk2;
_dos_setdrive(disk, &total); _dos_setdrive(disk, &total);
_dos_getdrive(&disk2); _dos_getdrive(&disk2);
if (disk == disk2) if (disk == disk2) {
{
fios = FiosAlloc(); fios = FiosAlloc();
fios->type = FIOS_TYPE_DRIVE; fios->type = FIOS_TYPE_DRIVE;
sprintf(fios->name, "%c:", 'A' + disk - 1); sprintf(fios->name, "%c:", 'A' + disk - 1);
@ -233,7 +223,7 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
} }
// Show subdirectories first // Show subdirectories first
dir = my_opendir(_fios_path, "*.*"); dir = opendir(_fios_path);
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir)) != NULL) { while ((dirent = readdir(dir)) != NULL) {
append_path(filename, _fios_path, dirent->d_name); append_path(filename, _fios_path, dirent->d_name);
@ -250,6 +240,14 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
closedir(dir); closedir(dir);
} }
{
/* XXX ugly global variables ... */
byte order = _savegame_sort_order;
_savegame_sort_order = 2; // sort ascending by name
qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems);
_savegame_sort_order = order;
}
// this is where to start sorting // this is where to start sorting
sort_start = _fios_count; sort_start = _fios_count;
@ -258,7 +256,7 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
* .SV0 Transport Tycoon Deluxe (Patch) scenario * .SV0 Transport Tycoon Deluxe (Patch) scenario
* .SS0 Transport Tycoon Deluxe preset scenario * .SS0 Transport Tycoon Deluxe preset scenario
*/ */
dir = my_opendir(_fios_path, "*.*"); dir = opendir(_fios_path);
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir)) != NULL) { while ((dirent = readdir(dir)) != NULL) {
char *t; char *t;
@ -295,8 +293,7 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems);
// Drives // Drives
if (mode != SLD_NEW_GAME) if (mode != SLD_NEW_GAME) {
{
unsigned save, disk, disk2, total; unsigned save, disk, disk2, total;
/* save original drive */ /* save original drive */
@ -304,13 +301,11 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
/* get available drive letters */ /* get available drive letters */
for (disk = 1; disk < 27; ++disk) for (disk = 1; disk < 27; ++disk) {
{
_dos_setdrive(disk, &total); _dos_setdrive(disk, &total);
_dos_getdrive(&disk2); _dos_getdrive(&disk2);
if (disk == disk2) if (disk == disk2) {
{
fios = FiosAlloc(); fios = FiosAlloc();
fios->type = FIOS_TYPE_DRIVE; fios->type = FIOS_TYPE_DRIVE;
sprintf(fios->name, "%c:", 'A' + disk - 1); sprintf(fios->name, "%c:", 'A' + disk - 1);
@ -347,12 +342,13 @@ char *FiosBrowseTo(const FiosItem *item)
case FIOS_TYPE_PARENT: case FIOS_TYPE_PARENT:
s = strrchr(path, '\\'); s = strrchr(path, '\\');
if (s != NULL) *s = '\0'; if (s != path + 2)
s[0] = '\0';
else
s[1] = '\0';
break; break;
case FIOS_TYPE_DIR: case FIOS_TYPE_DIR:
s = strchr(item->name, '\\');
if (s != NULL) *s = '\0';
if (path[3] != '\0') strcat(path, "\\"); if (path[3] != '\0') strcat(path, "\\");
strcat(path, item->name); strcat(path, item->name);
break; break;
@ -437,9 +433,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;
} }

26
unix.c
View File

@ -103,7 +103,7 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
} }
// Show subdirectories first // Show subdirectories first
dir = opendir(_fios_path[0] != '\0' ? _fios_path : "/"); dir = opendir(_fios_path);
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir)) != NULL) { while ((dirent = readdir(dir)) != NULL) {
snprintf(filename, lengthof(filename), "%s/%s", snprintf(filename, lengthof(filename), "%s/%s",
@ -138,7 +138,7 @@ 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] != '\0' ? _fios_path : "/"); dir = opendir(_fios_path);
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir)) != NULL) { while ((dirent = readdir(dir)) != NULL) {
char *t; char *t;
@ -193,6 +193,7 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
_fios_scn_path = malloc(MAX_PATH); _fios_scn_path = malloc(MAX_PATH);
strcpy(_fios_scn_path, _path.scenario_dir); strcpy(_fios_scn_path, _path.scenario_dir);
} }
_fios_path = _fios_scn_path; _fios_path = _fios_scn_path;
// Parent directory, only if not of the type C:\. // Parent directory, only if not of the type C:\.
@ -204,7 +205,7 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
} }
// Show subdirectories first // Show subdirectories first
dir = opendir(_fios_path[0] ? _fios_path : "/"); dir = opendir(_fios_path);
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir)) != NULL) { while ((dirent = readdir(dir)) != NULL) {
snprintf(filename, lengthof(filename), "%s/%s", snprintf(filename, lengthof(filename), "%s/%s",
@ -221,6 +222,14 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
closedir(dir); closedir(dir);
} }
{
/* XXX ugly global variables ... */
byte order = _savegame_sort_order;
_savegame_sort_order = 2; // sort ascending by name
qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems);
_savegame_sort_order = order;
}
// this is where to start sorting // this is where to start sorting
sort_start = _fios_count; sort_start = _fios_count;
@ -229,7 +238,7 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
* .SV0 Transport Tycoon Deluxe (Patch) scenario * .SV0 Transport Tycoon Deluxe (Patch) scenario
* .SS0 Transport Tycoon Deluxe preset scenario * .SS0 Transport Tycoon Deluxe preset scenario
*/ */
dir = opendir(_fios_path[0] ? _fios_path : "/"); dir = opendir(_fios_path);
if (dir != NULL) { if (dir != NULL) {
while ((dirent = readdir(dir)) != NULL) { while ((dirent = readdir(dir)) != NULL) {
char *t; char *t;
@ -286,13 +295,14 @@ char *FiosBrowseTo(const FiosItem *item)
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 != path)
s[0] = '\0';
else
s[1] = '\0';
break; break;
case FIOS_TYPE_DIR: case FIOS_TYPE_DIR:
s = strchr(item->name, '/'); if (path[1] != '\0') strcat(path, "/");
if (s != NULL) *s = '\0';
strcat(path, "/");
strcat(path, item->name); strcat(path, item->name);
break; break;

26
win32.c
View File

@ -647,6 +647,14 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
FindClose(h); FindClose(h);
} }
{
/* XXX ugly global variables ... */
byte order = _savegame_sort_order;
_savegame_sort_order = 2; // sort ascending by name
qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems);
_savegame_sort_order = order;
}
// this is where to start sorting // this is where to start sorting
sort_start = _fios_count; sort_start = _fios_count;
@ -672,7 +680,6 @@ FiosItem *FiosGetSavegameList(int *num, int mode)
*t = '\0'; // strip extension *t = '\0'; // strip extension
ttd_strlcpy(fios->title, fd.cFileName, lengthof(fios->title)); ttd_strlcpy(fios->title, fd.cFileName, lengthof(fios->title));
} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
if (t != NULL && ( if (t != NULL && (
strcasecmp(t, ".ss1") == 0 || strcasecmp(t, ".ss1") == 0 ||
@ -754,6 +761,14 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
FindClose(h); FindClose(h);
} }
{
/* XXX ugly global variables ... */
byte order = _savegame_sort_order;
_savegame_sort_order = 2; // sort ascending by name
qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems);
_savegame_sort_order = order;
}
// this is where to start sorting // this is where to start sorting
sort_start = _fios_count; sort_start = _fios_count;
@ -778,7 +793,6 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
*t = '\0'; // strip extension *t = '\0'; // strip extension
ttd_strlcpy(fios->title, fd.cFileName, lengthof(fios->title)); ttd_strlcpy(fios->title, fd.cFileName, lengthof(fios->title));
} 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) {
if (t != NULL && ( if (t != NULL && (
@ -842,13 +856,13 @@ char *FiosBrowseTo(const FiosItem *item)
case FIOS_TYPE_PARENT: case FIOS_TYPE_PARENT:
s = strrchr(path, '\\'); s = strrchr(path, '\\');
if (s != NULL) *s = '\0'; if (s != path + 2)
if (path[2] == '\0' ) strcat(path, "\\"); s[0] = '\0';
else
s[1] = '\0';
break; break;
case FIOS_TYPE_DIR: case FIOS_TYPE_DIR:
s = strchr(item->name, '\\');
if (s != NULL) *s = '\0';
if (path[3] != '\0') strcat(path, "\\"); if (path[3] != '\0') strcat(path, "\\");
strcat(path, item->name); strcat(path, item->name);
break; break;