DirectoryUtils.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Directory utility functions that are common to all posix and posix-like platforms.
  3. */
  4. #include "il2cpp-config.h"
  5. #include "StringUtils.h"
  6. #include "DirectoryUtils.h"
  7. namespace il2cpp
  8. {
  9. namespace utils
  10. {
  11. bool Match(const std::string name, size_t nameIndex, const std::string& pattern, const size_t patternIndex)
  12. {
  13. const size_t nameLength = name.length();
  14. for (size_t i = patternIndex, patternLength = pattern.length(); i < patternLength; ++i)
  15. {
  16. const char c = pattern[i];
  17. if (c == '*')
  18. {
  19. if (i + 1 == patternLength) // Star is last character, match everything.
  20. return true;
  21. do
  22. {
  23. // Check that we match the rest of the pattern against name.
  24. if (Match(name, nameIndex, pattern, i + 1))
  25. return true;
  26. }
  27. while (nameIndex++ < nameLength);
  28. return false;
  29. }
  30. else if (c == '?')
  31. {
  32. if (nameIndex == nameLength)
  33. return false;
  34. nameIndex++;
  35. }
  36. else
  37. {
  38. if (nameIndex == nameLength)
  39. {
  40. // A pattern ending with .* should match a file with no extension
  41. // The pattern "file.*" should match "file"
  42. if (c == '.' && i + 2 == patternLength && pattern[i + 1] == '*')
  43. return true;
  44. return false;
  45. }
  46. else if (name[nameIndex] != c)
  47. {
  48. return false;
  49. }
  50. nameIndex++;
  51. }
  52. }
  53. // All characters matched
  54. return nameIndex == nameLength;
  55. }
  56. bool Match(const std::string name, const std::string& pattern)
  57. {
  58. return Match(name, 0, pattern, 0);
  59. }
  60. std::string CollapseAdjacentStars(const std::string& pattern)
  61. {
  62. std::string matchPattern;
  63. matchPattern.reserve(pattern.length());
  64. // Collapse adjacent stars into one
  65. for (size_t i = 0, length = pattern.length(); i < length; ++i)
  66. {
  67. if (i > 0 && pattern[i] == '*' && pattern[i - 1] == '*')
  68. continue;
  69. matchPattern.append(1, pattern[i]);
  70. }
  71. return matchPattern;
  72. }
  73. }
  74. }