path.h - エンジンの C API リファレンス

path.h
  1. #pragma once
  2. #include "string.h"
  3. #include "vector.h"
  4. namespace stingray_plugin_foundation {
  5. // Convenience tools for manipulating paths.
  6. namespace path
  7. {
  8. // Strips the extension from path
  9. void strip_extension(DynamicString &path);
  10. // Strips the extension from `path` returns the result.
  11. DynamicString strip_extension(const char *path, Allocator &a);
  12. // Returns true if `path` has any extension.
  13. bool has_extension(const char *path);
  14. // Returns true if `path` has the specified extension. Extension comparison is case-insensitive.
  15. bool has_extension(const char *path, const char *pattern);
  16. // Joins `dir` to the beginning of all the file names in `files`.
  17. void join(const char *dir, const Vector<DynamicString> &files, Vector<DynamicString> &res);
  18. }
  19. }
  20. namespace stingray_plugin_foundation {
  21. namespace path {
  22. inline void strip_extension(DynamicString &path)
  23. {
  24. int n = path.size();
  25. for (int i = n - 1; i >= 0; --i) {
  26. if (path[i] == '.') {
  27. path.resize(i);
  28. return;
  29. }
  30. }
  31. }
  32. inline DynamicString strip_extension(const char *path, Allocator &a)
  33. {
  34. DynamicString s(a, path);
  35. strip_extension(s);
  36. return s;
  37. }
  38. inline bool has_extension(const char *path)
  39. {
  40. int n = strlen32(path);
  41. for (int i = n - 1; i >= 0; --i) {
  42. if (path[i] == '.')
  43. return true;
  44. }
  45. return false;
  46. }
  47. inline bool has_extension(const char *path, const char *pattern)
  48. {
  49. int n = strlen32(path);
  50. for (int i = n - 1; i >= 0; --i) {
  51. if (path[i] == '.') {
  52. return strequali(pattern, &path[i + 1]);
  53. }
  54. }
  55. return pattern[0] == '\0';
  56. }
  57. } // namespace path
  58. }