dirname.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "dirname.h"
  9. #include <algorithm>
  10. #include "verbose.h"
  11. IGL_INLINE std::string igl::dirname(const std::string & path)
  12. {
  13. if(path == "")
  14. {
  15. return std::string("");
  16. }
  17. // http://stackoverflow.com/questions/5077693/dirnamephp-similar-function-in-c
  18. std::string::const_reverse_iterator last_slash =
  19. std::find(
  20. path.rbegin(),
  21. path.rend(), '/');
  22. if( last_slash == path.rend() )
  23. {
  24. // No slashes found
  25. return std::string(".");
  26. }else if(1 == (last_slash.base() - path.begin()))
  27. {
  28. // Slash is first char
  29. return std::string("/");
  30. }else if(path.end() == last_slash.base() )
  31. {
  32. // Slash is last char
  33. std::string redo = std::string(path.begin(),path.end()-1);
  34. return igl::dirname(redo);
  35. }
  36. return std::string(path.begin(),last_slash.base()-1);
  37. }