src/fios.cpp
branchNewGRF_ports
changeset 10991 d8811e327d12
parent 10724 68a692eacf22
--- a/src/fios.cpp	Tue May 27 00:50:55 2008 +0000
+++ b/src/fios.cpp	Tue Jun 17 10:32:49 2008 +0000
@@ -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 <sys/types.h>
@@ -30,12 +29,9 @@
 #include "table/strings.h"
 
 /* Variables to display file lists */
-int _fios_num;
-
+SmallVector<FiosItem, 32> _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);
@@ -48,19 +44,6 @@
 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.
  * @param b A pointer to the second FiosItem to compare.
@@ -82,15 +65,12 @@
 	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 @@
 	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 @@
 			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 @@
 	{
 		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 @@
 
 			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 @@
 		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 @@
  * @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 @@
 
 	_fios_path = fios_save_path;
 
-	return FiosGetFileList(mode, &FiosGetSavegameListCallback);
+	FiosGetFileList(mode, &FiosGetSavegameListCallback);
 }
 
 /**
@@ -393,7 +376,7 @@
  * @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 @@
 
 	_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 @@
 }
 
 /* Get a list of Heightmaps */
-FiosItem *FiosGetHeightmapList(SaveLoadDialogMode mode)
+void FiosGetHeightmapList(SaveLoadDialogMode mode)
 {
 	static char *fios_hmap_path = NULL;
 
@@ -436,5 +419,5 @@
 
 	_fios_path = fios_hmap_path;
 
-	return FiosGetFileList(mode, &FiosGetHeightmapListCallback);
+	FiosGetFileList(mode, &FiosGetHeightmapListCallback);
 }