From f018471b36fe1ffaedf98430b4156ad369e26c66 Mon Sep 17 00:00:00 2001 From: PeterN Date: Thu, 29 Apr 2021 22:46:42 +0100 Subject: Cleanup: Remove old FiosList helper methods. (#9139) --- src/console_cmds.cpp | 4 +-- src/fios.cpp | 35 +++++++++----------- src/fios.h | 86 +----------------------------------------------- src/fios_gui.cpp | 20 +++++------ src/os/windows/win32.cpp | 2 +- 5 files changed, 30 insertions(+), 117 deletions(-) (limited to 'src') diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 9dce39909..401a52b1f 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -59,7 +59,7 @@ public: /** Declare the file storage cache as being invalid, also clears all stored files. */ void InvalidateFileList() { - this->Clear(); + this->clear(); this->file_list_valid = false; } @@ -403,7 +403,7 @@ DEF_CONSOLE_CMD(ConListFiles) } _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); } diff --git a/src/fios.cpp b/src/fios.cpp index b68da08f8..8528e8c62 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -63,11 +63,6 @@ bool FiosItem::operator< (const FiosItem &other) const 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. * @param abstract_filetype Kind of files to collect. @@ -75,7 +70,7 @@ FileList::~FileList() */ void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop) { - this->Clear(); + this->clear(); assert(fop == SLO_LOAD || fop == SLO_SAVE); switch (abstract_filetype) { @@ -107,7 +102,8 @@ void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperati */ 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->title) == 0) return item; } @@ -117,13 +113,14 @@ const FiosItem *FileList::FindItem(const char *file) int i = strtol(file, &endptr, 10); 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 * 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++) { + for (const auto &it : *this) { + const FiosItem *item = ⁢ if (strcmp(long_file, item->name) == 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)); if (type == FIOS_TYPE_INVALID) return false; - for (const FiosItem *fios = file_list.Begin(); fios != file_list.End(); fios++) { - if (filename == fios->name) return false; + for (const auto &fios : file_list) { + if (filename == fios.name) return false; } - FiosItem *fios = file_list.Append(); + FiosItem *fios = &file_list.emplace_back(); #ifdef _WIN32 // 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); @@ -367,13 +364,13 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c size_t sort_start; char d_name[sizeof(fios->name)]; - file_list.Clear(); + file_list.clear(); assert(_fios_path != nullptr); /* A parent directory link exists if we are not in the root directory */ if (!FiosIsRoot(_fios_path->c_str())) { - fios = file_list.Append(); + fios = &file_list.emplace_back(); fios->type = FIOS_TYPE_PARENT; fios->mtime = 0; 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) && (!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(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->mtime = 0; 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; _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; } /* This is where to start sorting for the filenames */ - sort_start = file_list.Length(); + sort_start = file_list.size(); /* Show files */ 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); } - std::sort(file_list.files.begin() + sort_start, file_list.files.end()); + std::sort(file_list.begin() + sort_start, file_list.end()); /* Show drives */ FiosGetDrives(file_list); - file_list.Compact(); + file_list.shrink_to_fit(); } /** diff --git a/src/fios.h b/src/fios.h index 3a16b6426..28a1cc6aa 100644 --- a/src/fios.h +++ b/src/fios.h @@ -109,94 +109,10 @@ struct FiosItem { }; /** List of file information. */ -class FileList { +class FileList : public std::vector { 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); const FiosItem *FindItem(const char *file); - - std::vector files; ///< The list of files. }; enum SortingBits { diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index e1133dd3c..8de6235d6 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -249,8 +249,8 @@ static void SortSaveGameList(FileList &file_list) * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE) * Only sort savegames/scenarios, not directories */ - for (const FiosItem *item = file_list.Begin(); item != file_list.End(); item++) { - switch (item->type) { + for (const auto &item : file_list) { + switch (item.type) { case FIOS_TYPE_DIR: sort_start++; break; case FIOS_TYPE_PARENT: sort_start++; 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 { @@ -437,14 +437,14 @@ public: uint y = r.top + WD_FRAMERECT_TOP; 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]) { /* The current item is filtered out : we do not show it */ 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) { 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++; i++; } - const FiosItem *file = this->fios_items.Get(y); + const FiosItem *file = &this->fios_items[y]; const char *name = FiosBrowseTo(file); if (name == nullptr) { @@ -734,7 +734,7 @@ public: if (!this->fios_items_shown[i]) y++; i++; } - const FiosItem *file = this->fios_items.Get(y); + const FiosItem *file = &this->fios_items[y]; if (file != this->highlighted) { this->highlighted = file; @@ -812,7 +812,7 @@ public: _fios_path_changed = true; 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; _load_check_data.Clear(); @@ -852,10 +852,10 @@ public: case SLIWD_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 /* 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()) { /* We don't filter anything out if the filter editbox is empty */ this->fios_items_shown[i] = true; diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index ddcfd4866..eee81be40 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -209,7 +209,7 @@ void FiosGetDrives(FileList &file_list) GetLogicalDriveStrings(lengthof(drives), drives); for (s = drives; *s != '\0';) { - FiosItem *fios = file_list.Append(); + FiosItem *fios = &file_list.emplace_back(); fios->type = FIOS_TYPE_DRIVE; fios->mtime = 0; seprintf(fios->name, lastof(fios->name), "%c:", s[0] & 0xFF); -- cgit v1.2.3-70-g09d2