-
Notifications
You must be signed in to change notification settings - Fork 0
/
namevalue.pas
163 lines (139 loc) · 3.59 KB
/
namevalue.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
{
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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. See the
GNU General Public License for more details.
}
// Copyright (c) 2010 - J. Aldo G. de Freitas Junior
Unit
NameValue;
Interface
Uses
Classes,
SysUtils,
Variants;
Type
ENameValue = Class(Exception);
TNameValuePair = Class(TObject)
Private
fName : String;
fValue : Variant;
Public
Constructor Create(Const aName : String; Const aValue : Variant);
Function Formatted : String;
Property Name : String Read fName Write fName;
Property Value : Variant Read fValue Write fValue;
End;
TNameValueList = Array Of TNameValuePair;
TNameValue = Class(TObject)
Private
fPairs : TNameValueList;
Public
Constructor Create;
Destructor Destroy; Override;
Function Add(Const aPair : TNameValuePair): TNameValuePair;
Procedure Delete(Const aPair : TNameValuePair);
Procedure Purge;
Function Find(Const aName : String): TNameValuePair;
Function Exists(Const aName : String): Boolean;
Procedure SetValue(Const aName : String; Const aValue : Variant);
Function GetValue(Const aName : String): Variant;
Function Formatted: String;
Property Pairs : TNameValueList Read fPairs Write fPairs;
End;
Implementation
{ TNameValuePair }
Constructor TNameValuePair.Create(Const aName : String; Const aValue : Variant);
Begin
Inherited Create;
fName := aName;
fValue := aValue;
End;
Function TNameValuePair.Formatted : String;
Begin
Result := fName + '="' + fValue + '"';
End;
{ TNameValue }
Constructor TNameValue.Create;
Begin
Inherited Create;
SetLength(fPairs, 0);
End;
Destructor TNameValue.Destroy;
Begin
Purge;
Inherited Destroy;
End;
Function TNameValue.Add(Const aPair : TNameValuePair): TNameValuePair;
Begin
SetLength(fPairs, Length(fPairs) + 1);
fPairs[High(fPairs)] := aPair;
Result := aPair;
End;
Procedure TNameValue.Delete(Const aPair : TNameValuePair);
Var
lCtrl1,
lCtrl2 : Integer;
Begin
For lCtrl1 := Low(fPairs) To High(fPairs) Do
If fPairs[lCtrl1] = aPair Then
Begin
FreeAndNil(fPairs[lCtrl1]);
For lCtrl2 := lCtrl1 To High(fPairs) - 1 Do
fPairs[lCtrl2] := fPairs[lCtrl2 + 1];
SetLength(fPairs, Length(fPairs) - 1);
End;
End;
Procedure TNameValue.Purge;
Var
lCtrl : Integer;
Begin
For lCtrl := Low(fPairs) To High(fPairs) Do
FreeAndNil(fPairs[lCtrl]);
SetLength(fPairs, 0);
End;
Function TNameValue.Find(Const aName : String): TNameValuePair;
Var
lCtrl : Integer;
Begin
Result := Nil;
For lCtrl := Low(fPairs) To High(fPairs) Do
If fPairs[lCtrl].Name = aName Then
Begin
Result := fPairs[lCtrl];
Break;
End;
End;
Function TNameValue.Exists(Const aName : String): Boolean;
Begin
Result := Find(aName) <> Nil;
End;
Procedure TNameValue.SetValue(Const aName : String; Const aValue : Variant);
Begin
If Exists(aName) Then
Find(aName).Value := aValue
Else
Add(TNameValuePair.Create(aName, aValue));
End;
Function TNameValue.GetValue(Const aName : String): Variant;
Begin
If Exists(aName) Then
Result := Find(aName).Value
Else
Raise ENameValue.Create('Theres no such name.');
End;
Function TNameValue.Formatted: String;
Var
lCtrl : Integer;
Begin
Result := '';
For lCtrl := Low(fPairs) To High(fPairs) Do
If lCtrl < High(fPairs) Then
Result := Result + fPairs[lCtrl].Formatted + ' '
Else
Result := Result + fPairs[lCtrl].Formatted;
End;
End.