1 |
|
---|
2 | {*****************************************************************************}
|
---|
3 | { }
|
---|
4 | { Tnt Delphi Unicode Controls }
|
---|
5 | { http://www.tntware.com/delphicontrols/unicode/ }
|
---|
6 | { Version: 2.3.0 }
|
---|
7 | { }
|
---|
8 | { Copyright (c) 2002-2007, Troy Wolbrink (troy.wolbrink@tntware.com) }
|
---|
9 | { }
|
---|
10 | {*****************************************************************************}
|
---|
11 |
|
---|
12 | unit TntWideStrings;
|
---|
13 |
|
---|
14 | {$INCLUDE TntCompilers.inc}
|
---|
15 |
|
---|
16 | interface
|
---|
17 |
|
---|
18 | {$IFDEF COMPILER_10_UP}
|
---|
19 | {$MESSAGE FATAL 'Do not refer to TntWideStrings.pas. It works correctly in Delphi 2006.'}
|
---|
20 | {$ENDIF}
|
---|
21 |
|
---|
22 | uses
|
---|
23 | Classes;
|
---|
24 |
|
---|
25 | {******************************************************************************}
|
---|
26 | { }
|
---|
27 | { Delphi 2005 introduced TWideStrings in WideStrings.pas. }
|
---|
28 | { Unfortunately, it was not ready for prime time. }
|
---|
29 | { Setting CommaText is not consistent, and it relies on CharNextW }
|
---|
30 | { Which is only available on Windows NT+. }
|
---|
31 | { }
|
---|
32 | {******************************************************************************}
|
---|
33 |
|
---|
34 | type
|
---|
35 | TWideStrings = class;
|
---|
36 |
|
---|
37 | { IWideStringsAdapter interface }
|
---|
38 | { Maintains link between TWideStrings and IWideStrings implementations }
|
---|
39 |
|
---|
40 | IWideStringsAdapter = interface
|
---|
41 | ['{25FE0E3B-66CB-48AA-B23B-BCFA67E8F5DA}']
|
---|
42 | procedure ReferenceStrings(S: TWideStrings);
|
---|
43 | procedure ReleaseStrings;
|
---|
44 | end;
|
---|
45 |
|
---|
46 | TWideStringsEnumerator = class
|
---|
47 | private
|
---|
48 | FIndex: Integer;
|
---|
49 | FStrings: TWideStrings;
|
---|
50 | public
|
---|
51 | constructor Create(AStrings: TWideStrings);
|
---|
52 | function GetCurrent: WideString;
|
---|
53 | function MoveNext: Boolean;
|
---|
54 | property Current: WideString read GetCurrent;
|
---|
55 | end;
|
---|
56 |
|
---|
57 | { TWideStrings class }
|
---|
58 |
|
---|
59 | TWideStrings = class(TPersistent)
|
---|
60 | private
|
---|
61 | FDefined: TStringsDefined;
|
---|
62 | FDelimiter: WideChar;
|
---|
63 | FQuoteChar: WideChar;
|
---|
64 | {$IFDEF COMPILER_7_UP}
|
---|
65 | FNameValueSeparator: WideChar;
|
---|
66 | {$ENDIF}
|
---|
67 | FUpdateCount: Integer;
|
---|
68 | FAdapter: IWideStringsAdapter;
|
---|
69 | function GetCommaText: WideString;
|
---|
70 | function GetDelimitedText: WideString;
|
---|
71 | function GetName(Index: Integer): WideString;
|
---|
72 | function GetValue(const Name: WideString): WideString;
|
---|
73 | procedure ReadData(Reader: TReader);
|
---|
74 | procedure SetCommaText(const Value: WideString);
|
---|
75 | procedure SetDelimitedText(const Value: WideString);
|
---|
76 | procedure SetStringsAdapter(const Value: IWideStringsAdapter);
|
---|
77 | procedure SetValue(const Name, Value: WideString);
|
---|
78 | procedure WriteData(Writer: TWriter);
|
---|
79 | function GetDelimiter: WideChar;
|
---|
80 | procedure SetDelimiter(const Value: WideChar);
|
---|
81 | function GetQuoteChar: WideChar;
|
---|
82 | procedure SetQuoteChar(const Value: WideChar);
|
---|
83 | function GetNameValueSeparator: WideChar;
|
---|
84 | {$IFDEF COMPILER_7_UP}
|
---|
85 | procedure SetNameValueSeparator(const Value: WideChar);
|
---|
86 | {$ENDIF}
|
---|
87 | function GetValueFromIndex(Index: Integer): WideString;
|
---|
88 | procedure SetValueFromIndex(Index: Integer; const Value: WideString);
|
---|
89 | protected
|
---|
90 | procedure AssignTo(Dest: TPersistent); override;
|
---|
91 | procedure DefineProperties(Filer: TFiler); override;
|
---|
92 | procedure Error(const Msg: WideString; Data: Integer); overload;
|
---|
93 | procedure Error(Msg: PResStringRec; Data: Integer); overload;
|
---|
94 | function ExtractName(const S: WideString): WideString;
|
---|
95 | function Get(Index: Integer): WideString; virtual; abstract;
|
---|
96 | function GetCapacity: Integer; virtual;
|
---|
97 | function GetCount: Integer; virtual; abstract;
|
---|
98 | function GetObject(Index: Integer): TObject; virtual;
|
---|
99 | function GetTextStr: WideString; virtual;
|
---|
100 | procedure Put(Index: Integer; const S: WideString); virtual;
|
---|
101 | procedure PutObject(Index: Integer; AObject: TObject); virtual;
|
---|
102 | procedure SetCapacity(NewCapacity: Integer); virtual;
|
---|
103 | procedure SetTextStr(const Value: WideString); virtual;
|
---|
104 | procedure SetUpdateState(Updating: Boolean); virtual;
|
---|
105 | property UpdateCount: Integer read FUpdateCount;
|
---|
106 | function CompareStrings(const S1, S2: WideString): Integer; virtual;
|
---|
107 | public
|
---|
108 | destructor Destroy; override;
|
---|
109 | function Add(const S: WideString): Integer; virtual;
|
---|
110 | function AddObject(const S: WideString; AObject: TObject): Integer; virtual;
|
---|
111 | procedure Append(const S: WideString);
|
---|
112 | procedure AddStrings(Strings: TStrings{TNT-ALLOW TStrings}); overload; virtual;
|
---|
113 | procedure AddStrings(Strings: TWideStrings); overload; virtual;
|
---|
114 | procedure Assign(Source: TPersistent); override;
|
---|
115 | procedure BeginUpdate;
|
---|
116 | procedure Clear; virtual; abstract;
|
---|
117 | procedure Delete(Index: Integer); virtual; abstract;
|
---|
118 | procedure EndUpdate;
|
---|
119 | function Equals(Strings: TWideStrings): Boolean;
|
---|
120 | procedure Exchange(Index1, Index2: Integer); virtual;
|
---|
121 | function GetEnumerator: TWideStringsEnumerator;
|
---|
122 | function GetTextW: PWideChar; virtual;
|
---|
123 | function IndexOf(const S: WideString): Integer; virtual;
|
---|
124 | function IndexOfName(const Name: WideString): Integer; virtual;
|
---|
125 | function IndexOfObject(AObject: TObject): Integer; virtual;
|
---|
126 | procedure Insert(Index: Integer; const S: WideString); virtual; abstract;
|
---|
127 | procedure InsertObject(Index: Integer; const S: WideString;
|
---|
128 | AObject: TObject); virtual;
|
---|
129 | procedure LoadFromFile(const FileName: WideString); virtual;
|
---|
130 | procedure LoadFromStream(Stream: TStream); virtual;
|
---|
131 | procedure Move(CurIndex, NewIndex: Integer); virtual;
|
---|
132 | procedure SaveToFile(const FileName: WideString); virtual;
|
---|
133 | procedure SaveToStream(Stream: TStream); virtual;
|
---|
134 | procedure SetTextW(const Text: PWideChar); virtual;
|
---|
135 | property Capacity: Integer read GetCapacity write SetCapacity;
|
---|
136 | property CommaText: WideString read GetCommaText write SetCommaText;
|
---|
137 | property Count: Integer read GetCount;
|
---|
138 | property Delimiter: WideChar read GetDelimiter write SetDelimiter;
|
---|
139 | property DelimitedText: WideString read GetDelimitedText write SetDelimitedText;
|
---|
140 | property Names[Index: Integer]: WideString read GetName;
|
---|
141 | property Objects[Index: Integer]: TObject read GetObject write PutObject;
|
---|
142 | property QuoteChar: WideChar read GetQuoteChar write SetQuoteChar;
|
---|
143 | property Values[const Name: WideString]: WideString read GetValue write SetValue;
|
---|
144 | property ValueFromIndex[Index: Integer]: WideString read GetValueFromIndex write SetValueFromIndex;
|
---|
145 | property NameValueSeparator: WideChar read GetNameValueSeparator {$IFDEF COMPILER_7_UP} write SetNameValueSeparator {$ENDIF};
|
---|
146 | property Strings[Index: Integer]: WideString read Get write Put; default;
|
---|
147 | property Text: WideString read GetTextStr write SetTextStr;
|
---|
148 | property StringsAdapter: IWideStringsAdapter read FAdapter write SetStringsAdapter;
|
---|
149 | end;
|
---|
150 |
|
---|
151 | PWideStringItem = ^TWideStringItem;
|
---|
152 | TWideStringItem = record
|
---|
153 | FString: WideString;
|
---|
154 | FObject: TObject;
|
---|
155 | end;
|
---|
156 |
|
---|
157 | PWideStringItemList = ^TWideStringItemList;
|
---|
158 | TWideStringItemList = array[0..MaxListSize] of TWideStringItem;
|
---|
159 |
|
---|
160 | implementation
|
---|
161 |
|
---|
162 | uses
|
---|
163 | Windows, SysUtils, TntSystem, {$IFDEF COMPILER_9_UP} WideStrUtils, {$ELSE} TntWideStrUtils, {$ENDIF}
|
---|
164 | TntSysUtils, TntClasses;
|
---|
165 |
|
---|
166 | { TWideStringsEnumerator }
|
---|
167 |
|
---|
168 | constructor TWideStringsEnumerator.Create(AStrings: TWideStrings);
|
---|
169 | begin
|
---|
170 | inherited Create;
|
---|
171 | FIndex := -1;
|
---|
172 | FStrings := AStrings;
|
---|
173 | end;
|
---|
174 |
|
---|
175 | function TWideStringsEnumerator.GetCurrent: WideString;
|
---|
176 | begin
|
---|
177 | Result := FStrings[FIndex];
|
---|
178 | end;
|
---|
179 |
|
---|
180 | function TWideStringsEnumerator.MoveNext: Boolean;
|
---|
181 | begin
|
---|
182 | Result := FIndex < FStrings.Count - 1;
|
---|
183 | if Result then
|
---|
184 | Inc(FIndex);
|
---|
185 | end;
|
---|
186 |
|
---|
187 | { TWideStrings }
|
---|
188 |
|
---|
189 | destructor TWideStrings.Destroy;
|
---|
190 | begin
|
---|
191 | StringsAdapter := nil;
|
---|
192 | inherited;
|
---|
193 | end;
|
---|
194 |
|
---|
195 | function TWideStrings.Add(const S: WideString): Integer;
|
---|
196 | begin
|
---|
197 | Result := GetCount;
|
---|
198 | Insert(Result, S);
|
---|
199 | end;
|
---|
200 |
|
---|
201 | function TWideStrings.AddObject(const S: WideString; AObject: TObject): Integer;
|
---|
202 | begin
|
---|
203 | Result := Add(S);
|
---|
204 | PutObject(Result, AObject);
|
---|
205 | end;
|
---|
206 |
|
---|
207 | procedure TWideStrings.Append(const S: WideString);
|
---|
208 | begin
|
---|
209 | Add(S);
|
---|
210 | end;
|
---|
211 |
|
---|
212 | procedure TWideStrings.AddStrings(Strings: TStrings{TNT-ALLOW TStrings});
|
---|
213 | var
|
---|
214 | I: Integer;
|
---|
215 | begin
|
---|
216 | BeginUpdate;
|
---|
217 | try
|
---|
218 | for I := 0 to Strings.Count - 1 do
|
---|
219 | AddObject(Strings[I], Strings.Objects[I]);
|
---|
220 | finally
|
---|
221 | EndUpdate;
|
---|
222 | end;
|
---|
223 | end;
|
---|
224 |
|
---|
225 | procedure TWideStrings.AddStrings(Strings: TWideStrings);
|
---|
226 | var
|
---|
227 | I: Integer;
|
---|
228 | begin
|
---|
229 | BeginUpdate;
|
---|
230 | try
|
---|
231 | for I := 0 to Strings.Count - 1 do
|
---|
232 | AddObject(Strings[I], Strings.Objects[I]);
|
---|
233 | finally
|
---|
234 | EndUpdate;
|
---|
235 | end;
|
---|
236 | end;
|
---|
237 |
|
---|
238 | procedure TWideStrings.Assign(Source: TPersistent);
|
---|
239 | begin
|
---|
240 | if Source is TWideStrings then
|
---|
241 | begin
|
---|
242 | BeginUpdate;
|
---|
243 | try
|
---|
244 | Clear;
|
---|
245 | FDefined := TWideStrings(Source).FDefined;
|
---|
246 | {$IFDEF COMPILER_7_UP}
|
---|
247 | FNameValueSeparator := TWideStrings(Source).FNameValueSeparator;
|
---|
248 | {$ENDIF}
|
---|
249 | FQuoteChar := TWideStrings(Source).FQuoteChar;
|
---|
250 | FDelimiter := TWideStrings(Source).FDelimiter;
|
---|
251 | AddStrings(TWideStrings(Source));
|
---|
252 | finally
|
---|
253 | EndUpdate;
|
---|
254 | end;
|
---|
255 | end
|
---|
256 | else if Source is TStrings{TNT-ALLOW TStrings} then
|
---|
257 | begin
|
---|
258 | BeginUpdate;
|
---|
259 | try
|
---|
260 | Clear;
|
---|
261 | {$IFDEF COMPILER_7_UP}
|
---|
262 | FNameValueSeparator := WideChar(TStrings{TNT-ALLOW TStrings}(Source).NameValueSeparator);
|
---|
263 | {$ENDIF}
|
---|
264 | FQuoteChar := WideChar(TStrings{TNT-ALLOW TStrings}(Source).QuoteChar);
|
---|
265 | FDelimiter := WideChar(TStrings{TNT-ALLOW TStrings}(Source).Delimiter);
|
---|
266 | AddStrings(TStrings{TNT-ALLOW TStrings}(Source));
|
---|
267 | finally
|
---|
268 | EndUpdate;
|
---|
269 | end;
|
---|
270 | end
|
---|
271 | else
|
---|
272 | inherited Assign(Source);
|
---|
273 | end;
|
---|
274 |
|
---|
275 | procedure TWideStrings.AssignTo(Dest: TPersistent);
|
---|
276 | var
|
---|
277 | I: Integer;
|
---|
278 | begin
|
---|
279 | if Dest is TWideStrings then Dest.Assign(Self)
|
---|
280 | else if Dest is TStrings{TNT-ALLOW TStrings} then
|
---|
281 | begin
|
---|
282 | TStrings{TNT-ALLOW TStrings}(Dest).BeginUpdate;
|
---|
283 | try
|
---|
284 | TStrings{TNT-ALLOW TStrings}(Dest).Clear;
|
---|
285 | {$IFDEF COMPILER_7_UP}
|
---|
286 | TStrings{TNT-ALLOW TStrings}(Dest).NameValueSeparator := AnsiChar(NameValueSeparator);
|
---|
287 | {$ENDIF}
|
---|
288 | TStrings{TNT-ALLOW TStrings}(Dest).QuoteChar := AnsiChar(QuoteChar);
|
---|
289 | TStrings{TNT-ALLOW TStrings}(Dest).Delimiter := AnsiChar(Delimiter);
|
---|
290 | for I := 0 to Count - 1 do
|
---|
291 | TStrings{TNT-ALLOW TStrings}(Dest).AddObject(Strings[I], Objects[I]);
|
---|
292 | finally
|
---|
293 | TStrings{TNT-ALLOW TStrings}(Dest).EndUpdate;
|
---|
294 | end;
|
---|
295 | end
|
---|
296 | else
|
---|
297 | inherited AssignTo(Dest);
|
---|
298 | end;
|
---|
299 |
|
---|
300 | procedure TWideStrings.BeginUpdate;
|
---|
301 | begin
|
---|
302 | if FUpdateCount = 0 then SetUpdateState(True);
|
---|
303 | Inc(FUpdateCount);
|
---|
304 | end;
|
---|
305 |
|
---|
306 | procedure TWideStrings.DefineProperties(Filer: TFiler);
|
---|
307 |
|
---|
308 | function DoWrite: Boolean;
|
---|
309 | begin
|
---|
310 | if Filer.Ancestor <> nil then
|
---|
311 | begin
|
---|
312 | Result := True;
|
---|
313 | if Filer.Ancestor is TWideStrings then
|
---|
314 | Result := not Equals(TWideStrings(Filer.Ancestor))
|
---|
315 | end
|
---|
316 | else Result := Count > 0;
|
---|
317 | end;
|
---|
318 |
|
---|
319 | begin
|
---|
320 | Filer.DefineProperty('Strings', ReadData, WriteData, DoWrite);
|
---|
321 | end;
|
---|
322 |
|
---|
323 | procedure TWideStrings.EndUpdate;
|
---|
324 | begin
|
---|
325 | Dec(FUpdateCount);
|
---|
326 | if FUpdateCount = 0 then SetUpdateState(False);
|
---|
327 | end;
|
---|
328 |
|
---|
329 | function TWideStrings.Equals(Strings: TWideStrings): Boolean;
|
---|
330 | var
|
---|
331 | I, Count: Integer;
|
---|
332 | begin
|
---|
333 | Result := False;
|
---|
334 | Count := GetCount;
|
---|
335 | if Count <> Strings.GetCount then Exit;
|
---|
336 | for I := 0 to Count - 1 do if Get(I) <> Strings.Get(I) then Exit;
|
---|
337 | Result := True;
|
---|
338 | end;
|
---|
339 |
|
---|
340 | procedure TWideStrings.Error(const Msg: WideString; Data: Integer);
|
---|
341 |
|
---|
342 | function ReturnAddr: Pointer;
|
---|
343 | asm
|
---|
344 | MOV EAX,[EBP+4]
|
---|
345 | end;
|
---|
346 |
|
---|
347 | begin
|
---|
348 | raise EStringListError.CreateFmt(Msg, [Data]) at ReturnAddr;
|
---|
349 | end;
|
---|
350 |
|
---|
351 | procedure TWideStrings.Error(Msg: PResStringRec; Data: Integer);
|
---|
352 | begin
|
---|
353 | Error(WideLoadResString(Msg), Data);
|
---|
354 | end;
|
---|
355 |
|
---|
356 | procedure TWideStrings.Exchange(Index1, Index2: Integer);
|
---|
357 | var
|
---|
358 | TempObject: TObject;
|
---|
359 | TempString: WideString;
|
---|
360 | begin
|
---|
361 | BeginUpdate;
|
---|
362 | try
|
---|
363 | TempString := Strings[Index1];
|
---|
364 | TempObject := Objects[Index1];
|
---|
365 | Strings[Index1] := Strings[Index2];
|
---|
366 | Objects[Index1] := Objects[Index2];
|
---|
367 | Strings[Index2] := TempString;
|
---|
368 | Objects[Index2] := TempObject;
|
---|
369 | finally
|
---|
370 | EndUpdate;
|
---|
371 | end;
|
---|
372 | end;
|
---|
373 |
|
---|
374 | function TWideStrings.ExtractName(const S: WideString): WideString;
|
---|
375 | var
|
---|
376 | P: Integer;
|
---|
377 | begin
|
---|
378 | Result := S;
|
---|
379 | P := Pos(NameValueSeparator, Result);
|
---|
380 | if P <> 0 then
|
---|
381 | SetLength(Result, P-1) else
|
---|
382 | SetLength(Result, 0);
|
---|
383 | end;
|
---|
384 |
|
---|
385 | function TWideStrings.GetCapacity: Integer;
|
---|
386 | begin // descendents may optionally override/replace this default implementation
|
---|
387 | Result := Count;
|
---|
388 | end;
|
---|
389 |
|
---|
390 | function TWideStrings.GetCommaText: WideString;
|
---|
391 | var
|
---|
392 | LOldDefined: TStringsDefined;
|
---|
393 | LOldDelimiter: WideChar;
|
---|
394 | LOldQuoteChar: WideChar;
|
---|
395 | begin
|
---|
396 | LOldDefined := FDefined;
|
---|
397 | LOldDelimiter := FDelimiter;
|
---|
398 | LOldQuoteChar := FQuoteChar;
|
---|
399 | Delimiter := ',';
|
---|
400 | QuoteChar := '"';
|
---|
401 | try
|
---|
402 | Result := GetDelimitedText;
|
---|
403 | finally
|
---|
404 | FDelimiter := LOldDelimiter;
|
---|
405 | FQuoteChar := LOldQuoteChar;
|
---|
406 | FDefined := LOldDefined;
|
---|
407 | end;
|
---|
408 | end;
|
---|
409 |
|
---|
410 | function TWideStrings.GetDelimitedText: WideString;
|
---|
411 | var
|
---|
412 | S: WideString;
|
---|
413 | P: PWideChar;
|
---|
414 | I, Count: Integer;
|
---|
415 | begin
|
---|
416 | Count := GetCount;
|
---|
417 | if (Count = 1) and (Get(0) = '') then
|
---|
418 | Result := WideString(QuoteChar) + QuoteChar
|
---|
419 | else
|
---|
420 | begin
|
---|
421 | Result := '';
|
---|
422 | for I := 0 to Count - 1 do
|
---|
423 | begin
|
---|
424 | S := Get(I);
|
---|
425 | P := PWideChar(S);
|
---|
426 | while not ((P^ in [WideChar(#0)..WideChar(' ')]) or (P^ = QuoteChar) or (P^ = Delimiter)) do
|
---|
427 | Inc(P);
|
---|
428 | if (P^ <> #0) then S := WideQuotedStr(S, QuoteChar);
|
---|
429 | Result := Result + S + Delimiter;
|
---|
430 | end;
|
---|
431 | System.Delete(Result, Length(Result), 1);
|
---|
432 | end;
|
---|
433 | end;
|
---|
434 |
|
---|
435 | function TWideStrings.GetName(Index: Integer): WideString;
|
---|
436 | begin
|
---|
437 | Result := ExtractName(Get(Index));
|
---|
438 | end;
|
---|
439 |
|
---|
440 | function TWideStrings.GetObject(Index: Integer): TObject;
|
---|
441 | begin
|
---|
442 | Result := nil;
|
---|
443 | end;
|
---|
444 |
|
---|
445 | function TWideStrings.GetEnumerator: TWideStringsEnumerator;
|
---|
446 | begin
|
---|
447 | Result := TWideStringsEnumerator.Create(Self);
|
---|
448 | end;
|
---|
449 |
|
---|
450 | function TWideStrings.GetTextW: PWideChar;
|
---|
451 | begin
|
---|
452 | Result := WStrNew(PWideChar(GetTextStr));
|
---|
453 | end;
|
---|
454 |
|
---|
455 | function TWideStrings.GetTextStr: WideString;
|
---|
456 | var
|
---|
457 | I, L, Size, Count: Integer;
|
---|
458 | P: PWideChar;
|
---|
459 | S, LB: WideString;
|
---|
460 | begin
|
---|
461 | Count := GetCount;
|
---|
462 | Size := 0;
|
---|
463 | LB := sLineBreak;
|
---|
464 | for I := 0 to Count - 1 do Inc(Size, Length(Get(I)) + Length(LB));
|
---|
465 | SetString(Result, nil, Size);
|
---|
466 | P := Pointer(Result);
|
---|
467 | for I := 0 to Count - 1 do
|
---|
468 | begin
|
---|
469 | S := Get(I);
|
---|
470 | L := Length(S);
|
---|
471 | if L <> 0 then
|
---|
472 | begin
|
---|
473 | System.Move(Pointer(S)^, P^, L * SizeOf(WideChar));
|
---|
474 | Inc(P, L);
|
---|
475 | end;
|
---|
476 | L := Length(LB);
|
---|
477 | if L <> 0 then
|
---|
478 | begin
|
---|
479 | System.Move(Pointer(LB)^, P^, L * SizeOf(WideChar));
|
---|
480 | Inc(P, L);
|
---|
481 | end;
|
---|
482 | end;
|
---|
483 | end;
|
---|
484 |
|
---|
485 | function TWideStrings.GetValue(const Name: WideString): WideString;
|
---|
486 | var
|
---|
487 | I: Integer;
|
---|
488 | begin
|
---|
489 | I := IndexOfName(Name);
|
---|
490 | if I >= 0 then
|
---|
491 | Result := Copy(Get(I), Length(Name) + 2, MaxInt) else
|
---|
492 | Result := '';
|
---|
493 | end;
|
---|
494 |
|
---|
495 | function TWideStrings.IndexOf(const S: WideString): Integer;
|
---|
496 | begin
|
---|
497 | for Result := 0 to GetCount - 1 do
|
---|
498 | if CompareStrings(Get(Result), S) = 0 then Exit;
|
---|
499 | Result := -1;
|
---|
500 | end;
|
---|
501 |
|
---|
502 | function TWideStrings.IndexOfName(const Name: WideString): Integer;
|
---|
503 | var
|
---|
504 | P: Integer;
|
---|
505 | S: WideString;
|
---|
506 | begin
|
---|
507 | for Result := 0 to GetCount - 1 do
|
---|
508 | begin
|
---|
509 | S := Get(Result);
|
---|
510 | P := Pos(NameValueSeparator, S);
|
---|
511 | if (P <> 0) and (CompareStrings(Copy(S, 1, P - 1), Name) = 0) then Exit;
|
---|
512 | end;
|
---|
513 | Result := -1;
|
---|
514 | end;
|
---|
515 |
|
---|
516 | function TWideStrings.IndexOfObject(AObject: TObject): Integer;
|
---|
517 | begin
|
---|
518 | for Result := 0 to GetCount - 1 do
|
---|
519 | if GetObject(Result) = AObject then Exit;
|
---|
520 | Result := -1;
|
---|
521 | end;
|
---|
522 |
|
---|
523 | procedure TWideStrings.InsertObject(Index: Integer; const S: WideString;
|
---|
524 | AObject: TObject);
|
---|
525 | begin
|
---|
526 | Insert(Index, S);
|
---|
527 | PutObject(Index, AObject);
|
---|
528 | end;
|
---|
529 |
|
---|
530 | procedure TWideStrings.LoadFromFile(const FileName: WideString);
|
---|
531 | var
|
---|
532 | Stream: TStream;
|
---|
533 | begin
|
---|
534 | Stream := TTntFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
|
---|
535 | try
|
---|
536 | LoadFromStream(Stream);
|
---|
537 | finally
|
---|
538 | Stream.Free;
|
---|
539 | end;
|
---|
540 | end;
|
---|
541 |
|
---|
542 | procedure TWideStrings.LoadFromStream(Stream: TStream);
|
---|
543 | var
|
---|
544 | Size: Integer;
|
---|
545 | S: WideString;
|
---|
546 | begin
|
---|
547 | BeginUpdate;
|
---|
548 | try
|
---|
549 | Size := Stream.Size - Stream.Position;
|
---|
550 | SetString(S, nil, Size div SizeOf(WideChar));
|
---|
551 | Stream.Read(Pointer(S)^, Length(S) * SizeOf(WideChar));
|
---|
552 | SetTextStr(S);
|
---|
553 | finally
|
---|
554 | EndUpdate;
|
---|
555 | end;
|
---|
556 | end;
|
---|
557 |
|
---|
558 | procedure TWideStrings.Move(CurIndex, NewIndex: Integer);
|
---|
559 | var
|
---|
560 | TempObject: TObject;
|
---|
561 | TempString: WideString;
|
---|
562 | begin
|
---|
563 | if CurIndex <> NewIndex then
|
---|
564 | begin
|
---|
565 | BeginUpdate;
|
---|
566 | try
|
---|
567 | TempString := Get(CurIndex);
|
---|
568 | TempObject := GetObject(CurIndex);
|
---|
569 | Delete(CurIndex);
|
---|
570 | InsertObject(NewIndex, TempString, TempObject);
|
---|
571 | finally
|
---|
572 | EndUpdate;
|
---|
573 | end;
|
---|
574 | end;
|
---|
575 | end;
|
---|
576 |
|
---|
577 | procedure TWideStrings.Put(Index: Integer; const S: WideString);
|
---|
578 | var
|
---|
579 | TempObject: TObject;
|
---|
580 | begin
|
---|
581 | TempObject := GetObject(Index);
|
---|
582 | Delete(Index);
|
---|
583 | InsertObject(Index, S, TempObject);
|
---|
584 | end;
|
---|
585 |
|
---|
586 | procedure TWideStrings.PutObject(Index: Integer; AObject: TObject);
|
---|
587 | begin
|
---|
588 | end;
|
---|
589 |
|
---|
590 | procedure TWideStrings.ReadData(Reader: TReader);
|
---|
591 | begin
|
---|
592 | if Reader.NextValue in [vaString, vaLString] then
|
---|
593 | SetTextStr(Reader.ReadString) {JCL compatiblity}
|
---|
594 | else if Reader.NextValue = vaWString then
|
---|
595 | SetTextStr(Reader.ReadWideString) {JCL compatiblity}
|
---|
596 | else begin
|
---|
597 | BeginUpdate;
|
---|
598 | try
|
---|
599 | Clear;
|
---|
600 | Reader.ReadListBegin;
|
---|
601 | while not Reader.EndOfList do
|
---|
602 | if Reader.NextValue in [vaString, vaLString] then
|
---|
603 | Add(Reader.ReadString) {TStrings compatiblity}
|
---|
604 | else
|
---|
605 | Add(Reader.ReadWideString);
|
---|
606 | Reader.ReadListEnd;
|
---|
607 | finally
|
---|
608 | EndUpdate;
|
---|
609 | end;
|
---|
610 | end;
|
---|
611 | end;
|
---|
612 |
|
---|
613 | procedure TWideStrings.SaveToFile(const FileName: WideString);
|
---|
614 | var
|
---|
615 | Stream: TStream;
|
---|
616 | begin
|
---|
617 | Stream := TTntFileStream.Create(FileName, fmCreate);
|
---|
618 | try
|
---|
619 | SaveToStream(Stream);
|
---|
620 | finally
|
---|
621 | Stream.Free;
|
---|
622 | end;
|
---|
623 | end;
|
---|
624 |
|
---|
625 | procedure TWideStrings.SaveToStream(Stream: TStream);
|
---|
626 | var
|
---|
627 | SW: WideString;
|
---|
628 | begin
|
---|
629 | SW := GetTextStr;
|
---|
630 | Stream.WriteBuffer(PWideChar(SW)^, Length(SW) * SizeOf(WideChar));
|
---|
631 | end;
|
---|
632 |
|
---|
633 | procedure TWideStrings.SetCapacity(NewCapacity: Integer);
|
---|
634 | begin
|
---|
635 | // do nothing - descendents may optionally implement this method
|
---|
636 | end;
|
---|
637 |
|
---|
638 | procedure TWideStrings.SetCommaText(const Value: WideString);
|
---|
639 | begin
|
---|
640 | Delimiter := ',';
|
---|
641 | QuoteChar := '"';
|
---|
642 | SetDelimitedText(Value);
|
---|
643 | end;
|
---|
644 |
|
---|
645 | procedure TWideStrings.SetStringsAdapter(const Value: IWideStringsAdapter);
|
---|
646 | begin
|
---|
647 | if FAdapter <> nil then FAdapter.ReleaseStrings;
|
---|
648 | FAdapter := Value;
|
---|
649 | if FAdapter <> nil then FAdapter.ReferenceStrings(Self);
|
---|
650 | end;
|
---|
651 |
|
---|
652 | procedure TWideStrings.SetTextW(const Text: PWideChar);
|
---|
653 | begin
|
---|
654 | SetTextStr(Text);
|
---|
655 | end;
|
---|
656 |
|
---|
657 | procedure TWideStrings.SetTextStr(const Value: WideString);
|
---|
658 | var
|
---|
659 | P, Start: PWideChar;
|
---|
660 | S: WideString;
|
---|
661 | begin
|
---|
662 | BeginUpdate;
|
---|
663 | try
|
---|
664 | Clear;
|
---|
665 | P := Pointer(Value);
|
---|
666 | if P <> nil then
|
---|
667 | while P^ <> #0 do
|
---|
668 | begin
|
---|
669 | Start := P;
|
---|
670 | while not (P^ in [WideChar(#0), WideChar(#10), WideChar(#13)]) and (P^ <> WideLineSeparator) do
|
---|
671 | Inc(P);
|
---|
672 | SetString(S, Start, P - Start);
|
---|
673 | Add(S);
|
---|
674 | if P^ = #13 then Inc(P);
|
---|
675 | if P^ = #10 then Inc(P);
|
---|
676 | if P^ = WideLineSeparator then Inc(P);
|
---|
677 | end;
|
---|
678 | finally
|
---|
679 | EndUpdate;
|
---|
680 | end;
|
---|
681 | end;
|
---|
682 |
|
---|
683 | procedure TWideStrings.SetUpdateState(Updating: Boolean);
|
---|
684 | begin
|
---|
685 | end;
|
---|
686 |
|
---|
687 | procedure TWideStrings.SetValue(const Name, Value: WideString);
|
---|
688 | var
|
---|
689 | I: Integer;
|
---|
690 | begin
|
---|
691 | I := IndexOfName(Name);
|
---|
692 | if Value <> '' then
|
---|
693 | begin
|
---|
694 | if I < 0 then I := Add('');
|
---|
695 | Put(I, Name + NameValueSeparator + Value);
|
---|
696 | end else
|
---|
697 | begin
|
---|
698 | if I >= 0 then Delete(I);
|
---|
699 | end;
|
---|
700 | end;
|
---|
701 |
|
---|
702 | procedure TWideStrings.WriteData(Writer: TWriter);
|
---|
703 | var
|
---|
704 | I: Integer;
|
---|
705 | begin
|
---|
706 | Writer.WriteListBegin;
|
---|
707 | for I := 0 to Count-1 do begin
|
---|
708 | Writer.WriteWideString(Get(I));
|
---|
709 | end;
|
---|
710 | Writer.WriteListEnd;
|
---|
711 | end;
|
---|
712 |
|
---|
713 | procedure TWideStrings.SetDelimitedText(const Value: WideString);
|
---|
714 | var
|
---|
715 | P, P1: PWideChar;
|
---|
716 | S: WideString;
|
---|
717 | begin
|
---|
718 | BeginUpdate;
|
---|
719 | try
|
---|
720 | Clear;
|
---|
721 | P := PWideChar(Value);
|
---|
722 | while P^ in [WideChar(#1)..WideChar(' ')] do
|
---|
723 | Inc(P);
|
---|
724 | while P^ <> #0 do
|
---|
725 | begin
|
---|
726 | if P^ = QuoteChar then
|
---|
727 | S := WideExtractQuotedStr(P, QuoteChar)
|
---|
728 | else
|
---|
729 | begin
|
---|
730 | P1 := P;
|
---|
731 | while (P^ > ' ') and (P^ <> Delimiter) do
|
---|
732 | Inc(P);
|
---|
733 | SetString(S, P1, P - P1);
|
---|
734 | end;
|
---|
735 | Add(S);
|
---|
736 | while P^ in [WideChar(#1)..WideChar(' ')] do
|
---|
737 | Inc(P);
|
---|
738 | if P^ = Delimiter then
|
---|
739 | begin
|
---|
740 | P1 := P;
|
---|
741 | Inc(P1);
|
---|
742 | if P1^ = #0 then
|
---|
743 | Add('');
|
---|
744 | repeat
|
---|
745 | Inc(P);
|
---|
746 | until not (P^ in [WideChar(#1)..WideChar(' ')]);
|
---|
747 | end;
|
---|
748 | end;
|
---|
749 | finally
|
---|
750 | EndUpdate;
|
---|
751 | end;
|
---|
752 | end;
|
---|
753 |
|
---|
754 | function TWideStrings.GetDelimiter: WideChar;
|
---|
755 | begin
|
---|
756 | if not (sdDelimiter in FDefined) then
|
---|
757 | Delimiter := ',';
|
---|
758 | Result := FDelimiter;
|
---|
759 | end;
|
---|
760 |
|
---|
761 | function TWideStrings.GetQuoteChar: WideChar;
|
---|
762 | begin
|
---|
763 | if not (sdQuoteChar in FDefined) then
|
---|
764 | QuoteChar := '"';
|
---|
765 | Result := FQuoteChar;
|
---|
766 | end;
|
---|
767 |
|
---|
768 | procedure TWideStrings.SetDelimiter(const Value: WideChar);
|
---|
769 | begin
|
---|
770 | if (FDelimiter <> Value) or not (sdDelimiter in FDefined) then
|
---|
771 | begin
|
---|
772 | Include(FDefined, sdDelimiter);
|
---|
773 | FDelimiter := Value;
|
---|
774 | end
|
---|
775 | end;
|
---|
776 |
|
---|
777 | procedure TWideStrings.SetQuoteChar(const Value: WideChar);
|
---|
778 | begin
|
---|
779 | if (FQuoteChar <> Value) or not (sdQuoteChar in FDefined) then
|
---|
780 | begin
|
---|
781 | Include(FDefined, sdQuoteChar);
|
---|
782 | FQuoteChar := Value;
|
---|
783 | end
|
---|
784 | end;
|
---|
785 |
|
---|
786 | function TWideStrings.CompareStrings(const S1, S2: WideString): Integer;
|
---|
787 | begin
|
---|
788 | Result := WideCompareText(S1, S2);
|
---|
789 | end;
|
---|
790 |
|
---|
791 | function TWideStrings.GetNameValueSeparator: WideChar;
|
---|
792 | begin
|
---|
793 | {$IFDEF COMPILER_7_UP}
|
---|
794 | if not (sdNameValueSeparator in FDefined) then
|
---|
795 | NameValueSeparator := '=';
|
---|
796 | Result := FNameValueSeparator;
|
---|
797 | {$ELSE}
|
---|
798 | Result := '=';
|
---|
799 | {$ENDIF}
|
---|
800 | end;
|
---|
801 |
|
---|
802 | {$IFDEF COMPILER_7_UP}
|
---|
803 | procedure TWideStrings.SetNameValueSeparator(const Value: WideChar);
|
---|
804 | begin
|
---|
805 | if (FNameValueSeparator <> Value) or not (sdNameValueSeparator in FDefined) then
|
---|
806 | begin
|
---|
807 | Include(FDefined, sdNameValueSeparator);
|
---|
808 | FNameValueSeparator := Value;
|
---|
809 | end
|
---|
810 | end;
|
---|
811 | {$ENDIF}
|
---|
812 |
|
---|
813 | function TWideStrings.GetValueFromIndex(Index: Integer): WideString;
|
---|
814 | begin
|
---|
815 | if Index >= 0 then
|
---|
816 | Result := Copy(Get(Index), Length(Names[Index]) + 2, MaxInt) else
|
---|
817 | Result := '';
|
---|
818 | end;
|
---|
819 |
|
---|
820 | procedure TWideStrings.SetValueFromIndex(Index: Integer; const Value: WideString);
|
---|
821 | begin
|
---|
822 | if Value <> '' then
|
---|
823 | begin
|
---|
824 | if Index < 0 then Index := Add('');
|
---|
825 | Put(Index, Names[Index] + NameValueSeparator + Value);
|
---|
826 | end
|
---|
827 | else
|
---|
828 | if Index >= 0 then Delete(Index);
|
---|
829 | end;
|
---|
830 |
|
---|
831 | end.
|
---|