Menu

[r2998]: / trunk / Src / UURIParams.pas  Maximize  Restore  History

Download this file

168 lines (144 with data), 4.3 kB

  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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2010-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Encapsulates URI query string parameters and generates a query string from
* them.
}
unit UURIParams;
interface
uses
// Delphi
Classes, Generics.Collections;
type
{
TURIParams:
Class that encapsulates URI query string parameters and generates a query
string from them.
}
TURIParams = class(TObject)
strict private
fDict: TDictionary<string,string>;
{Dictionary mapping param names to values}
procedure Add(const Params: TStrings); overload;
{Adds zero or more new parameters to object.
@param Params [in] String list containing parameters to be added. Each
element is a string in name=value format.
}
public
constructor Create; overload;
{Constructs object that contains no parameters.
}
constructor Create(const Params: TStrings); overload;
{Constructs object containing zero or more parameters.
@param Params [in] String list where each element specifies a parameter
in name=value format.
}
destructor Destroy; override;
{Tears down object.
}
procedure Add(const Name, Value: string); overload;
{Adds a single new parameter to object.
@param Name [in] Name of parameter to be added.
@param Value [in] Value of parameter to be added.
}
function EncodedQueryString: string;
{Creates a URI encoded query string containing details of all recorded
parameters.
@return Required query string.
}
function IsEmpty: Boolean;
{Checks if the object is empty, i.e. has no recorded parameters.
@return True if there are no recorded parameters, False otherwise.
}
end;
implementation
uses
// Delphi
SysUtils,
// Project
UComparers, UURIEncode;
{ TURIParams }
procedure TURIParams.Add(const Name, Value: string);
{Adds a single new parameter to object.
@param Name [in] Name of parameter to be added.
@param Value [in] Value of parameter to be added.
}
begin
fDict.Add(Name, Value);
end;
procedure TURIParams.Add(const Params: TStrings);
{Adds zero or more new parameters to object.
@param Params [in] String list containing parameters to be added. Each
element is a string in name=value format.
}
var
Idx: Integer; // loops through all elements of Params
begin
Assert(Assigned(Params), ClassName + '.Add(TStrings): Params is nil');
for Idx := 0 to Pred(Params.Count) do
Add(Params.Names[Idx], Params.ValueFromIndex[Idx]);
end;
constructor TURIParams.Create;
{Constructs object that contains no parameters.
}
begin
inherited Create;
fDict := TDictionary<string,string>.Create(
24, TTextEqualityComparer.Create
);
end;
constructor TURIParams.Create(const Params: TStrings);
{Constructs object containing zero or more parameters.
@param Params [in] String list where each element specifies a parameter in
name=value format.
}
begin
Create;
Add(Params);
end;
destructor TURIParams.Destroy;
{Tears down object.
}
begin
fDict.Free;
inherited;
end;
function TURIParams.EncodedQueryString: string;
{Creates a URI encoded query string containing details of all recorded
parameters.
@return Required query string.
}
var
AParam: TPair<string,string>; // references each recorded parameter
SB: TStringBuilder; // used to build query string
begin
SB := TStringBuilder.Create;
try
for AParam in fDict do
begin
if SB.Length > 0 then
SB.Append('&');
SB.Append(URIEncodeQueryString(AParam.Key));
if AParam.Value <> '' then
SB.AppendFormat('=%s', [URIEncodeQueryString(AParam.Value)]);
end;
Result := SB.ToString;
finally
SB.Free;
end;
end;
function TURIParams.IsEmpty: Boolean;
{Checks if the object is empty, i.e. has no recorded parameters.
@return True if there are no recorded parameters, False otherwise.
}
begin
Result := fDict.Count = 0;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.