summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/console_cmds.cpp48
-rw-r--r--src/fios.cpp61
-rw-r--r--src/fios.h10
-rw-r--r--src/misc_gui.cpp28
-rw-r--r--src/network/network_gui.cpp12
5 files changed, 60 insertions, 99 deletions
diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp
index 3bd461b56..4eee5e3ce 100644
--- a/src/console_cmds.cpp
+++ b/src/console_cmds.cpp
@@ -215,32 +215,25 @@ DEF_CONSOLE_CMD(ConSaveConfig)
static const FiosItem* GetFiosItem(const char* file)
{
- int i;
-
_saveload_mode = SLD_LOAD_GAME;
BuildFileList();
- for (i = 0; i < _fios_num; i++) {
- if (strcmp(file, _fios_list[i].name) == 0) break;
- if (strcmp(file, _fios_list[i].title) == 0) break;
+ for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
+ if (strcmp(file, item->name) == 0) return item;
+ if (strcmp(file, item->title) == 0) return item;
}
- if (i == _fios_num) { // If no name matches, try to parse it as number
- char* endptr;
-
- i = strtol(file, &endptr, 10);
- if (file == endptr || *endptr != '\0') i = -1;
- }
+ /* If no name matches, try to parse it as number */
+ char *endptr;
+ int i = strtol(file, &endptr, 10);
+ if (file == endptr || *endptr != '\0') i = -1;
- return IsInsideMM(i, 0, _fios_num) ? &_fios_list[i] : NULL;
+ return IsInsideMM(i, 0, _fios_items.Length()) ? _fios_items.Get(i) : NULL;
}
DEF_CONSOLE_CMD(ConLoad)
{
- const FiosItem *item;
- const char *file;
-
if (argc == 0) {
IConsoleHelp("Load a game by name or index. Usage: 'load <file | number>'");
return true;
@@ -248,8 +241,8 @@ DEF_CONSOLE_CMD(ConLoad)
if (argc != 2) return false;
- file = argv[1];
- item = GetFiosItem(file);
+ const char *file = argv[1];
+ const FiosItem *item = GetFiosItem(file);
if (item != NULL) {
switch (item->type) {
case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: {
@@ -272,9 +265,6 @@ DEF_CONSOLE_CMD(ConLoad)
DEF_CONSOLE_CMD(ConRemove)
{
- const FiosItem* item;
- const char* file;
-
if (argc == 0) {
IConsoleHelp("Remove a savegame by name or index. Usage: 'rm <file | number>'");
return true;
@@ -282,8 +272,8 @@ DEF_CONSOLE_CMD(ConRemove)
if (argc != 2) return false;
- file = argv[1];
- item = GetFiosItem(file);
+ const char *file = argv[1];
+ const FiosItem *item = GetFiosItem(file);
if (item != NULL) {
if (!FiosDelete(item->name))
IConsolePrintF(CC_ERROR, "%s: Failed to delete file", file);
@@ -299,8 +289,6 @@ DEF_CONSOLE_CMD(ConRemove)
/* List all the files in the current dir via console */
DEF_CONSOLE_CMD(ConListFiles)
{
- int i;
-
if (argc == 0) {
IConsoleHelp("List all loadable savegames and directories in the current dir via console. Usage: 'ls | dir'");
return true;
@@ -308,9 +296,8 @@ DEF_CONSOLE_CMD(ConListFiles)
BuildFileList();
- for (i = 0; i < _fios_num; i++) {
- const FiosItem *item = &_fios_list[i];
- IConsolePrintF(CC_DEFAULT, "%d) %s", i, item->title);
+ for (uint i = 0; i < _fios_items.Length(); i++) {
+ IConsolePrintF(CC_DEFAULT, "%d) %s", i, _fios_items[i].title);
}
FiosFreeSavegameList();
@@ -320,9 +307,6 @@ DEF_CONSOLE_CMD(ConListFiles)
/* Change the dir via console */
DEF_CONSOLE_CMD(ConChangeDirectory)
{
- const FiosItem *item;
- const char *file;
-
if (argc == 0) {
IConsoleHelp("Change the dir via console. Usage: 'cd <directory | number>'");
return true;
@@ -330,8 +314,8 @@ DEF_CONSOLE_CMD(ConChangeDirectory)
if (argc != 2) return false;
- file = argv[1];
- item = GetFiosItem(file);
+ const char *file = argv[1];
+ const FiosItem *item = GetFiosItem(file);
if (item != NULL) {
switch (item->type) {
case FIOS_TYPE_DIR: case FIOS_TYPE_DRIVE: case FIOS_TYPE_PARENT:
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 <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 bool FiosGetDiskFreeSpace(const char *path, uint32 *tot);
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 @@ 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);
}
diff --git a/src/fios.h b/src/fios.h
index ca4fa1361..7ec8e6767 100644
--- a/src/fios.h
+++ b/src/fios.h
@@ -6,6 +6,7 @@
#define FIOS_H
#include "strings_type.h"
+#include "misc/smallvec.h"
enum {
/**
@@ -80,8 +81,7 @@ enum {
};
/* Variables to display file lists */
-extern FiosItem *_fios_list; ///< defined in misc_gui.cpp
-extern int _fios_num; ///< defined in fios.cpp, read_only version of _fios_count
+extern SmallVector<FiosItem, 32> _fios_items; ///< defined in fios.cpp
extern SmallFiosItem _file_to_saveload;
extern SaveLoadDialogMode _saveload_mode; ///< defined in misc_gui.cpp
extern byte _savegame_sort_order;
@@ -90,11 +90,11 @@ extern byte _savegame_sort_order;
void ShowSaveLoadDialog(SaveLoadDialogMode mode);
/* Get a list of savegames */
-FiosItem *FiosGetSavegameList(SaveLoadDialogMode mode);
+void FiosGetSavegameList(SaveLoadDialogMode mode);
/* Get a list of scenarios */
-FiosItem *FiosGetScenarioList(SaveLoadDialogMode mode);
+void FiosGetScenarioList(SaveLoadDialogMode mode);
/* Get a list of Heightmaps */
-FiosItem *FiosGetHeightmapList(SaveLoadDialogMode mode);
+void FiosGetHeightmapList(SaveLoadDialogMode mode);
/* Free the list of savegames */
void FiosFreeSavegameList();
/* Browse to. Returns a filename w/path if we reached a file. */
diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp
index 518e79457..a87e0bb99 100644
--- a/src/misc_gui.cpp
+++ b/src/misc_gui.cpp
@@ -50,7 +50,6 @@
#include "table/strings.h"
/* Variables to display file lists */
-FiosItem *_fios_list;
SaveLoadDialogMode _saveload_mode;
@@ -1300,11 +1299,11 @@ void BuildFileList()
case SLD_NEW_GAME:
case SLD_LOAD_SCENARIO:
case SLD_SAVE_SCENARIO:
- _fios_list = FiosGetScenarioList(_saveload_mode); break;
+ FiosGetScenarioList(_saveload_mode); break;
case SLD_LOAD_HEIGHTMAP:
- _fios_list = FiosGetHeightmapList(_saveload_mode); break;
+ FiosGetHeightmapList(_saveload_mode); break;
- default: _fios_list = FiosGetSavegameList(_saveload_mode); break;
+ default: FiosGetSavegameList(_saveload_mode); break;
}
}
@@ -1333,8 +1332,8 @@ static void MakeSortedSaveGameList()
* Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
* Only sort savegames/scenarios, not directories
*/
- for (int i = 0; i < _fios_num; i++) {
- switch (_fios_list[i].type) {
+ for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
+ switch (item->type) {
case FIOS_TYPE_DIR: sort_start++; break;
case FIOS_TYPE_PARENT: sort_start++; break;
case FIOS_TYPE_DRIVE: sort_end++; break;
@@ -1342,9 +1341,9 @@ static void MakeSortedSaveGameList()
}
}
- uint s_amount = _fios_num - sort_start - sort_end;
+ uint s_amount = _fios_items.Length() - sort_start - sort_end;
if (s_amount > 0) {
- qsort(_fios_list + sort_start, s_amount, sizeof(FiosItem), compare_FiosItems);
+ qsort(_fios_items.Get(sort_start), s_amount, sizeof(FiosItem), compare_FiosItems);
}
}
@@ -1443,10 +1442,9 @@ struct SaveLoadWindow : public QueryStringBaseWindow {
virtual void OnPaint()
{
- int pos;
int y;
- SetVScrollCount(this, _fios_num);
+ SetVScrollCount(this, _fios_items.Length());
this->DrawWidgets();
DrawFiosTexts(this->width);
@@ -1459,8 +1457,8 @@ struct SaveLoadWindow : public QueryStringBaseWindow {
this->DrawSortButtonState(_savegame_sort_order & SORT_BY_NAME ? 2 : 3, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
y = this->widget[7].top + 1;
- for (pos = this->vscroll.pos; pos < _fios_num; pos++) {
- const FiosItem *item = _fios_list + pos;
+ for (uint pos = this->vscroll.pos; pos < _fios_items.Length(); pos++) {
+ const FiosItem *item = _fios_items.Get(pos);
DoDrawStringTruncated(item->title, 4, y, _fios_colors[item->type], this->width - 18);
y += 10;
@@ -1497,14 +1495,12 @@ struct SaveLoadWindow : public QueryStringBaseWindow {
case 7: { // Click the listbox
int y = (pt.y - this->widget[widget].top - 1) / 10;
- char *name;
- const FiosItem *file;
if (y < 0 || (y += this->vscroll.pos) >= this->vscroll.count) return;
- file = _fios_list + y;
+ const FiosItem *file = _fios_items.Get(y);
- name = FiosBrowseTo(file);
+ char *name = FiosBrowseTo(file);
if (name != NULL) {
if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
_switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_SCENARIO : SM_LOAD;
diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp
index 9125fd2e6..c2f170ef8 100644
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -670,7 +670,7 @@ struct NetworkStartServerWindow : public QueryStringBaseWindow {
_saveload_mode = SLD_NEW_GAME;
BuildFileList();
this->vscroll.cap = 12;
- this->vscroll.count = _fios_num + 1;
+ this->vscroll.count = _fios_items.Length();
this->afilter = CS_ALPHANUMERAL;
InitializeTextBuffer(&this->text, this->edit_str_buf, lengthof(this->edit_str_buf), 160);
@@ -683,7 +683,7 @@ struct NetworkStartServerWindow : public QueryStringBaseWindow {
virtual void OnPaint()
{
- int y = NSSWND_START, pos;
+ int y = NSSWND_START;
const FiosItem *item;
/* draw basic widgets */
@@ -703,9 +703,8 @@ struct NetworkStartServerWindow : public QueryStringBaseWindow {
/* draw list of maps */
GfxFillRect(11, 63, 258, 215, 0xD7); // black background of maps list
- pos = this->vscroll.pos;
- while (pos < _fios_num + 1) {
- item = _fios_list + pos - 1;
+ for (uint pos = this->vscroll.pos; pos < _fios_items.Length() + 1; pos++) {
+ item = _fios_items.Get(pos - 1);
if (item == this->map || (pos == 0 && this->map == NULL))
GfxFillRect(11, y - 1, 258, y + 10, 155); // show highlighted item with a different colour
@@ -714,7 +713,6 @@ struct NetworkStartServerWindow : public QueryStringBaseWindow {
} else {
DoDrawString(item->title, 14, y, _fios_colors[item->type] );
}
- pos++;
y += NSSWND_ROWSIZE;
if (y >= this->vscroll.cap * NSSWND_ROWSIZE + NSSWND_START) break;
@@ -746,7 +744,7 @@ struct NetworkStartServerWindow : public QueryStringBaseWindow {
y += this->vscroll.pos;
if (y >= this->vscroll.count) return;
- this->map = (y == 0) ? NULL : _fios_list + y - 1;
+ this->map = (y == 0) ? NULL : _fios_items.Get(y - 1);
this->SetDirty();
} break;