PathUtils.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include "il2cpp-config.h"
  3. #include <string>
  4. #include "StringView.h"
  5. namespace il2cpp
  6. {
  7. namespace utils
  8. {
  9. namespace PathUtils
  10. {
  11. std::string BasenameNoExtension(const std::string& path);
  12. std::string PathNoExtension(const std::string& path);
  13. template<typename CharType>
  14. std::basic_string<CharType> Basename(const utils::StringView<CharType>& path)
  15. {
  16. if (path.IsEmpty())
  17. return std::basic_string<CharType>(1, static_cast<CharType>('.'));
  18. const size_t pos = path.RFind(IL2CPP_DIR_SEPARATOR);
  19. // No seperators. Path is filename
  20. if (pos == utils::StringView<CharType>::NPos())
  21. return std::basic_string<CharType>(path.Str(), path.Length());
  22. return std::basic_string<CharType>(path.Str() + pos + 1, path.Length() - pos - 1);
  23. }
  24. template<typename CharType>
  25. std::basic_string<CharType> Basename(const std::basic_string<CharType>& path)
  26. {
  27. return Basename(utils::StringView<CharType>(path));
  28. }
  29. template<typename CharType>
  30. std::basic_string<CharType> DirectoryName(const utils::StringView<CharType>& path)
  31. {
  32. if (path.IsEmpty())
  33. return std::basic_string<CharType>();
  34. const size_t pos = path.RFind(IL2CPP_DIR_SEPARATOR);
  35. if (pos == utils::StringView<CharType>::NPos())
  36. return std::basic_string<CharType>(1, static_cast<CharType>('.'));
  37. if (pos == 0)
  38. return std::basic_string<CharType>(1, static_cast<CharType>('/'));
  39. return std::basic_string<CharType>(path.Str(), pos);
  40. }
  41. template<typename CharType>
  42. std::basic_string<CharType> Combine(const utils::StringView<CharType>& path1, const utils::StringView<CharType>& path2)
  43. {
  44. std::basic_string<CharType> result;
  45. result.reserve(path1.Length() + path2.Length() + 1);
  46. result.append(path1.Str(), path1.Length());
  47. result.append(1, static_cast<CharType>(IL2CPP_DIR_SEPARATOR));
  48. result.append(path2.Str(), path2.Length());
  49. return result;
  50. }
  51. template<typename CharType>
  52. std::basic_string<CharType> DirectoryName(const std::basic_string<CharType>& path)
  53. {
  54. return DirectoryName(utils::StringView<CharType>(path));
  55. }
  56. template<typename CharType>
  57. std::basic_string<CharType> Combine(const std::basic_string<CharType>& path1, const std::basic_string<CharType>& path2)
  58. {
  59. return Combine(utils::StringView<CharType>(path1), utils::StringView<CharType>(path2));
  60. }
  61. template<typename CharType>
  62. std::basic_string<CharType> Combine(const std::basic_string<CharType>& path1, const utils::StringView<CharType>& path2)
  63. {
  64. return Combine(utils::StringView<CharType>(path1), path2);
  65. }
  66. template<typename CharType>
  67. std::basic_string<CharType> Combine(const utils::StringView<CharType>& path1, const std::basic_string<CharType>& path2)
  68. {
  69. return Combine(path1, utils::StringView<CharType>(path2));
  70. }
  71. }
  72. } /* utils */
  73. } /* il2cpp */