1
0

Option.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. ((Value[Index] == L'-') && (SwitchMark == L'-') && (Value[2] == L'-'))) // allow --puttygen-switches
  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. UnicodeString TOptions::ConsumeParam()
  134. {
  135. UnicodeString Result = Param[1];
  136. ParamsProcessed(1, 1);
  137. return Result;
  138. }
  139. //---------------------------------------------------------------------------
  140. bool __fastcall TOptions::GetEmpty()
  141. {
  142. return FOptions.empty();
  143. }
  144. //---------------------------------------------------------------------------
  145. bool __fastcall TOptions::FindSwitch(const UnicodeString Switch,
  146. UnicodeString & Value, int & ParamsStart, int & ParamsCount, bool CaseSensitive, bool & ValueSet)
  147. {
  148. ParamsStart = 0;
  149. ValueSet = false;
  150. int Index = 0;
  151. bool Found = false;
  152. while ((Index < int(FOptions.size())) && !Found)
  153. {
  154. if (FOptions[Index].Type == otParam)
  155. {
  156. ParamsStart++;
  157. }
  158. else if (FOptions[Index].Type == otSwitch)
  159. {
  160. if ((!CaseSensitive && SameText(FOptions[Index].Name, Switch)) ||
  161. (CaseSensitive && SameStr(FOptions[Index].Name, Switch)))
  162. {
  163. Found = true;
  164. Value = FOptions[Index].Value;
  165. ValueSet = FOptions[Index].ValueSet;
  166. FOptions[Index].Used = true;
  167. }
  168. }
  169. Index++;
  170. }
  171. ParamsCount = 0;
  172. if (Found)
  173. {
  174. ParamsStart++;
  175. while ((Index + ParamsCount < int(FOptions.size())) &&
  176. (FOptions[Index + ParamsCount].Type == otParam))
  177. {
  178. ParamsCount++;
  179. }
  180. }
  181. else
  182. {
  183. ParamsStart = 0;
  184. }
  185. return Found;
  186. }
  187. //---------------------------------------------------------------------------
  188. bool __fastcall TOptions::FindSwitch(const UnicodeString Switch, UnicodeString & Value)
  189. {
  190. bool ValueSet;
  191. return FindSwitch(Switch, Value, ValueSet);
  192. }
  193. //---------------------------------------------------------------------------
  194. bool __fastcall TOptions::FindSwitch(const UnicodeString Switch, UnicodeString & Value, bool & ValueSet)
  195. {
  196. int ParamsStart;
  197. int ParamsCount;
  198. return FindSwitch(Switch, Value, ParamsStart, ParamsCount, false, ValueSet);
  199. }
  200. //---------------------------------------------------------------------------
  201. bool __fastcall TOptions::FindSwitch(const UnicodeString Switch)
  202. {
  203. UnicodeString Value;
  204. int ParamsStart;
  205. int ParamsCount;
  206. bool ValueSet;
  207. return FindSwitch(Switch, Value, ParamsStart, ParamsCount, false, ValueSet);
  208. }
  209. //---------------------------------------------------------------------------
  210. bool __fastcall TOptions::FindSwitchCaseSensitive(const UnicodeString Switch)
  211. {
  212. UnicodeString Value;
  213. int ParamsStart;
  214. int ParamsCount;
  215. bool ValueSet;
  216. return FindSwitch(Switch, Value, ParamsStart, ParamsCount, true, ValueSet);
  217. }
  218. //---------------------------------------------------------------------------
  219. bool __fastcall TOptions::FindSwitch(const UnicodeString Switch,
  220. TStrings * Params, int ParamsMax)
  221. {
  222. return DoFindSwitch(Switch, Params, ParamsMax, false);
  223. }
  224. //---------------------------------------------------------------------------
  225. bool __fastcall TOptions::FindSwitchCaseSensitive(const UnicodeString Switch,
  226. TStrings * Params, int ParamsMax)
  227. {
  228. return DoFindSwitch(Switch, Params, ParamsMax, true);
  229. }
  230. //---------------------------------------------------------------------------
  231. bool __fastcall TOptions::DoFindSwitch(const UnicodeString Switch,
  232. TStrings * Params, int ParamsMax, bool CaseSensitive)
  233. {
  234. UnicodeString Value;
  235. int ParamsStart;
  236. int ParamsCount;
  237. bool ValueSet;
  238. bool Result = FindSwitch(Switch, Value, ParamsStart, ParamsCount, CaseSensitive, ValueSet);
  239. if (Result)
  240. {
  241. int AParamsCount;
  242. if (TryStrToInt(Value, AParamsCount) && (AParamsCount < ParamsCount))
  243. {
  244. ParamsCount = AParamsCount;
  245. }
  246. if ((ParamsMax >= 0) && (ParamsCount > ParamsMax))
  247. {
  248. ParamsCount = ParamsMax;
  249. }
  250. int Index = 0;
  251. while (Index < ParamsCount)
  252. {
  253. Params->Add(Param[ParamsStart + Index]);
  254. Index++;
  255. }
  256. ParamsProcessed(ParamsStart, ParamsCount);
  257. }
  258. return Result;
  259. }
  260. //---------------------------------------------------------------------------
  261. UnicodeString __fastcall TOptions::SwitchValue(const UnicodeString Switch,
  262. const UnicodeString Default)
  263. {
  264. UnicodeString Value;
  265. FindSwitch(Switch, Value);
  266. if (Value.IsEmpty())
  267. {
  268. Value = Default;
  269. }
  270. return Value;
  271. }
  272. //---------------------------------------------------------------------------
  273. bool __fastcall TOptions::SwitchValue(const UnicodeString Switch, bool Default, bool DefaultOnNonExistence)
  274. {
  275. bool Result;
  276. int IntValue;
  277. UnicodeString Value;
  278. if (!FindSwitch(Switch, Value))
  279. {
  280. Result = DefaultOnNonExistence;
  281. }
  282. else if (Value.IsEmpty())
  283. {
  284. Result = Default;
  285. }
  286. else if (SameText(Value, "on"))
  287. {
  288. Result = true;
  289. }
  290. else if (SameText(Value, "off"))
  291. {
  292. Result = false;
  293. }
  294. else if (TryStrToInt(Value, IntValue))
  295. {
  296. Result = (IntValue != 0);
  297. }
  298. else
  299. {
  300. throw Exception(FMTLOAD(URL_OPTION_BOOL_VALUE_ERROR, (Value)));
  301. }
  302. return Result;
  303. }
  304. //---------------------------------------------------------------------------
  305. bool __fastcall TOptions::SwitchValue(const UnicodeString Switch, bool Default)
  306. {
  307. return SwitchValue(Switch, Default, Default);
  308. }
  309. //---------------------------------------------------------------------------
  310. bool __fastcall TOptions::UnusedSwitch(UnicodeString & Switch)
  311. {
  312. bool Result = false;
  313. size_t Index = 0;
  314. while (!Result && (Index < FOptions.size()))
  315. {
  316. if ((FOptions[Index].Type == otSwitch) &&
  317. !FOptions[Index].Used)
  318. {
  319. Switch = FOptions[Index].Name;
  320. Result = true;
  321. }
  322. ++Index;
  323. }
  324. return Result;
  325. }
  326. //---------------------------------------------------------------------------
  327. bool __fastcall TOptions::WasSwitchAdded(UnicodeString & Switch, UnicodeString & Value, wchar_t & SwitchMark)
  328. {
  329. bool Result =
  330. DebugAlwaysTrue(FOptions.size() > 0) &&
  331. (FOptions.back().Type == otSwitch);
  332. if (Result)
  333. {
  334. TOption & Option = FOptions.back();
  335. Switch = Option.Name;
  336. Value = Option.Value;
  337. SwitchMark = Option.SwitchMark;
  338. }
  339. return Result;
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TOptions::ParamsProcessed(int ParamsStart, int ParamsCount)
  343. {
  344. if (ParamsCount > 0)
  345. {
  346. DebugAssert((ParamsStart >= 0) && ((ParamsStart - ParamsCount + 1) <= FParamCount));
  347. size_t Index = 0;
  348. while ((Index < FOptions.size()) && (ParamsStart > 0))
  349. {
  350. if (FOptions[Index].Type == otParam)
  351. {
  352. --ParamsStart;
  353. if (ParamsStart == 0)
  354. {
  355. while (ParamsCount > 0)
  356. {
  357. DebugAssert(Index < FOptions.size());
  358. DebugAssert(FOptions[Index].Type == otParam);
  359. FOptions.erase(FOptions.begin() + Index);
  360. --FParamCount;
  361. --ParamsCount;
  362. }
  363. }
  364. }
  365. Index++;
  366. }
  367. }
  368. }
  369. //---------------------------------------------------------------------------
  370. void __fastcall TOptions::LogOptions(TLogOptionEvent OnLogOption)
  371. {
  372. for (size_t Index = 0; Index < FOriginalOptions.size(); Index++)
  373. {
  374. const TOption & Option = FOriginalOptions[Index];
  375. UnicodeString LogStr;
  376. switch (Option.Type)
  377. {
  378. case otParam:
  379. LogStr = FORMAT(L"Parameter: %s", (Option.Value));
  380. DebugAssert(Option.Name.IsEmpty());
  381. break;
  382. case otSwitch:
  383. LogStr =
  384. FORMAT(L"Switch: %s%s%s%s",
  385. (FSwitchMarks[1], Option.Name, (Option.Value.IsEmpty() ? UnicodeString() : FSwitchValueDelimiters.SubString(1, 1)), Option.Value));
  386. break;
  387. default:
  388. DebugFail();
  389. break;
  390. }
  391. OnLogOption(LogStr);
  392. }
  393. }