Option.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "Option.h"
  6. #include "TextsCore.h"
  7. #include "System.StrUtils.hpp"
  8. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. //---------------------------------------------------------------------------
  11. const wchar_t ArrayValueDelimiter = L'[';
  12. const wchar_t ArrayValueEnd = L']';
  13. //---------------------------------------------------------------------------
  14. __fastcall TOptions::TOptions()
  15. {
  16. FSwitchMarks = L"/-";
  17. FSwitchValueDelimiters = UnicodeString(L"=:") + ArrayValueDelimiter;
  18. FNoMoreSwitches = false;
  19. FParamCount = 0;
  20. }
  21. //---------------------------------------------------------------------------
  22. __fastcall TOptions::TOptions(const TOptions & Source)
  23. {
  24. FSwitchMarks = Source.FSwitchMarks;
  25. FSwitchValueDelimiters = Source.FSwitchValueDelimiters;
  26. FOptions = Source.FOptions;
  27. FOriginalOptions = Source.FOriginalOptions;
  28. FNoMoreSwitches = Source.FNoMoreSwitches;
  29. FParamCount = Source.FParamCount;
  30. }
  31. //---------------------------------------------------------------------------
  32. void __fastcall TOptions::Parse(const UnicodeString & CmdLine)
  33. {
  34. UnicodeString ACmdLine = CmdLine;
  35. UnicodeString Param;
  36. while (CutToken(ACmdLine, Param))
  37. {
  38. Add(Param);
  39. }
  40. }
  41. //---------------------------------------------------------------------------
  42. void __fastcall TOptions::Add(UnicodeString Value)
  43. {
  44. if (!FNoMoreSwitches &&
  45. (Value.Length() == 2) &&
  46. (Value[1] == Value[2]) &&
  47. (FSwitchMarks.Pos(Value[1]) > 0))
  48. {
  49. FNoMoreSwitches = true;
  50. }
  51. else
  52. {
  53. bool Switch = false;
  54. int Index = 0; // shut up
  55. wchar_t SwitchMark = L'\0';
  56. wchar_t ValueDelimiter = L'\0';
  57. if (!FNoMoreSwitches &&
  58. (Value.Length() >= 2) &&
  59. (FSwitchMarks.Pos(Value[1]) > 0))
  60. {
  61. Index = 2;
  62. Switch = true;
  63. SwitchMark = Value[1];
  64. while (Switch && (Index <= Value.Length()))
  65. {
  66. if (Value.IsDelimiter(FSwitchValueDelimiters, Index))
  67. {
  68. ValueDelimiter = Value[Index];
  69. break;
  70. }
  71. // This is to treat /home/martin as parameter, not as switch.
  72. else if ((Value[Index] == L'?') ||
  73. IsLetter(Value[Index]) ||
  74. ((Index == 2) && (Value[1] == L'-') && (Value[Index] == L'-'))) // allow --certificate
  75. {
  76. // noop
  77. }
  78. else
  79. {
  80. Switch = false;
  81. break;
  82. }
  83. ++Index;
  84. }
  85. }
  86. TOption Option;
  87. if (Switch)
  88. {
  89. Option.Type = otSwitch;
  90. Option.Name = Value.SubString(2, Index - 2);
  91. Option.Value = Value.SubString(Index + 1, Value.Length());
  92. if ((ValueDelimiter == ArrayValueDelimiter) && EndsStr(ArrayValueEnd, Option.Value))
  93. {
  94. Option.Value.SetLength(Option.Value.Length() - 1);
  95. }
  96. Option.ValueSet = (Index <= Value.Length());
  97. }
  98. else
  99. {
  100. Option.Type = otParam;
  101. Option.Value = Value;
  102. Option.ValueSet = false; // unused
  103. ++FParamCount;
  104. }
  105. Option.Used = false;
  106. Option.SwitchMark = SwitchMark;
  107. FOptions.push_back(Option);
  108. }
  109. FOriginalOptions = FOptions;
  110. }
  111. //---------------------------------------------------------------------------
  112. UnicodeString __fastcall TOptions::GetParam(int Index)
  113. {
  114. DebugAssert((Index >= 1) && (Index <= FParamCount));
  115. UnicodeString Result;
  116. size_t I = 0;
  117. while ((I < FOptions.size()) && (Index > 0))
  118. {
  119. if (FOptions[I].Type == otParam)
  120. {
  121. --Index;
  122. if (Index == 0)
  123. {
  124. Result = FOptions[I].Value;
  125. FOptions[I].Used = true;
  126. }
  127. }
  128. ++I;
  129. }
  130. return Result;
  131. }
  132. //---------------------------------------------------------------------------
  133. bool __fastcall TOptions::GetEmpty()
  134. {
  135. return FOptions.empty();
  136. }
  137. //---------------------------------------------------------------------------
  138. bool __fastcall TOptions::FindSwitch(const UnicodeString Switch,
  139. UnicodeString & Value, int & ParamsStart, int & ParamsCount, bool CaseSensitive, bool & ValueSet)
  140. {
  141. ParamsStart = 0;
  142. ValueSet = false;
  143. int Index = 0;
  144. bool Found = false;
  145. while ((Index < int(FOptions.size())) && !Found)
  146. {
  147. if (FOptions[Index].Type == otParam)
  148. {
  149. ParamsStart++;
  150. }
  151. else if (FOptions[Index].Type == otSwitch)
  152. {
  153. if ((!CaseSensitive && SameText(FOptions[Index].Name, Switch)) ||
  154. (CaseSensitive && SameStr(FOptions[Index].Name, Switch)))
  155. {
  156. Found = true;
  157. Value = FOptions[Index].Value;
  158. ValueSet = FOptions[Index].ValueSet;
  159. FOptions[Index].Used = true;
  160. }
  161. }
  162. Index++;
  163. }
  164. ParamsCount = 0;
  165. if (Found)
  166. {
  167. ParamsStart++;
  168. while ((Index + ParamsCount < int(FOptions.size())) &&
  169. (FOptions[Index + ParamsCount].Type == otParam))
  170. {
  171. ParamsCount++;
  172. }
  173. }
  174. else
  175. {
  176. ParamsStart = 0;
  177. }
  178. return Found;
  179. }
  180. //---------------------------------------------------------------------------
  181. bool __fastcall TOptions::FindSwitch(const UnicodeString Switch, UnicodeString & Value)
  182. {
  183. bool ValueSet;
  184. return FindSwitch(Switch, Value, ValueSet);
  185. }
  186. //---------------------------------------------------------------------------
  187. bool __fastcall TOptions::FindSwitch(const UnicodeString Switch, UnicodeString & Value, bool & ValueSet)
  188. {
  189. int ParamsStart;
  190. int ParamsCount;
  191. return FindSwitch(Switch, Value, ParamsStart, ParamsCount, false, ValueSet);
  192. }
  193. //---------------------------------------------------------------------------
  194. bool __fastcall TOptions::FindSwitch(const UnicodeString Switch)
  195. {
  196. UnicodeString Value;
  197. int ParamsStart;
  198. int ParamsCount;
  199. bool ValueSet;
  200. return FindSwitch(Switch, Value, ParamsStart, ParamsCount, false, ValueSet);
  201. }
  202. //---------------------------------------------------------------------------
  203. bool __fastcall TOptions::FindSwitchCaseSensitive(const UnicodeString Switch)
  204. {
  205. UnicodeString Value;
  206. int ParamsStart;
  207. int ParamsCount;
  208. bool ValueSet;
  209. return FindSwitch(Switch, Value, ParamsStart, ParamsCount, true, ValueSet);
  210. }
  211. //---------------------------------------------------------------------------
  212. bool __fastcall TOptions::FindSwitch(const UnicodeString Switch,
  213. TStrings * Params, int ParamsMax)
  214. {
  215. return DoFindSwitch(Switch, Params, ParamsMax, false);
  216. }
  217. //---------------------------------------------------------------------------
  218. bool __fastcall TOptions::FindSwitchCaseSensitive(const UnicodeString Switch,
  219. TStrings * Params, int ParamsMax)
  220. {
  221. return DoFindSwitch(Switch, Params, ParamsMax, true);
  222. }
  223. //---------------------------------------------------------------------------
  224. bool __fastcall TOptions::DoFindSwitch(const UnicodeString Switch,
  225. TStrings * Params, int ParamsMax, bool CaseSensitive)
  226. {
  227. UnicodeString Value;
  228. int ParamsStart;
  229. int ParamsCount;
  230. bool ValueSet;
  231. bool Result = FindSwitch(Switch, Value, ParamsStart, ParamsCount, CaseSensitive, ValueSet);
  232. if (Result)
  233. {
  234. int AParamsCount;
  235. if (TryStrToInt(Value, AParamsCount) && (AParamsCount < ParamsCount))
  236. {
  237. ParamsCount = AParamsCount;
  238. }
  239. if ((ParamsMax >= 0) && (ParamsCount > ParamsMax))
  240. {
  241. ParamsCount = ParamsMax;
  242. }
  243. int Index = 0;
  244. while (Index < ParamsCount)
  245. {
  246. Params->Add(Param[ParamsStart + Index]);
  247. Index++;
  248. }
  249. ParamsProcessed(ParamsStart, ParamsCount);
  250. }
  251. return Result;
  252. }
  253. //---------------------------------------------------------------------------
  254. UnicodeString __fastcall TOptions::SwitchValue(const UnicodeString Switch,
  255. const UnicodeString Default)
  256. {
  257. UnicodeString Value;
  258. FindSwitch(Switch, Value);
  259. if (Value.IsEmpty())
  260. {
  261. Value = Default;
  262. }
  263. return Value;
  264. }
  265. //---------------------------------------------------------------------------
  266. bool __fastcall TOptions::SwitchValue(const UnicodeString Switch, bool Default, bool DefaultOnNonExistence)
  267. {
  268. bool Result;
  269. int IntValue;
  270. UnicodeString Value;
  271. if (!FindSwitch(Switch, Value))
  272. {
  273. Result = DefaultOnNonExistence;
  274. }
  275. else if (Value.IsEmpty())
  276. {
  277. Result = Default;
  278. }
  279. else if (SameText(Value, "on"))
  280. {
  281. Result = true;
  282. }
  283. else if (SameText(Value, "off"))
  284. {
  285. Result = false;
  286. }
  287. else if (TryStrToInt(Value, IntValue))
  288. {
  289. Result = (IntValue != 0);
  290. }
  291. else
  292. {
  293. throw Exception(FMTLOAD(URL_OPTION_BOOL_VALUE_ERROR, (Value)));
  294. }
  295. return Result;
  296. }
  297. //---------------------------------------------------------------------------
  298. bool __fastcall TOptions::SwitchValue(const UnicodeString Switch, bool Default)
  299. {
  300. return SwitchValue(Switch, Default, Default);
  301. }
  302. //---------------------------------------------------------------------------
  303. bool __fastcall TOptions::UnusedSwitch(UnicodeString & Switch)
  304. {
  305. bool Result = false;
  306. size_t Index = 0;
  307. while (!Result && (Index < FOptions.size()))
  308. {
  309. if ((FOptions[Index].Type == otSwitch) &&
  310. !FOptions[Index].Used)
  311. {
  312. Switch = FOptions[Index].Name;
  313. Result = true;
  314. }
  315. ++Index;
  316. }
  317. return Result;
  318. }
  319. //---------------------------------------------------------------------------
  320. bool __fastcall TOptions::WasSwitchAdded(UnicodeString & Switch, UnicodeString & Value, wchar_t & SwitchMark)
  321. {
  322. bool Result =
  323. DebugAlwaysTrue(FOptions.size() > 0) &&
  324. (FOptions.back().Type == otSwitch);
  325. if (Result)
  326. {
  327. TOption & Option = FOptions.back();
  328. Switch = Option.Name;
  329. Value = Option.Value;
  330. SwitchMark = Option.SwitchMark;
  331. }
  332. return Result;
  333. }
  334. //---------------------------------------------------------------------------
  335. void __fastcall TOptions::ParamsProcessed(int ParamsStart, int ParamsCount)
  336. {
  337. if (ParamsCount > 0)
  338. {
  339. DebugAssert((ParamsStart >= 0) && ((ParamsStart - ParamsCount + 1) <= FParamCount));
  340. size_t Index = 0;
  341. while ((Index < FOptions.size()) && (ParamsStart > 0))
  342. {
  343. if (FOptions[Index].Type == otParam)
  344. {
  345. --ParamsStart;
  346. if (ParamsStart == 0)
  347. {
  348. while (ParamsCount > 0)
  349. {
  350. DebugAssert(Index < FOptions.size());
  351. DebugAssert(FOptions[Index].Type == otParam);
  352. FOptions.erase(FOptions.begin() + Index);
  353. --FParamCount;
  354. --ParamsCount;
  355. }
  356. }
  357. }
  358. Index++;
  359. }
  360. }
  361. }
  362. //---------------------------------------------------------------------------
  363. void __fastcall TOptions::LogOptions(TLogOptionEvent OnLogOption)
  364. {
  365. for (size_t Index = 0; Index < FOriginalOptions.size(); Index++)
  366. {
  367. const TOption & Option = FOriginalOptions[Index];
  368. UnicodeString LogStr;
  369. switch (Option.Type)
  370. {
  371. case otParam:
  372. LogStr = FORMAT(L"Parameter: %s", (Option.Value));
  373. DebugAssert(Option.Name.IsEmpty());
  374. break;
  375. case otSwitch:
  376. LogStr =
  377. FORMAT(L"Switch: %s%s%s%s",
  378. (FSwitchMarks[1], Option.Name, (Option.Value.IsEmpty() ? UnicodeString() : FSwitchValueDelimiters.SubString(1, 1)), Option.Value));
  379. break;
  380. default:
  381. DebugFail();
  382. break;
  383. }
  384. OnLogOption(LogStr);
  385. }
  386. }