| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include <Common.h>
- #include "ProgParams.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- TProgramParams * TProgramParams::SInstance = NULL;
- //---------------------------------------------------------------------------
- TProgramParams * __fastcall TProgramParams::Instance()
- {
- assert(SInstance != NULL);
- return SInstance;
- }
- //---------------------------------------------------------------------------
- TProgramParams::TProgramParams()
- {
- assert(SInstance == NULL);
- SInstance = this;
- FParameters = new TStringList();
- for (int i = 0; i <= ::ParamCount(); i++)
- {
- FParameters->Add(ParamStr(i));
- }
- FSwitchMarks = "-/";
- FSwitchValueDelimiters = ":=";
- FIgnoreCase = true;
- ResetParamCount();
- }
- //---------------------------------------------------------------------------
- TProgramParams::~TProgramParams()
- {
- delete FParameters;
- assert(SInstance == this);
- SInstance = NULL;
- }
- //---------------------------------------------------------------------------
- void __fastcall TProgramParams::ResetParamCount()
- {
- for (int i = 0; i < (sizeof(FParamCount) / sizeof(*FParamCount)); i++)
- {
- FParamCount[i] = -1;
- }
- }
- //---------------------------------------------------------------------------
- Integer __fastcall TProgramParams::GetCount()
- {
- return FParameters->Count - 1;
- }
- //---------------------------------------------------------------------------
- TParamType __fastcall TProgramParams::ParamType(Integer Index, AnsiString & Value)
- {
- if (Index < 1 || Index > Count) throw Exception("");
- TParamType Result;
- Value = FParameters->Strings[Index];
- if ((Value.Length() >= 2) && (FSwitchMarks.Pos(Value[1]) > 0))
- {
- Result = ptSwitch;
- for (int i = 2; i <= Value.Length(); i++)
- {
- if ((i > 2) && (Value.IsDelimiter(FSwitchValueDelimiters, i)))
- {
- break;
- }
- else if ((Value[i] != '?') && ((UpCase(Value[i]) < 'A') || (UpCase(Value[i]) > 'Z')))
- {
- Result = ptParam;
- break;
- }
- }
- if (Result == ptSwitch)
- {
- Value.Delete(1, 1);
- }
- }
- else
- {
- Result = ptParam;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- Integer __fastcall TProgramParams::GetParamTypeCount(int Type)
- {
- if (FParamCount[Type] < 0)
- {
- FParamCount[Type] = 0;
- AnsiString S;
- for (Integer Index = 1; Index <= Count; Index++)
- {
- if (ParamType(Index, S) == Type)
- {
- FParamCount[Type]++;
- }
- }
- }
- return FParamCount[Type];
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TProgramParams::GetParam(Integer Index)
- {
- if (Index < 0 || Index > ParamCount) throw Exception("");
- if (Index == 0) return FParameters->Strings[0];
- else
- {
- AnsiString S;
- Integer P = 1;
- while (Index > 0)
- {
- if (ParamType(P, S) == ptParam) Index--;
- P++;
- }
- return S;
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TProgramParams::FindSwitch(const AnsiString Switch,
- AnsiString & Value, int & ParamsStart, int & ParamsCount)
- {
- ParamsStart = 0;
- int Index = 1;
- bool Found = false;
- while ((Index <= Count) && !Found)
- {
- AnsiString S;
- if (ParamType(Index, S) == ptParam) ParamsStart++;
- else
- if (ParamType(Index, S) == ptSwitch)
- {
- bool HasValue = false;
- AnsiString Sw, V;
- Integer Pos = 1;
- while (Pos <= S.Length() && !HasValue)
- {
- HasValue = S.IsDelimiter(FSwitchValueDelimiters, Pos);
- if (!HasValue) Pos++;
- }
- if (HasValue)
- {
- Sw = S.SubString(1, Pos-1);
- V = S.SubString(Pos+1, S.Length()-Pos);
- }
- else
- {
- Sw = S;
- V = "";
- };
- if ((IgnoreCase && (AnsiCompareText(Switch, Sw) == 0)) ||
- (!IgnoreCase && (AnsiCompareStr(Switch, Sw) == 0)))
- {
- Value = V;
- Found = true;
- }
- }
- Index++;
- }
- ParamsCount = 0;
- if (Found)
- {
- AnsiString S;
- ParamsStart++;
- while ((Index + ParamsCount <= Count) &&
- (ParamType(Index + ParamsCount, S) == ptParam))
- {
- ParamsCount++;
- }
- }
- else
- {
- ParamsStart = 0;
- }
- return Found;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TProgramParams::FindSwitch(const AnsiString Switch, AnsiString & Value)
- {
- int ParamsStart;
- int ParamsCount;
- return FindSwitch(Switch, Value, ParamsStart, ParamsCount);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TProgramParams::FindSwitch(const AnsiString Switch)
- {
- AnsiString Value;
- int ParamsStart;
- int ParamsCount;
- return FindSwitch(Switch, Value, ParamsStart, ParamsCount);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TProgramParams::FindSwitch(const AnsiString Switch,
- TStrings * Params, int ParamsMax)
- {
- AnsiString Value;
- int ParamsStart;
- int ParamsCount;
- bool Result = FindSwitch(Switch, Value, ParamsStart, ParamsCount);
- if (Result)
- {
- if ((ParamsMax >= 0) && (ParamsCount > ParamsMax))
- {
- ParamsCount = ParamsMax;
- }
- int Index = 0;
- while (Index < ParamsCount)
- {
- Params->Add(Param[ParamsStart + Index]);
- Index++;
- }
- ParamsProcessed(ParamsStart, ParamsCount);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TProgramParams::SwitchValue(const AnsiString Switch, const AnsiString Default)
- {
- AnsiString Value;
- FindSwitch(Switch, Value);
- if (Value.IsEmpty()) Value = Default;
- return Value;
- }
- //---------------------------------------------------------------------------
- void __fastcall TProgramParams::ParamsProcessed(int ParamsStart, int ParamsCount)
- {
- int Index = 1;
- int Position = 0;
- while (Index <= this->Count)
- {
- AnsiString S;
- if (ParamType(Index, S) == ptParam)
- {
- Position++;
- if ((Position >= ParamsStart) && (Position < ParamsStart + ParamsCount))
- {
- FParameters->Delete(Index);
- Index--;
- }
- }
- Index++;
- }
- ResetParamCount();
- }
|