Add helper functions to get last path segment

pull/621/head
Jonathan G Rennison 5 months ago
parent 7fdcbced09
commit 0ab4b8ea31

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

@ -150,13 +150,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"] = StrLastPathSegment(_current_language->file);
survey["language"]["name"] = _current_language->name;
survey["language"]["isocode"] = _current_language->isocode;
}

@ -420,6 +420,17 @@ void StrTrimInPlace(std::string &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.
* @param str The string to look at.

@ -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);
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 StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix);
[[nodiscard]] bool StrEndsWith(const std::string_view str, const std::string_view suffix);

@ -2268,7 +2268,7 @@ bool ReadLanguagePack(const LanguageMetadata *lang)
_current_language = lang;
const TextDirection old_text_dir = _current_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;
SetCurrentGrfLangID(_current_language->newgrflangid);
@ -2444,7 +2444,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;
const char *lang_file = StrLastPathSegment(lng.file);
if (_config_language_file == lang_file) {
chosen_language = &lng;
break;

Loading…
Cancel
Save