(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.
authorDarkvater
Sat, 05 Aug 2006 00:16:24 +0000
changeset 4220 e3bee34c9076
parent 4219 72768a8f3a97
child 4221 ff4a040f30c6
(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
Makefile
fios.c
functions.h
hal.h
openttd.vcproj
openttd_vs80.vcproj
os2.c
unix.c
win32.c
--- a/Makefile	Fri Aug 04 23:45:20 2006 +0000
+++ b/Makefile	Sat Aug 05 00:16:24 2006 +0000
@@ -655,6 +655,7 @@
 SRCS += engine.c
 SRCS += engine_gui.c
 SRCS += fileio.c
+SRCS += fios.c
 SRCS += gfx.c
 SRCS += gfxinit.c
 SRCS += graph_gui.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fios.c	Sat Aug 05 00:16:24 2006 +0000
@@ -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;
+}
--- a/functions.h	Fri Aug 04 23:45:20 2006 +0000
+++ b/functions.h	Sat Aug 05 00:16:24 2006 +0000
@@ -241,7 +241,6 @@
 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);
--- a/hal.h	Fri Aug 04 23:45:20 2006 +0000
+++ b/hal.h	Sat Aug 05 00:16:24 2006 +0000
@@ -89,6 +89,8 @@
 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);
 
--- a/openttd.vcproj	Fri Aug 04 23:45:20 2006 +0000
+++ b/openttd.vcproj	Sat Aug 05 00:16:24 2006 +0000
@@ -226,6 +226,9 @@
 				RelativePath=".\fileio.c">
 			</File>
 			<File
+				RelativePath=".\fios.c">
+			</File>
+			<File
 				RelativePath=".\gfx.c">
 			</File>
 			<File
--- a/openttd_vs80.vcproj	Fri Aug 04 23:45:20 2006 +0000
+++ b/openttd_vs80.vcproj	Sat Aug 05 00:16:24 2006 +0000
@@ -310,6 +310,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\fios.c"
+				>
+			</File>
+			<File
 				RelativePath=".\gfx.c"
 				>
 			</File>
--- a/os2.c	Fri Aug 04 23:45:20 2006 +0000
+++ b/os2.c	Sat Aug 05 00:16:24 2006 +0000
@@ -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 @@
 	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 @@
 	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, '\\');
--- a/unix.c	Fri Aug 04 23:45:20 2006 +0000
+++ b/unix.c	Sat Aug 05 00:16:24 2006 +0000
@@ -48,36 +48,11 @@
 		#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 @@
 	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 @@
 	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)
 {
--- a/win32.c	Fri Aug 04 23:45:20 2006 +0000
+++ b/win32.c	Sat Aug 05 00:16:24 2006 +0000
@@ -707,20 +707,11 @@
 	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 @@
 	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 @@
 	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 @@
 	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;