From d6cd3b1605a8ab40a13f7fca805333c3ccefc52e Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 4 Sep 2016 12:54:52 +0000 Subject: [PATCH] (svn r27644) -Codechange: Split GetFiosItem into BuildFileList and FindItem, and move both to FileList. --- src/console_cmds.cpp | 48 ++++++---------------------------------- src/fios.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++ src/fios.h | 5 +++-- src/fios_gui.cpp | 25 ++++----------------- 4 files changed, 66 insertions(+), 64 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index ae6e5458e1..28c540dacc 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 271f270762..c670ee3391 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -69,6 +69,58 @@ FileList::~FileList() this->Clear(); } +/** + * 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 diff --git a/src/fios.h b/src/fios.h index 888e3c2362..c1da5720f0 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 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 fdd38da6ca..179a397efb 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 */