From ee7b3de2f58c22d5667724471a366c23764de46d Mon Sep 17 00:00:00 2001 From: Darkvater Date: Sat, 5 Aug 2006 00:16:24 +0000 Subject: [PATCH] (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 | 1 + fios.c | 98 +++++++++++++++++++++++++++++++++++++++++++++ functions.h | 1 - hal.h | 2 + openttd.vcproj | 3 ++ openttd_vs80.vcproj | 4 ++ os2.c | 68 ++----------------------------- unix.c | 67 ++----------------------------- win32.c | 71 ++------------------------------ 9 files changed, 117 insertions(+), 198 deletions(-) create mode 100644 fios.c diff --git a/Makefile b/Makefile index 344374b1cd..8be09363ea 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 0000000000..8e1f75ba2d --- /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 +#include + +#ifdef WIN32 +# include +#else +# include +# include +#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 f0e3f4339d..08e91badd8 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 87ec8d7a8c..e4591c1565 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 fae8e0b593..107c9a446f 100644 --- a/openttd.vcproj +++ b/openttd.vcproj @@ -225,6 +225,9 @@ + + diff --git a/openttd_vs80.vcproj b/openttd_vs80.vcproj index 091fe29b09..f7740f6237 100644 --- a/openttd_vs80.vcproj +++ b/openttd_vs80.vcproj @@ -309,6 +309,10 @@ RelativePath=".\fileio.c" > + + diff --git a/os2.c b/os2.c index 581f9c7551..264a65cee7 100644 --- a/os2.c +++ b/os2.c @@ -24,37 +24,11 @@ #include #include -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 a72fcec579..2e41a15046 100644 --- a/unix.c +++ b/unix.c @@ -48,36 +48,11 @@ ULONG __stack = (1024*1024)*2; // maybe not that much is needed actually ;) #include #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 6f2c76921b..bfd6ce0e45 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;