path.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "dynamic_string.h"
  6. #include "path.h"
  7. #include <ctype.h> // isalpha
  8. #include <string.h> // strrchr
  9. namespace crown
  10. {
  11. #if CROWN_PLATFORM_POSIX
  12. const char PATH_SEPARATOR = '/';
  13. #elif CROWN_PLATFORM_WINDOWS
  14. const char PATH_SEPARATOR = '\\';
  15. #endif // CROWN_PLATFORM_POSIX
  16. namespace path
  17. {
  18. bool is_absolute(const char* path)
  19. {
  20. CE_ENSURE(NULL != path);
  21. #if CROWN_PLATFORM_POSIX
  22. return strlen32(path) > 0
  23. && path[0] == PATH_SEPARATOR
  24. ;
  25. #elif CROWN_PLATFORM_WINDOWS
  26. return strlen32(path) > 2
  27. && isalpha(path[0])
  28. && path[1] == ':'
  29. && path[2] == PATH_SEPARATOR
  30. ;
  31. #endif
  32. }
  33. bool is_relative(const char* path)
  34. {
  35. CE_ENSURE(NULL != path);
  36. return !is_absolute(path);
  37. }
  38. bool is_root(const char* path)
  39. {
  40. CE_ENSURE(NULL != path);
  41. #if CROWN_PLATFORM_POSIX
  42. return is_absolute(path) && strlen32(path) == 1;
  43. #elif CROWN_PLATFORM_WINDOWS
  44. return is_absolute(path) && strlen32(path) == 3;
  45. #endif
  46. }
  47. void join(DynamicString& path, const char* path_a, const char* path_b)
  48. {
  49. CE_ENSURE(NULL != path_a);
  50. CE_ENSURE(NULL != path_b);
  51. const u32 la = strlen32(path_a);
  52. const u32 lb = strlen32(path_b);
  53. path.reserve(la + lb + 1);
  54. path += path_a;
  55. path += PATH_SEPARATOR;
  56. path += path_b;
  57. }
  58. const char* basename(const char* path)
  59. {
  60. CE_ENSURE(NULL != path);
  61. const char* ls = strrchr(path, '/');
  62. return ls == NULL ? path : ls + 1;
  63. }
  64. const char* extension(const char* path)
  65. {
  66. CE_ENSURE(NULL != path);
  67. const char* ld = strrchr(path, '.');
  68. return ld == NULL ? NULL : ld + 1;
  69. }
  70. bool has_trailing_separator(const char* path)
  71. {
  72. CE_ENSURE(NULL != path);
  73. return path[strlen32(path) - 1] == PATH_SEPARATOR;
  74. }
  75. inline bool any_separator(char c)
  76. {
  77. return c == '/' || c == '\\';
  78. }
  79. void reduce(DynamicString& clean, const char* path)
  80. {
  81. if (path == NULL)
  82. return;
  83. char cc = any_separator(*path) ? PATH_SEPARATOR : *path;
  84. clean += cc;
  85. ++path;
  86. for (; *path; ++path)
  87. {
  88. if (cc == PATH_SEPARATOR && (any_separator(*path) || *path == '.'))
  89. continue;
  90. cc = any_separator(*path) ? PATH_SEPARATOR : *path;
  91. clean += cc;
  92. }
  93. if (has_trailing_separator(clean.c_str()))
  94. array::pop_back(clean._data);
  95. }
  96. } // namespace path
  97. } // namespace crown