diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 90dd2544ca..5a3345d512 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -457,7 +457,7 @@ DEF_CONSOLE_CMD(ConRemove) _console_file_list.ValidateFileList(); const FiosItem *item = _console_file_list.FindItem(file); if (item != nullptr) { - if (unlink(item->name) != 0) { + if (unlink(item->name.c_str()) != 0) { IConsolePrint(CC_ERROR, "Failed to delete '{}'.", item->name); } } else { diff --git a/src/fios.cpp b/src/fios.cpp index 314a582927..4ac6a6798f 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -56,7 +56,7 @@ bool FiosItem::operator< (const FiosItem &other) const int r = false; if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) { - r = (*this).mtime - other.mtime; + r = this->mtime - other.mtime; } else { r = StrNaturalCompare((*this).title, other.title); } @@ -101,29 +101,29 @@ void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperati * or a numbered entry into the filename list. * @return The information on the file, or \c nullptr if the file is not available. */ -const FiosItem *FileList::FindItem(const char *file) +const FiosItem *FileList::FindItem(const std::string_view file) { for (const auto &it : *this) { const FiosItem *item = ⁢ - if (strcmp(file, item->name) == 0) return item; - if (strcmp(file, item->title) == 0) return item; + if (file == item->name) return item; + if (file == item->title) return item; } /* If no name matches, try to parse it as number */ char *endptr; - int i = std::strtol(file, &endptr, 10); - if (file == endptr || *endptr != '\0') i = -1; + int i = std::strtol(file.data(), &endptr, 10); + if (file.data() == endptr || *endptr != '\0') i = -1; 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); + std::string long_file(file); + long_file += ".sav"; 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; + if (long_file == item->name) return item; + if (long_file == item->title) return item; } return nullptr; @@ -153,7 +153,7 @@ bool FiosBrowseTo(const FiosItem *item) case FIOS_TYPE_DRIVE: #if defined(_WIN32) || defined(__OS2__) assert(_fios_path != nullptr); - *_fios_path = std::string{ item->title[0] } + ":" PATHSEP; + *_fios_path = std::string{ item->title, 0, 1 } + ":" PATHSEP; #endif break; @@ -334,7 +334,7 @@ bool FiosFileScanner::AddFile(const std::string &filename, size_t basepath_lengt } fios->type = type; - strecpy(fios->name, filename.c_str(), lastof(fios->name)); + fios->name = filename; /* If the file doesn't have a title, use its filename */ const char *t = fios_title; @@ -342,8 +342,7 @@ bool FiosFileScanner::AddFile(const std::string &filename, size_t basepath_lengt auto ps = filename.rfind(PATHSEPCHAR); t = filename.c_str() + (ps == std::string::npos ? 0 : ps + 1); } - strecpy(fios->title, t, lastof(fios->title)); - StrMakeValidInPlace(fios->title, lastof(fios->title)); + fios->title = StrMakeValid(t); return true; } @@ -363,7 +362,6 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c DIR *dir; FiosItem *fios; size_t sort_start; - char d_name[sizeof(fios->name)]; file_list.clear(); @@ -374,28 +372,27 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c fios = &file_list.emplace_back(); fios->type = FIOS_TYPE_PARENT; fios->mtime = 0; - strecpy(fios->name, "..", lastof(fios->name)); + fios->name = ".."; SetDParamStr(0, ".."); - GetString(fios->title, STR_SAVELOAD_PARENT_DIRECTORY, lastof(fios->title)); + fios->title = GetString(STR_SAVELOAD_PARENT_DIRECTORY); } /* Show subdirectories */ if ((dir = ttd_opendir(_fios_path->c_str())) != nullptr) { while ((dirent = readdir(dir)) != nullptr) { - strecpy(d_name, FS2OTTD(dirent->d_name).c_str(), lastof(d_name)); + std::string d_name = FS2OTTD(dirent->d_name); /* found file must be directory, but not '.' or '..' */ if (FiosIsValidFile(_fios_path->c_str(), dirent, &sb) && S_ISDIR(sb.st_mode) && (!FiosIsHiddenFile(dirent) || StrStartsWithIgnoreCase(PERSONAL_DIR, d_name)) && - strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) { + d_name != "." && d_name != "..") { fios = &file_list.emplace_back(); fios->type = FIOS_TYPE_DIR; fios->mtime = 0; - strecpy(fios->name, d_name, lastof(fios->name)); - std::string dirname = std::string(d_name) + PATHSEP; + fios->name = d_name; + std::string dirname = fios->name + PATHSEP; SetDParamStr(0, dirname); - GetString(fios->title, STR_SAVELOAD_DIRECTORY, lastof(fios->title)); - StrMakeValidInPlace(fios->title, lastof(fios->title)); + fios->title = GetString(STR_SAVELOAD_DIRECTORY); } } closedir(dir); diff --git a/src/fios.h b/src/fios.h index 9cc48ac927..0d9b170541 100644 --- a/src/fios.h +++ b/src/fios.h @@ -88,8 +88,8 @@ extern LoadCheckData _load_check_data; struct FiosItem { FiosType type; uint64 mtime; - char title[64]; - char name[MAX_PATH]; + std::string title; + std::string name; bool operator< (const FiosItem &other) const; }; @@ -97,7 +97,7 @@ struct FiosItem { class FileList : public std::vector { public: void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop); - const FiosItem *FindItem(const char *file); + const FiosItem *FindItem(const std::string_view file); }; enum SortingBits { diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index 7a1cf0a06c..11d74b5e4c 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -378,24 +378,22 @@ public: /* Select the initial directory. */ o_dir.type = FIOS_TYPE_DIRECT; - std::string dir; switch (this->abstract_filetype) { case FT_SAVEGAME: - dir = FioFindDirectory(SAVE_DIR); + o_dir.name = FioFindDirectory(SAVE_DIR); break; case FT_SCENARIO: - dir = FioFindDirectory(SCENARIO_DIR); + o_dir.name = FioFindDirectory(SCENARIO_DIR); break; case FT_HEIGHTMAP: - dir = FioFindDirectory(HEIGHTMAP_DIR); + o_dir.name = FioFindDirectory(HEIGHTMAP_DIR); break; default: - dir = _personal_dir; + o_dir.name = _personal_dir; } - strecpy(o_dir.name, dir.c_str(), lastof(o_dir.name)); switch (this->fop) { case SLO_SAVE: @@ -693,7 +691,7 @@ public: } if (this->fop == SLO_SAVE) { /* Copy clicked name to editbox */ - this->filename_editbox.text.Assign(file->title); + this->filename_editbox.text.Assign(file->title.c_str()); this->SetWidgetDirty(WID_SL_SAVE_OSK_TITLE); } } else if (!_load_check_data.HasErrors()) { @@ -876,7 +874,7 @@ public: items_shown_count++; } else { this->string_filter.ResetState(); - this->string_filter.AddLine(this->fios_items[i].title); + this->string_filter.AddLine(this->fios_items[i].title.c_str()); /* We set the vector to show this fios element as filtered depending on the result of the filter */ this->fios_items_shown[i] = this->string_filter.GetState(); if (this->fios_items_shown[i]) items_shown_count++; diff --git a/src/os/os2/os2.cpp b/src/os/os2/os2.cpp index fbcd230eb7..be62ab466a 100644 --- a/src/os/os2/os2.cpp +++ b/src/os/os2/os2.cpp @@ -76,11 +76,12 @@ void FiosGetDrives(FileList &file_list) fios->type = FIOS_TYPE_DRIVE; fios->mtime = 0; #ifndef __INNOTEK_LIBC__ - snprintf(fios->name, lengthof(fios->name), "%c:", 'A' + disk - 1); + fios->name += 'A' + disk - 1; #else - snprintf(fios->name, lengthof(fios->name), "%c:", disk); + fios->name += (char)disk; #endif - strecpy(fios->title, fios->name, lastof(fios->title)); + fios->name += ':'; + fios->title = fios->name; } } diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index 1a482daea5..b1040d1d5f 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -185,8 +185,9 @@ void FiosGetDrives(FileList &file_list) FiosItem *fios = &file_list.emplace_back(); fios->type = FIOS_TYPE_DRIVE; fios->mtime = 0; - seprintf(fios->name, lastof(fios->name), "%c:", s[0] & 0xFF); - strecpy(fios->title, fios->name, lastof(fios->title)); + fios->name += (char)(s[0] & 0xFF); + fios->name += ':'; + fios->title = fios->name; while (*s++ != '\0') { /* Nothing */ } } }