path.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2012-2016 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. namespace path
  12. {
  13. bool is_absolute(const char* path)
  14. {
  15. CE_ASSERT(path != NULL, "Path must be != NULL");
  16. #if CROWN_PLATFORM_POSIX
  17. return strlen32(path) > 0
  18. && path[0] == PATH_SEPARATOR
  19. ;
  20. #elif CROWN_PLATFORM_WINDOWS
  21. return strlen32(path) > 2
  22. && isalpha(path[0])
  23. && path[1] == ':'
  24. && path[2] == PATH_SEPARATOR
  25. ;
  26. #endif
  27. }
  28. bool is_root(const char* path)
  29. {
  30. CE_ASSERT(path != NULL, "Path must be != NULL");
  31. #if CROWN_PLATFORM_POSIX
  32. return is_absolute(path) && strlen32(path) == 1;
  33. #elif CROWN_PLATFORM_WINDOWS
  34. return is_absolute(path) && strlen32(path) == 3;
  35. #endif
  36. }
  37. void join(const char* a, const char* b, DynamicString& path)
  38. {
  39. const u32 la = strlen32(a);
  40. const u32 lb = strlen32(b);
  41. path.reserve(la + lb + 1);
  42. path += a;
  43. path += PATH_SEPARATOR;
  44. path += b;
  45. }
  46. const char* basename(const char* path)
  47. {
  48. const char* ls = strrchr(path, '/');
  49. return ls == NULL ? path : ls + 1;
  50. }
  51. const char* extension(const char* path)
  52. {
  53. const char* ld = strrchr(path, '.');
  54. return ld == NULL ? NULL : ld + 1;
  55. }
  56. } // namespace path
  57. } // namespace crown