| 1 | unit StringTables;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 | { TStringTable }
|
|---|
| 10 |
|
|---|
| 11 | TStringTable = class
|
|---|
| 12 | private
|
|---|
| 13 | Lines: TStringList;
|
|---|
| 14 | function IsLabel(Text, Name: string): Boolean;
|
|---|
| 15 | public
|
|---|
| 16 | constructor Create;
|
|---|
| 17 | destructor Destroy; override;
|
|---|
| 18 | function LoadFromFile(const FileName: String): Boolean;
|
|---|
| 19 | function GetHandle(const Item: string): Integer;
|
|---|
| 20 | function LookupByHandle(Handle: Integer; Index: Integer = -1): string;
|
|---|
| 21 | function Lookup(const Item: string; Index: Integer = -1): string;
|
|---|
| 22 | function Search(const Content: string; var Handle, Index: Integer): Boolean;
|
|---|
| 23 | end;
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | implementation
|
|---|
| 27 |
|
|---|
| 28 | uses
|
|---|
| 29 | SysUtils;
|
|---|
| 30 |
|
|---|
| 31 | function TStringTable.IsLabel(Text, Name: string): Boolean;
|
|---|
| 32 | begin
|
|---|
| 33 | Result := ((Copy(Text, 1, 1) = '#') and (Copy(Text, 2, Length(Name)) = Name))
|
|---|
| 34 | and ((Length(Text) = (Length(Name) + 1)) or (Copy(Text, Length(Name) + 2, 1) = ' '));
|
|---|
| 35 | end;
|
|---|
| 36 |
|
|---|
| 37 | constructor TStringTable.Create;
|
|---|
| 38 | begin
|
|---|
| 39 | Lines := TStringList.Create;
|
|---|
| 40 | end;
|
|---|
| 41 |
|
|---|
| 42 | destructor TStringTable.Destroy;
|
|---|
| 43 | begin
|
|---|
| 44 | FreeAndNil(Lines);
|
|---|
| 45 | end;
|
|---|
| 46 |
|
|---|
| 47 | function TStringTable.LoadFromFile(const FileName: String): Boolean;
|
|---|
| 48 | begin
|
|---|
| 49 | Result := True;
|
|---|
| 50 | Lines.Clear;
|
|---|
| 51 | try
|
|---|
| 52 | Lines.LoadFromFile(FileName);
|
|---|
| 53 | except
|
|---|
| 54 | Result := False;
|
|---|
| 55 | end;
|
|---|
| 56 | end;
|
|---|
| 57 |
|
|---|
| 58 | function TStringTable.GetHandle(const Item: string): Integer;
|
|---|
| 59 | var
|
|---|
| 60 | I: Integer;
|
|---|
| 61 | begin
|
|---|
| 62 | I := 0;
|
|---|
| 63 | while (I < Lines.Count) and not IsLabel(Lines[I], Item) do
|
|---|
| 64 | Inc(I);
|
|---|
| 65 | if I < Lines.Count then Result := I
|
|---|
| 66 | else Result := -1;
|
|---|
| 67 | end;
|
|---|
| 68 |
|
|---|
| 69 | function TStringTable.LookupByHandle(Handle: Integer; Index: Integer): string;
|
|---|
| 70 | var
|
|---|
| 71 | S: string;
|
|---|
| 72 | begin
|
|---|
| 73 | if Index < 0 then begin
|
|---|
| 74 | if Handle < 0 then begin
|
|---|
| 75 | Result := '';
|
|---|
| 76 | Exit;
|
|---|
| 77 | end else begin
|
|---|
| 78 | if Pos(' ', Lines[Handle]) = 0 then S := ''
|
|---|
| 79 | else S := Copy(Lines[Handle], Pos(' ', Lines[Handle]) + 1, MaxInt);
|
|---|
| 80 | while ((Handle + 1) < Lines.Count) and (Copy(Lines[Handle + 1], 1, 1) <> '#') do begin
|
|---|
| 81 | Inc(Handle);
|
|---|
| 82 | if (Length(Lines[Handle]) > 0) and (Lines[Handle][1] <> '''') then begin
|
|---|
| 83 | if (S <> '') and (S[Length(S)] <> '\') then
|
|---|
| 84 | S := S + ' ';
|
|---|
| 85 | S := S + Lines[Handle];
|
|---|
| 86 | end;
|
|---|
| 87 | end;
|
|---|
| 88 | Result := S;
|
|---|
| 89 | end;
|
|---|
| 90 | end else
|
|---|
| 91 | if (Handle + Index + 1) >= Lines.Count then begin
|
|---|
| 92 | Result := '';
|
|---|
| 93 | Exit;
|
|---|
| 94 | end else Result := Lines[Handle + Index + 1];
|
|---|
| 95 | while (Result <> '') and ((Result[1] = ' ') or (Result[1] = #9)) do
|
|---|
| 96 | Delete(Result, 1, 1);
|
|---|
| 97 | while (Result <> '') and ((Result[Length(Result)] = ' ') or
|
|---|
| 98 | (Result[Length(Result)] = #9)) do
|
|---|
| 99 | Delete(Result, Length(Result), 1);
|
|---|
| 100 | if Result = '' then Result := '*';
|
|---|
| 101 | end;
|
|---|
| 102 |
|
|---|
| 103 | function TStringTable.Lookup(const Item: string; Index: Integer): string;
|
|---|
| 104 | var
|
|---|
| 105 | Handle: Integer;
|
|---|
| 106 | begin
|
|---|
| 107 | Handle := GetHandle(Item);
|
|---|
| 108 | if Handle >= 0 then Result := LookupByHandle(Handle, Index)
|
|---|
| 109 | else Result := '';
|
|---|
| 110 | if Result = '' then begin
|
|---|
| 111 | if Index < 0 then Result := Format('[%s]', [Item])
|
|---|
| 112 | else Result := Format('[%s %d]', [Item, Index]);
|
|---|
| 113 | end;
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | { might become necessary for 1.3
|
|---|
| 117 |
|
|---|
| 118 | function TStringTable.Lookup(const Fallback: TStringTable; const Item: string; Index: Integer): string;
|
|---|
| 119 | var
|
|---|
| 120 | Handle: Integer;
|
|---|
| 121 | begin
|
|---|
| 122 | Handle:=Gethandle(Item);
|
|---|
| 123 | if Handle>=0 then Result:=LookupByHandle(Handle, Index)
|
|---|
| 124 | else Result:='';
|
|---|
| 125 | if Result='' then
|
|---|
| 126 | Result:=Fallback.Lookup(Item, Index);
|
|---|
| 127 | end;
|
|---|
| 128 |
|
|---|
| 129 | function TStringTable.TryLookup(const Item: string; Index: Integer): string;
|
|---|
| 130 | var
|
|---|
| 131 | Handle: Integer;
|
|---|
| 132 | begin
|
|---|
| 133 | Handle:=Gethandle(Item);
|
|---|
| 134 | if Handle>=0 then Result:=LookupByHandle(Handle, Index)
|
|---|
| 135 | else Result:='';
|
|---|
| 136 | end; }
|
|---|
| 137 |
|
|---|
| 138 | function TStringTable.Search(const Content: string;
|
|---|
| 139 | var Handle, Index: Integer): Boolean;
|
|---|
| 140 | var
|
|---|
| 141 | H, I: Integer;
|
|---|
| 142 | UContent: string;
|
|---|
| 143 | begin
|
|---|
| 144 | UContent := UpperCase(Content);
|
|---|
| 145 | H := Handle;
|
|---|
| 146 | if H < 0 then
|
|---|
| 147 | I := 0
|
|---|
| 148 | else
|
|---|
| 149 | I := Index + 1;
|
|---|
| 150 | repeat
|
|---|
| 151 | if H + I + 1 >= Lines.Count then
|
|---|
| 152 | begin
|
|---|
| 153 | Result := False;
|
|---|
| 154 | Exit;
|
|---|
| 155 | end;
|
|---|
| 156 | if Copy(Lines[H + I + 1], 1, 1) = '#' then
|
|---|
| 157 | begin
|
|---|
| 158 | H := H + I + 1;
|
|---|
| 159 | I := -1;
|
|---|
| 160 | end;
|
|---|
| 161 | if (H >= 0) and not ((Length(Lines[H + I + 1]) > 0) and (Lines[H + I + 1][1] in ['#', ':', ';'])) and
|
|---|
| 162 | (Pos(UContent, UpperCase(Lines[H + I + 1])) > 0) then
|
|---|
| 163 | begin
|
|---|
| 164 | Index := I;
|
|---|
| 165 | Handle := H;
|
|---|
| 166 | Result := True;
|
|---|
| 167 | Exit;
|
|---|
| 168 | end;
|
|---|
| 169 | Inc(I);
|
|---|
| 170 | until False;
|
|---|
| 171 | end;
|
|---|
| 172 |
|
|---|
| 173 | end.
|
|---|