Option.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "Option.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. //---------------------------------------------------------------------------
  9. __fastcall TOptions::TOptions()
  10. {
  11. FSwitchMarks = "-/";
  12. FSwitchValueDelimiters = ":=";
  13. FNoMoreSwitches = false;
  14. FParamCount = 0;
  15. }
  16. //---------------------------------------------------------------------------
  17. void __fastcall TOptions::Add(AnsiString Value)
  18. {
  19. if (!FNoMoreSwitches &&
  20. (Value.Length() == 2) &&
  21. (Value[1] == Value[2]) &&
  22. (FSwitchMarks.Pos(Value[1]) > 0))
  23. {
  24. FNoMoreSwitches = true;
  25. }
  26. else
  27. {
  28. bool Switch = false;
  29. int Index = 0; // shut up
  30. if (!FNoMoreSwitches &&
  31. (Value.Length() >= 2) &&
  32. (FSwitchMarks.Pos(Value[1]) > 0))
  33. {
  34. Index = 2;
  35. Switch = true;
  36. while (Switch && (Index <= Value.Length()))
  37. {
  38. if (Value.IsDelimiter(FSwitchValueDelimiters, Index))
  39. {
  40. break;
  41. }
  42. // this is to treat /home/martin as parameter, not as switch
  43. else if ((Value[Index] != '?') && ((UpCase(Value[Index]) < 'A') || (UpCase(Value[Index]) > 'Z')))
  44. {
  45. Switch = false;
  46. break;
  47. }
  48. ++Index;
  49. }
  50. }
  51. if (Switch)
  52. {
  53. TOption Option;
  54. Option.Type = otSwitch;
  55. Option.Name = Value.SubString(2, Index - 2);
  56. Option.Value = Value.SubString(Index + 1, Value.Length());
  57. Option.Used = false;
  58. FOptions.push_back(Option);
  59. }
  60. else
  61. {
  62. TOption Option;
  63. Option.Type = otParam;
  64. Option.Value = Value;
  65. Option.Used = false;
  66. FOptions.push_back(Option);
  67. ++FParamCount;
  68. }
  69. }
  70. }
  71. //---------------------------------------------------------------------------
  72. AnsiString __fastcall TOptions::GetParam(int Index)
  73. {
  74. assert((Index >= 1) && (Index <= FParamCount));
  75. AnsiString Result;
  76. size_t I = 0;
  77. while ((I < FOptions.size()) && (Index > 0))
  78. {
  79. if (FOptions[I].Type == otParam)
  80. {
  81. --Index;
  82. if (Index == 0)
  83. {
  84. Result = FOptions[I].Value;
  85. FOptions[I].Used = true;
  86. }
  87. }
  88. ++I;
  89. }
  90. return Result;
  91. }
  92. //---------------------------------------------------------------------------
  93. bool __fastcall TOptions::GetEmpty()
  94. {
  95. return FOptions.empty();
  96. }
  97. //---------------------------------------------------------------------------
  98. bool __fastcall TOptions::FindSwitch(const AnsiString Switch,
  99. AnsiString & Value, int & ParamsStart, int & ParamsCount)
  100. {
  101. ParamsStart = 0;
  102. int Index = 0;
  103. bool Found = false;
  104. while ((Index < int(FOptions.size())) && !Found)
  105. {
  106. AnsiString S;
  107. if (FOptions[Index].Type == otParam)
  108. {
  109. ParamsStart++;
  110. }
  111. else if (FOptions[Index].Type == otSwitch)
  112. {
  113. if (AnsiSameText(FOptions[Index].Name, Switch))
  114. {
  115. Found = true;
  116. Value = FOptions[Index].Value;
  117. FOptions[Index].Used = true;
  118. }
  119. }
  120. Index++;
  121. }
  122. ParamsCount = 0;
  123. if (Found)
  124. {
  125. ParamsStart++;
  126. while ((Index + ParamsCount < int(FOptions.size())) &&
  127. (FOptions[Index + ParamsCount].Type == otParam))
  128. {
  129. ParamsCount++;
  130. }
  131. }
  132. else
  133. {
  134. ParamsStart = 0;
  135. }
  136. return Found;
  137. }
  138. //---------------------------------------------------------------------------
  139. bool __fastcall TOptions::FindSwitch(const AnsiString Switch, AnsiString & Value)
  140. {
  141. int ParamsStart;
  142. int ParamsCount;
  143. return FindSwitch(Switch, Value, ParamsStart, ParamsCount);
  144. }
  145. //---------------------------------------------------------------------------
  146. bool __fastcall TOptions::FindSwitch(const AnsiString Switch)
  147. {
  148. AnsiString Value;
  149. int ParamsStart;
  150. int ParamsCount;
  151. return FindSwitch(Switch, Value, ParamsStart, ParamsCount);
  152. }
  153. //---------------------------------------------------------------------------
  154. bool __fastcall TOptions::FindSwitch(const AnsiString Switch,
  155. TStrings * Params, int ParamsMax)
  156. {
  157. AnsiString Value;
  158. int ParamsStart;
  159. int ParamsCount;
  160. bool Result = FindSwitch(Switch, Value, ParamsStart, ParamsCount);
  161. if (Result)
  162. {
  163. if ((ParamsMax >= 0) && (ParamsCount > ParamsMax))
  164. {
  165. ParamsCount = ParamsMax;
  166. }
  167. int Index = 0;
  168. while (Index < ParamsCount)
  169. {
  170. Params->Add(Param[ParamsStart + Index]);
  171. Index++;
  172. }
  173. ParamsProcessed(ParamsStart, ParamsCount);
  174. }
  175. return Result;
  176. }
  177. //---------------------------------------------------------------------------
  178. AnsiString __fastcall TOptions::SwitchValue(const AnsiString Switch,
  179. const AnsiString Default)
  180. {
  181. AnsiString Value;
  182. FindSwitch(Switch, Value);
  183. if (Value.IsEmpty())
  184. {
  185. Value = Default;
  186. }
  187. return Value;
  188. }
  189. //---------------------------------------------------------------------------
  190. bool __fastcall TOptions::UnusedSwitch(AnsiString & Switch)
  191. {
  192. bool Result = false;
  193. size_t Index = 0;
  194. while (!Result && (Index < FOptions.size()))
  195. {
  196. if ((FOptions[Index].Type == otSwitch) &&
  197. !FOptions[Index].Used)
  198. {
  199. Switch = FOptions[Index].Name;
  200. Result = true;
  201. }
  202. ++Index;
  203. }
  204. return Result;
  205. }
  206. //---------------------------------------------------------------------------
  207. void __fastcall TOptions::ParamsProcessed(int ParamsStart, int ParamsCount)
  208. {
  209. if (ParamsCount > 0)
  210. {
  211. assert((ParamsStart >= 0) && ((ParamsStart - ParamsCount + 1) <= FParamCount));
  212. size_t Index = 0;
  213. while ((Index < FOptions.size()) && (ParamsStart > 0))
  214. {
  215. AnsiString S;
  216. if (FOptions[Index].Type == otParam)
  217. {
  218. --ParamsStart;
  219. if (ParamsStart == 0)
  220. {
  221. while (ParamsCount > 0)
  222. {
  223. assert(Index < FOptions.size());
  224. assert(FOptions[Index].Type == otParam);
  225. FOptions.erase(FOptions.begin() + Index);
  226. --FParamCount;
  227. --ParamsCount;
  228. }
  229. }
  230. }
  231. Index++;
  232. }
  233. }
  234. }