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