summaryrefslogtreecommitdiff
path: root/utils/usubst.pp
blob: cf553ae37370d9a999aacd62345d91b5047c7527 (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
{$mode objfpc}
{$H+}
{
    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 : 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.