basename.cpp 1.1 KB

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