(svn r27644) -Codechange: Split GetFiosItem into BuildFileList and FindItem, and move both to FileList.

This commit is contained in:
alberth 2016-09-04 12:54:52 +00:00
parent 95bb103a23
commit d6cd3b1605
4 changed files with 66 additions and 64 deletions

View File

@ -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:

View File

@ -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

View File

@ -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 */

View File

@ -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 */