mirror of https://github.com/OpenTTD/OpenTTD
Cleanup: Remove old FiosList helper methods. (#9139)
parent
9a8756d7ed
commit
f018471b36
|
@ -59,7 +59,7 @@ public:
|
||||||
/** Declare the file storage cache as being invalid, also clears all stored files. */
|
/** Declare the file storage cache as being invalid, also clears all stored files. */
|
||||||
void InvalidateFileList()
|
void InvalidateFileList()
|
||||||
{
|
{
|
||||||
this->Clear();
|
this->clear();
|
||||||
this->file_list_valid = false;
|
this->file_list_valid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -403,7 +403,7 @@ DEF_CONSOLE_CMD(ConListFiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
_console_file_list.ValidateFileList(true);
|
_console_file_list.ValidateFileList(true);
|
||||||
for (uint i = 0; i < _console_file_list.Length(); i++) {
|
for (uint i = 0; i < _console_file_list.size(); i++) {
|
||||||
IConsolePrintF(CC_DEFAULT, "%d) %s", i, _console_file_list[i].title);
|
IConsolePrintF(CC_DEFAULT, "%d) %s", i, _console_file_list[i].title);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
35
src/fios.cpp
35
src/fios.cpp
|
@ -63,11 +63,6 @@ bool FiosItem::operator< (const FiosItem &other) const
|
||||||
return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
|
return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileList::~FileList()
|
|
||||||
{
|
|
||||||
this->Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a file list with the given kind of files, for the stated purpose.
|
* Construct a file list with the given kind of files, for the stated purpose.
|
||||||
* @param abstract_filetype Kind of files to collect.
|
* @param abstract_filetype Kind of files to collect.
|
||||||
|
@ -75,7 +70,7 @@ FileList::~FileList()
|
||||||
*/
|
*/
|
||||||
void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop)
|
void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop)
|
||||||
{
|
{
|
||||||
this->Clear();
|
this->clear();
|
||||||
|
|
||||||
assert(fop == SLO_LOAD || fop == SLO_SAVE);
|
assert(fop == SLO_LOAD || fop == SLO_SAVE);
|
||||||
switch (abstract_filetype) {
|
switch (abstract_filetype) {
|
||||||
|
@ -107,7 +102,8 @@ void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperati
|
||||||
*/
|
*/
|
||||||
const FiosItem *FileList::FindItem(const char *file)
|
const FiosItem *FileList::FindItem(const char *file)
|
||||||
{
|
{
|
||||||
for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
|
for (const auto &it : *this) {
|
||||||
|
const FiosItem *item = ⁢
|
||||||
if (strcmp(file, item->name) == 0) return item;
|
if (strcmp(file, item->name) == 0) return item;
|
||||||
if (strcmp(file, item->title) == 0) return item;
|
if (strcmp(file, item->title) == 0) return item;
|
||||||
}
|
}
|
||||||
|
@ -117,13 +113,14 @@ const FiosItem *FileList::FindItem(const char *file)
|
||||||
int i = strtol(file, &endptr, 10);
|
int i = strtol(file, &endptr, 10);
|
||||||
if (file == endptr || *endptr != '\0') i = -1;
|
if (file == endptr || *endptr != '\0') i = -1;
|
||||||
|
|
||||||
if (IsInsideMM(i, 0, this->Length())) return this->Get(i);
|
if (IsInsideMM(i, 0, this->size())) return &this->at(i);
|
||||||
|
|
||||||
/* As a last effort assume it is an OpenTTD savegame and
|
/* As a last effort assume it is an OpenTTD savegame and
|
||||||
* that the ".sav" part was not given. */
|
* that the ".sav" part was not given. */
|
||||||
char long_file[MAX_PATH];
|
char long_file[MAX_PATH];
|
||||||
seprintf(long_file, lastof(long_file), "%s.sav", file);
|
seprintf(long_file, lastof(long_file), "%s.sav", file);
|
||||||
for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
|
for (const auto &it : *this) {
|
||||||
|
const FiosItem *item = ⁢
|
||||||
if (strcmp(long_file, item->name) == 0) return item;
|
if (strcmp(long_file, item->name) == 0) return item;
|
||||||
if (strcmp(long_file, item->title) == 0) return item;
|
if (strcmp(long_file, item->title) == 0) return item;
|
||||||
}
|
}
|
||||||
|
@ -302,11 +299,11 @@ bool FiosFileScanner::AddFile(const std::string &filename, size_t basepath_lengt
|
||||||
FiosType type = this->callback_proc(this->fop, filename, ext.c_str(), fios_title, lastof(fios_title));
|
FiosType type = this->callback_proc(this->fop, filename, ext.c_str(), fios_title, lastof(fios_title));
|
||||||
if (type == FIOS_TYPE_INVALID) return false;
|
if (type == FIOS_TYPE_INVALID) return false;
|
||||||
|
|
||||||
for (const FiosItem *fios = file_list.Begin(); fios != file_list.End(); fios++) {
|
for (const auto &fios : file_list) {
|
||||||
if (filename == fios->name) return false;
|
if (filename == fios.name) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
FiosItem *fios = file_list.Append();
|
FiosItem *fios = &file_list.emplace_back();
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// Retrieve the file modified date using GetFileTime rather than stat to work around an obscure MSVC bug that affects Windows XP
|
// Retrieve the file modified date using GetFileTime rather than stat to work around an obscure MSVC bug that affects Windows XP
|
||||||
HANDLE fh = CreateFile(OTTD2FS(filename).c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
|
HANDLE fh = CreateFile(OTTD2FS(filename).c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
|
||||||
|
@ -367,13 +364,13 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
|
||||||
size_t sort_start;
|
size_t sort_start;
|
||||||
char d_name[sizeof(fios->name)];
|
char d_name[sizeof(fios->name)];
|
||||||
|
|
||||||
file_list.Clear();
|
file_list.clear();
|
||||||
|
|
||||||
assert(_fios_path != nullptr);
|
assert(_fios_path != nullptr);
|
||||||
|
|
||||||
/* A parent directory link exists if we are not in the root directory */
|
/* A parent directory link exists if we are not in the root directory */
|
||||||
if (!FiosIsRoot(_fios_path->c_str())) {
|
if (!FiosIsRoot(_fios_path->c_str())) {
|
||||||
fios = file_list.Append();
|
fios = &file_list.emplace_back();
|
||||||
fios->type = FIOS_TYPE_PARENT;
|
fios->type = FIOS_TYPE_PARENT;
|
||||||
fios->mtime = 0;
|
fios->mtime = 0;
|
||||||
strecpy(fios->name, "..", lastof(fios->name));
|
strecpy(fios->name, "..", lastof(fios->name));
|
||||||
|
@ -390,7 +387,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
|
||||||
if (FiosIsValidFile(_fios_path->c_str(), dirent, &sb) && S_ISDIR(sb.st_mode) &&
|
if (FiosIsValidFile(_fios_path->c_str(), dirent, &sb) && S_ISDIR(sb.st_mode) &&
|
||||||
(!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
|
(!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
|
||||||
strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
|
strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
|
||||||
fios = file_list.Append();
|
fios = &file_list.emplace_back();
|
||||||
fios->type = FIOS_TYPE_DIR;
|
fios->type = FIOS_TYPE_DIR;
|
||||||
fios->mtime = 0;
|
fios->mtime = 0;
|
||||||
strecpy(fios->name, d_name, lastof(fios->name));
|
strecpy(fios->name, d_name, lastof(fios->name));
|
||||||
|
@ -407,12 +404,12 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
|
||||||
{
|
{
|
||||||
SortingBits order = _savegame_sort_order;
|
SortingBits order = _savegame_sort_order;
|
||||||
_savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
|
_savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
|
||||||
std::sort(file_list.files.begin(), file_list.files.end());
|
std::sort(file_list.begin(), file_list.end());
|
||||||
_savegame_sort_order = order;
|
_savegame_sort_order = order;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This is where to start sorting for the filenames */
|
/* This is where to start sorting for the filenames */
|
||||||
sort_start = file_list.Length();
|
sort_start = file_list.size();
|
||||||
|
|
||||||
/* Show files */
|
/* Show files */
|
||||||
FiosFileScanner scanner(fop, callback_proc, file_list);
|
FiosFileScanner scanner(fop, callback_proc, file_list);
|
||||||
|
@ -422,12 +419,12 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
|
||||||
scanner.Scan(nullptr, subdir, true, true);
|
scanner.Scan(nullptr, subdir, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::sort(file_list.files.begin() + sort_start, file_list.files.end());
|
std::sort(file_list.begin() + sort_start, file_list.end());
|
||||||
|
|
||||||
/* Show drives */
|
/* Show drives */
|
||||||
FiosGetDrives(file_list);
|
FiosGetDrives(file_list);
|
||||||
|
|
||||||
file_list.Compact();
|
file_list.shrink_to_fit();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
86
src/fios.h
86
src/fios.h
|
@ -109,94 +109,10 @@ struct FiosItem {
|
||||||
};
|
};
|
||||||
|
|
||||||
/** List of file information. */
|
/** List of file information. */
|
||||||
class FileList {
|
class FileList : public std::vector<FiosItem> {
|
||||||
public:
|
public:
|
||||||
~FileList();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a new entry in the file list.
|
|
||||||
* @return Pointer to the new items to be initialized.
|
|
||||||
*/
|
|
||||||
inline FiosItem *Append()
|
|
||||||
{
|
|
||||||
return &this->files.emplace_back();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the number of files in the list.
|
|
||||||
* @return The number of files stored in the list.
|
|
||||||
*/
|
|
||||||
inline size_t Length() const
|
|
||||||
{
|
|
||||||
return this->files.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a pointer to the first file information.
|
|
||||||
* @return Address of the first file information.
|
|
||||||
*/
|
|
||||||
inline const FiosItem *Begin() const
|
|
||||||
{
|
|
||||||
return this->files.data();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a pointer behind the last file information.
|
|
||||||
* @return Address behind the last file information.
|
|
||||||
*/
|
|
||||||
inline const FiosItem *End() const
|
|
||||||
{
|
|
||||||
return this->Begin() + this->Length();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a pointer to the indicated file information. File information must exist.
|
|
||||||
* @return Address of the indicated existing file information.
|
|
||||||
*/
|
|
||||||
inline const FiosItem *Get(size_t index) const
|
|
||||||
{
|
|
||||||
return this->files.data() + index;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a pointer to the indicated file information. File information must exist.
|
|
||||||
* @return Address of the indicated existing file information.
|
|
||||||
*/
|
|
||||||
inline FiosItem *Get(size_t index)
|
|
||||||
{
|
|
||||||
return this->files.data() + index;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline const FiosItem &operator[](size_t index) const
|
|
||||||
{
|
|
||||||
return this->files[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a reference to the indicated file information. File information must exist.
|
|
||||||
* @return The requested file information.
|
|
||||||
*/
|
|
||||||
inline FiosItem &operator[](size_t index)
|
|
||||||
{
|
|
||||||
return this->files[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Remove all items from the list. */
|
|
||||||
inline void Clear()
|
|
||||||
{
|
|
||||||
this->files.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Compact the list down to the smallest block size boundary. */
|
|
||||||
inline void Compact()
|
|
||||||
{
|
|
||||||
this->files.shrink_to_fit();
|
|
||||||
}
|
|
||||||
|
|
||||||
void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop);
|
void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop);
|
||||||
const FiosItem *FindItem(const char *file);
|
const FiosItem *FindItem(const char *file);
|
||||||
|
|
||||||
std::vector<FiosItem> files; ///< The list of files.
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum SortingBits {
|
enum SortingBits {
|
||||||
|
|
|
@ -249,8 +249,8 @@ static void SortSaveGameList(FileList &file_list)
|
||||||
* Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
|
* Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
|
||||||
* Only sort savegames/scenarios, not directories
|
* Only sort savegames/scenarios, not directories
|
||||||
*/
|
*/
|
||||||
for (const FiosItem *item = file_list.Begin(); item != file_list.End(); item++) {
|
for (const auto &item : file_list) {
|
||||||
switch (item->type) {
|
switch (item.type) {
|
||||||
case FIOS_TYPE_DIR: sort_start++; break;
|
case FIOS_TYPE_DIR: sort_start++; break;
|
||||||
case FIOS_TYPE_PARENT: sort_start++; break;
|
case FIOS_TYPE_PARENT: sort_start++; break;
|
||||||
case FIOS_TYPE_DRIVE: sort_end++; break;
|
case FIOS_TYPE_DRIVE: sort_end++; break;
|
||||||
|
@ -258,7 +258,7 @@ static void SortSaveGameList(FileList &file_list)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::sort(file_list.files.begin() + sort_start, file_list.files.end() - sort_end);
|
std::sort(file_list.begin() + sort_start, file_list.end() - sort_end);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SaveLoadWindow : public Window {
|
struct SaveLoadWindow : public Window {
|
||||||
|
@ -437,14 +437,14 @@ public:
|
||||||
|
|
||||||
uint y = r.top + WD_FRAMERECT_TOP;
|
uint y = r.top + WD_FRAMERECT_TOP;
|
||||||
uint scroll_pos = this->vscroll->GetPosition();
|
uint scroll_pos = this->vscroll->GetPosition();
|
||||||
for (uint row = 0; row < this->fios_items.Length(); row++) {
|
for (uint row = 0; row < this->fios_items.size(); row++) {
|
||||||
if (!this->fios_items_shown[row]) {
|
if (!this->fios_items_shown[row]) {
|
||||||
/* The current item is filtered out : we do not show it */
|
/* The current item is filtered out : we do not show it */
|
||||||
scroll_pos++;
|
scroll_pos++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (row < scroll_pos) continue;
|
if (row < scroll_pos) continue;
|
||||||
const FiosItem *item = this->fios_items.Get(row);
|
const FiosItem *item = &this->fios_items[row];
|
||||||
|
|
||||||
if (item == this->selected) {
|
if (item == this->selected) {
|
||||||
GfxFillRect(r.left + 1, y, r.right, y + this->resize.step_height, PC_DARK_BLUE);
|
GfxFillRect(r.left + 1, y, r.right, y + this->resize.step_height, PC_DARK_BLUE);
|
||||||
|
@ -651,7 +651,7 @@ public:
|
||||||
if (!this->fios_items_shown[i]) y++;
|
if (!this->fios_items_shown[i]) y++;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
const FiosItem *file = this->fios_items.Get(y);
|
const FiosItem *file = &this->fios_items[y];
|
||||||
|
|
||||||
const char *name = FiosBrowseTo(file);
|
const char *name = FiosBrowseTo(file);
|
||||||
if (name == nullptr) {
|
if (name == nullptr) {
|
||||||
|
@ -734,7 +734,7 @@ public:
|
||||||
if (!this->fios_items_shown[i]) y++;
|
if (!this->fios_items_shown[i]) y++;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
const FiosItem *file = this->fios_items.Get(y);
|
const FiosItem *file = &this->fios_items[y];
|
||||||
|
|
||||||
if (file != this->highlighted) {
|
if (file != this->highlighted) {
|
||||||
this->highlighted = file;
|
this->highlighted = file;
|
||||||
|
@ -812,7 +812,7 @@ public:
|
||||||
|
|
||||||
_fios_path_changed = true;
|
_fios_path_changed = true;
|
||||||
this->fios_items.BuildFileList(this->abstract_filetype, this->fop);
|
this->fios_items.BuildFileList(this->abstract_filetype, this->fop);
|
||||||
this->vscroll->SetCount((uint)this->fios_items.Length());
|
this->vscroll->SetCount((uint)this->fios_items.size());
|
||||||
this->selected = nullptr;
|
this->selected = nullptr;
|
||||||
_load_check_data.Clear();
|
_load_check_data.Clear();
|
||||||
|
|
||||||
|
@ -852,10 +852,10 @@ public:
|
||||||
|
|
||||||
case SLIWD_FILTER_CHANGES:
|
case SLIWD_FILTER_CHANGES:
|
||||||
/* Filter changes */
|
/* Filter changes */
|
||||||
this->fios_items_shown.resize(this->fios_items.Length());
|
this->fios_items_shown.resize(this->fios_items.size());
|
||||||
uint items_shown_count = 0; ///< The number of items shown in the list
|
uint items_shown_count = 0; ///< The number of items shown in the list
|
||||||
/* We pass through every fios item */
|
/* We pass through every fios item */
|
||||||
for (uint i = 0; i < this->fios_items.Length(); i++) {
|
for (uint i = 0; i < this->fios_items.size(); i++) {
|
||||||
if (this->string_filter.IsEmpty()) {
|
if (this->string_filter.IsEmpty()) {
|
||||||
/* We don't filter anything out if the filter editbox is empty */
|
/* We don't filter anything out if the filter editbox is empty */
|
||||||
this->fios_items_shown[i] = true;
|
this->fios_items_shown[i] = true;
|
||||||
|
|
|
@ -209,7 +209,7 @@ void FiosGetDrives(FileList &file_list)
|
||||||
|
|
||||||
GetLogicalDriveStrings(lengthof(drives), drives);
|
GetLogicalDriveStrings(lengthof(drives), drives);
|
||||||
for (s = drives; *s != '\0';) {
|
for (s = drives; *s != '\0';) {
|
||||||
FiosItem *fios = file_list.Append();
|
FiosItem *fios = &file_list.emplace_back();
|
||||||
fios->type = FIOS_TYPE_DRIVE;
|
fios->type = FIOS_TYPE_DRIVE;
|
||||||
fios->mtime = 0;
|
fios->mtime = 0;
|
||||||
seprintf(fios->name, lastof(fios->name), "%c:", s[0] & 0xFF);
|
seprintf(fios->name, lastof(fios->name), "%c:", s[0] & 0xFF);
|
||||||
|
|
Loading…
Reference in New Issue