Skip to content

Commit

Permalink
Filesystem: added optional remove_extension parameter to base_name
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz authored and GregoryLundberg committed Nov 30, 2017
1 parent d65df4c commit c20a685
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/filesystem.hpp
Expand Up @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions src/filesystem_boost.cpp
Expand Up @@ -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)
Expand Down

0 comments on commit c20a685

Please sign in to comment.