From 8a40ca49c6c12ce47b2a333f5297d65abe957b86 Mon Sep 17 00:00:00 2001 From: skidd13 Date: Mon, 2 Jun 2008 14:19:27 +0000 Subject: (svn r13359) -Codechange: convert _fios_items to a SmallVector -Cleanup: some reincarnations of _fios_items in the code --- src/fios.cpp | 61 ++++++++++++++++++++++-------------------------------------- 1 file changed, 22 insertions(+), 39 deletions(-) (limited to 'src/fios.cpp') diff --git a/src/fios.cpp b/src/fios.cpp index 2145378a3..73c3b9ae2 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -10,7 +10,6 @@ #include "heightmap.h" #include "fios.h" #include "fileio.h" -#include "core/alloc_func.hpp" #include "functions.h" #include "string_func.h" #include @@ -30,12 +29,9 @@ #include "table/strings.h" /* Variables to display file lists */ -int _fios_num; - +SmallVector _fios_items; static char *_fios_path; -static FiosItem *_fios_items; SmallFiosItem _file_to_saveload; -static int _fios_count, _fios_alloc; /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */ extern bool FiosIsRoot(const char *path); @@ -47,19 +43,6 @@ 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); -/** - * Allocate a new FiosItem. - * @return A pointer to the newly allocated FiosItem. - */ -FiosItem *FiosAlloc() -{ - if (_fios_count == _fios_alloc) { - _fios_alloc += 256; - _fios_items = ReallocT(_fios_items, _fios_alloc); - } - 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. @@ -82,15 +65,12 @@ int CDECL compare_FiosItems(const void *a, const void *b) return r; } -/** - * Free the list of savegames - */ +/** Clear the list */ void FiosFreeSavegameList() { - free(_fios_items); - _fios_items = NULL; - _fios_alloc = _fios_count = 0; -} + _fios_items.Clear(); + _fios_items.Compact(); +}; /** * Get descriptive texts. Returns the path and free space @@ -229,9 +209,11 @@ static FiosItem *FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_ int sort_start; char d_name[sizeof(fios->name)]; + _fios_items.Clear(); + /* A parent directory link exists if we are not in the root directory */ if (!FiosIsRoot(_fios_path) && mode != SLD_NEW_GAME) { - fios = FiosAlloc(); + fios = _fios_items.Append(); fios->type = FIOS_TYPE_PARENT; fios->mtime = 0; ttd_strlcpy(fios->name, "..", lengthof(fios->name)); @@ -247,7 +229,7 @@ static FiosItem *FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_ if (FiosIsValidFile(_fios_path, dirent, &sb) && (sb.st_mode & S_IFDIR) && (!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) && strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) { - fios = FiosAlloc(); + fios = _fios_items.Append(); fios->type = FIOS_TYPE_DIR; fios->mtime = 0; ttd_strlcpy(fios->name, d_name, lengthof(fios->name)); @@ -262,12 +244,12 @@ static FiosItem *FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_ { byte order = _savegame_sort_order; _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING; - qsort(_fios_items, _fios_count, sizeof(FiosItem), compare_FiosItems); + qsort(_fios_items.Begin(), _fios_items.Length(), sizeof(FiosItem), compare_FiosItems); _savegame_sort_order = order; } /* This is where to start sorting for the filenames */ - sort_start = _fios_count; + sort_start = _fios_items.Length(); /* Show files */ dir = ttd_opendir(_fios_path); @@ -285,7 +267,7 @@ static FiosItem *FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_ FiosType type = callback_proc(mode, d_name, t, fios_title); if (type != FIOS_TYPE_INVALID) { - fios = FiosAlloc(); + fios = _fios_items.Append(); fios->mtime = sb.st_mtime; fios->type = type; ttd_strlcpy(fios->name, d_name, lengthof(fios->name)); @@ -300,13 +282,14 @@ static FiosItem *FiosGetFileList(SaveLoadDialogMode mode, fios_getlist_callback_ closedir(dir); } - qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); + qsort(_fios_items.Get(sort_start), _fios_items.Length() - sort_start, sizeof(FiosItem), compare_FiosItems); /* Show drives */ if (mode != SLD_NEW_GAME) FiosGetDrives(); - _fios_num = _fios_count; - return _fios_items; + _fios_items.Compact(); + + return _fios_items.Begin(); } /** @@ -345,7 +328,7 @@ static FiosType FiosGetSavegameListCallback(SaveLoadDialogMode mode, const char * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog. * @see FiosGetFileList */ -FiosItem *FiosGetSavegameList(SaveLoadDialogMode mode) +void FiosGetSavegameList(SaveLoadDialogMode mode) { static char *fios_save_path = NULL; @@ -356,7 +339,7 @@ FiosItem *FiosGetSavegameList(SaveLoadDialogMode mode) _fios_path = fios_save_path; - return FiosGetFileList(mode, &FiosGetSavegameListCallback); + FiosGetFileList(mode, &FiosGetSavegameListCallback); } /** @@ -393,7 +376,7 @@ static FiosType FiosGetScenarioListCallback(SaveLoadDialogMode mode, const char * @return A pointer to an array of FiosItem representing all the files to be shown in the save/load dialog. * @see FiosGetFileList */ -FiosItem *FiosGetScenarioList(SaveLoadDialogMode mode) +void FiosGetScenarioList(SaveLoadDialogMode mode) { static char *fios_scn_path = NULL; @@ -405,7 +388,7 @@ FiosItem *FiosGetScenarioList(SaveLoadDialogMode mode) _fios_path = fios_scn_path; - return FiosGetFileList(mode, &FiosGetScenarioListCallback); + FiosGetFileList(mode, &FiosGetScenarioListCallback); } static FiosType FiosGetHeightmapListCallback(SaveLoadDialogMode mode, const char *file, const char *ext, char *title) @@ -425,7 +408,7 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadDialogMode mode, const char } /* Get a list of Heightmaps */ -FiosItem *FiosGetHeightmapList(SaveLoadDialogMode mode) +void FiosGetHeightmapList(SaveLoadDialogMode mode) { static char *fios_hmap_path = NULL; @@ -436,5 +419,5 @@ FiosItem *FiosGetHeightmapList(SaveLoadDialogMode mode) _fios_path = fios_hmap_path; - return FiosGetFileList(mode, &FiosGetHeightmapListCallback); + FiosGetFileList(mode, &FiosGetHeightmapListCallback); } -- cgit v1.2.3-54-g00ecf