summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarkvater <Darkvater@openttd.org>2006-08-05 00:53:09 +0000
committerDarkvater <Darkvater@openttd.org>2006-08-05 00:53:09 +0000
commit54f199e495e17e011f4b24aa1b294b5e99235ce5 (patch)
tree060490cbd9c8e7eb1a0f6a326ba4b273c535f7d7
parentbd458586a097f271c60b3cb69aef6dab8a42dc1d (diff)
downloadopenttd-54f199e495e17e011f4b24aa1b294b5e99235ce5.tar.xz
(svn r5766) - Cleanup: Unify FiosBrowseTo and FiosGetDescText
-rw-r--r--fios.c71
-rw-r--r--os2.c79
-rw-r--r--unix.c111
-rw-r--r--win32.c67
4 files changed, 104 insertions, 224 deletions
diff --git a/fios.c b/fios.c
index 080e58de3..b40eb0f26 100644
--- a/fios.c
+++ b/fios.c
@@ -31,6 +31,7 @@ int _fios_count, _fios_alloc;
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 @@ void FiosFreeSavegameList(void)
_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;
diff --git a/os2.c b/os2.c
index b25f827f8..3783d493e 100644
--- a/os2.c
+++ b/os2.c
@@ -58,82 +58,27 @@ void FiosGetDrives(void)
_dos_setdrive(save, &total); // restore the original drive
}
-bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
+bool FiosGetDiskFreeSpace(const char *path, uint32 *tot)
{
- char filename[MAX_PATH];
-
- snprintf(filename, lengthof(filename), "%s" PATHSEP "%s", path, ent->d_name);
- if (stat(filename, sb) != 0) return false;
-
- return (ent->d_name[0] != '.'); // hidden file
-}
-
-// Browse to
-char *FiosBrowseTo(const FiosItem *item)
-{
- char *path = _fios_path;
- char *s;
+ struct diskfree_t free;
+ char drive = path[0] - 'A' + 1;
- 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;
- }
+ if (tot != NULL && _getdiskfree(drive, &free) == 0) {
+ *tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
+ return true;
}
- return NULL;
+ return false;
}
-/**
- * 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 FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
{
- struct diskfree_t free;
- char drive;
-
- *path = _fios_path;
- drive = *path[0] - 'A' + 1;
+ char filename[MAX_PATH];
- if (tot != NULL && _getdiskfree(drive, &free) == 0) {
- *tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
- return STR_4005_BYTES_FREE;
- }
+ snprintf(filename, lengthof(filename), "%s" PATHSEP "%s", path, ent->d_name);
+ if (stat(filename, sb) != 0) return false;
- return STR_4006_UNABLE_TO_READ_DRIVE;
+ return (ent->d_name[0] != '.'); // hidden file
}
static void ChangeWorkingDirectory(char *exe)
diff --git a/unix.c b/unix.c
index 81f00410d..72f9e9d32 100644
--- a/unix.c
+++ b/unix.c
@@ -52,20 +52,6 @@ extern char *_fios_path;
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 @@ void FiosGetDrives(void)
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 @@ bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb
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)
{
diff --git a/win32.c b/win32.c
index 817b05a48..ee42bba38 100644
--- a/win32.c
+++ b/win32.c
@@ -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 @@ bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb
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)