usubst.pp 2.3 KB

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