📄 uoptions.pas
字号:
~param Name the name of the option
~param OptionType the type the option has to be
~param Index out: the index of the option if found
~result if the option could be found }
function TOptionWrapper.GetOptionIndex(const Name: String;
OptionType: TOptionType;
var Index: Cardinal): Boolean;
var i :Cardinal; //counter through the options
Option :TOptionDescription; //the descriptions of the options
begin
Result := False;
i := Count; //get number of options
if i > 0 then //some options?
begin
repeat //search through all options
Dec(i);
Description(i, Option); //get option
until (i = 0) or (CompareText(Name, Option.Name) = 0);
Result := (Option.DataType = OptionType) and //option found and valid?
((i <> 0) or (CompareText(Name, Option.Name) = 0));
if Result then //option found?
Index := i; //return index
end;
end;
{Sets option to the value. It is ignored if the option is unknown.
~param Name the name of the option to set
~param Value the value to set as a string, if enclosed in single quotes "'",
they will be removed before setting it
~result if the value was set (i.e. option known and value valid) }
function TOptionWrapper.SetOptionByName(const Name, Value: String): Boolean;
var i :Cardinal; //counter through the options
Descr :TOptionDescription; //the descriptions of the options
UnQuote :PChar; //to unquote the string
RealValue :String; //the real value to set
OValue :TOptionValue; //value to set
begin
Result := False;
i := Count; //get number of options
if i > 0 then //some options?
begin
repeat //search through all options
Dec(i);
Description(i, Descr); //get option
until (i = 0) or (CompareText(Name, Descr.Name) = 0);
if (i <> 0) or (CompareText(Name, Descr.Name) = 0) then //option found?
begin
if (Value <> '') and (Value[1] = '''') then //value is quoted?
begin
UnQuote := Pointer(Value);
RealValue := AnsiExtractQuotedStr(UnQuote, ''''); //un-quote the string
end
else
RealValue := Value; //just use the value
//parse the string depending on type
Result := StringToValue(RealValue, OValue, Descr);
if Result then //value successfully converted?
Option[i] := OValue; //set value of option
end;
end;
end;
{Gets the value of an option.
~param Name the name of the option whose value should be returned
~param Value out: the value of the named option or '' if it does not exist
~returns whether the option exists }
function TOptionWrapper.GetOptionAsStringByName(const Name: String;
var Value: String): Boolean;
var i :Cardinal; //counter through the options
Descr :TOptionDescription; //the descriptions of the options
begin
Result := False;
i := Count; //get number of options
if i > 0 then //some options?
begin
repeat //search through all options
Dec(i);
Description(i, Descr); //get option
until (i = 0) or (CompareText(Name, Descr.Name) = 0);
if (i <> 0) or (CompareText(Name, Descr.Name) = 0) then //option found?
begin
Value := ValueToString(Option[i], Descr); //get it as a string
Result := True; //it has been found
end
else
Value := '';
end
else
Value := '';
end;
{Reads "Name=Value"-lines and sets the values. Does not handle quoted strings.
~param Options list of string in format "Name=Value" }
procedure TOptionWrapper.LoadOptionList(Options: TStrings);
var i :Integer; //counter through list
S :String; //an entry in the list
j :Integer; //index of the "=" character
begin
for i := 0 to Options.Count - 1 do //for each entry
begin
S := Options[i]; //get the entry
j := pos('=', S); //check if it is "Name=Value"
if j <> 0 then //and set the option
SetOptionByName(copy(S, 1, j - 1), copy(S, j + 1, high(length(S))));
end;
end;
{Reads the section of the default topic and sets the options to the read
values.
~param Options ini-object containing information to set options to }
procedure TOptionWrapper.LoadIniOptions(Options: TCustomIniFile);
var List :TStringList; //list of options in the ini-file
i :Integer; //counter through the options
begin
if DefaultTopic = '' then
raise Exception.Create('Can''t load options from ini! No topic defined for options!');
if Options.SectionExists(DefaultTopic) then //section exists?
begin
List := TStringList.Create;
try
Options.ReadSection(DefaultTopic, List); //read names in section
for i := 0 to List.Count - 1 do //for each option set the value
SetOptionByName(List[i], Options.ReadString(DefaultTopic, List[i], ''));
finally
List.Free;
end;
end;
end;
{Reads the sections of all topics and sets the options to the read values.
~param Options ini-object containing information to set options to }
procedure TOptionWrapper.LoadAllIniOptions(Options: TCustomIniFile);
{Reads all options of the class and its ancestor classes.
~param TheClass the class to load options for }
procedure SetOptions(TheClass: TClass);
var Name :String; //the name of the class
List :TStringList; //list of options in the ini-file
i :Integer; //counter through the options
begin
//not top-most class (TCommentExtractor itself)?
if TheClass <> FBaseClass then
//load the ancestors classes' options
SetOptions(TheClass.ClassParent);
//load options of the class
Name := TheClass.ClassName; //get name of the class
if Options.SectionExists(Name) then //section exists?
begin
List := TStringList.Create;
try
//read names of options in the section
Options.ReadSection(Name, List);
for i := 0 to List.Count - 1 do //for each option set the value
SetOptionByName(List[i], Options.ReadString(Name, List[i], ''));
finally
List.Free;
end;
end;
end;
{Reads all options of the single class. }
procedure SetOptionOfSingleClass;
var i :Integer; //counter through the options
Topic :String; //topic of the options
Descr :TOptionDescription; //descriptions of each option
Value :String; //read value as a string
UnQuote :PChar; //to unquote the string
OValue :TOptionValue; //value to set
begin
for i := 0 to Count - 1 do //for each option
begin
Topic := Self.Topic(i); //get current topic
if Topic <> '' then //topic defined to read from?
begin
Description(i, Descr); //get description of option
if Options.ValueExists(Topic, Descr.Name) then //value in the ini file?
begin
Value := Options.ReadString(Topic, Descr.Name, ''); //read the value
if (Value <> '') and (Value[1] = '''') then //value is quoted?
begin
UnQuote := Pointer(Value);
Value := AnsiExtractQuotedStr(UnQuote, ''''); //un-quote the string
end;
//parse the string depending on type
if StringToValue(Value, OValue, Descr) then //successfully converted?
Option[i] := OValue; //set value of option
end //if Options.ValueExists(Topic, Descr.Name)
end; //if Topic <> ''
end; //for i := 0 to Count - 1
end;
begin
if Assigned(FBaseClass) then //load as a hierarchy?
SetOptions(GetStartClass) //load all options of all classes
else
SetOptionOfSingleClass; //load all options of the one class
end;
{ * * * *** * * *** TCollectionOptionWrapper *** * * *** * * * }
{Creates the wrapper and save the references to the wrappers.
~param Wrappers the list of wrappers }
constructor TCollectionOptionWrapper.Create(const Wrappers: array of
TOptionWrapper);
var i :Integer; //counter through the wrappers
Index :Integer; //number of wrappers
Count :Cardinal; //number of options
begin
inherited Create; //create the object
SetLength(FWrappers, Length(Wrappers)); //create buffer for wrappers
Index := 0; //write at the beginning
for i := 0 to Length(Wrappers) - 1 do //for each wrapper
if Assigned(Wrappers[i]) then //valid wrapper?
begin
FWrappers[Index] := Wrappers[i]; //use it
Inc(Index); //write at the next position
end;
SetLength(FWrappers, Index); //set size of buffer
SetLength(FCountSums, Index); //create buffer for counts
Count := 0; //no options so far
for i := 0 to Index - 1 do //for each wrapper
begin //save summed up count
Inc(Count, FWrappers[i].Count); //of options
FCountSums[i] := Count;
end;
end;
{Returns the wrapper and the corrected index.
~param Index in: the global index of all options;
out: the index in the returned wrapper
~result the wrapper containing the requested option }
function TCollectionOptionWrapper.GetWrapper(
var Index: Cardinal): TOptionWrapper;
var i :Integer; //counter through the wrappers
begin
i := 0; //select wrapper by index of option
while (i < Length(FCountSums)) and (Index >= FCountSums[i]) do
Inc(i);
if i = Length(FCountSums) then //index too big?
raise EInvalidOption.Create('Invalid index for option supplied!');
if i > 0 then //not the first set of options?
//adjust index for selected wrapper
Dec(Index, FCountSums[i - 1]);
Result := FWrappers[i]; //return the selected wrapper
end;
{Returns the number of available options.
~result the number of available options }
function TCollectionOptionWrapper.Count: Cardinal;
begin
Result := FCountSums[Length(FCountSums) - 1]; //return number of all options
end;
{Gets a description of an option.
~param Index index of the option to get data of
~param Desc out: the description of the option (name, type, default value,
etc.) }
procedure TCollectionOptionWrapper.Description(Index: Cardinal;
var Desc: TOptionDescription);
var OldIndex :Cardinal; //the original index
begin
OldIndex := Index; //save the original index
GetWrapper(Index).Description(Index, Desc); //get description from wrapper
//an index to another option is used?
if (Desc.HelpOptionIndex <> HelpOptionIndexShowThisTopic) and
(Desc.HelpOptionIndex <> HelpOptionIndexShowMainTopic) and
(Desc.HelpOptionIndex <> HelpOptionIndexNoHelp) then
//offset the index by the same offset as this index used
Inc(Desc.HelpOptionIndex, OldIndex - Index);
end;
{Gets the value of an option. Call ~[link Description] to get the type and
the meaning of the option.
~param Index index of the option to get the value of
~result the value of the option }
function TCollectionOptionWrapper.GetOption(Index: Cardinal): TOptionValue;
begin
Result := GetWrapper(Index).GetOption(Index); //get option from the wrapper
end;
{Sets the value of an option. Call ~[link Description] to get the type and
the meaning of the option.
~param Index index of the option to set the value
~param Value the new value of the option}
procedure TCollectionOptionWrapper.SetOption(Index: Cardinal;
const Value: TOptionValue);
begin
GetWrapper(Index).SetOption(Index, Value); //set the option for the wrapper
end;
{Gets the topic of an option, the name of the class of the generator the option
is defined in.
~param Index index of the option to get the topic of
~result the topic of the option }
function TCollectionOptionWrapper.Topic(Index: Cardinal): String;
begin
Result := GetWrapper(Index).Topic(Index); //return topic from wrapper
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -