| 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: 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: 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 | begin
|
|---|
| 205 | LeftCutString(Value, Scheme, ':');
|
|---|
| 206 | if Copy(Value, 1, 2) = '//' then begin
|
|---|
| 207 | Value := Copy(Value, 3, Length(Value));
|
|---|
| 208 | LeftCutString(Value, Authority, URIPathSeparator);
|
|---|
| 209 | end;
|
|---|
| 210 | RightCutString(Value, Fragment, '#');
|
|---|
| 211 | RightCutString(Value, Query, '?', '=&');
|
|---|
| 212 | //if Scheme = 'file' then Delete(Value, 1, 1); // Remove beginning slash
|
|---|
| 213 | Path.Parse(Value);
|
|---|
| 214 | end;
|
|---|
| 215 |
|
|---|
| 216 | constructor TURI.Create;
|
|---|
| 217 | begin
|
|---|
| 218 | Path := TFileName.Create;
|
|---|
| 219 | Path.Directory.DirSeparator := URIPathSeparator;
|
|---|
| 220 | end;
|
|---|
| 221 |
|
|---|
| 222 | procedure TURI.Clear;
|
|---|
| 223 | begin
|
|---|
| 224 | Scheme := '';
|
|---|
| 225 | Authority := '';
|
|---|
| 226 | Path.Parse('');
|
|---|
| 227 | Fragment := '';
|
|---|
| 228 | Query := '';
|
|---|
| 229 | end;
|
|---|
| 230 |
|
|---|
| 231 | destructor TURI.Destroy;
|
|---|
| 232 | begin
|
|---|
| 233 | Path.Free;
|
|---|
| 234 | inherited Destroy;
|
|---|
| 235 | end;
|
|---|
| 236 |
|
|---|
| 237 | procedure TURI.Assign(Source: TPersistent);
|
|---|
| 238 | begin
|
|---|
| 239 | if Source is TURI then begin
|
|---|
| 240 | Scheme := TURI(Source).Scheme;
|
|---|
| 241 | Authority := TURI(Source).Authority;
|
|---|
| 242 | Path.Assign(TURI(Source).Path);
|
|---|
| 243 | Fragment := TURI(Source).Fragment;
|
|---|
| 244 | Query := TURI(Source).Query;
|
|---|
| 245 | end else inherited Assign(Source);
|
|---|
| 246 | end;
|
|---|
| 247 |
|
|---|
| 248 | { TURL }
|
|---|
| 249 |
|
|---|
| 250 | function TURL.GetAsString: string;
|
|---|
| 251 | begin
|
|---|
| 252 | Result := '';
|
|---|
| 253 | if Scheme <> '' then Result := Scheme + '://';
|
|---|
| 254 | if UserName <> '' then begin
|
|---|
| 255 | Result := Result + UserName;
|
|---|
| 256 | if UserName <> '' then Result := Result + ':' + Password;
|
|---|
| 257 | Result := Result + '@';
|
|---|
| 258 | end;
|
|---|
| 259 | if Host <> '' then Result := Result + Host;
|
|---|
| 260 | if Port <> 0 then Result := Result + ':' + IntToStr(Port);
|
|---|
| 261 | if Path.Combine <> '' then Result := Result + Path.Combine;
|
|---|
| 262 | if Query <> '' then Result := Result + '?' + Query;
|
|---|
| 263 | if Fragment <> '' then Result := Result + '#' + Fragment;
|
|---|
| 264 | end;
|
|---|
| 265 |
|
|---|
| 266 | procedure TURL.SetAsString(Value: string);
|
|---|
| 267 | var
|
|---|
| 268 | HostAddr: string;
|
|---|
| 269 | HostPort: string;
|
|---|
| 270 | TempPath: string;
|
|---|
| 271 | begin
|
|---|
| 272 | LeftCutString(Value, Scheme, '://');
|
|---|
| 273 | if LeftCutString(Value, UserName, ':') then LeftCutString(Value, Password, '@')
|
|---|
| 274 | else LeftCutString(Value, UserName, '@');
|
|---|
| 275 | RightCutString(Value, Fragment, '#');
|
|---|
| 276 | RightCutString(Value, Query, '?', '=&');
|
|---|
| 277 | if LeftCutString(Value, HostAddr, ':', '.') then begin
|
|---|
| 278 | LeftCutString(Value, HostPort, '');
|
|---|
| 279 | Port := StrToInt(HostPort);
|
|---|
| 280 | end else LeftCutString(Value, HostAddr, '', '.');
|
|---|
| 281 | Host := HostAddr;
|
|---|
| 282 | LeftCutString(Value, TempPath, '', URIPathSeparator + '.');
|
|---|
| 283 | Path.Parse(TempPath);
|
|---|
| 284 | end;
|
|---|
| 285 |
|
|---|
| 286 | constructor TURL.Create;
|
|---|
| 287 | begin
|
|---|
| 288 |
|
|---|
| 289 | end;
|
|---|
| 290 |
|
|---|
| 291 | destructor TURL.Destroy;
|
|---|
| 292 | begin
|
|---|
| 293 | inherited Destroy;
|
|---|
| 294 | end;
|
|---|
| 295 |
|
|---|
| 296 | { TFileName }
|
|---|
| 297 |
|
|---|
| 298 | function TFileName.Combine(Parts: TFileNameParts): string;
|
|---|
| 299 | begin
|
|---|
| 300 | Result := '';
|
|---|
| 301 | if (fnpDrive in Parts) and (Drive <> '') then Result := Result + Drive;
|
|---|
| 302 | if (fnpDirectory in Parts) and (Directory.AsString <> '') then
|
|---|
| 303 | Result := Result + Directory.AsString;
|
|---|
| 304 | if (fnpName in Parts) then Result := Result + Name;
|
|---|
| 305 | if (fnpExtension in Parts) and (Extension <> '') then
|
|---|
| 306 | Result := Result + Extension;
|
|---|
| 307 | end;
|
|---|
| 308 |
|
|---|
| 309 | procedure TFileName.Parse(AValue: string);
|
|---|
| 310 | begin
|
|---|
| 311 | if Pos(ExtensionSeparator, AValue) > 0 then begin
|
|---|
| 312 | RightCutString(AValue, Extension, ExtensionSeparator);
|
|---|
| 313 | Extension := ExtensionSeparator + Extension;
|
|---|
| 314 | end else Extension := '';
|
|---|
| 315 | if Pos(Directory.DirSeparator, AValue) > 0 then
|
|---|
| 316 | RightCutString(AValue, Name, Directory.DirSeparator)
|
|---|
| 317 | else begin
|
|---|
| 318 | Name := AValue;
|
|---|
| 319 | AValue := '';
|
|---|
| 320 | end;
|
|---|
| 321 | if Pos(DriveSeparator, AValue) > 0 then begin
|
|---|
| 322 | LeftCutString(AValue, Drive, DriveSeparator);
|
|---|
| 323 | Drive := Drive + DriveSeparator;
|
|---|
| 324 | end else Drive := '';
|
|---|
| 325 | if (Drive <> '') and (AValue = '') then
|
|---|
| 326 | Directory.AsString := Directory.DirSeparator
|
|---|
| 327 | else Directory.AsString := AValue;
|
|---|
| 328 | end;
|
|---|
| 329 |
|
|---|
| 330 | procedure TFileName.Assign(Source: TFileName);
|
|---|
| 331 | begin
|
|---|
| 332 | Name := Source.Name;
|
|---|
| 333 | Extension := Source.Extension;
|
|---|
| 334 | Drive := Source.Drive;
|
|---|
| 335 | Directory.Assign(Source.Directory);
|
|---|
| 336 | end;
|
|---|
| 337 |
|
|---|
| 338 | constructor TFileName.Create;
|
|---|
| 339 | begin
|
|---|
| 340 | Directory := TPath.Create;
|
|---|
| 341 | end;
|
|---|
| 342 |
|
|---|
| 343 | destructor TFileName.Destroy;
|
|---|
| 344 | begin
|
|---|
| 345 | Directory.Free;
|
|---|
| 346 | inherited Destroy;
|
|---|
| 347 | end;
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 | end.
|
|---|
| 351 |
|
|---|