Codechange: Make FiosItem's name and title std::string

This commit is contained in:
Rubidium 2023-04-16 21:56:10 +02:00 committed by rubidium42
parent ee723f26ba
commit 3a822fcd01
6 changed files with 37 additions and 40 deletions

View File

@ -457,7 +457,7 @@ DEF_CONSOLE_CMD(ConRemove)
_console_file_list.ValidateFileList(); _console_file_list.ValidateFileList();
const FiosItem *item = _console_file_list.FindItem(file); const FiosItem *item = _console_file_list.FindItem(file);
if (item != nullptr) { if (item != nullptr) {
if (unlink(item->name) != 0) { if (unlink(item->name.c_str()) != 0) {
IConsolePrint(CC_ERROR, "Failed to delete '{}'.", item->name); IConsolePrint(CC_ERROR, "Failed to delete '{}'.", item->name);
} }
} else { } else {

View File

@ -56,7 +56,7 @@ bool FiosItem::operator< (const FiosItem &other) const
int r = false; int r = false;
if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) { if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
r = (*this).mtime - other.mtime; r = this->mtime - other.mtime;
} else { } else {
r = StrNaturalCompare((*this).title, other.title); 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. * or a numbered entry into the filename list.
* @return The information on the file, or \c nullptr if the file is not available. * @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) { for (const auto &it : *this) {
const FiosItem *item = &it; const FiosItem *item = &it;
if (strcmp(file, item->name) == 0) return item; if (file == item->name) return item;
if (strcmp(file, item->title) == 0) return item; if (file == item->title) return item;
} }
/* If no name matches, try to parse it as number */ /* If no name matches, try to parse it as number */
char *endptr; char *endptr;
int i = std::strtol(file, &endptr, 10); int i = std::strtol(file.data(), &endptr, 10);
if (file == endptr || *endptr != '\0') i = -1; if (file.data() == endptr || *endptr != '\0') i = -1;
if (IsInsideMM(i, 0, this->size())) return &this->at(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]; std::string long_file(file);
seprintf(long_file, lastof(long_file), "%s.sav", file); long_file += ".sav";
for (const auto &it : *this) { for (const auto &it : *this) {
const FiosItem *item = &it; const FiosItem *item = &it;
if (strcmp(long_file, item->name) == 0) return item; if (long_file == item->name) return item;
if (strcmp(long_file, item->title) == 0) return item; if (long_file == item->title) return item;
} }
return nullptr; return nullptr;
@ -153,7 +153,7 @@ bool FiosBrowseTo(const FiosItem *item)
case FIOS_TYPE_DRIVE: case FIOS_TYPE_DRIVE:
#if defined(_WIN32) || defined(__OS2__) #if defined(_WIN32) || defined(__OS2__)
assert(_fios_path != nullptr); assert(_fios_path != nullptr);
*_fios_path = std::string{ item->title[0] } + ":" PATHSEP; *_fios_path = std::string{ item->title, 0, 1 } + ":" PATHSEP;
#endif #endif
break; break;
@ -334,7 +334,7 @@ bool FiosFileScanner::AddFile(const std::string &filename, size_t basepath_lengt
} }
fios->type = type; 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 */ /* If the file doesn't have a title, use its filename */
const char *t = fios_title; 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); auto ps = filename.rfind(PATHSEPCHAR);
t = filename.c_str() + (ps == std::string::npos ? 0 : ps + 1); t = filename.c_str() + (ps == std::string::npos ? 0 : ps + 1);
} }
strecpy(fios->title, t, lastof(fios->title)); fios->title = StrMakeValid(t);
StrMakeValidInPlace(fios->title, lastof(fios->title));
return true; return true;
} }
@ -363,7 +362,6 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
DIR *dir; DIR *dir;
FiosItem *fios; FiosItem *fios;
size_t sort_start; size_t sort_start;
char d_name[sizeof(fios->name)];
file_list.clear(); file_list.clear();
@ -374,28 +372,27 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
fios = &file_list.emplace_back(); 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)); fios->name = "..";
SetDParamStr(0, ".."); SetDParamStr(0, "..");
GetString(fios->title, STR_SAVELOAD_PARENT_DIRECTORY, lastof(fios->title)); fios->title = GetString(STR_SAVELOAD_PARENT_DIRECTORY);
} }
/* Show subdirectories */ /* Show subdirectories */
if ((dir = ttd_opendir(_fios_path->c_str())) != nullptr) { if ((dir = ttd_opendir(_fios_path->c_str())) != nullptr) {
while ((dirent = readdir(dir)) != 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 '..' */ /* found file must be directory, but not '.' or '..' */
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) || StrStartsWithIgnoreCase(PERSONAL_DIR, d_name)) && (!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 = &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)); fios->name = d_name;
std::string dirname = std::string(d_name) + PATHSEP; std::string dirname = fios->name + PATHSEP;
SetDParamStr(0, dirname); SetDParamStr(0, dirname);
GetString(fios->title, STR_SAVELOAD_DIRECTORY, lastof(fios->title)); fios->title = GetString(STR_SAVELOAD_DIRECTORY);
StrMakeValidInPlace(fios->title, lastof(fios->title));
} }
} }
closedir(dir); closedir(dir);

View File

@ -88,8 +88,8 @@ extern LoadCheckData _load_check_data;
struct FiosItem { struct FiosItem {
FiosType type; FiosType type;
uint64 mtime; uint64 mtime;
char title[64]; std::string title;
char name[MAX_PATH]; std::string name;
bool operator< (const FiosItem &other) const; bool operator< (const FiosItem &other) const;
}; };
@ -97,7 +97,7 @@ struct FiosItem {
class FileList : public std::vector<FiosItem> { class FileList : public std::vector<FiosItem> {
public: public:
void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop); void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop);
const FiosItem *FindItem(const char *file); const FiosItem *FindItem(const std::string_view file);
}; };
enum SortingBits { enum SortingBits {

View File

@ -378,24 +378,22 @@ public:
/* Select the initial directory. */ /* Select the initial directory. */
o_dir.type = FIOS_TYPE_DIRECT; o_dir.type = FIOS_TYPE_DIRECT;
std::string dir;
switch (this->abstract_filetype) { switch (this->abstract_filetype) {
case FT_SAVEGAME: case FT_SAVEGAME:
dir = FioFindDirectory(SAVE_DIR); o_dir.name = FioFindDirectory(SAVE_DIR);
break; break;
case FT_SCENARIO: case FT_SCENARIO:
dir = FioFindDirectory(SCENARIO_DIR); o_dir.name = FioFindDirectory(SCENARIO_DIR);
break; break;
case FT_HEIGHTMAP: case FT_HEIGHTMAP:
dir = FioFindDirectory(HEIGHTMAP_DIR); o_dir.name = FioFindDirectory(HEIGHTMAP_DIR);
break; break;
default: default:
dir = _personal_dir; o_dir.name = _personal_dir;
} }
strecpy(o_dir.name, dir.c_str(), lastof(o_dir.name));
switch (this->fop) { switch (this->fop) {
case SLO_SAVE: case SLO_SAVE:
@ -693,7 +691,7 @@ public:
} }
if (this->fop == SLO_SAVE) { if (this->fop == SLO_SAVE) {
/* Copy clicked name to editbox */ /* 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); this->SetWidgetDirty(WID_SL_SAVE_OSK_TITLE);
} }
} else if (!_load_check_data.HasErrors()) { } else if (!_load_check_data.HasErrors()) {
@ -876,7 +874,7 @@ public:
items_shown_count++; items_shown_count++;
} else { } else {
this->string_filter.ResetState(); 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 */ /* 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(); this->fios_items_shown[i] = this->string_filter.GetState();
if (this->fios_items_shown[i]) items_shown_count++; if (this->fios_items_shown[i]) items_shown_count++;

View File

@ -76,11 +76,12 @@ void FiosGetDrives(FileList &file_list)
fios->type = FIOS_TYPE_DRIVE; fios->type = FIOS_TYPE_DRIVE;
fios->mtime = 0; fios->mtime = 0;
#ifndef __INNOTEK_LIBC__ #ifndef __INNOTEK_LIBC__
snprintf(fios->name, lengthof(fios->name), "%c:", 'A' + disk - 1); fios->name += 'A' + disk - 1;
#else #else
snprintf(fios->name, lengthof(fios->name), "%c:", disk); fios->name += (char)disk;
#endif #endif
strecpy(fios->title, fios->name, lastof(fios->title)); fios->name += ':';
fios->title = fios->name;
} }
} }

View File

@ -185,8 +185,9 @@ void FiosGetDrives(FileList &file_list)
FiosItem *fios = &file_list.emplace_back(); 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); fios->name += (char)(s[0] & 0xFF);
strecpy(fios->title, fios->name, lastof(fios->title)); fios->name += ':';
fios->title = fios->name;
while (*s++ != '\0') { /* Nothing */ } while (*s++ != '\0') { /* Nothing */ }
} }
} }