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.
|
---|