dirname.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #if defined (WIN32)
  18. char del('\\');
  19. #else
  20. char del('/');
  21. #endif
  22. // http://stackoverflow.com/questions/5077693/dirnamephp-similar-function-in-c
  23. std::string::const_reverse_iterator last_slash =
  24. std::find(
  25. path.rbegin(),
  26. path.rend(),del);
  27. if( last_slash == path.rend() )
  28. {
  29. // No slashes found
  30. return std::string(".");
  31. }else if(1 == (last_slash.base() - path.begin()))
  32. {
  33. // Slash is first char
  34. return std::string(1,del);
  35. }else if(path.end() == last_slash.base() )
  36. {
  37. // Slash is last char
  38. std::string redo = std::string(path.begin(),path.end()-1);
  39. return igl::dirname(redo);
  40. }
  41. return std::string(path.begin(),last_slash.base()-1);
  42. }