home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Delphi / eXpertDevelopmentKit / SOURCE / XDKSNIPS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-01-26  |  5.7 KB  |  213 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {       eXpert Development Kit                          }
  4. {                                                       }
  5. {       Copyright (c) 1996,97 Sergey Orlik              }
  6. {       - product manager of Borland Russia             }
  7. {                                                       }
  8. {*******************************************************}
  9. unit XDKSnips;
  10.  
  11. interface
  12. uses
  13.   SysUtils, Classes;
  14.  
  15. type
  16.   ExdkSnippetError = class(Exception);
  17.  
  18.   TxdkSnippet = class(TComponent)
  19.   private
  20.     FCode: TStrings;
  21.     FParams: TStrings;
  22.     FParamBracket: char;
  23.     FResultCode : TSTrings;
  24.     procedure SetCode(Value:TStrings);
  25.     procedure SetCodeText(Value:string);
  26.     function  GetCodeText:string;
  27.     procedure SetCodeStream(Value:TStream);
  28.     function  GetCodeStream:TStream;  //create new stream; you need free it after using!
  29.     procedure SetParams(Value:TStrings);
  30.     function  GetParamValue(const ParamName:string):string;
  31.     procedure SetParamValue(const ParamName:string; const Value:string);
  32.   public
  33.     constructor Create(AOwner:TComponent); override;
  34.     destructor Destroy; override;
  35.     function ParamsCount:integer;
  36.     // analize code and replace params to corresponding values in FResultCode;
  37.     // **you need explicit call DoResultCode** in your methods before getting result code.
  38.     procedure ExtractParams; //analize code and fill list of params
  39.     procedure DoResultCode;
  40.     function ResultCode:TStrings;
  41.     function ResultCodeText:string;
  42.     // create new TMemoryStream - you need free it after using!
  43.     function ResultCodeStream:TStream;
  44.     property CodeText: string read GetCodeText write SetCodeText;
  45.     // create new TMemoryStream - you need free it after using!
  46.     property CodeStream: TStream read GetCodeStream write SetCodeStream;
  47.     property Params:TStrings read FParams write SetParams;
  48.     property ParamValues[const ParamName: string]:string read GetParamValue write SetParamValue;
  49.   published
  50.     property Code: TStrings read FCode write SetCode;
  51.     property ParamBracket:char read FParamBracket write  FParamBracket default '%';
  52.   end;
  53.  
  54. //====================================================================
  55. implementation
  56. //====================================================================
  57.  
  58. //====================================================================
  59. // TxdkSnippet
  60.  
  61. constructor TxdkSnippet.Create(AOwner: TComponent);
  62. begin
  63.   inherited Create(AOwner);
  64.   FCode:=TStringList.Create;
  65.   FParamBracket:='%';
  66.   FParams:=TStringList.Create;
  67.   FResultCode:=TStringList.Create;
  68. end;
  69.  
  70. destructor TxdkSnippet.Destroy;
  71. begin
  72.   FResultCode.Free;
  73.   FParams.Free;
  74.   FCode.Free;
  75.   inherited Destroy;
  76. end;
  77.  
  78. function TxdkSnippet.ParamsCount:integer;
  79. begin
  80.   Result:=FParams.Count;
  81. end;
  82.  
  83. { working with code template - 'snippet' }
  84. procedure TxdkSnippet.SetCode(Value:TStrings);
  85. begin
  86.   FCode.Assign(Value);
  87. end;
  88.  
  89. procedure TxdkSnippet.SetCodeText(Value:string);
  90. begin
  91.   FCode.Text:=Value;
  92. end;
  93.  
  94. function TxdkSnippet.GetCodeText:string;
  95. begin
  96.   Result:=FCode.Text;
  97. end;
  98.  
  99. procedure TxdkSnippet.SetCodeStream(Value:TStream);
  100. begin
  101.   if Value<>nil then
  102.   begin
  103.     FCode.LoadFromStream(Value);
  104.   end;
  105. end;
  106.  
  107. function TxdkSnippet.GetCodeStream:TStream;
  108. begin
  109.   Result:=TMemoryStream.Create;
  110.   FCode.SaveToStream(Result);
  111. end;
  112.  
  113. { working with params (and values) }
  114.  
  115. procedure TxdkSnippet.SetParams(Value:TStrings);
  116. begin
  117.   FParams.Assign(Value);
  118. end;
  119.  
  120. // ExtractParams analize code template and fill list of params (each param in list is unique)
  121. procedure TxdkSnippet.ExtractParams;
  122. var
  123.   CodeBuf : string;
  124.   Len,
  125.   CurPos  : integer;
  126.   CurChar : char;
  127.   isParam : boolean;
  128.   Param   : string;
  129. begin
  130.   FParams.Clear;
  131.   CurPos:=0;
  132.   CodeBuf:=GetCodeText;
  133.   Len:=Length(CodeBuf);
  134.   if Len<3 then Exit;
  135.   isParam:=false;
  136.   repeat
  137.     CurChar:=CodeBuf[CurPos];
  138.     if CurChar=FParamBracket then
  139.     begin
  140.       isParam:=not isParam;
  141.       if isParam then
  142.         Param:=EmptyStr
  143.       else
  144.         if FParams.IndexOfName(Param)=-1 then
  145.           FParams.Append(Param);
  146.       inc(CurPos);
  147.       Continue;
  148.     end;
  149.     if isParam then
  150.       Param:=Param+CurChar;
  151.     inc(CurPos);
  152.   until CurPos>=Len;
  153. end;
  154.  
  155. function TxdkSnippet.GetParamValue(const ParamName:string):string;
  156. begin
  157.   Result:=FParams.Values[ParamName];
  158. end;
  159.  
  160. procedure TxdkSnippet.SetParamValue(const ParamName:string; const Value:string);
  161. begin
  162.   FParams.Values[ParamName]:=Value;
  163. end;
  164.  
  165. { working with result code snippet }
  166.  
  167. // DoResultCode analize code template and replace all params to corresponding values
  168. // sorry, this method is working properly, but not optimized ... :(
  169. procedure TxdkSnippet.DoResultCode;
  170. var
  171.   CodeBuf : string;
  172.   i, Len,
  173.   CurPos  : integer;
  174.  
  175.   function NonCasePos(Substr:string;S:string):integer;
  176.   begin
  177.      Result:=Pos(UpperCase(Substr),UpperCase(S));
  178.   end;
  179.  
  180. begin
  181.   CodeBuf:=GetCodeText;
  182.   for i:=0 to FParams.Count-1 do
  183.   begin
  184.     if FParams.Values[Params.Names[i]]=EmptyStr then continue;
  185.     repeat
  186.       CurPos:=NonCasePos(FParamBracket+Params.Names[i]+FParamBracket,CodeBuf);
  187.       if CurPos=0 then Break;
  188.       Len:=Length(Params.Names[i])+2;
  189.       System.Delete(CodeBuf,CurPos,Len);
  190.       System.Insert(FParams.Values[Params.Names[i]],CodeBuf,CurPos);
  191.     until CurPos=0;
  192.   end;
  193.   FResultCode.Text:=CodeBuf;
  194. end;
  195.  
  196. function TxdkSnippet.ResultCode:TStrings;
  197. begin
  198.   Result:=FResultCode;
  199. end;
  200.  
  201. function TxdkSnippet.ResultCodeText:string;
  202. begin
  203.   Result:=FResultCode.Text;
  204. end;
  205.  
  206. function TxdkSnippet.ResultCodeStream:TStream;
  207. begin
  208.   Result:=TMemoryStream.Create;
  209.   FResultCode.SaveToStream(Result);
  210. end;
  211.  
  212. end.
  213.