Add helper functions to get last path segment

This commit is contained in:
Jonathan G Rennison
2024-01-05 22:13:17 +00:00
parent 7fdcbced09
commit 0ab4b8ea31
5 changed files with 22 additions and 15 deletions

View File

@@ -1051,12 +1051,7 @@ std::string MidiFile::GetSMFFile(const MusicSongInfo &song)
char basename[MAX_PATH]; char basename[MAX_PATH];
{ {
const char *fnstart = strrchr(song.filename.c_str(), PATHSEPCHAR); const char *fnstart = StrLastPathSegment(song.filename);
if (fnstart == nullptr) {
fnstart = song.filename.c_str();
} else {
fnstart++;
}
/* Remove all '.' characters from filename */ /* Remove all '.' characters from filename */
char *wp = basename; char *wp = basename;

View File

@@ -150,13 +150,7 @@ static void SurveyConfiguration(nlohmann::json &survey)
{ {
survey["network"] = _networking ? (_network_server ? "server" : "client") : "no"; survey["network"] = _networking ? (_network_server ? "server" : "client") : "no";
if (_current_language != nullptr) { if (_current_language != nullptr) {
std::string_view language_basename(_current_language->file); survey["language"]["filename"] = StrLastPathSegment(_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"]["name"] = _current_language->name; survey["language"]["name"] = _current_language->name;
survey["language"]["isocode"] = _current_language->isocode; survey["language"]["isocode"] = _current_language->isocode;
} }

View File

@@ -420,6 +420,17 @@ void StrTrimInPlace(std::string &str)
StrRightTrimInPlace(str); StrRightTrimInPlace(str);
} }
const char *StrLastPathSegment(const char *path)
{
const char *best = path;
for (; *path != '\0'; path++) {
if (*path == PATHSEPCHAR || *path == '/') {
if (*(path + 1) != '\0') best = path + 1;
}
}
return best;
}
/** /**
* Check whether the given string starts with the given prefix. * Check whether the given string starts with the given prefix.
* @param str The string to look at. * @param str The string to look at.

View File

@@ -66,6 +66,13 @@ bool strtolower(std::string &str, std::string::size_type offs = 0);
[[nodiscard]] bool StrValid(const char *str, const char *last) NOACCESS(2); [[nodiscard]] bool StrValid(const char *str, const char *last) NOACCESS(2);
void StrTrimInPlace(std::string &str); void StrTrimInPlace(std::string &str);
const char *StrLastPathSegment(const char *path);
inline const char *StrLastPathSegment(const std::string &path)
{
return StrLastPathSegment(path.c_str());
}
[[nodiscard]] bool StrStartsWith(const std::string_view str, const std::string_view prefix); [[nodiscard]] bool StrStartsWith(const std::string_view str, const std::string_view prefix);
[[nodiscard]] bool StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix); [[nodiscard]] bool StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix);
[[nodiscard]] bool StrEndsWith(const std::string_view str, const std::string_view suffix); [[nodiscard]] bool StrEndsWith(const std::string_view str, const std::string_view suffix);

View File

@@ -2268,7 +2268,7 @@ bool ReadLanguagePack(const LanguageMetadata *lang)
_current_language = lang; _current_language = lang;
const TextDirection old_text_dir = _current_text_dir; const TextDirection old_text_dir = _current_text_dir;
_current_text_dir = (TextDirection)_current_language->text_dir; _current_text_dir = (TextDirection)_current_language->text_dir;
const char *c_file = strrchr(_current_language->file, PATHSEPCHAR) + 1; const char *c_file = StrLastPathSegment(_current_language->file);
_config_language_file = c_file; _config_language_file = c_file;
SetCurrentGrfLangID(_current_language->newgrflangid); SetCurrentGrfLangID(_current_language->newgrflangid);
@@ -2444,7 +2444,7 @@ void InitializeLanguagePacks()
/* We are trying to find a default language. The priority is by /* We are trying to find a default language. The priority is by
* configuration file, local environment and last, if nothing found, * configuration file, local environment and last, if nothing found,
* English. */ * English. */
const char *lang_file = strrchr(lng.file, PATHSEPCHAR) + 1; const char *lang_file = StrLastPathSegment(lng.file);
if (_config_language_file == lang_file) { if (_config_language_file == lang_file) {
chosen_language = &lng; chosen_language = &lng;
break; break;