summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoralberth <alberth@openttd.org>2016-09-04 12:54:52 +0000
committeralberth <alberth@openttd.org>2016-09-04 12:54:52 +0000
commitd6cd3b1605a8ab40a13f7fca805333c3ccefc52e (patch)
treea660b072060aa26cc65a8b4e9ed9ad6b1781b51d /src
parent95bb103a233c9986777897c0d8b26f6f7c1cf08f (diff)
downloadopenttd-d6cd3b1605a8ab40a13f7fca805333c3ccefc52e.tar.xz
(svn r27644) -Codechange: Split GetFiosItem into BuildFileList and FindItem, and move both to FileList.
Diffstat (limited to 'src')
-rw-r--r--src/console_cmds.cpp48
-rw-r--r--src/fios.cpp52
-rw-r--r--src/fios.h5
-rw-r--r--src/fios_gui.cpp25
4 files changed, 66 insertions, 64 deletions
diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp
index ae6e5458e..28c540dac 100644
--- a/src/console_cmds.cpp
+++ b/src/console_cmds.cpp
@@ -315,42 +315,6 @@ DEF_CONSOLE_CMD(ConSaveConfig)
return true;
}
-/**
- * Get savegame file informations.
- * @param file The savegame filename to return information about. Can be the actual name
- * or a numbered entry into the filename list.
- * @return FiosItem The information on the file.
- */
-static const FiosItem *GetFiosItem(const char *file)
-{
- _saveload_mode = SLD_LOAD_GAME;
- BuildFileList(_saveload_mode);
-
- 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 no name matches, try to parse it as number */
- char *endptr;
- int i = strtol(file, &endptr, 10);
- if (file == endptr || *endptr != '\0') i = -1;
-
- if (IsInsideMM(i, 0, _fios_items.Length())) return _fios_items.Get(i);
-
- /* As a last effort assume it is an OpenTTD savegame and
- * that the ".sav" part was not given. */
- char long_file[MAX_PATH];
- seprintf(long_file, lastof(long_file), "%s.sav", file);
- for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
- if (strcmp(long_file, item->name) == 0) return item;
- if (strcmp(long_file, item->title) == 0) return item;
- }
-
- return NULL;
-}
-
-
DEF_CONSOLE_CMD(ConLoad)
{
if (argc == 0) {
@@ -361,7 +325,8 @@ DEF_CONSOLE_CMD(ConLoad)
if (argc != 2) return false;
const char *file = argv[1];
- const FiosItem *item = GetFiosItem(file);
+ _fios_items.BuildFileList(SLD_LOAD_GAME);
+ const FiosItem *item = _fios_items.FindItem(file);
if (item != NULL) {
switch (item->type) {
case FIOS_TYPE_FILE: case FIOS_TYPE_OLDFILE: {
@@ -393,7 +358,8 @@ DEF_CONSOLE_CMD(ConRemove)
if (argc != 2) return false;
const char *file = argv[1];
- const FiosItem *item = GetFiosItem(file);
+ _fios_items.BuildFileList(SLD_LOAD_GAME);
+ const FiosItem *item = _fios_items.FindItem(file);
if (item != NULL) {
if (!FiosDelete(item->name)) {
IConsolePrintF(CC_ERROR, "%s: Failed to delete file", file);
@@ -415,8 +381,7 @@ DEF_CONSOLE_CMD(ConListFiles)
return true;
}
- BuildFileList(_saveload_mode);
-
+ _fios_items.BuildFileList(_saveload_mode);
for (uint i = 0; i < _fios_items.Length(); i++) {
IConsolePrintF(CC_DEFAULT, "%d) %s", i, _fios_items[i].title);
}
@@ -436,7 +401,8 @@ DEF_CONSOLE_CMD(ConChangeDirectory)
if (argc != 2) return false;
const char *file = argv[1];
- const FiosItem *item = GetFiosItem(file);
+ _fios_items.BuildFileList(SLD_LOAD_GAME);
+ const FiosItem *item = _fios_items.FindItem(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 271f27076..c670ee339 100644
--- a/src/fios.cpp
+++ b/src/fios.cpp
@@ -70,6 +70,58 @@ FileList::~FileList()
}
/**
+ * Construct a file list containing file appropriate for the specified \a mode.
+ * @param mode Kind of files required in the list.
+ */
+void FileList::BuildFileList(SaveLoadDialogMode mode)
+{
+ this->Clear();
+
+ switch (mode) {
+ case SLD_LOAD_SCENARIO:
+ case SLD_SAVE_SCENARIO:
+ FiosGetScenarioList(mode, *this); break;
+ case SLD_SAVE_HEIGHTMAP:
+ case SLD_LOAD_HEIGHTMAP:
+ FiosGetHeightmapList(mode, *this); break;
+
+ default: FiosGetSavegameList(mode, *this); break;
+ }
+}
+
+/**
+ * Find file information of a file by its name from the file list.
+ * @param file The filename to return information about. Can be the actual name
+ * or a numbered entry into the filename list.
+ * @return The information on the file, or \c NULL if the file is not available.
+ */
+const FiosItem *FileList::FindItem(const char *file)
+{
+ for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
+ if (strcmp(file, item->name) == 0) return item;
+ if (strcmp(file, item->title) == 0) return item;
+ }
+
+ /* 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;
+
+ if (IsInsideMM(i, 0, this->Length())) return this->Get(i);
+
+ /* As a last effort assume it is an OpenTTD savegame and
+ * that the ".sav" part was not given. */
+ char long_file[MAX_PATH];
+ seprintf(long_file, lastof(long_file), "%s.sav", file);
+ for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
+ if (strcmp(long_file, item->name) == 0) return item;
+ if (strcmp(long_file, item->title) == 0) return item;
+ }
+
+ return NULL;
+}
+
+/**
* Get descriptive texts. Returns the path and free space
* left on the device
* @param path string describing the path
diff --git a/src/fios.h b/src/fios.h
index 888e3c236..c1da5720f 100644
--- a/src/fios.h
+++ b/src/fios.h
@@ -200,6 +200,9 @@ public:
this->files.Compact();
}
+ void BuildFileList(SaveLoadDialogMode mode);
+ const FiosItem *FindItem(const char *file);
+
SmallVector<FiosItem, 32> files; ///< The list of files.
};
@@ -235,6 +238,4 @@ int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b);
extern const TextColour _fios_colours[];
-void BuildFileList(SaveLoadDialogMode mode);
-
#endif /* FIOS_H */
diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp
index fdd38da6c..179a397ef 100644
--- a/src/fios_gui.cpp
+++ b/src/fios_gui.cpp
@@ -193,26 +193,6 @@ const TextColour _fios_colours[] = {
TC_ORANGE, TC_LIGHT_BROWN, TC_ORANGE, TC_ORANGE, TC_YELLOW
};
-void BuildFileList(SaveLoadDialogMode mode)
-{
- _fios_path_changed = true;
- _fios_items.Clear();
-
- switch (mode) {
- case SLD_LOAD_SCENARIO:
- case SLD_SAVE_SCENARIO:
- FiosGetScenarioList(mode, _fios_items); break;
- case SLD_SAVE_HEIGHTMAP:
- case SLD_LOAD_HEIGHTMAP:
- FiosGetHeightmapList(mode, _fios_items); break;
-
- default: FiosGetSavegameList(mode, _fios_items); break;
- }
-
- /* Invalidate saveload window */
- InvalidateWindowData(WC_SAVELOAD, 0, 2, true);
-}
-
static void MakeSortedSaveGameList()
{
uint sort_start = 0;
@@ -683,7 +663,10 @@ public:
this->selected = NULL;
_load_check_data.Clear();
if (!gui_scope) break;
- BuildFileList(_saveload_mode);
+
+ _fios_path_changed = true;
+ _fios_items.BuildFileList(_saveload_mode);
+ InvalidateWindowData(WC_SAVELOAD, 0, 2, true);
/* FALL THROUGH */
case 1:
/* Selection changes */