CommandLineParser.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // CommandLineParser.cs
  2. using System;
  3. using System.Collections;
  4. namespace SevenZip.CommandLineParser
  5. {
  6. public enum SwitchType
  7. {
  8. Simple,
  9. PostMinus,
  10. LimitedPostString,
  11. UnLimitedPostString,
  12. PostChar
  13. }
  14. public class SwitchForm
  15. {
  16. public string IDString;
  17. public SwitchType Type;
  18. public bool Multi;
  19. public int MinLen;
  20. public int MaxLen;
  21. public string PostCharSet;
  22. public SwitchForm(string idString, SwitchType type, bool multi,
  23. int minLen, int maxLen, string postCharSet)
  24. {
  25. IDString = idString;
  26. Type = type;
  27. Multi = multi;
  28. MinLen = minLen;
  29. MaxLen = maxLen;
  30. PostCharSet = postCharSet;
  31. }
  32. public SwitchForm(string idString, SwitchType type, bool multi, int minLen):
  33. this(idString, type, multi, minLen, 0, "")
  34. {
  35. }
  36. public SwitchForm(string idString, SwitchType type, bool multi):
  37. this(idString, type, multi, 0)
  38. {
  39. }
  40. }
  41. public class SwitchResult
  42. {
  43. public bool ThereIs;
  44. public bool WithMinus;
  45. public ArrayList PostStrings = new ArrayList();
  46. public int PostCharIndex;
  47. public SwitchResult()
  48. {
  49. ThereIs = false;
  50. }
  51. }
  52. public class Parser
  53. {
  54. public ArrayList NonSwitchStrings = new ArrayList();
  55. SwitchResult[] _switches;
  56. public Parser(int numSwitches)
  57. {
  58. _switches = new SwitchResult[numSwitches];
  59. for (int i = 0; i < numSwitches; i++)
  60. _switches[i] = new SwitchResult();
  61. }
  62. bool ParseString(string srcString, SwitchForm[] switchForms)
  63. {
  64. int len = srcString.Length;
  65. if (len == 0)
  66. return false;
  67. int pos = 0;
  68. if (!IsItSwitchChar(srcString[pos]))
  69. return false;
  70. while (pos < len)
  71. {
  72. if (IsItSwitchChar(srcString[pos]))
  73. pos++;
  74. const int kNoLen = -1;
  75. int matchedSwitchIndex = 0;
  76. int maxLen = kNoLen;
  77. for (int switchIndex = 0; switchIndex < _switches.Length; switchIndex++)
  78. {
  79. int switchLen = switchForms[switchIndex].IDString.Length;
  80. if (switchLen <= maxLen || pos + switchLen > len)
  81. continue;
  82. if (String.Compare(switchForms[switchIndex].IDString, 0,
  83. srcString, pos, switchLen, true) == 0)
  84. {
  85. matchedSwitchIndex = switchIndex;
  86. maxLen = switchLen;
  87. }
  88. }
  89. if (maxLen == kNoLen)
  90. throw new Exception("maxLen == kNoLen");
  91. SwitchResult matchedSwitch = _switches[matchedSwitchIndex];
  92. SwitchForm switchForm = switchForms[matchedSwitchIndex];
  93. if ((!switchForm.Multi) && matchedSwitch.ThereIs)
  94. throw new Exception("switch must be single");
  95. matchedSwitch.ThereIs = true;
  96. pos += maxLen;
  97. int tailSize = len - pos;
  98. SwitchType type = switchForm.Type;
  99. switch (type)
  100. {
  101. case SwitchType.PostMinus:
  102. {
  103. if (tailSize == 0)
  104. matchedSwitch.WithMinus = false;
  105. else
  106. {
  107. matchedSwitch.WithMinus = (srcString[pos] == kSwitchMinus);
  108. if (matchedSwitch.WithMinus)
  109. pos++;
  110. }
  111. break;
  112. }
  113. case SwitchType.PostChar:
  114. {
  115. if (tailSize < switchForm.MinLen)
  116. throw new Exception("switch is not full");
  117. string charSet = switchForm.PostCharSet;
  118. const int kEmptyCharValue = -1;
  119. if (tailSize == 0)
  120. matchedSwitch.PostCharIndex = kEmptyCharValue;
  121. else
  122. {
  123. int index = charSet.IndexOf(srcString[pos]);
  124. if (index < 0)
  125. matchedSwitch.PostCharIndex = kEmptyCharValue;
  126. else
  127. {
  128. matchedSwitch.PostCharIndex = index;
  129. pos++;
  130. }
  131. }
  132. break;
  133. }
  134. case SwitchType.LimitedPostString:
  135. case SwitchType.UnLimitedPostString:
  136. {
  137. int minLen = switchForm.MinLen;
  138. if (tailSize < minLen)
  139. throw new Exception("switch is not full");
  140. if (type == SwitchType.UnLimitedPostString)
  141. {
  142. matchedSwitch.PostStrings.Add(srcString.Substring(pos));
  143. return true;
  144. }
  145. String stringSwitch = srcString.Substring(pos, minLen);
  146. pos += minLen;
  147. for (int i = minLen; i < switchForm.MaxLen && pos < len; i++, pos++)
  148. {
  149. char c = srcString[pos];
  150. if (IsItSwitchChar(c))
  151. break;
  152. stringSwitch += c;
  153. }
  154. matchedSwitch.PostStrings.Add(stringSwitch);
  155. break;
  156. }
  157. }
  158. }
  159. return true;
  160. }
  161. public void ParseStrings(SwitchForm[] switchForms, string[] commandStrings)
  162. {
  163. int numCommandStrings = commandStrings.Length;
  164. bool stopSwitch = false;
  165. for (int i = 0; i < numCommandStrings; i++)
  166. {
  167. string s = commandStrings[i];
  168. if (stopSwitch)
  169. NonSwitchStrings.Add(s);
  170. else
  171. if (s == kStopSwitchParsing)
  172. stopSwitch = true;
  173. else
  174. if (!ParseString(s, switchForms))
  175. NonSwitchStrings.Add(s);
  176. }
  177. }
  178. public SwitchResult this[int index] { get { return _switches[index]; } }
  179. public static int ParseCommand(CommandForm[] commandForms, string commandString,
  180. out string postString)
  181. {
  182. for (int i = 0; i < commandForms.Length; i++)
  183. {
  184. string id = commandForms[i].IDString;
  185. if (commandForms[i].PostStringMode)
  186. {
  187. if (commandString.IndexOf(id) == 0)
  188. {
  189. postString = commandString.Substring(id.Length);
  190. return i;
  191. }
  192. }
  193. else
  194. if (commandString == id)
  195. {
  196. postString = "";
  197. return i;
  198. }
  199. }
  200. postString = "";
  201. return -1;
  202. }
  203. static bool ParseSubCharsCommand(int numForms, CommandSubCharsSet[] forms,
  204. string commandString, ArrayList indices)
  205. {
  206. indices.Clear();
  207. int numUsedChars = 0;
  208. for (int i = 0; i < numForms; i++)
  209. {
  210. CommandSubCharsSet charsSet = forms[i];
  211. int currentIndex = -1;
  212. int len = charsSet.Chars.Length;
  213. for (int j = 0; j < len; j++)
  214. {
  215. char c = charsSet.Chars[j];
  216. int newIndex = commandString.IndexOf(c);
  217. if (newIndex >= 0)
  218. {
  219. if (currentIndex >= 0)
  220. return false;
  221. if (commandString.IndexOf(c, newIndex + 1) >= 0)
  222. return false;
  223. currentIndex = j;
  224. numUsedChars++;
  225. }
  226. }
  227. if (currentIndex == -1 && !charsSet.EmptyAllowed)
  228. return false;
  229. indices.Add(currentIndex);
  230. }
  231. return (numUsedChars == commandString.Length);
  232. }
  233. const char kSwitchID1 = '-';
  234. const char kSwitchID2 = '/';
  235. const char kSwitchMinus = '-';
  236. const string kStopSwitchParsing = "--";
  237. static bool IsItSwitchChar(char c)
  238. {
  239. return (c == kSwitchID1 || c == kSwitchID2);
  240. }
  241. }
  242. public class CommandForm
  243. {
  244. public string IDString = "";
  245. public bool PostStringMode = false;
  246. public CommandForm(string idString, bool postStringMode)
  247. {
  248. IDString = idString;
  249. PostStringMode = postStringMode;
  250. }
  251. }
  252. class CommandSubCharsSet
  253. {
  254. public string Chars = "";
  255. public bool EmptyAllowed = false;
  256. }
  257. }