(svn r2860) Fix some issues in the savegame/scenario list code:
authortron
Fri, 12 Aug 2005 06:37:48 +0000
changeset 2334 b4e5c353bf38
parent 2333 4c72a7c53c9a
child 2335 1222aa57deb7
(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
os2.c
unix.c
win32.c
--- a/os2.c	Thu Aug 11 21:51:05 2005 +0000
+++ b/os2.c	Fri Aug 12 06:37:48 2005 +0000
@@ -61,14 +61,6 @@
 }
 
 
-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)
 {
 	if (path[2] == '\\' && path[3] == '\0')
@@ -92,7 +84,7 @@
 		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:\.
 	if (_fios_path[3] != '\0') {
@@ -104,7 +96,7 @@
 	}
 
 	// Show subdirectories first
-	dir = my_opendir(_fios_path, "*.*");
+	dir = opendir(_fios_path);
 	if (dir != NULL) {
 		while ((dirent = readdir(dir)) != NULL) {
 			append_path(filename, _fios_path, dirent->d_name);
@@ -138,7 +130,7 @@
 	 * .SV1 Transport Tycoon Deluxe (Patch) saved game
 	 * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game
 	 */
-	dir = my_opendir(_fios_path, "*.*");
+	dir = opendir(_fios_path);
 	if (dir != NULL) {
 		while ((dirent = readdir(dir)) != NULL) {
 			char *t;
@@ -184,15 +176,13 @@
 		_dos_getdrive(&save);
 
 		/* get available drive letters */
-		for (disk = 1; disk < 27; ++disk)
-		{
+		for (disk = 1; disk < 27; ++disk) {
 			uint disk2;
 
 			_dos_setdrive(disk, &total);
 			_dos_getdrive(&disk2);
 
-			if (disk == disk2)
-			{
+			if (disk == disk2) {
 				fios = FiosAlloc();
 				fios->type = FIOS_TYPE_DRIVE;
 				sprintf(fios->name, "%c:", 'A' + disk - 1);
@@ -233,7 +223,7 @@
 	}
 
 	// Show subdirectories first
-	dir = my_opendir(_fios_path, "*.*");
+	dir = opendir(_fios_path);
 	if (dir != NULL) {
 		while ((dirent = readdir(dir)) != NULL) {
 			append_path(filename, _fios_path, dirent->d_name);
@@ -250,6 +240,14 @@
 		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
 	sort_start = _fios_count;
 
@@ -258,7 +256,7 @@
 	 * .SV0 Transport Tycoon Deluxe (Patch) scenario
 	 * .SS0 Transport Tycoon Deluxe preset scenario
 	 */
-	dir = my_opendir(_fios_path, "*.*");
+	dir = opendir(_fios_path);
 	if (dir != NULL) {
 		while ((dirent = readdir(dir)) != NULL) {
 			char *t;
@@ -295,8 +293,7 @@
 	qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems);
 
 	// Drives
-	if (mode != SLD_NEW_GAME)
-	{
+	if (mode != SLD_NEW_GAME) {
 		unsigned save, disk, disk2, total;
 
 		/* save original drive */
@@ -304,13 +301,11 @@
 
 		/* get available drive letters */
 
-		for (disk = 1; disk < 27; ++disk)
-		{
+		for (disk = 1; disk < 27; ++disk) {
 			_dos_setdrive(disk, &total);
 			_dos_getdrive(&disk2);
 
-			if (disk == disk2)
-			{
+			if (disk == disk2) {
 				fios = FiosAlloc();
 				fios->type = FIOS_TYPE_DRIVE;
 				sprintf(fios->name, "%c:", 'A' + disk - 1);
@@ -347,13 +342,14 @@
 
 		case FIOS_TYPE_PARENT:
 			s = strrchr(path, '\\');
-			if (s != NULL) *s = '\0';
+			if (s != path + 2)
+				s[0] = '\0';
+			else
+				s[1] = '\0';
 			break;
 
 		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);
 			break;
 
@@ -437,9 +433,10 @@
 
 	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;
 			}
--- a/unix.c	Thu Aug 11 21:51:05 2005 +0000
+++ b/unix.c	Fri Aug 12 06:37:48 2005 +0000
@@ -103,7 +103,7 @@
 	}
 
 	// Show subdirectories first
-	dir = opendir(_fios_path[0] != '\0' ? _fios_path : "/");
+	dir = opendir(_fios_path);
 	if (dir != NULL) {
 		while ((dirent = readdir(dir)) != NULL) {
 			snprintf(filename, lengthof(filename), "%s/%s",
@@ -138,7 +138,7 @@
 	 * .SV1 Transport Tycoon Deluxe (Patch) saved 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) {
 		while ((dirent = readdir(dir)) != NULL) {
 			char *t;
@@ -193,6 +193,7 @@
 		_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:\.
@@ -204,7 +205,7 @@
 	}
 
 	// Show subdirectories first
-	dir = opendir(_fios_path[0] ? _fios_path : "/");
+	dir = opendir(_fios_path);
 	if (dir != NULL) {
 		while ((dirent = readdir(dir)) != NULL) {
 			snprintf(filename, lengthof(filename), "%s/%s",
@@ -221,6 +222,14 @@
 		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
 	sort_start = _fios_count;
 
@@ -229,7 +238,7 @@
 	 * .SV0 Transport Tycoon Deluxe (Patch) scenario
 	 * .SS0 Transport Tycoon Deluxe preset scenario
 	 */
-	dir = opendir(_fios_path[0] ? _fios_path : "/");
+	dir = opendir(_fios_path);
 	if (dir != NULL) {
 		while ((dirent = readdir(dir)) != NULL) {
 			char *t;
@@ -244,7 +253,7 @@
 				fios->mtime = sb.st_mtime;
 				ttd_strlcpy(fios->name, dirent->d_name, lengthof(fios->name));
 
-				*t  = '\0'; // strip extension
+				*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) {
@@ -286,13 +295,14 @@
 	switch (item->type) {
 		case FIOS_TYPE_PARENT:
 			s = strrchr(path, '/');
-			if (s != NULL) *s = '\0';
+			if (s != path)
+				s[0] = '\0';
+			else
+				s[1] = '\0';
 			break;
 
 		case FIOS_TYPE_DIR:
-			s = strchr(item->name, '/');
-			if (s != NULL) *s = '\0';
-			strcat(path, "/");
+			if (path[1] != '\0') strcat(path, "/");
 			strcat(path, item->name);
 			break;
 
--- a/win32.c	Thu Aug 11 21:51:05 2005 +0000
+++ b/win32.c	Fri Aug 12 06:37:48 2005 +0000
@@ -647,6 +647,14 @@
 		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
 	sort_start = _fios_count;
 
@@ -672,7 +680,6 @@
 
 				*t = '\0'; // strip extension
 				ttd_strlcpy(fios->title, fd.cFileName, lengthof(fios->title));
-
 			} else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) {
 				if (t != NULL && (
 							strcasecmp(t, ".ss1") == 0 ||
@@ -754,6 +761,14 @@
 		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
 	sort_start = _fios_count;
 
@@ -776,9 +791,8 @@
 				fios->mtime = *(uint64*)&fd.ftLastWriteTime;
 				ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name));
 
-				*t  = '\0'; // strip extension
+				*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 (t != NULL && (
@@ -842,14 +856,14 @@
 
 		case FIOS_TYPE_PARENT:
 			s = strrchr(path, '\\');
-			if (s != NULL) *s = '\0';
-			if (path[2] == '\0' ) strcat(path, "\\");
+			if (s != path + 2)
+				s[0] = '\0';
+			else
+				s[1] = '\0';
 			break;
 
 		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);
 			break;