BSA_PathUtil.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Better Streaming Assets, Piotr Gwiazdowski <gwiazdorrr+github at gmail.com>, 2017
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. namespace Better.StreamingAssets
  9. {
  10. public static partial class PathUtil
  11. {
  12. private enum NormalizeState
  13. {
  14. PrevSlash,
  15. PrevDot,
  16. PrevDoubleDot,
  17. NothingSpecial,
  18. }
  19. public static bool IsDirectorySeparator(char c)
  20. {
  21. return c == '/' || c == '\\';
  22. }
  23. public static string FixTrailingDirectorySeparators(string path)
  24. {
  25. if ( path.Length >= 2 )
  26. {
  27. var lastChar = path[path.Length - 1];
  28. var prevChar = path[path.Length - 2];
  29. if ( PathUtil.IsDirectorySeparator(lastChar) && PathUtil.IsDirectorySeparator(prevChar) )
  30. {
  31. return path.TrimEnd('\\', '/') + lastChar;
  32. }
  33. }
  34. return path;
  35. }
  36. public static string CombineSlash(string a, string b)
  37. {
  38. if ( a == null )
  39. throw new ArgumentNullException("a");
  40. if ( b == null )
  41. throw new ArgumentNullException("b");
  42. if ( string.IsNullOrEmpty(b) )
  43. return a;
  44. if ( string.IsNullOrEmpty(a) )
  45. return b;
  46. if (b[0] == '/')
  47. return b;
  48. if ( IsDirectorySeparator(a[a.Length -1]) )
  49. return a + b;
  50. else
  51. return a + '/' + b;
  52. }
  53. public static string NormalizeRelativePath(string relative, bool forceTrailingSlash = false)
  54. {
  55. if (string.IsNullOrEmpty(relative))
  56. throw new System.ArgumentException("Empty or null", "relative");
  57. StringBuilder output = new StringBuilder(relative.Length);
  58. NormalizeState state = NormalizeState.PrevSlash;
  59. output.Append('/');
  60. int startIndex = 0;
  61. int lastIndexPlus1 = relative.Length;
  62. if ( relative[0] == '"' && relative.Length > 2 && relative[relative.Length - 1] == '"')
  63. {
  64. startIndex++;
  65. lastIndexPlus1--;
  66. }
  67. for ( int i = startIndex; i <= lastIndexPlus1; ++i )
  68. {
  69. if (i == lastIndexPlus1 || relative[i] == '/' || relative[i] == '\\')
  70. {
  71. if ( state == NormalizeState.PrevSlash || state == NormalizeState.PrevDot )
  72. {
  73. // do nothing
  74. }
  75. else if ( state == NormalizeState.PrevDoubleDot )
  76. {
  77. if ( output.Length == 1 )
  78. throw new System.IO.IOException("Invalid path: double dot error (before " + i + ")");
  79. // on level up!
  80. int j;
  81. for ( j = output.Length - 2; j >= 0 && output[j] != '/'; --j)
  82. {
  83. }
  84. output.Remove(j + 1, output.Length - j - 1);
  85. }
  86. else if ( i < lastIndexPlus1 || forceTrailingSlash )
  87. {
  88. output.Append('/');
  89. }
  90. state = NormalizeState.PrevSlash;
  91. }
  92. else if ( relative[i] == '.' )
  93. {
  94. if ( state == NormalizeState.PrevSlash )
  95. {
  96. state = NormalizeState.PrevDot;
  97. }
  98. else if ( state == NormalizeState.PrevDot )
  99. {
  100. state = NormalizeState.PrevDoubleDot;
  101. }
  102. else if ( state == NormalizeState.PrevDoubleDot )
  103. {
  104. state = NormalizeState.NothingSpecial;
  105. output.Append("...");
  106. }
  107. else
  108. {
  109. output.Append('.');
  110. }
  111. }
  112. else
  113. {
  114. if ( state == NormalizeState.PrevDot )
  115. {
  116. output.Append('.');
  117. }
  118. else if ( state == NormalizeState.PrevDoubleDot )
  119. {
  120. output.Append("..");
  121. }
  122. if (!IsValidCharacter(relative[i]))
  123. throw new System.IO.IOException("Invalid characters");
  124. output.Append(relative[i]);
  125. state = NormalizeState.NothingSpecial;
  126. }
  127. }
  128. return output.ToString();
  129. }
  130. public static bool IsValidCharacter(char c)
  131. {
  132. if (c == '\"' || c == '<' || c == '>' || c == '|' || c < 32 || c == ':' || c == '*' || c == '?')
  133. return false;
  134. return true;
  135. }
  136. public static Regex WildcardToRegex(string pattern)
  137. {
  138. return new Regex("^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$", RegexOptions.IgnoreCase);
  139. }
  140. }
  141. }