You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.5 KiB

6 years ago
6 years ago
6 years ago
  1. /*
  2. Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include "fs_path.hpp"
  15. #include <string>
  16. #include <dirent.h>
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19. #include <unistd.h>
  20. using std::string;
  21. namespace fs
  22. {
  23. namespace path
  24. {
  25. string
  26. dirname(const char *path_)
  27. {
  28. string path(path_);
  29. return fs::path::dirname(path);
  30. }
  31. string
  32. dirname(const string &path_)
  33. {
  34. std::size_t i;
  35. i = path_.size() - 1;
  36. while((i > 0) && (path_[i] == '/'))
  37. i--;
  38. while((i > 0) && (path_[i] != '/'))
  39. i--;
  40. while((i > 0) && (path_[i] == '/'))
  41. i--;
  42. return path_.substr(0,i+1);
  43. }
  44. string
  45. basename(const string &path_)
  46. {
  47. return path_.substr(path_.find_last_of('/')+1);
  48. }
  49. }
  50. }