Option.cpp 11 KB

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