basename.cpp 798 B

12345678910111213141516171819202122232425262728293031
  1. #include "basename.h"
  2. #include <algorithm>
  3. IGL_INLINE std::string igl::basename(const std::string & path)
  4. {
  5. if(path == "")
  6. {
  7. return std::string("");
  8. }
  9. // http://stackoverflow.com/questions/5077693/dirnamephp-similar-function-in-c
  10. std::string::const_reverse_iterator last_slash =
  11. std::find(
  12. path.rbegin(),
  13. path.rend(), '/');
  14. if( last_slash == path.rend() )
  15. {
  16. // No slashes found
  17. return path;
  18. }else if(1 == (last_slash.base() - path.begin()))
  19. {
  20. // Slash is first char
  21. return std::string(path.begin()+1,path.end());
  22. }else if(path.end() == last_slash.base() )
  23. {
  24. // Slash is last char
  25. std::string redo = std::string(path.begin(),path.end()-1);
  26. return igl::basename(redo);
  27. }
  28. return std::string(last_slash.base(),path.end());
  29. }