dirname.cpp 811 B

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