path.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "path.h"
  6. #include "platform.h"
  7. #include <ctype.h> // isalpha
  8. #include <string.h> // strlen, strrchr
  9. namespace crown
  10. {
  11. namespace path
  12. {
  13. bool is_absolute_path(const char* path)
  14. {
  15. CE_ASSERT(path != NULL, "Path must be != NULL");
  16. #if CROWN_PLATFORM_POSIX
  17. return strlen(path) > 0 && path[0] == SEPARATOR;
  18. #elif CROWN_PLATFORM_WINDOWS
  19. return strlen(path) > 2 && isalpha(path[0]) && path[1] == ':' && path[2] == SEPARATOR;
  20. #endif
  21. }
  22. bool is_root_path(const char* path)
  23. {
  24. CE_ASSERT(path != NULL, "Path must be != NULL");
  25. #if CROWN_PLATFORM_POSIX
  26. return is_absolute_path(path) && strlen(path) == 1;
  27. #elif CROWN_PLATFORM_WINDOWS
  28. return is_absolute_path(path) && strlen(path) == 3;
  29. #endif
  30. }
  31. void join(const char* p1, const char* p2, DynamicString& path)
  32. {
  33. path += p1;
  34. path += SEPARATOR;
  35. path += p2;
  36. }
  37. const char* basename(const char* path)
  38. {
  39. const char* ls = strrchr(path, '/');
  40. return ls == NULL ? path : ls + 1;
  41. }
  42. const char* extension(const char* path)
  43. {
  44. const char* ld = strrchr(path, '.');
  45. return ld == NULL ? NULL : ld + 1;
  46. }
  47. } // namespace path
  48. } // namespace crown