summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarkvater <Darkvater@openttd.org>2006-08-05 00:16:24 +0000
committerDarkvater <Darkvater@openttd.org>2006-08-05 00:16:24 +0000
commit48ea9fde324a580acf4722825618097b4dc93bd2 (patch)
tree1954e63d4a4f7f4359f1efecd9bf6d5a25348572
parentdcda134080b58a264805831ddb53c6254938ba30 (diff)
downloadopenttd-48ea9fde324a580acf4722825618097b4dc93bd2.tar.xz
(svn r5764) - Cleanup: - Cleanup: Move the now unified FiosAlloc, compare_FiosItems, FiosFreeSavegameList, FiosMakeSavegameName, FiosDelete and FileExists to newly created file fios.c where it belongs.
- Fix: forgot to remove GetLanguageList from functions.h in previous commit
-rw-r--r--Makefile1
-rw-r--r--fios.c98
-rw-r--r--functions.h1
-rw-r--r--hal.h2
-rw-r--r--openttd.vcproj3
-rw-r--r--openttd_vs80.vcproj4
-rw-r--r--os2.c68
-rw-r--r--unix.c67
-rw-r--r--win32.c71
9 files changed, 117 insertions, 198 deletions
diff --git a/Makefile b/Makefile
index 344374b1c..8be09363e 100644
--- a/Makefile
+++ b/Makefile
@@ -655,6 +655,7 @@ SRCS += elrail.c
SRCS += engine.c
SRCS += engine_gui.c
SRCS += fileio.c
+SRCS += fios.c
SRCS += gfx.c
SRCS += gfxinit.c
SRCS += graph_gui.c
diff --git a/fios.c b/fios.c
new file mode 100644
index 000000000..8e1f75ba2
--- /dev/null
+++ b/fios.c
@@ -0,0 +1,98 @@
+/* $Id$ */
+
+/** @file fios.c
+ * This file contains functions for building file lists for the save/load dialogs.
+ */
+
+#include "stdafx.h"
+#include "openttd.h"
+#include "hal.h"
+#include "string.h"
+#include "variables.h"
+#include "functions.h"
+#include "table/strings.h"
+#include "hal.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#ifdef WIN32
+# include <io.h>
+#else
+# include <unistd.h>
+# include <dirent.h>
+#endif /* WIN32 */
+
+char *_fios_path;
+FiosItem *_fios_items;
+int _fios_count, _fios_alloc;
+
+/**
+ * Allocate a new FiosItem.
+ * @return A pointer to the newly allocated FiosItem.
+ */
+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++];
+}
+
+/**
+ * Compare two FiosItem's. Used with qsort when sorting the file list.
+ * @param a A pointer to the first FiosItem to compare.
+ * @param a A pointer to the second FiosItem to compare.
+ * @return -1, 0 or 1, depending on how the two items should be sorted.
+ */
+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;
+}
+
+/**
+ * Free the list of savegames
+ */
+void FiosFreeSavegameList(void)
+{
+ free(_fios_items);
+ _fios_items = NULL;
+ _fios_alloc = _fios_count = 0;
+}
+
+void FiosMakeSavegameName(char *buf, const char *name, size_t size)
+{
+ const char *extension, *period;
+
+ extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
+
+ /* Don't append the extension if it is already there */
+ period = strrchr(name, '.');
+ if (period != NULL && strcasecmp(period, extension) == 0) extension = "";
+
+ snprintf(buf, size, "%s" PATHSEP "%s%s", _fios_path, name, extension);
+}
+
+bool FiosDelete(const char *name)
+{
+ char filename[512];
+
+ FiosMakeSavegameName(filename, name, lengthof(filename));
+ return unlink(filename) == 0;
+}
+
+bool FileExists(const char *filename)
+{
+ return access(filename, 0) == 0;
+}
diff --git a/functions.h b/functions.h
index f0e3f4339..08e91badd 100644
--- a/functions.h
+++ b/functions.h
@@ -241,7 +241,6 @@ bool ReadLanguagePack(int index);
void InitializeLanguagePacks(void);
const char *GetCurrentLocale(const char *param);
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
-int GetLanguageList(char **languages, int max);
void LoadFromConfig(void);
void SaveToConfig(void);
diff --git a/hal.h b/hal.h
index 87ec8d7a8..e4591c156 100644
--- a/hal.h
+++ b/hal.h
@@ -89,6 +89,8 @@ StringID FiosGetDescText(const char **path, uint32 *tot);
bool FiosDelete(const char *name);
// Make a filename from a name
void FiosMakeSavegameName(char *buf, const char *name, size_t size);
+// Allocate a new FiosItem
+FiosItem *FiosAlloc(void);
int CDECL compare_FiosItems(const void *a, const void *b);
diff --git a/openttd.vcproj b/openttd.vcproj
index fae8e0b59..107c9a446 100644
--- a/openttd.vcproj
+++ b/openttd.vcproj
@@ -226,6 +226,9 @@
RelativePath=".\fileio.c">
</File>
<File
+ RelativePath=".\fios.c">
+ </File>
+ <File
RelativePath=".\gfx.c">
</File>
<File
diff --git a/openttd_vs80.vcproj b/openttd_vs80.vcproj
index 091fe29b0..f7740f623 100644
--- a/openttd_vs80.vcproj
+++ b/openttd_vs80.vcproj
@@ -310,6 +310,10 @@
>
</File>
<File
+ RelativePath=".\fios.c"
+ >
+ </File>
+ <File
RelativePath=".\gfx.c"
>
</File>
diff --git a/os2.c b/os2.c
index 581f9c755..264a65cee 100644
--- a/os2.c
+++ b/os2.c
@@ -24,37 +24,11 @@
#include <os2.h>
#include <i86.h>
-static char *_fios_path;
+extern 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;
-}
-
+extern FiosItem *_fios_items;
+extern int _fios_count, _fios_alloc;
static void append_path(char *out, const char *path, const char *file)
{
@@ -320,15 +294,6 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
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)
{
@@ -397,33 +362,6 @@ StringID FiosGetDescText(const char **path, uint32 *tot)
return STR_4006_UNABLE_TO_READ_DRIVE;
}
-void FiosMakeSavegameName(char *buf, const char *name, size_t size)
-{
- const char* extension;
- const char* period;
-
- extension = (_game_mode == GM_EDITOR ? ".scn" : ".sav");
-
- // Don't append the extension, if it is already there
- period = strrchr(name, '.');
- if (period != NULL && strcasecmp(period, extension) == 0) extension = "";
-
- snprintf(buf, size, "%s\\%s%s", _fios_path, name, extension);
-}
-
-bool FiosDelete(const char *name)
-{
- char path[512];
-
- FiosMakeSavegameName(path, name, sizeof(path));
- return unlink(path) == 0;
-}
-
-bool FileExists(const char *filename)
-{
- return access(filename, 0) == 0;
-}
-
static void ChangeWorkingDirectory(char *exe)
{
char *s = strrchr(exe, '\\');
diff --git a/unix.c b/unix.c
index a72fcec57..2e41a1504 100644
--- a/unix.c
+++ b/unix.c
@@ -48,36 +48,11 @@ ULONG __stack = (1024*1024)*2; // maybe not that much is needed actually ;)
#include <SDL.h>
#endif
#endif
-static char *_fios_path;
+extern 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;
-}
+extern FiosItem *_fios_items;
+extern int _fios_count, _fios_alloc;
#if !defined(__MORPHOS__) && !defined(__AMIGAOS__)
#define ISROOT(__p) (__p[1] == '\0')
@@ -297,15 +272,6 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
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)
{
@@ -387,33 +353,6 @@ StringID FiosGetDescText(const char **path, uint32 *tot)
return STR_4005_BYTES_FREE;
}
-void FiosMakeSavegameName(char *buf, const char *name, size_t size)
-{
- const char* extension;
- const char* period;
-
- extension = (_game_mode == GM_EDITOR ? ".scn" : ".sav");
-
- // Don't append the extension, if it is already there
- period = strrchr(name, '.');
- if (period != NULL && strcasecmp(period, extension) == 0) extension = "";
-
- snprintf(buf, size, "%s/%s%s", _fios_path, name, extension);
-}
-
-bool FiosDelete(const char *name)
-{
- char path[512];
-
- FiosMakeSavegameName(path, name, sizeof(path));
- return unlink(path) == 0;
-}
-
-bool FileExists(const char *filename)
-{
- return access(filename, 0) == 0;
-}
-
#if defined(__BEOS__) || defined(__linux__)
static void ChangeWorkingDirectory(char *exe)
{
diff --git a/win32.c b/win32.c
index 6f2c76921..bfd6ce0e4 100644
--- a/win32.c
+++ b/win32.c
@@ -707,20 +707,11 @@ int closedir(DIR *d)
return 0;
}
-static char *_fios_path;
+extern 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++];
-}
+extern FiosItem *_fios_items;
+extern int _fios_count, _fios_alloc;
static HANDLE MyFindFirstFile(const char *path, const char *file, WIN32_FIND_DATA *fd)
{
@@ -736,23 +727,6 @@ static HANDLE MyFindFirstFile(const char *path, const char *file, WIN32_FIND_DAT
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)
{
@@ -985,15 +959,6 @@ FiosItem *FiosGetScenarioList(int *num, int mode)
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)
{
@@ -1067,36 +1032,6 @@ StringID FiosGetDescText(const char **path, uint32 *tot)
return sid;
}
-void FiosMakeSavegameName(char *buf, const char *name, size_t size)
-{
- const char* extension;
- const char* period;
-
- extension = (_game_mode == GM_EDITOR ? ".scn" : ".sav");
-
- // Don't append the extension, if it is already there
- period = strrchr(name, '.');
- if (period != NULL && strcasecmp(period, extension) == 0) extension = "";
-
- snprintf(buf, size, "%s\\%s%s", _fios_path, name, extension);
-}
-
-bool FiosDelete(const char *name)
-{
- char path[512];
-
- FiosMakeSavegameName(path, name, sizeof(path));
- return DeleteFile(path) != 0;
-}
-
-bool FileExists(const char *filename)
-{
- HANDLE hand = CreateFile(filename, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
- if (hand == INVALID_HANDLE_VALUE) return false;
- CloseHandle(hand);
- return true;
-}
-
static int ParseCommandLine(char *line, char **argv, int max_argc)
{
int n = 0;