hpath.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "hpath.h"
  2. #include "hplatform.h"
  3. bool HPath::exists(const char* path) {
  4. return access(path, 0) == 0;
  5. }
  6. bool HPath::isdir(const char* path) {
  7. if (access(path, 0) != 0) return false;
  8. struct stat st;
  9. memset(&st, 0, sizeof(st));
  10. stat(path, &st);
  11. return S_ISDIR(st.st_mode);
  12. }
  13. bool HPath::isfile(const char* path) {
  14. if (access(path, 0) != 0) return false;
  15. struct stat st;
  16. memset(&st, 0, sizeof(st));
  17. stat(path, &st);
  18. return S_ISREG(st.st_mode);
  19. }
  20. bool HPath::islink(const char* path) {
  21. #ifdef OS_WIN
  22. return HPath::isdir(path) && (GetFileAttributesA(path) & FILE_ATTRIBUTE_REPARSE_POINT);
  23. #else
  24. if (access(path, 0) != 0) return false;
  25. struct stat st;
  26. memset(&st, 0, sizeof(st));
  27. lstat(path, &st);
  28. return S_ISLNK(st.st_mode);
  29. #endif
  30. }
  31. std::string HPath::basename(const std::string& filepath) {
  32. std::string::size_type pos1 = filepath.find_last_not_of("/\\");
  33. if (pos1 == std::string::npos) {
  34. return "/";
  35. }
  36. std::string::size_type pos2 = filepath.find_last_of("/\\", pos1);
  37. if (pos2 == std::string::npos) {
  38. pos2 = 0;
  39. } else {
  40. pos2++;
  41. }
  42. return filepath.substr(pos2, pos1-pos2+1);
  43. }
  44. std::string HPath::dirname(const std::string& filepath) {
  45. std::string::size_type pos1 = filepath.find_last_not_of("/\\");
  46. if (pos1 == std::string::npos) {
  47. return "/";
  48. }
  49. std::string::size_type pos2 = filepath.find_last_of("/\\", pos1);
  50. if (pos2 == std::string::npos) {
  51. return ".";
  52. } else if (pos2 == 0) {
  53. pos2 = 1;
  54. }
  55. return filepath.substr(0, pos2);
  56. }
  57. std::string HPath::filename(const std::string& filepath) {
  58. std::string::size_type pos1 = filepath.find_last_of("/\\");
  59. if (pos1 == std::string::npos) {
  60. pos1 = 0;
  61. } else {
  62. pos1++;
  63. }
  64. std::string file = filepath.substr(pos1);
  65. std::string::size_type pos2 = file.find_last_of(".");
  66. if (pos2 == std::string::npos) {
  67. return file;
  68. }
  69. return file.substr(0, pos2);
  70. }
  71. std::string HPath::suffixname(const std::string& filepath) {
  72. std::string::size_type pos1 = filepath.find_last_of("/\\");
  73. if (pos1 == std::string::npos) {
  74. pos1 = 0;
  75. } else {
  76. pos1++;
  77. }
  78. std::string file = filepath.substr(pos1);
  79. std::string::size_type pos2 = file.find_last_of(".");
  80. if (pos2 == std::string::npos) {
  81. return "";
  82. }
  83. return file.substr(pos2+1);
  84. }
  85. std::string HPath::join(const std::string& dir, const std::string& filename) {
  86. char separator = '/';
  87. #ifdef OS_WIN
  88. if (dir.find_first_of("\\") != std::string::npos) {
  89. separator = '\\';
  90. }
  91. #endif
  92. std::string filepath(dir);
  93. if (dir[dir.length()-1] != separator) {
  94. filepath += separator;
  95. }
  96. filepath += filename;
  97. return filepath;
  98. }