ProgParams.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "ProgParams.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. //---------------------------------------------------------------------------
  9. TProgramParams * TProgramParams::SInstance = NULL;
  10. //---------------------------------------------------------------------------
  11. TProgramParams * __fastcall TProgramParams::Instance()
  12. {
  13. assert(SInstance != NULL);
  14. return SInstance;
  15. }
  16. //---------------------------------------------------------------------------
  17. TProgramParams::TProgramParams()
  18. {
  19. assert(SInstance == NULL);
  20. SInstance = this;
  21. FParameters = new TStringList();
  22. for (int i = 0; i <= ::ParamCount(); i++)
  23. {
  24. FParameters->Add(ParamStr(i));
  25. }
  26. FSwitchMarks = "-/";
  27. FSwitchValueDelimiters = ":=";
  28. FIgnoreCase = true;
  29. ResetParamCount();
  30. }
  31. //---------------------------------------------------------------------------
  32. TProgramParams::~TProgramParams()
  33. {
  34. delete FParameters;
  35. assert(SInstance == this);
  36. SInstance = NULL;
  37. }
  38. //---------------------------------------------------------------------------
  39. void __fastcall TProgramParams::ResetParamCount()
  40. {
  41. for (int i = 0; i < (sizeof(FParamCount) / sizeof(*FParamCount)); i++)
  42. {
  43. FParamCount[i] = -1;
  44. }
  45. }
  46. //---------------------------------------------------------------------------
  47. Integer __fastcall TProgramParams::GetCount()
  48. {
  49. return FParameters->Count - 1;
  50. }
  51. //---------------------------------------------------------------------------
  52. TParamType __fastcall TProgramParams::ParamType(Integer Index, AnsiString & Value)
  53. {
  54. if (Index < 1 || Index > Count) throw Exception("");
  55. TParamType Result;
  56. Value = FParameters->Strings[Index];
  57. if ((Value.Length() >= 2) && (FSwitchMarks.Pos(Value[1]) > 0))
  58. {
  59. Result = ptSwitch;
  60. for (int i = 2; i <= Value.Length(); i++)
  61. {
  62. if ((i > 2) && (Value.IsDelimiter(FSwitchValueDelimiters, i)))
  63. {
  64. break;
  65. }
  66. else if ((Value[i] != '?') && ((UpCase(Value[i]) < 'A') || (UpCase(Value[i]) > 'Z')))
  67. {
  68. Result = ptParam;
  69. break;
  70. }
  71. }
  72. if (Result == ptSwitch)
  73. {
  74. Value.Delete(1, 1);
  75. }
  76. }
  77. else
  78. {
  79. Result = ptParam;
  80. }
  81. return Result;
  82. }
  83. //---------------------------------------------------------------------------
  84. Integer __fastcall TProgramParams::GetParamTypeCount(int Type)
  85. {
  86. if (FParamCount[Type] < 0)
  87. {
  88. FParamCount[Type] = 0;
  89. AnsiString S;
  90. for (Integer Index = 1; Index <= Count; Index++)
  91. {
  92. if (ParamType(Index, S) == Type)
  93. {
  94. FParamCount[Type]++;
  95. }
  96. }
  97. }
  98. return FParamCount[Type];
  99. }
  100. //---------------------------------------------------------------------------
  101. AnsiString __fastcall TProgramParams::GetParam(Integer Index)
  102. {
  103. if (Index < 0 || Index > ParamCount) throw Exception("");
  104. if (Index == 0) return FParameters->Strings[0];
  105. else
  106. {
  107. AnsiString S;
  108. Integer P = 1;
  109. while (Index > 0)
  110. {
  111. if (ParamType(P, S) == ptParam) Index--;
  112. P++;
  113. }
  114. return S;
  115. }
  116. }
  117. //---------------------------------------------------------------------------
  118. bool __fastcall TProgramParams::FindSwitch(const AnsiString Switch,
  119. AnsiString & Value, int & ParamsStart, int & ParamsCount)
  120. {
  121. ParamsStart = 0;
  122. int Index = 1;
  123. bool Found = false;
  124. while ((Index <= Count) && !Found)
  125. {
  126. AnsiString S;
  127. if (ParamType(Index, S) == ptParam) ParamsStart++;
  128. else
  129. if (ParamType(Index, S) == ptSwitch)
  130. {
  131. bool HasValue = false;
  132. AnsiString Sw, V;
  133. Integer Pos = 1;
  134. while (Pos <= S.Length() && !HasValue)
  135. {
  136. HasValue = S.IsDelimiter(FSwitchValueDelimiters, Pos);
  137. if (!HasValue) Pos++;
  138. }
  139. if (HasValue)
  140. {
  141. Sw = S.SubString(1, Pos-1);
  142. V = S.SubString(Pos+1, S.Length()-Pos);
  143. }
  144. else
  145. {
  146. Sw = S;
  147. V = "";
  148. };
  149. if ((IgnoreCase && (AnsiCompareText(Switch, Sw) == 0)) ||
  150. (!IgnoreCase && (AnsiCompareStr(Switch, Sw) == 0)))
  151. {
  152. Value = V;
  153. Found = true;
  154. }
  155. }
  156. Index++;
  157. }
  158. ParamsCount = 0;
  159. if (Found)
  160. {
  161. AnsiString S;
  162. ParamsStart++;
  163. while ((Index + ParamsCount <= Count) &&
  164. (ParamType(Index + ParamsCount, S) == ptParam))
  165. {
  166. ParamsCount++;
  167. }
  168. }
  169. else
  170. {
  171. ParamsStart = 0;
  172. }
  173. return Found;
  174. }
  175. //---------------------------------------------------------------------------
  176. bool __fastcall TProgramParams::FindSwitch(const AnsiString Switch, AnsiString & Value)
  177. {
  178. int ParamsStart;
  179. int ParamsCount;
  180. return FindSwitch(Switch, Value, ParamsStart, ParamsCount);
  181. }
  182. //---------------------------------------------------------------------------
  183. bool __fastcall TProgramParams::FindSwitch(const AnsiString Switch)
  184. {
  185. AnsiString Value;
  186. int ParamsStart;
  187. int ParamsCount;
  188. return FindSwitch(Switch, Value, ParamsStart, ParamsCount);
  189. }
  190. //---------------------------------------------------------------------------
  191. bool __fastcall TProgramParams::FindSwitch(const AnsiString Switch,
  192. TStrings * Params, int ParamsMax)
  193. {
  194. AnsiString Value;
  195. int ParamsStart;
  196. int ParamsCount;
  197. bool Result = FindSwitch(Switch, Value, ParamsStart, ParamsCount);
  198. if (Result)
  199. {
  200. if ((ParamsMax >= 0) && (ParamsCount > ParamsMax))
  201. {
  202. ParamsCount = ParamsMax;
  203. }
  204. int Index = 0;
  205. while (Index < ParamsCount)
  206. {
  207. Params->Add(Param[ParamsStart + Index]);
  208. Index++;
  209. }
  210. ParamsProcessed(ParamsStart, ParamsCount);
  211. }
  212. return Result;
  213. }
  214. //---------------------------------------------------------------------------
  215. AnsiString __fastcall TProgramParams::SwitchValue(const AnsiString Switch, const AnsiString Default)
  216. {
  217. AnsiString Value;
  218. FindSwitch(Switch, Value);
  219. if (Value.IsEmpty()) Value = Default;
  220. return Value;
  221. }
  222. //---------------------------------------------------------------------------
  223. void __fastcall TProgramParams::ParamsProcessed(int ParamsStart, int ParamsCount)
  224. {
  225. int Index = 1;
  226. int Position = 0;
  227. while (Index <= this->Count)
  228. {
  229. AnsiString S;
  230. if (ParamType(Index, S) == ptParam)
  231. {
  232. Position++;
  233. if ((Position >= ParamsStart) && (Position < ParamsStart + ParamsCount))
  234. {
  235. FParameters->Delete(Index);
  236. Index--;
  237. }
  238. }
  239. Index++;
  240. }
  241. ResetParamCount();
  242. }