source: tags/1.3.1/Packages/CevoComponents/StringTables.pas

Last change on this file was 300, checked in by chronos, 3 years ago
  • Modified: Code cleanup.
File size: 4.3 KB
Line 
1unit StringTables;
2
3interface
4
5uses
6 Classes;
7
8type
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
26implementation
27
28uses
29 SysUtils;
30
31function TStringTable.IsLabel(Text, Name: string): Boolean;
32begin
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) = ' '));
35end;
36
37constructor TStringTable.Create;
38begin
39 Lines := TStringList.Create;
40end;
41
42destructor TStringTable.Destroy;
43begin
44 FreeAndNil(Lines);
45end;
46
47function TStringTable.LoadFromFile(const FileName: String): boolean;
48begin
49 Result := True;
50 Lines.Clear;
51 try
52 Lines.LoadFromFile(FileName);
53 except
54 Result := False;
55 end;
56end;
57
58function TStringTable.GetHandle(const Item: string): integer;
59var
60 I: Integer;
61begin
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;
67end;
68
69function TStringTable.LookupByHandle(Handle: integer; Index: integer): string;
70var
71 s: string;
72begin
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 := '*';
101end;
102
103function TStringTable.Lookup(const Item: string; Index: Integer): string;
104var
105 Handle: Integer;
106begin
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;
114end;
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
138function TStringTable.Search(const Content: string;
139 var Handle, Index: integer): boolean;
140var
141 h, i: integer;
142 UContent: string;
143begin
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;
171end;
172
173end.
Note: See TracBrowser for help on using the repository browser.