summaryrefslogtreecommitdiff
path: root/sparcbw/compiler/utils/usubst.pp
blob: 660459f74d609b445cb8680f406097d8b699da99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
{$mode objfpc}
{$H+}
{
    $Id: usubst.pp,v 1.1 2005/02/05 10:25:30 peter Exp $
    This file is part of Free Pascal build tools
    Copyright (c) 2005 by Michael Van Canneyt

    Implements string substitutions

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
unit usubst;

interface

uses SysUtils,Classes;

// Add N=V pair to list.
Procedure AddToList(List : TStrings; Const N,V : String);
// Split NV to N/V and call AddToList
Function  AddPair(List : TStrings; Const NV : String) : Boolean;
// Perform substitutions in S, from List.
Function  DoSubStitutions(List : TStrings; Var S : String) : Integer;

implementation

Procedure AddToList(List : TStrings; Const N,V : String);

var
  I : Integer;

begin
  I:=List.IndexOfName(N);
  If (V='') then
    begin
    If (I<>-1) then
      List.Delete(I)
    end
  else
    begin
    If (I=-1) then
      List.Add(N+'='+V)
    else
      List[I]:=N+'='+V;
    end;
end;

Function AddPair(List : TStrings; Const NV : String) : Boolean;

Var
  P,I : Integer;
  N,V : string;

begin
  P:=Pos('=',NV);
  Result:=(P<>0);
  If Result then
    begin
    V:=NV;
    N:=Copy(V,1,P-1);
    Delete(V,1,P);
    AddToList(List,N,V);
    end;
end;

Function DoSubstitutions(List : TStrings; Var S : String) : Integer;

Var
  N,T : String;
  P : Integer;

begin
  Result:=0;
  T:=S;
  S:='';
  P:=Pos('%',T);
  While (P>0) do
    begin
    S:=S+Copy(T,1,P-1);
    Delete(T,1,P);
    If (Length(T)>0) then
      if (T[1]='%') then
        begin
        S:=S+'%';
        Delete(T,1,1);
        end
      else
        begin
        P:=Pos('%',T);
        If (P=0) then
          S:=S+'%'
        else
          begin
          N:=Copy(T,1,P-1);
          Delete(T,1,P);
          S:=S+List.Values[N];
          end;
        end;
    P:=Pos('%',T);
    end;
  S:=S+T;
end;

end.
{
  $Log: usubst.pp,v $
  Revision 1.1  2005/02/05 10:25:30  peter
    * move tools to compiler/utils/

  Revision 1.2  2005/01/09 15:19:03  peter
    * fix linebreak

  Revision 1.1  2005/01/09 13:36:12  michael
  + Initial implementation of installer tools


}