Option.cpp 9.8 KB

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