diff --git a/src/crashlog.cpp b/src/crashlog.cpp index 0d613fdd10..141b2bb67e 100644 --- a/src/crashlog.cpp +++ b/src/crashlog.cpp @@ -34,6 +34,7 @@ #include "company_base.h" #include "company_func.h" #include "3rdparty/fmt/chrono.h" +#include "3rdparty/fmt/std.h" #ifdef WITH_ALLEGRO # include @@ -167,7 +168,7 @@ void CrashLog::LogConfiguration(std::back_insert_iterator &output_i BlitterFactory::GetCurrentBlitter() == nullptr ? "none" : BlitterFactory::GetCurrentBlitter()->GetName(), BaseGraphics::GetUsedSet() == nullptr ? "none" : BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet() == nullptr ? UINT32_MAX : BaseGraphics::GetUsedSet()->version, - _current_language == nullptr ? "none" : _current_language->file, + _current_language == nullptr ? "none" : _current_language->file.filename(), MusicDriver::GetInstance() == nullptr ? "none" : MusicDriver::GetInstance()->GetName(), BaseMusic::GetUsedSet() == nullptr ? "none" : BaseMusic::GetUsedSet()->name, BaseMusic::GetUsedSet() == nullptr ? UINT32_MAX : BaseMusic::GetUsedSet()->version, diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp index adfdcd6ebe..d3b58b9391 100644 --- a/src/game/game_text.cpp +++ b/src/game/game_text.cpp @@ -384,19 +384,7 @@ void ReconsiderGameScriptLanguage() { if (_current_data == nullptr) return; - char temp[MAX_PATH]; - strecpy(temp, _current_language->file, lastof(temp)); - - /* Remove the extension */ - char *l = strrchr(temp, '.'); - assert(l != nullptr); - *l = '\0'; - - /* Skip the path */ - char *language = strrchr(temp, PATHSEPCHAR); - assert(language != nullptr); - language++; - + std::string language = _current_language->file.stem().string(); for (auto &p : _current_data->compiled_strings) { if (p.language == language) { _current_data->cur_language = &p; diff --git a/src/language.h b/src/language.h index 0966ccb197..79dda356b3 100644 --- a/src/language.h +++ b/src/language.h @@ -14,6 +14,7 @@ #include #endif /* WITH_ICU_I18N */ #include "strings_type.h" +#include static const uint8 CASE_GENDER_LEN = 16; ///< The (maximum) length of a case/gender string. static const uint8 MAX_NUM_GENDERS = 8; ///< Maximum number of supported genders. @@ -90,7 +91,7 @@ static_assert(sizeof(LanguagePackHeader) % 4 == 0); /** Metadata about a single language. */ struct LanguageMetadata : public LanguagePackHeader { - char file[MAX_PATH]; ///< Name of the file we read this data from. + std::filesystem::path file; ///< Name of the file we read this data from. }; /** Type for the list of language meta data. */ diff --git a/src/network/network_survey.cpp b/src/network/network_survey.cpp index b3f64662a6..97b0c43f41 100644 --- a/src/network/network_survey.cpp +++ b/src/network/network_survey.cpp @@ -176,13 +176,7 @@ static void SurveyConfiguration(nlohmann::json &survey) { survey["network"] = _networking ? (_network_server ? "server" : "client") : "no"; if (_current_language != nullptr) { - std::string_view language_basename(_current_language->file); - auto e = language_basename.rfind(PATHSEPCHAR); - if (e != std::string::npos) { - language_basename = language_basename.substr(e + 1); - } - - survey["language"]["filename"] = language_basename; + survey["language"]["filename"] = _current_language->file.filename().string(); survey["language"]["name"] = _current_language->name; survey["language"]["isocode"] = _current_language->isocode; } diff --git a/src/strings.cpp b/src/strings.cpp index ebe05671be..808b7daf10 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -42,6 +42,7 @@ #include "table/strings.h" #include "table/control_codes.h" +#include "3rdparty/fmt/std.h" #include "strings_internal.h" @@ -1827,7 +1828,7 @@ bool ReadLanguagePack(const LanguageMetadata *lang) { /* Current language pack */ size_t len = 0; - std::unique_ptr lang_pack(reinterpret_cast(ReadFileToMem(lang->file, len, 1U << 20).release())); + std::unique_ptr lang_pack(reinterpret_cast(ReadFileToMem(lang->file.string(), len, 1U << 20).release())); if (!lang_pack) return false; /* End of read data (+ terminating zero added in ReadFileToMem()) */ @@ -1882,8 +1883,7 @@ bool ReadLanguagePack(const LanguageMetadata *lang) _current_language = lang; _current_text_dir = (TextDirection)_current_language->text_dir; - const char *c_file = strrchr(_current_language->file, PATHSEPCHAR) + 1; - _config_language_file = c_file; + _config_language_file = _current_language->file.filename().string(); SetCurrentGrfLangID(_current_language->newgrflangid); #ifdef _WIN32 @@ -2007,9 +2007,9 @@ static bool GetLanguageFileHeader(const char *file, LanguagePackHeader *hdr) * Gets a list of languages from the given directory. * @param path the base directory to search in */ -static void GetLanguageList(const char *path) +static void GetLanguageList(const std::string &path) { - DIR *dir = ttd_opendir(path); + DIR *dir = ttd_opendir(path.c_str()); if (dir != nullptr) { struct dirent *dirent; while ((dirent = readdir(dir)) != nullptr) { @@ -2020,10 +2020,10 @@ static void GetLanguageList(const char *path) if (extension == nullptr || strcmp(extension, ".lng") != 0) continue; LanguageMetadata lmd; - seprintf(lmd.file, lastof(lmd.file), "%s%s", path, d_name.c_str()); + lmd.file = path + d_name; /* Check whether the file is of the correct version */ - if (!GetLanguageFileHeader(lmd.file, &lmd)) { + if (!GetLanguageFileHeader(lmd.file.string().c_str(), &lmd)) { Debug(misc, 3, "{} is not a valid language file", lmd.file); } else if (GetLanguage(lmd.newgrflangid) != nullptr) { Debug(misc, 3, "{}'s language ID is already known", lmd.file); @@ -2042,8 +2042,7 @@ static void GetLanguageList(const char *path) void InitializeLanguagePacks() { for (Searchpath sp : _valid_searchpaths) { - std::string path = FioGetDirectory(sp, LANG_DIR); - GetLanguageList(path.c_str()); + GetLanguageList(FioGetDirectory(sp, LANG_DIR)); } if (_languages.size() == 0) UserError("No available language packs (invalid versions?)"); @@ -2060,8 +2059,7 @@ void InitializeLanguagePacks() /* We are trying to find a default language. The priority is by * configuration file, local environment and last, if nothing found, * English. */ - const char *lang_file = strrchr(lng.file, PATHSEPCHAR) + 1; - if (_config_language_file == lang_file) { + if (_config_language_file == lng.file.filename()) { chosen_language = &lng; break; }