123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include <Common.h>
- #include "Option.h"
- #include "TextsCore.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- __fastcall TOptions::TOptions()
- {
- FSwitchMarks = L"-/";
- FSwitchValueDelimiters = L":=";
- FNoMoreSwitches = false;
- FParamCount = 0;
- }
- //---------------------------------------------------------------------------
- void __fastcall TOptions::Add(UnicodeString Value)
- {
- if (!FNoMoreSwitches &&
- (Value.Length() == 2) &&
- (Value[1] == Value[2]) &&
- (FSwitchMarks.Pos(Value[1]) > 0))
- {
- FNoMoreSwitches = true;
- }
- else
- {
- bool Switch = false;
- int Index = 0; // shut up
- if (!FNoMoreSwitches &&
- (Value.Length() >= 2) &&
- (FSwitchMarks.Pos(Value[1]) > 0))
- {
- Index = 2;
- Switch = true;
- while (Switch && (Index <= Value.Length()))
- {
- if (Value.IsDelimiter(FSwitchValueDelimiters, Index))
- {
- break;
- }
- // this is to treat /home/martin as parameter, not as switch
- else if ((Value[Index] != L'?') && !IsLetter(Value[Index]))
- {
- Switch = false;
- break;
- }
- ++Index;
- }
- }
- if (Switch)
- {
- TOption Option;
- Option.Type = otSwitch;
- Option.Name = Value.SubString(2, Index - 2);
- Option.Value = Value.SubString(Index + 1, Value.Length());
- Option.Used = false;
- FOptions.push_back(Option);
- }
- else
- {
- TOption Option;
- Option.Type = otParam;
- Option.Value = Value;
- Option.Used = false;
- FOptions.push_back(Option);
- ++FParamCount;
- }
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TOptions::GetParam(int Index)
- {
- assert((Index >= 1) && (Index <= FParamCount));
- UnicodeString Result;
- size_t I = 0;
- while ((I < FOptions.size()) && (Index > 0))
- {
- if (FOptions[I].Type == otParam)
- {
- --Index;
- if (Index == 0)
- {
- Result = FOptions[I].Value;
- FOptions[I].Used = true;
- }
- }
- ++I;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOptions::GetEmpty()
- {
- return FOptions.empty();
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOptions::FindSwitch(const UnicodeString Switch,
- UnicodeString & Value, int & ParamsStart, int & ParamsCount)
- {
- ParamsStart = 0;
- int Index = 0;
- bool Found = false;
- while ((Index < int(FOptions.size())) && !Found)
- {
- if (FOptions[Index].Type == otParam)
- {
- ParamsStart++;
- }
- else if (FOptions[Index].Type == otSwitch)
- {
- if (AnsiSameText(FOptions[Index].Name, Switch))
- {
- Found = true;
- Value = FOptions[Index].Value;
- FOptions[Index].Used = true;
- }
- }
- Index++;
- }
- ParamsCount = 0;
- if (Found)
- {
- ParamsStart++;
- while ((Index + ParamsCount < int(FOptions.size())) &&
- (FOptions[Index + ParamsCount].Type == otParam))
- {
- ParamsCount++;
- }
- }
- else
- {
- ParamsStart = 0;
- }
- return Found;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOptions::FindSwitch(const UnicodeString Switch, UnicodeString & Value)
- {
- int ParamsStart;
- int ParamsCount;
- return FindSwitch(Switch, Value, ParamsStart, ParamsCount);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOptions::FindSwitch(const UnicodeString Switch)
- {
- UnicodeString Value;
- int ParamsStart;
- int ParamsCount;
- return FindSwitch(Switch, Value, ParamsStart, ParamsCount);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOptions::FindSwitch(const UnicodeString Switch,
- TStrings * Params, int ParamsMax)
- {
- UnicodeString 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;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TOptions::SwitchValue(const UnicodeString Switch,
- const UnicodeString Default)
- {
- UnicodeString Value;
- FindSwitch(Switch, Value);
- if (Value.IsEmpty())
- {
- Value = Default;
- }
- return Value;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOptions::SwitchValue(const UnicodeString Switch, bool Default, bool DefaultOnNonExistence)
- {
- bool Result;
- int IntValue;
- UnicodeString Value;
- if (!FindSwitch(Switch, Value))
- {
- Result = DefaultOnNonExistence;
- }
- else if (Value.IsEmpty())
- {
- Result = Default;
- }
- else if (SameText(Value, "on"))
- {
- Result = true;
- }
- else if (SameText(Value, "off"))
- {
- Result = false;
- }
- else if (TryStrToInt(Value, IntValue))
- {
- Result = (IntValue != 0);
- }
- else
- {
- throw Exception(FMTLOAD(URL_OPTION_BOOL_VALUE_ERROR, (Value)));
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOptions::SwitchValue(const UnicodeString Switch, bool Default)
- {
- return SwitchValue(Switch, Default, Default);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOptions::UnusedSwitch(UnicodeString & Switch)
- {
- bool Result = false;
- size_t Index = 0;
- while (!Result && (Index < FOptions.size()))
- {
- if ((FOptions[Index].Type == otSwitch) &&
- !FOptions[Index].Used)
- {
- Switch = FOptions[Index].Name;
- Result = true;
- }
- ++Index;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TOptions::ParamsProcessed(int ParamsStart, int ParamsCount)
- {
- if (ParamsCount > 0)
- {
- assert((ParamsStart >= 0) && ((ParamsStart - ParamsCount + 1) <= FParamCount));
- size_t Index = 0;
- while ((Index < FOptions.size()) && (ParamsStart > 0))
- {
- if (FOptions[Index].Type == otParam)
- {
- --ParamsStart;
- if (ParamsStart == 0)
- {
- while (ParamsCount > 0)
- {
- assert(Index < FOptions.size());
- assert(FOptions[Index].Type == otParam);
- FOptions.erase(FOptions.begin() + Index);
- --FParamCount;
- --ParamsCount;
- }
- }
- }
- Index++;
- }
- }
- }
|