(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

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