-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParamUnit.pas
181 lines (155 loc) · 3.78 KB
/
ParamUnit.pas
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
unit ParamUnit;
{*
参数管理器。可以将字串中的变量替换为相应的值。
建议使用的变量名格式:
${var}
{
*}
interface
uses SysUtils, StrUtils, Classes;
type
TParamList = class(TObject)
private
FList: TStrings;
function GetCount: Integer;
procedure SetKeyValue(S: string);
public
constructor Create;
destructor Destroy; override;
procedure SetParam(const Key, Value: string);
function GetParam(const Key: string; var Value: string): Boolean; overload;
function GetParam(Index: Integer; var Key: string; var Value: string): Boolean; overload;
function GetList(const Split: string): string;
procedure SetList(const S, Split: string);
procedure Clear;
property Count: Integer read GetCount;
function ReplaceAllParam(const S: string): string;
class function ReplaceParam(const S, Key, Value: string): string;
class function FindSubStr(const S, BeginS, EndS: string; var R: string): Boolean;
end;
implementation
{ TParam }
procedure TParamList.SetKeyValue(S: string);
var
I: Integer;
Key, Value: string;
begin
I := Pos('=', S);
if I <= 0 then Exit;
Key := LeftStr(S, I - 1);
Value := MidStr(S, I + 1, MaxInt);
SetParam(Key, Value);
end;
procedure TParamList.Clear;
begin
FList.Clear;
end;
constructor TParamList.Create;
begin
FList := TStringList.Create;
end;
destructor TParamList.Destroy;
begin
FreeAndNil(FList);
inherited;
end;
function TParamList.GetCount: Integer;
begin
Result := FList.Count;
end;
function TParamList.GetList(const Split: string): string;
var
I: Integer;
begin
Result := '';
if Count = 0 then Exit;
for I := 0 to Count - 2 do
begin
Result := Result + FList[I] + Split;
end;
Result := Result + FList[Count - 1];
end;
procedure TParamList.SetList(const S, Split: string);
var
B, E: Integer;
begin
B := 1;
E := 0;
while E < Length(S) do
begin
E := PosEx(Split, S, E + 1);
if E = 0 then E := Length(S) + 1;
SetKeyValue(MidStr(S, B, E - B));
B := E + 1;
end;
end;
function TParamList.GetParam(Index: Integer; var Key: string; var Value: string): Boolean;
var
I: Integer;
begin
Result := False;
I := Pos('=', FList[Index]);
if (I <> -1) then
begin
Key := LeftStr(FList[Index], I - 1);
Value := MidStr(FList[Index], I + 1, MAXINT);
Result := True;
end;
end;
function TParamList.GetParam(const Key: string; var Value: string): Boolean;
var
Index, J: Integer;
begin
Result := False;
Index := FList.IndexOfName(Key);
if (Index <> -1) then
begin
J := Pos('=', FList[Index]);
if (J <> 0) then
begin
Value := MidStr(FList[Index], J + 1, MAXINT);
Result := True;
end;
end;
end;
procedure TParamList.SetParam(const Key, Value: string);
var
I: Integer;
begin
I := FList.IndexOfName(Trim(Key));
if (I <> -1) then FList.Delete(I);
FList.Add(Trim(Key) + '=' + Value);
end;
function TParamList.ReplaceAllParam(const S: string): string;
var
I: Integer;
Key: string;
Value: string;
begin
Result := S;
for I := 0 to FList.Count - 1 do
begin
if GetParam(I, Key, Value) then
begin
Result := ReplaceParam(Result, Key, Value);
end;
end;
end;
class function TParamList.ReplaceParam(const S, Key, Value: string): string;
begin
Result := StringReplace(S, Key, Value, [rfReplaceAll, rfIgnoreCase]);
end;
class function TParamList.FindSubStr(const S, BeginS, EndS: string; var R: string): Boolean;
var
B, E: Integer;
begin
Result := False;
B := Pos(BeginS, S);
if (B = 0) then Exit;
B := B + Length(BeginS);
E := PosEx(EndS, S, B);
if (E = 0) then E := MaxInt;
R := MidStr(S, B, E - B);
Result := True;
end;
end.