source: branches/AlphaChannel/Packages/CevoComponents/StringTables.pas

Last change on this file was 174, checked in by chronos, 5 years ago
  • Fixed: Disable MoveAction for TDrawDlg instances after visible change.
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
25implementation
26
27uses
28 SysUtils;
29
30function TStringTable.IsLabel(Text, Name: string): Boolean;
31begin
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) = ' '));
34end;
35
36constructor TStringTable.Create;
37begin
38 Lines := TStringList.Create;
39end;
40
41destructor TStringTable.Destroy;
42begin
43 FreeAndNil(Lines);
44end;
45
46function TStringTable.LoadFromFile(const FileName: String): boolean;
47begin
48 Result := True;
49 Lines.Clear;
50 try
51 Lines.LoadFromFile(FileName);
52 except
53 Result := False;
54 end;
55end;
56
57function TStringTable.GetHandle(const Item: string): integer;
58var
59 I: Integer;
60begin
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;
66end;
67
68function TStringTable.LookupByHandle(Handle: integer; Index: integer): string;
69var
70 s: string;
71begin
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 := '*';
99end;
100
101function TStringTable.Lookup(const Item: string; Index: integer): string;
102var
103 Handle: integer;
104begin
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])
115end;
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
139function TStringTable.Search(const Content: string;
140 var Handle, Index: integer): boolean;
141var
142 h, i: integer;
143 UContent: string;
144begin
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;
172end;
173
174end.
Note: See TracBrowser for help on using the repository browser.