Recent

Author Topic: User tags/masks as index of StringsList  (Read 4719 times)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: User tags/masks as index of StringsList
« Reply #15 on: March 14, 2018, 10:49:32 pm »
here is a quick and dirty example
Code: Pascal  [Select][+][-]
  1. type
  2.   TConstArray        = array of TVarRec;
  3.  
  4. function ToArrayOfConst(aList:TStringList):TConstArray;
  5. begin
  6.   SetLength(Result,aList.Count);
  7.   for vCntr := 0 to aList.Count-1 do begin
  8.     Result[vCntr].VType := vtAnsiString;
  9.     Result[vCntr].VAnsiString := @aList[vCntr];
  10.   end;
  11. end;
  12.  
  13. function FormatString(const aFormat:string; aList:TStringList);
  14. var
  15.   vValues:TConstArray;
  16. begin
  17.   vValues := ToArrayOfConst(aList);
  18.   Result  := Format(aFormat, vValues);
  19.   SetLength(vValues, 0);
  20. end;
  21.  
Example of use.
your users set their string in the ini as %0:S %1:S %2:S, %0:S, %1:S and then
Code: Pascal  [Select][+][-]
  1. var
  2.   MValueList :TStringList;
  3. begin
  4.   MyValueList := TStringList.Create;
  5.   try
  6.     MyValueList.Add('Value 1');
  7.     MyValueList.Add('Value 2');
  8.     MyValueList.Add('Value 3');
  9.     MyValueList.Add('Value 4');
  10.     MyValueList.Add('Value 5');
  11.     MyValueList.Add('Value 6');
  12.     MyValueList.Add('Value 7');
  13.     MyValueList.Add('Value 8');
  14.     MyValueList.Add('Value 9');
  15.  
  16.     WriteLn(FormatString(<ReadFormatFromIni>, MyValueList));
  17.   finally
  18.     myvaluelist.free;
  19.   end;
  20.  
<ReadFormatFromIni> can be any process you see fit.

Let me repeat my self. Everything has been typed directly in the browser. You need to test it to make sure it works its just a skeleton to get you going.
« Last Edit: March 14, 2018, 11:00:30 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: User tags/masks as index of StringsList
« Reply #16 on: March 14, 2018, 11:36:29 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32. Function GetMacroValue (Const S:String):String;
  33. Type  TMyType =
  34.  Record
  35.   TagName:String[10];
  36.   TagValue:String[255];
  37.  End;
  38. Const Myvalues:Array[0..1] of TmyType = ((TagName:'input1';TagValue:'Today is a great day'),
  39.                            (TagName:'input2';TagValue:'Tomorrow maybe better'));
  40. Var
  41.  I:Integer;
  42. begin
  43.   I := 0;
  44.   While (I <= High(MyValues))and(MyValues[I].TagName <> S) Do Inc(I);
  45.   If I <= High(MyValues) Then result := MyValues[I].TagValue Else Result := '{'+S+' :BAD ENTRY}';
  46. end;
  47.  
  48. Function MacroBuildString(Const S:String):String;
  49. Var
  50.   IsMacro:Boolean;
  51.   I:Integer;
  52.   MacroStr :String;
  53. Begin
  54.   IsMacro := False;
  55.   For I := 1 to Length(S) do
  56.    begin
  57.      if S[I] = '&' Then //handle Macros.
  58.       Begin
  59.        IsMacro:= Not IsMacro;
  60.        if IsMacro Then Macrostr := '' Else //If starting new macro the clear previous value;
  61.             result := Result+GetMacroValue(MacroStr); //Attached the results
  62.       end else
  63.      if IsMacro Then MacroStr := MacroStr+S[I] Else //Build mac string
  64.      Result := Result+S[I]; //Build main string outside of macro..
  65.    end;
  66. end;
  67.  
  68. procedure TForm1.Button1Click(Sender: TObject);
  69. begin
  70.   ShowMessage(MacroBuildstring(Edit1.Text));
  71. end;
  72.  
  73. end.
  74.  
  75.  

Run that through, enter %inpu1& along with other text to see what you come up with ..
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018