usubst.pp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. {$mode objfpc}
  2. {$H+}
  3. {
  4. This file is part of Free Pascal build tools
  5. Copyright (c) 2005 by Michael Van Canneyt
  6. Implements string substitutions
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. unit usubst;
  14. interface
  15. uses SysUtils,Classes;
  16. // Add N=V pair to list.
  17. Procedure AddToList(List : TStrings; Const N,V : String);
  18. // Split NV to N/V and call AddToList
  19. Function AddPair(List : TStrings; Const NV : String) : Boolean;
  20. // Perform substitutions in S, from List.
  21. Function DoSubStitutions(List : TStrings; Var S : String) : Integer;
  22. implementation
  23. Procedure AddToList(List : TStrings; Const N,V : String);
  24. var
  25. I : Integer;
  26. begin
  27. I:=List.IndexOfName(N);
  28. If (V='') then
  29. begin
  30. If (I<>-1) then
  31. List.Delete(I)
  32. end
  33. else
  34. begin
  35. If (I=-1) then
  36. List.Add(N+'='+V)
  37. else
  38. List[I]:=N+'='+V;
  39. end;
  40. end;
  41. Function AddPair(List : TStrings; Const NV : String) : Boolean;
  42. Var
  43. P : Integer;
  44. N,V : string;
  45. begin
  46. P:=Pos('=',NV);
  47. Result:=(P<>0);
  48. If Result then
  49. begin
  50. V:=NV;
  51. N:=Copy(V,1,P-1);
  52. Delete(V,1,P);
  53. AddToList(List,N,V);
  54. end;
  55. end;
  56. Function DoSubstitutions(List : TStrings; Var S : String) : Integer;
  57. Var
  58. N,T : String;
  59. P : Integer;
  60. begin
  61. Result:=0;
  62. T:=S;
  63. S:='';
  64. P:=Pos('%',T);
  65. While (P>0) do
  66. begin
  67. S:=S+Copy(T,1,P-1);
  68. Delete(T,1,P);
  69. If (Length(T)>0) then
  70. if (T[1]='%') then
  71. begin
  72. S:=S+'%';
  73. Delete(T,1,1);
  74. end
  75. else
  76. begin
  77. P:=Pos('%',T);
  78. If (P=0) then
  79. S:=S+'%'
  80. else
  81. begin
  82. N:=Copy(T,1,P-1);
  83. Delete(T,1,P);
  84. S:=S+List.Values[N];
  85. end;
  86. end;
  87. P:=Pos('%',T);
  88. end;
  89. S:=S+T;
  90. end;
  91. end.