Option.cpp 10 KB

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