(svn r5766) - Cleanup: Unify FiosBrowseTo and FiosGetDescText
authorDarkvater
Sat, 05 Aug 2006 00:53:09 +0000
changeset 4222 951fbf19b1ad
parent 4221 bb06fe8dcce2
child 4223 0f9535dda7ed
(svn r5766) - Cleanup: Unify FiosBrowseTo and FiosGetDescText
fios.c
os2.c
unix.c
win32.c
--- a/fios.c	Sat Aug 05 00:47:32 2006 +0000
+++ b/fios.c	Sat Aug 05 00:53:09 2006 +0000
@@ -31,6 +31,7 @@
 extern bool FiosIsRoot(const char *path);
 extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
 extern void FiosGetDrives(void);
+extern bool FiosGetDiskFreeSpace(const char *path, uint32 *tot);
 
 /* get the name of an oldstyle savegame */
 extern void GetOldSaveGameName(char *title, const char *path, const char *file);
@@ -80,6 +81,76 @@
 	_fios_alloc = _fios_count = 0;
 }
 
+/**
+ * Get descriptive texts. Returns the path and free space
+ * left on the device
+ * @param path string describing the path
+ * @param total_free total free space in megabytes, optional (can be NULL)
+ * @return StringID describing the path (free space or failure)
+ */
+StringID FiosGetDescText(const char **path, uint32 *total_free)
+{
+	*path = _fios_path;
+	return FiosGetDiskFreeSpace(*path, total_free) ? STR_4005_BYTES_FREE : STR_4006_UNABLE_TO_READ_DRIVE;
+}
+
+/* Browse to a new path based on the passed FiosItem struct
+ * @param *item FiosItem object telling us what to do
+ * @return a string if we have given a file as a target, otherwise NULL */
+char *FiosBrowseTo(const FiosItem *item)
+{
+	char *s;
+	char *path = _fios_path;
+
+	switch (item->type) {
+#if defined(WIN32) || defined(__OS2__)
+	case FIOS_TYPE_DRIVE: sprintf(path, "%c:" PATHSEP, item->title[0]); break;
+#endif
+
+	case FIOS_TYPE_PARENT:
+		/* Check for possible NULL ptr (not required for UNIXes, but AmigaOS-alikes) */
+		if ((s = strrchr(path, PATHSEPCHAR)) != NULL) {
+			s[1] = '\0'; // go up a directory
+			if (!FiosIsRoot(path)) s[0] = '\0';
+		}
+#if defined(__MORPHOS__) || defined(__AMIGAOS__)
+		/* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
+		else if ((s = strrchr(path, ':')) != NULL) s[1] = '\0';
+#endif
+		break;
+
+	case FIOS_TYPE_DIR:
+		if (!FiosIsRoot(path)) strcat(path, PATHSEP);
+		strcat(path, item->name);
+		break;
+
+	case FIOS_TYPE_DIRECT:
+		sprintf(path, "%s" PATHSEP, item->name);
+		s = strrchr(path, PATHSEPCHAR);
+		if (s != NULL && s[1] == '\0') s[0] = '\0'; // strip trailing slash
+		break;
+
+	case FIOS_TYPE_FILE:
+	case FIOS_TYPE_OLDFILE:
+	case FIOS_TYPE_SCENARIO:
+	case FIOS_TYPE_OLD_SCENARIO: {
+		static char str_buffr[512];
+
+#if defined(__MORPHOS__) || defined(__AMIGAOS__)
+		/* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
+		if (FiosIsRoot(path)) {
+			snprintf(str_buffr, lengthof(str_buffr), "%s:%s", path, item->name);
+		} else // XXX - only next line!
+#endif
+		snprintf(str_buffr, lengthof(str_buffr), "%s" PATHSEP "%s", path, item->name);
+
+		return str_buffr;
+	}
+	}
+
+	return NULL;
+}
+
 void FiosMakeSavegameName(char *buf, const char *name, size_t size)
 {
 	const char *extension, *period;
--- a/os2.c	Sat Aug 05 00:47:32 2006 +0000
+++ b/os2.c	Sat Aug 05 00:53:09 2006 +0000
@@ -58,6 +58,19 @@
 	_dos_setdrive(save, &total); // restore the original drive
 }
 
+bool FiosGetDiskFreeSpace(const char *path, uint32 *tot)
+{
+	struct diskfree_t free;
+	char drive = path[0] - 'A' + 1;
+
+	if (tot != NULL && _getdiskfree(drive, &free) == 0) {
+		*tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
+		return true;
+	}
+
+	return false;
+}
+
 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
 {
 	char filename[MAX_PATH];
@@ -68,74 +81,6 @@
 	return (ent->d_name[0] != '.'); // hidden file
 }
 
-// Browse to
-char *FiosBrowseTo(const FiosItem *item)
-{
-	char *path = _fios_path;
-	char *s;
-
-	switch (item->type) {
-		case FIOS_TYPE_DRIVE:
-			sprintf(path, "%c:\\", item->title[0]);
-			break;
-
-		case FIOS_TYPE_PARENT:
-			s = strrchr(path, '\\');
-			if (s != path + 2) {
-				s[0] = '\0';
-			} else {
-				s[1] = '\0';
-			}
-			break;
-
-		case FIOS_TYPE_DIR:
-			if (path[3] != '\0') strcat(path, "\\");
-			strcat(path, item->name);
-			break;
-
-		case FIOS_TYPE_DIRECT:
-			sprintf(path, "%s\\", item->name);
-			s = strrchr(path, '\\');
-			if (s[1] == '\0') s[0] = '\0'; // strip trailing slash
-			break;
-
-		case FIOS_TYPE_FILE:
-		case FIOS_TYPE_OLDFILE:
-		case FIOS_TYPE_SCENARIO:
-		case FIOS_TYPE_OLD_SCENARIO: {
-			static char str_buffr[512];
-
-			sprintf(str_buffr, "%s\\%s", path, item->name);
-			return str_buffr;
-		}
-	}
-
-	return NULL;
-}
-
-/**
- * Get descriptive texts. Returns the path and free space
- * left on the device
- * @param path string describing the path
- * @param tfs total free space in megabytes, optional (can be NULL)
- * @return StringID describing the path (free space or failure)
- */
-StringID FiosGetDescText(const char **path, uint32 *tot)
-{
-	struct diskfree_t free;
-	char drive;
-
-	*path = _fios_path;
-	drive = *path[0] - 'A' + 1;
-
-	if (tot != NULL && _getdiskfree(drive, &free) == 0) {
-		*tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
-		return STR_4005_BYTES_FREE;
-	}
-
-	return STR_4006_UNABLE_TO_READ_DRIVE;
-}
-
 static void ChangeWorkingDirectory(char *exe)
 {
 	char *s = strrchr(exe, '\\');
--- a/unix.c	Sat Aug 05 00:47:32 2006 +0000
+++ b/unix.c	Sat Aug 05 00:53:09 2006 +0000
@@ -52,20 +52,6 @@
 extern FiosItem *_fios_items;
 extern int _fios_count, _fios_alloc;
 
-#if !defined(__MORPHOS__) && !defined(__AMIGAOS__)
-#define ISROOT(__p)  (__p[1] == '\0')
-#define PATHTEMPLATE "%s/%s"
-#else
-/*  on MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory".
- *  This is some evil magic which tries to handle this transparently w/o
- *  disturbing code with too much #ifdefs. It's not possible to switch the
- *  volume, but at least it doesn't crash :) (tokai)
- */
-static bool __isroot; /* not very thread save, but will do in this case */
-#define ISROOT(__p)  (__isroot = (__p[strlen(__p)-1] == ':'))
-#define PATHTEMPLATE (__isroot ? "%s:%s" : "%s/%s")
-#endif
-
 bool FiosIsRoot(const char *path)
 {
 #if !defined(__MORPHOS__) && !defined(__AMIGAOS__)
@@ -82,6 +68,22 @@
 	return;
 }
 
+bool FiosGetDiskFreeSpace(const char *path, uint32 *tot)
+{
+	uint32 free = 0;
+
+#ifdef HAS_STATVFS
+	{
+		struct statvfs s;
+
+		if (statvfs(path, &s) != 0) return false;
+		free = (uint64)s.f_frsize * s.f_bavail >> 20;
+	}
+#endif
+	if (tot != NULL) *tot = free;
+	return true;
+}
+
 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
 {
 	char filename[MAX_PATH];
@@ -99,87 +101,6 @@
 	return (ent->d_name[0] != '.'); // hidden file
 }
 
-// Browse to
-char *FiosBrowseTo(const FiosItem *item)
-{
-	char *path = _fios_path;
-	char *s;
-
-	switch (item->type) {
-		case FIOS_TYPE_PARENT:
-			/* Check for possible NULL ptr (not required for UNIXes, but AmigaOS-alikes) */
-			if ((s = strrchr(path, '/'))) {
-				if (s != path) {
-					s[0] = '\0';
-				} else {
-					s[1] = '\0';
-				}
-			}
-#if defined(__MORPHOS__) || defined(__AMIGAOS__)
-			else {
-				if ((s = strrchr(path, ':'))) {
-					s[1] = '\0';
-				}
-			}
-#endif
-			break;
-
-		case FIOS_TYPE_DIR:
-			if (!ISROOT(path)) strcat(path, "/");
-			strcat(path, item->name);
-			break;
-
-		case FIOS_TYPE_DIRECT:
-			sprintf(path, "%s/", item->name);
-			s = strrchr(path, '/');
-			if (s[1] == '\0') s[0] = '\0'; // strip trailing slash
-			break;
-
-		case FIOS_TYPE_FILE:
-		case FIOS_TYPE_OLDFILE:
-		case FIOS_TYPE_SCENARIO:
-		case FIOS_TYPE_OLD_SCENARIO: {
-			static char str_buffr[512];
-
-#if defined(__MORPHOS__) || defined(__AMIGAOS__)
-			ISROOT(path); /* init __isroot for PATHTEMPLATE */
-#endif
-
-			sprintf(str_buffr, PATHTEMPLATE, path, item->name);
-			return str_buffr;
-		}
-	}
-
-	return NULL;
-}
-
-/**
- * Get descriptive texts. Returns the path and free space
- * left on the device
- * @param path string describing the path
- * @param tfs total free space in megabytes, optional (can be NULL)
- * @return StringID describing the path (free space or failure)
- */
-StringID FiosGetDescText(const char **path, uint32 *tot)
-{
-	uint32 free = 0;
-	*path = _fios_path;
-
-#ifdef HAS_STATVFS
-	{
-		struct statvfs s;
-
-		if (statvfs(*path, &s) == 0) {
-			free = (uint64)s.f_frsize * s.f_bavail >> 20;
-		} else {
-			return STR_4006_UNABLE_TO_READ_DRIVE;
-		}
-	}
-#endif
-	if (tot != NULL) *tot = free;
-	return STR_4005_BYTES_FREE;
-}
-
 #if defined(__BEOS__) || defined(__linux__)
 static void ChangeWorkingDirectory(char *exe)
 {
--- a/win32.c	Sat Aug 05 00:47:32 2006 +0000
+++ b/win32.c	Sat Aug 05 00:53:09 2006 +0000
@@ -8,7 +8,6 @@
 #include "macros.h"
 #include "saveload.h"
 #include "string.h"
-#include "table/strings.h"
 #include "gfx.h"
 #include "window.h"
 #include <windows.h>
@@ -743,77 +742,21 @@
 	return true;
 }
 
-// Browse to
-char *FiosBrowseTo(const FiosItem *item)
-{
-	char *path = _fios_path;
-	char *s;
-
-	switch (item->type) {
-		case FIOS_TYPE_DRIVE:
-			sprintf(path, "%c:\\", item->title[0]);
-			break;
-
-		case FIOS_TYPE_PARENT:
-			s = strrchr(path, '\\');
-			if (s != path + 2) {
-				s[0] = '\0';
-			} else {
-				s[1] = '\0';
-			}
-			break;
-
-		case FIOS_TYPE_DIR:
-			if (path[3] != '\0') strcat(path, "\\");
-			strcat(path, item->name);
-			break;
-
-		case FIOS_TYPE_DIRECT:
-			sprintf(path, "%s\\", item->name);
-			s = strrchr(path, '\\');
-			if (s[1] == '\0') s[0] = '\0'; // strip trailing slash
-			break;
-
-		case FIOS_TYPE_FILE:
-		case FIOS_TYPE_OLDFILE:
-		case FIOS_TYPE_SCENARIO:
-		case FIOS_TYPE_OLD_SCENARIO: {
-			static char str_buffr[512];
-
-			sprintf(str_buffr, "%s\\%s", path, item->name);
-			return str_buffr;
-		}
-	}
-
-	return NULL;
-}
-
-/**
- * Get descriptive texts. Returns the path and free space
- * left on the device
- * @param path string describing the path
- * @param tfs total free space in megabytes, optional (can be NULL)
- * @return StringID describing the path (free space or failure)
- */
-StringID FiosGetDescText(const char **path, uint32 *tot)
+bool FiosGetDiskFreeSpace(const char *path, uint32 *tot)
 {
 	UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS);  // disable 'no-disk' message box
+	bool retval = false;
 	char root[4];
 	DWORD spc, bps, nfc, tnc;
-	StringID sid;
 
-	*path = _fios_path;
-
-	sprintf(root, "%c:\\", _fios_path[0]);
+	snprintf(root, lengthof(root), "%c:" PATHSEP, path[0]);
 	if (tot != NULL && GetDiskFreeSpace(root, &spc, &bps, &nfc, &tnc)) {
 		*tot = ((spc * bps) * (uint64)nfc) >> 20;
-		sid = STR_4005_BYTES_FREE;
-	} else {
-		sid = STR_4006_UNABLE_TO_READ_DRIVE;
+		retval = true;
 	}
 
 	SetErrorMode(sem); // reset previous setting
-	return sid;
+	return retval;
 }
 
 static int ParseCommandLine(char *line, char **argv, int max_argc)