dirname.cpp 1.1 KB

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