Option.cpp 11 KB

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