1 | unit UURI;
|
---|
2 |
|
---|
3 | // Date: 2011-04-04
|
---|
4 |
|
---|
5 | {$mode delphi}
|
---|
6 |
|
---|
7 | interface
|
---|
8 |
|
---|
9 | uses
|
---|
10 | Classes, SysUtils;
|
---|
11 |
|
---|
12 | const
|
---|
13 | URIPathSeparator = '/';
|
---|
14 |
|
---|
15 | type
|
---|
16 |
|
---|
17 | { TPath }
|
---|
18 |
|
---|
19 | TPath = class
|
---|
20 | private
|
---|
21 | function GetAsString: string;
|
---|
22 | procedure SetAsString(AValue: string);
|
---|
23 | public
|
---|
24 | Items: TStringList;
|
---|
25 | IsAbsolute: Boolean;
|
---|
26 | DirSeparator: string;
|
---|
27 | procedure Assign(Source: TPath);
|
---|
28 | constructor Create;
|
---|
29 | destructor Destroy; override;
|
---|
30 | property AsString: string read GetAsString write SetAsString;
|
---|
31 | end;
|
---|
32 |
|
---|
33 | { TFileName }
|
---|
34 |
|
---|
35 | TFileNamePart = (fnpDrive, fnpDirectory, fnpName, fnpExtension);
|
---|
36 | TFileNameParts = set of TFileNamePart;
|
---|
37 |
|
---|
38 | TFileName = class
|
---|
39 | private
|
---|
40 | public
|
---|
41 | Drive: string;
|
---|
42 | Directory: TPath;
|
---|
43 | Name: string;
|
---|
44 | Extension: string;
|
---|
45 | function Combine(Parts: TFileNameParts = [fnpDrive, fnpDirectory, fnpName, fnpExtension]): string;
|
---|
46 | procedure Parse(AValue: string);
|
---|
47 | procedure Assign(Source: TFileName);
|
---|
48 | constructor Create;
|
---|
49 | destructor Destroy; override;
|
---|
50 | end;
|
---|
51 |
|
---|
52 | { TURI }
|
---|
53 |
|
---|
54 | TURI = class(TPersistent)
|
---|
55 | private
|
---|
56 | function GetAsString: string;
|
---|
57 | procedure SetAsString(Value: string);
|
---|
58 | public
|
---|
59 | Scheme: string;
|
---|
60 | Authority: string;
|
---|
61 | Path: TFileName;
|
---|
62 | Query: string;
|
---|
63 | Fragment: string;
|
---|
64 | constructor Create;
|
---|
65 | procedure Clear;
|
---|
66 | destructor Destroy; override;
|
---|
67 | procedure Assign(Source: TPersistent); override;
|
---|
68 | property AsString: string read GetAsString write SetAsString;
|
---|
69 | end;
|
---|
70 |
|
---|
71 | { TURL }
|
---|
72 |
|
---|
73 | TURL = class(TURI)
|
---|
74 | private
|
---|
75 | function GetAsString: string;
|
---|
76 | procedure SetAsString(Value: string);
|
---|
77 | public
|
---|
78 | UserName: string;
|
---|
79 | Password: string;
|
---|
80 | Host: string;
|
---|
81 | Port: Word;
|
---|
82 | constructor Create;
|
---|
83 | destructor Destroy; override;
|
---|
84 | property AsString: string read GetAsString write SetAsString;
|
---|
85 | end;
|
---|
86 |
|
---|
87 | implementation
|
---|
88 |
|
---|
89 | function LeftCutString(var Source: string; out Output: string; Delimiter: string; Allowed: string = ''): Boolean;
|
---|
90 | var
|
---|
91 | I, J: Integer;
|
---|
92 | Matched: Boolean;
|
---|
93 | begin
|
---|
94 | I := 1;
|
---|
95 | Matched := True;
|
---|
96 | while (I < Length(Source)) and Matched do begin
|
---|
97 | Matched := True;
|
---|
98 | if (Source[I] = Delimiter) then Matched := False;
|
---|
99 | //for J := 1 to Length(Allowed) do
|
---|
100 | // if Source[I] = Allowed[J] then Matched := True;
|
---|
101 | if Matched then Inc(I);
|
---|
102 | end;
|
---|
103 | if (Delimiter = Copy(Source, I, Length(Delimiter))) or (I = Length(Source)) then begin
|
---|
104 | Output := Copy(Source, 1, I - 1);
|
---|
105 | Delete(Source, 1, Length(Output) + Length(Delimiter));
|
---|
106 | Result := True;
|
---|
107 | end else begin
|
---|
108 | Output := '';
|
---|
109 | Result := False;
|
---|
110 | end;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | function RightCutString(var Source: string; out Output: string; Delimiter: string; Allowed: string = ''): Boolean;
|
---|
114 | var
|
---|
115 | I, J: Integer;
|
---|
116 | Matched: Boolean;
|
---|
117 | begin
|
---|
118 | I := Length(Source);
|
---|
119 | Matched := True;
|
---|
120 | while (I > 0) and Matched do begin
|
---|
121 | Matched := True;
|
---|
122 | if (Source[I] = Delimiter) then Matched := False;
|
---|
123 | //for J := 1 to Length(Allowed) do
|
---|
124 | // if Source[I] = Allowed[J] then Matched := True;
|
---|
125 | if Matched then Dec(I);
|
---|
126 | end;
|
---|
127 | if (Delimiter = Copy(Source, I - Length(Delimiter) + 1, Length(Delimiter))) or (I = 0) then begin
|
---|
128 | Output := Copy(Source, I + 1, Length(Source) - I);
|
---|
129 | Delete(Source, I, Length(Output) + Length(Delimiter));
|
---|
130 | Result := True;
|
---|
131 | end else begin
|
---|
132 | Output := '';
|
---|
133 | Result := False;
|
---|
134 | end;
|
---|
135 | end;
|
---|
136 |
|
---|
137 | { TPath }
|
---|
138 |
|
---|
139 | function TPath.GetAsString: string;
|
---|
140 | var
|
---|
141 | I: Integer;
|
---|
142 | begin
|
---|
143 | if IsAbsolute then Result := DirSeparator
|
---|
144 | else Result := '';
|
---|
145 | for I := 0 to Items.Count - 1 do
|
---|
146 | Result := Result + Items[I] + DirSeparator;
|
---|
147 | end;
|
---|
148 |
|
---|
149 | procedure TPath.SetAsString(AValue: string);
|
---|
150 | var
|
---|
151 | Name: string;
|
---|
152 | begin
|
---|
153 | Items.Clear;
|
---|
154 | if Length(AValue) > 0 then begin
|
---|
155 | if AValue[1] = DirSeparator then begin
|
---|
156 | IsAbsolute := True;
|
---|
157 | Delete(AValue, 1, 1);
|
---|
158 | end else IsAbsolute := False;
|
---|
159 | while Pos(DirSeparator, AValue) > 0 do begin
|
---|
160 | Name := Copy(AValue, 1, Pos(DirSeparator, AValue) - 1);
|
---|
161 | Delete(AValue, 1, Pos(DirSeparator, AValue));
|
---|
162 | Items.Add(Name);
|
---|
163 | end;
|
---|
164 | if Length(AValue) > 0 then
|
---|
165 | Items.Add(AValue);
|
---|
166 | end else IsAbsolute := False;
|
---|
167 | end;
|
---|
168 |
|
---|
169 | procedure TPath.Assign(Source: TPath);
|
---|
170 | begin
|
---|
171 | IsAbsolute := Source.IsAbsolute;
|
---|
172 | Items.Assign(Source.Items);
|
---|
173 | DirSeparator := Source.DirSeparator;
|
---|
174 | end;
|
---|
175 |
|
---|
176 | constructor TPath.Create;
|
---|
177 | begin
|
---|
178 | Items := TStringList.Create;
|
---|
179 | DirSeparator := DirectorySeparator;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | destructor TPath.Destroy;
|
---|
183 | begin
|
---|
184 | Items.Free;
|
---|
185 | inherited Destroy;
|
---|
186 | end;
|
---|
187 |
|
---|
188 | { TURI }
|
---|
189 |
|
---|
190 | function TURI.GetAsString: string;
|
---|
191 | begin
|
---|
192 | Result := '';
|
---|
193 | if Scheme <> '' then Result := Scheme + ':';
|
---|
194 | if Path.Combine <> '' then begin
|
---|
195 | Result := Result + '//' + Authority;
|
---|
196 | if Scheme = 'file' then Result := Result + URIPathSeparator;
|
---|
197 | Result := Result + Path.Combine;
|
---|
198 | end;
|
---|
199 | if Query <> '' then Result := Result + '?' + Query;
|
---|
200 | if Fragment <> '' then Result := Result + '#' + Fragment;
|
---|
201 | end;
|
---|
202 |
|
---|
203 | procedure TURI.SetAsString(Value: string);
|
---|
204 | var
|
---|
205 | HostAddr: string;
|
---|
206 | HostPort: string;
|
---|
207 | begin
|
---|
208 | LeftCutString(Value, Scheme, ':');
|
---|
209 | if Copy(Value, 1, 2) = '//' then begin
|
---|
210 | Value := Copy(Value, 3, Length(Value));
|
---|
211 | LeftCutString(Value, Authority, URIPathSeparator);
|
---|
212 | end;
|
---|
213 | RightCutString(Value, Fragment, '#');
|
---|
214 | RightCutString(Value, Query, '?', '=&');
|
---|
215 | //if Scheme = 'file' then Delete(Value, 1, 1); // Remove beginning slash
|
---|
216 | Path.Parse(Value);
|
---|
217 | end;
|
---|
218 |
|
---|
219 | constructor TURI.Create;
|
---|
220 | begin
|
---|
221 | Path := TFileName.Create;
|
---|
222 | Path.Directory.DirSeparator := URIPathSeparator;
|
---|
223 | end;
|
---|
224 |
|
---|
225 | procedure TURI.Clear;
|
---|
226 | begin
|
---|
227 | Scheme := '';
|
---|
228 | Authority := '';
|
---|
229 | Path.Parse('');
|
---|
230 | Fragment := '';
|
---|
231 | Query := '';
|
---|
232 | end;
|
---|
233 |
|
---|
234 | destructor TURI.Destroy;
|
---|
235 | begin
|
---|
236 | Path.Free;
|
---|
237 | inherited Destroy;
|
---|
238 | end;
|
---|
239 |
|
---|
240 | procedure TURI.Assign(Source: TPersistent);
|
---|
241 | begin
|
---|
242 | if Source is TURI then begin
|
---|
243 | Scheme := TURI(Source).Scheme;
|
---|
244 | Authority := TURI(Source).Authority;
|
---|
245 | Path.Assign(TURI(Source).Path);
|
---|
246 | Fragment := TURI(Source).Fragment;
|
---|
247 | Query := TURI(Source).Query;
|
---|
248 | end else inherited Assign(Source);
|
---|
249 | end;
|
---|
250 |
|
---|
251 | { TURL }
|
---|
252 |
|
---|
253 | function TURL.GetAsString: string;
|
---|
254 | begin
|
---|
255 | Result := '';
|
---|
256 | if Scheme <> '' then Result := Scheme + '://';
|
---|
257 | if UserName <> '' then begin
|
---|
258 | Result := Result + UserName;
|
---|
259 | if UserName <> '' then Result := Result + ':' + Password;
|
---|
260 | Result := Result + '@';
|
---|
261 | end;
|
---|
262 | if Host <> '' then Result := Result + Host;
|
---|
263 | if Port <> 0 then Result := Result + ':' + IntToStr(Port);
|
---|
264 | if Path.Combine <> '' then Result := Result + Path.Combine;
|
---|
265 | if Query <> '' then Result := Result + '?' + Query;
|
---|
266 | if Fragment <> '' then Result := Result + '#' + Fragment;
|
---|
267 | end;
|
---|
268 |
|
---|
269 | procedure TURL.SetAsString(Value: string);
|
---|
270 | var
|
---|
271 | HostAddr: string;
|
---|
272 | HostPort: string;
|
---|
273 | TempPath: string;
|
---|
274 | begin
|
---|
275 | LeftCutString(Value, Scheme, '://');
|
---|
276 | if LeftCutString(Value, UserName, ':') then LeftCutString(Value, Password, '@')
|
---|
277 | else LeftCutString(Value, UserName, '@');
|
---|
278 | RightCutString(Value, Fragment, '#');
|
---|
279 | RightCutString(Value, Query, '?', '=&');
|
---|
280 | if LeftCutString(Value, HostAddr, ':', '.') then begin
|
---|
281 | LeftCutString(Value, HostPort, '');
|
---|
282 | Port := StrToInt(HostPort);
|
---|
283 | end else LeftCutString(Value, HostAddr, '', '.');
|
---|
284 | Host := HostAddr;
|
---|
285 | LeftCutString(Value, TempPath, '', URIPathSeparator + '.');
|
---|
286 | Path.Parse(TempPath);
|
---|
287 | end;
|
---|
288 |
|
---|
289 | constructor TURL.Create;
|
---|
290 | begin
|
---|
291 |
|
---|
292 | end;
|
---|
293 |
|
---|
294 | destructor TURL.Destroy;
|
---|
295 | begin
|
---|
296 | inherited Destroy;
|
---|
297 | end;
|
---|
298 |
|
---|
299 | { TFileName }
|
---|
300 |
|
---|
301 | function TFileName.Combine(Parts: TFileNameParts): string;
|
---|
302 | begin
|
---|
303 | Result := '';
|
---|
304 | if (fnpDrive in Parts) and (Drive <> '') then Result := Result + Drive;
|
---|
305 | if (fnpDirectory in Parts) and (Directory.AsString <> '') then
|
---|
306 | Result := Result + Directory.AsString;
|
---|
307 | if (fnpName in Parts) then Result := Result + Name;
|
---|
308 | if (fnpExtension in Parts) and (Extension <> '') then
|
---|
309 | Result := Result + Extension;
|
---|
310 | end;
|
---|
311 |
|
---|
312 | procedure TFileName.Parse(AValue: string);
|
---|
313 | begin
|
---|
314 | if Pos(ExtensionSeparator, AValue) > 0 then begin
|
---|
315 | RightCutString(AValue, Extension, ExtensionSeparator);
|
---|
316 | Extension := ExtensionSeparator + Extension;
|
---|
317 | end else Extension := '';
|
---|
318 | if Pos(Directory.DirSeparator, AValue) > 0 then
|
---|
319 | RightCutString(AValue, Name, Directory.DirSeparator)
|
---|
320 | else begin
|
---|
321 | Name := AValue;
|
---|
322 | AValue := '';
|
---|
323 | end;
|
---|
324 | if Pos(DriveSeparator, AValue) > 0 then begin
|
---|
325 | LeftCutString(AValue, Drive, DriveSeparator);
|
---|
326 | Drive := Drive + DriveSeparator;
|
---|
327 | end else Drive := '';
|
---|
328 | if (Drive <> '') and (AValue = '') then
|
---|
329 | Directory.AsString := Directory.DirSeparator
|
---|
330 | else Directory.AsString := AValue;
|
---|
331 | end;
|
---|
332 |
|
---|
333 | procedure TFileName.Assign(Source: TFileName);
|
---|
334 | begin
|
---|
335 | Name := Source.Name;
|
---|
336 | Extension := Source.Extension;
|
---|
337 | Drive := Source.Drive;
|
---|
338 | Directory.Assign(Source.Directory);
|
---|
339 | end;
|
---|
340 |
|
---|
341 | constructor TFileName.Create;
|
---|
342 | begin
|
---|
343 | Directory := TPath.Create;
|
---|
344 | end;
|
---|
345 |
|
---|
346 | destructor TFileName.Destroy;
|
---|
347 | begin
|
---|
348 | Directory.Free;
|
---|
349 | inherited Destroy;
|
---|
350 | end;
|
---|
351 |
|
---|
352 |
|
---|
353 | end.
|
---|
354 |
|
---|