Filesystem: added optional remove_extension parameter to base_name

This commit is contained in:
Charles Dang 2017-10-06 14:24:29 +11:00
parent b2fcc8d862
commit 04175cddc6
2 changed files with 12 additions and 3 deletions

View file

@ -180,8 +180,11 @@ bool ends_with(const std::string& str, const std::string& suffix);
/**
* Returns the base filename of a file, with directory name stripped.
* Equivalent to a portable basename() function.
*
* If @a remove_extension is true, the filename extension will be stripped
* from the returned value.
*/
std::string base_name(const std::string& file);
std::string base_name(const std::string& file, const bool remove_extension = false);
/**
* Returns the directory name of a file, with filename stripped.

View file

@ -908,9 +908,15 @@ int dir_size(const std::string& pname)
return size_sum;
}
std::string base_name(const std::string& file)
std::string base_name(const std::string& file, const bool remove_extension)
{
return path(file).filename().string();
std::string res = path(file).filename().string();
if(remove_extension) {
return res.substr(0, res.rfind("."));
}
return res;
}
std::string directory_name(const std::string& file)