-
Notifications
You must be signed in to change notification settings - Fork 3
/
StrCopyUtils.pas
734 lines (624 loc) · 17.4 KB
/
StrCopyUtils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
{*******************************************************}
{ }
{ 04/2014 MaxiDonkey Library }
{ }
{*******************************************************}
unit StrCopyUtils;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Dialogs, System.StrUtils, uRegistry;
function GetAfterStr(const Text, Str_Pattern: string):string;
function GetBeforStr(const Text, Str_Pattern: string):string;
function GetBetween(const Text, Start_Pattern, Finish_Pattern: string):string;
function GetBeaftStr(const Text, Str_Pattern: string; var StartStr, EndStr: string):string;
function DelSubStr(const Text, Str_Pattern: string):string;
function NameStr(const Text: string; Sep : string = '='):string;
function ValueStr(const Text: string; Sep : string = '='):string;
procedure GetNameValue(const Text: string; var AName, AValue: string; Sep : string = '=');
function SetNameValue(const AName, AValue: string; Sep : string = '='):string;
{Replace a substring in a text}
function SubstrSeplaceInText(const Src, SubStr, ReplaceStr: string):string; overload;
function SubstrSeplaceInText(const Src, Prefix, SubStr, ReplaceStr, Suffix: string):string; overload;
{Replace "' ' by "'"}
function QuoteFix(const ASt: string):string;
function ColumnFix(const ASt: string):string;
function ProperQuoted(const ASt: string):string;
{Supression d'un caractère en début et fin de chaîne}
function CharDel(const Text: string; const ACh: Char):string;
function QuotedDel(Text: string):string; // '
function DblQuotedDel(Text: string):string; // "
function BraceDel(Text: string):string; // { }
function BracketDel(Text: string):string; // [ ]
function ParenthesisDel(Text: string):string; // ( )
function SlashDel(Text: string):string; // /
function AntiSlashDel(Text: string):string; // \
{Atext sur une seule ligne de text}
function AddStrToLst(const AText: string; Item: string):string;
function ConcatStr(const AText: string; Item: string; Sep : string = ';'):string;
{Catalogs extract value}
function CatalogExtractStr(const List: TStrings; const AName: string):string; overload;
function CatalogExtractStr(const List: TStrings; Index: Integer):string; overload;
function CatalogExtractStr(const AText, AName: string):string; overload;
function CatalogExtractStr(const AText: string; Index: Integer):string; overload;
{Catalogs replace value}
procedure CatalogReplaceStr(const List: TStrings; const AName, AValue: string); overload;
procedure CatalogReplaceStr(const List: TStrings; Index : Integer; const AValue: string); overload;
function CatalogReplaceStr(const AText, AName, AValue: string):string; overload;
function CatalogReplaceStr(const AText : string; Index: Integer; const AValue: string):string; overload;
{Catalogs Initialize}
procedure CatalogInit(const List: TStrings; AFields: array of string); overload;
function CatalogInit(AFields: array of string):string; overload;
{List Routines}
function ListFusion(const Src, New: string):string;
function Belongs(const Src, AItem: string):Boolean;
function TextFileContent(const FileName: string):string;
procedure SaveTextFile(const FileName, Content: string);
function TextToList(const AText: string; const Sep: string; AddSep: Boolean = False): string;
{Tick}
function TickCount:Cardinal;
function TickCountStr:string;
function TickStrToCardinal(const ATick: string):Cardinal;
function SetTickText(const AText: string):string;
function TickToDuration(const TStart, TEnd: Cardinal):TTime;
type
TTickStackFifo = class
private
FPile: TStringList;
function GetPileText: string;
procedure SetPileText(const Value: string);
function ExtractFirstItem:string;
function ReadHighItem:string;
public
procedure Clear;
function Count:Integer;
function HighItem(var Tick: Cardinal; var Value: string):Boolean;
procedure Stack(const Value: string);
function Unstack(var Tick: Cardinal; var Value: string):Boolean;
procedure SaveToRegistry;
procedure LoadFromRegistry;
property PileText: string read GetPileText write SetPileText;
constructor Create(const APileStr: string);
destructor Destroy; override;
end;
implementation
function GetAfterStr(const Text, Str_Pattern: string):string;
var
P : Integer;
begin
Result := EmptyStr;
P := Pos(Str_Pattern, Text);
if P > 0 then Result := Trim( Copy(Text, P + Length(Str_Pattern), Length(Text)) );
end;
function GetBeforStr(const Text, Str_Pattern: string):string;
var
P : Integer;
begin
Result := EmptyStr;
P := Pos(Str_Pattern, Text);
if P > 0 then Result := Trim( Copy(Text, 1, P - 1 ) );
end;
function GetBeaftStr(const Text, Str_Pattern: string; var StartStr,
EndStr: string):string;
var
P : Integer;
begin
StartStr := EmptyStr;
EndStr := EmptyStr;
P := Pos(Str_Pattern, Text);
if P > 0 then begin
StartStr := Trim( Copy(Text, 1, P - 1 ) );
EndStr := Trim( Copy(Text, P + Length(Str_Pattern), Length(Text)) );
Result := StartStr + EndStr;
end else begin
StartStr := Text;
Result := Text;
end;
end;
function DelSubStr(const Text, Str_Pattern: string):string;
var
Find : Boolean;
begin
Result := Text;
Find := AnsiPos(AnsiUpperCase(Str_Pattern), AnsiUpperCase(Text)) > 0;
if Find then Result := Trim(ReplaceText(Text, Str_Pattern, EmptyStr));
end;
function GetBetween(const Text, Start_Pattern, Finish_Pattern: string):string;
begin
Result := GetAfterStr(Text, Start_Pattern);
Result := GetBeforStr(Result, Finish_Pattern);
end;
function NameStr(const Text: string; Sep : string):string;
begin
Result := GetBeforStr(Text, Sep)
end;
function ValueStr(const Text: string; Sep : string):string;
begin
Result := GetAfterStr(Text, Sep)
end;
procedure GetNameValue(const Text: string; var AName, AValue: string;
Sep : string);
begin
AName := NameStr(Text, Sep);
AValue := ValueStr(Text, Sep);
end;
function SetNameValue(const AName, AValue: string; Sep : string):string;
begin
Result := Format('%s%s%s', [AName, Sep, AValue])
end;
function CharDel(const Text: string; const ACh: Char):string;
var
L : Integer;
begin
Result := Trim(Text);
if Length( Result ) = 0 then Exit;
if Result[1] = ACh then Result := Copy(Result, 2, Length(Result) );
L := Length(Result);
if Result[L] = ACh then Result := Copy(Result, 1, L - 1);
end;
function QuotedDel(Text: string):string;
begin
Result := CharDel(Text, '''')
end;
function DblQuotedDel(Text: string):string;
begin
Result := CharDel(Text, '"')
end;
function BraceDel(Text: string):string;
begin
Result := CharDel(Text, '{');
Result := CharDel(Result, '}');
end;
function BracketDel(Text: string):string;
begin
Result := CharDel(Text, '[');
Result := CharDel(Result, ']');
end;
function ParenthesisDel(Text: string):string;
begin
Result := CharDel(Text, '(');
Result := CharDel(Result, ')');
end;
function SlashDel(Text: string):string;
begin
Result := CharDel(Text, '/')
end;
function AntiSlashDel(Text: string):string;
begin
Result := CharDel(Text, '\')
end;
function AddStrToLst(const AText: string; Item: string):string;
begin
with TStringList.Create do
try
Text := AText;
Add( Item );
Result := Text;
finally
Free
end
end;
function ConcatStr(const AText: string; Item: string; Sep : string):string;
begin
Result := Trim( AText );
if Result = EmptyStr then Result := Item
else Result := Format('%s%s%s', [Result, Sep, Item])
end;
function CatalogExtractStr(const List: TStrings; const AName: string):string;
var
Index : Integer;
begin
Result := EmptyStr;
with List do begin
Index := IndexOfName( AName );
if Index > -1 then Result := ValueFromIndex[ Index ]
end
end;
function CatalogExtractStr(const List: TStrings; Index: Integer):string;
begin
Result := EmptyStr;
with List do
if (Index > -1) and (Index < Count) then Result := ValueFromIndex[ Index ]
end;
function CatalogExtractStr(const AText, AName: string):string;
var
Index : Integer;
begin
Result := EmptyStr;
with TStringList.Create do
try
Text := AText;
Index := IndexOfName( AName );
if Index > -1 then Result := ValueFromIndex[ Index ]
finally
Free
end
end;
function CatalogExtractStr(const AText: string; Index: Integer):string;
begin
Result := EmptyStr;
with TStringList.Create do
try
Text := AText;
if (Index > -1) and (Index < Count) then Result := ValueFromIndex[ Index ]
finally
Free
end
end;
procedure CatalogReplaceStr(const List: TStrings; const AName, AValue: string);
{AList <- le catalog complet modifié}
var
Index : Integer;
begin
with List do begin
Index := IndexOfName( AName );
if Index > -1 then ValueFromIndex[Index] := AValue
end;
end;
procedure CatalogReplaceStr(const List: TStrings; Index : Integer; const AValue: string);
{AList <- le catalog complet modifié}
begin
with List do
if (Index > -1) and (Index < Count) then ValueFromIndex[Index] := AValue
end;
function CatalogReplaceStr(const AText, AName, AValue: string):string;
{Result <- le catalog complet modifié}
var
Index : Integer;
begin
Result := AText;
with TStringList.Create do
try
Text := AText;
Index := IndexOfName( AName );
if Index > -1 then begin
ValueFromIndex[Index] := AValue;
Result := Text;
end;
finally
Free
end
end;
function CatalogReplaceStr(const AText : string; Index: Integer; const AValue: string):string;
{Result <- le catalog complet modifié}
begin
Result := AText;
with TStringList.Create do
try
Text := AText;
if (Index > -1) and (Index < Count) then begin
ValueFromIndex[Index] := AValue;
Result := Text;
end;
finally
Free
end
end;
procedure CatalogInit(const List: TStrings; AFields: array of string);
var
I : Integer;
begin
with List do begin
Clear;
for I := Low( AFields ) to High( AFields ) do
Add( Format('%s=', [AFields[I]]) );
end
end;
function CatalogInit(AFields: array of string):string;
var
I : Integer;
begin
with TStringList.Create do
try
for I := Low( AFields ) to High( AFields ) do
Add( Format('%s=', [AFields[I]]) );
Result := Text;
finally
Free
end
end;
function TickCount:Cardinal;
begin
Result := GetTickCount;
end;
function TickCountStr:string;
begin
Result := Format('%d', [TickCount])
end;
function TickStrToCardinal(const ATick: string):Cardinal;
begin
try
Result := StrToInt( ATick )
except
on E: Exception do Result := 0
end
end;
function SetTickText(const AText: string):string;
begin
Result := SetNameValue(TickCountStr, AText)
end;
function TickToDuration(const TStart, TEnd: Cardinal):TTime;
var
h, m, s: Word;
d : Integer;
begin
d := Trunc( (TEnd - TSTart) / MSecsPerSec );
h := d div 3600;
d := d mod 3600;
m := d div 60;
s := d mod 60;
Result := EncodeTime(h,m,s,0)
end;
{ TTickStackFifo }
procedure TTickStackFifo.Clear;
begin
with FPile do Clear;
end;
function TTickStackFifo.Count: Integer;
begin
with FPile do Result := Count;
end;
constructor TTickStackFifo.Create(const APileStr: string);
begin
inherited Create;
FPile := TStringList.Create;
PileText := APileStr;
end;
destructor TTickStackFifo.Destroy;
begin
FPile.Free;
inherited;
end;
function TTickStackFifo.ExtractFirstItem: string;
begin
with FPile do
try
Result := Strings[0];
Delete(0);
except
Result := EmptyStr
end;
end;
function TTickStackFifo.GetPileText: string;
begin
with FPile do Result := Text
end;
function TTickStackFifo.HighItem(var Tick: Cardinal;
var Value: string): Boolean;
var
Buffer: string;
begin
with FPile do if Count > 0 then begin
GetNameValue( ReadHighItem, Buffer, Value );
Tick := TickStrToCardinal( Buffer );
Result := True;
end else
Result := False;
end;
procedure TTickStackFifo.LoadFromRegistry;
var
Buffer: string;
begin
KeyRead(BufferKey, 'TickStackFifo', Buffer);
PileText := Buffer;
end;
function TTickStackFifo.ReadHighItem: string;
begin
with FPile do Result := Strings[ Pred( Count ) ]
end;
procedure TTickStackFifo.SaveToRegistry;
begin
with FPile do KeyWrite(BufferKey, 'TickStackFifo', Text);
end;
procedure TTickStackFifo.SetPileText(const Value: string);
begin
with FPile do Text := Value
end;
procedure TTickStackFifo.Stack(const Value: string);
begin
with FPile do Add( SetTickText( Value ) )
end;
function TTickStackFifo.Unstack(var Tick: Cardinal; var Value: string): Boolean;
var
Buffer: string;
begin
with FPile do if Count > 0 then begin
GetNameValue( ExtractFirstItem, Buffer, Value );
Tick := TickStrToCardinal( Buffer );
Result := True;
end else
Result := False;
end;
function QuoteFix(const ASt: string):string;
begin
Result := ASt;
while Pos(''' ', Result) > 0 do Result := ReplaceText(Result, ''' ', '''');
Result := ColumnFix( ProperQuoted(Result) );
end;
function ColumnFix(const ASt: string):string;
begin
Result := ASt;
while Pos(' , ', Result) > 0 do Result := ReplaceText(Result, ' , ', ', ');
while Pos(' ,', Result) > 0 do Result := ReplaceText(Result, ' ,', '.');
end;
{Replace a substring in a text}
function SubstrSeplaceInText(const Src, SubStr, ReplaceStr: string):string;
begin
Result := SubstrSeplaceInText(Src, EmptyStr, SubStr, ReplaceStr, EmptyStr)
end; {SubstrSeplaceInText}
function SubstrSeplaceInText(const Src, Prefix, SubStr, ReplaceStr, Suffix: string):string;
var
LOut : TStringList;
Buffer : string;
procedure Initialize; begin
LOut := TStringList.Create;
LOut.Sorted := True
end;
function Finalize:string; begin
Result := LOut.Text;
LOut.Free
end;
procedure ProcessOn(const S: string); begin
if S = EmptyStr then Exit;
Buffer := System.StrUtils.ReplaceStr(S, SubStr, ReplaceStr);
Buffer := Format('%s%s%s', [Prefix, Buffer, Suffix]);
Buffer := ProperQuoted(Buffer);
with LOut do if IndexOf(Buffer) = -1 then Add(Buffer)
end;
begin
Result := Src;
Initialize;
with TStringList.Create do
try
Sorted := True;
Text := Src;
with GetEnumerator do
try
while MoveNext do ProcessOn( Trim(Current) )
finally
Free
end
finally
Free;
Result := Finalize
end
end; {SubstrSeplaceInText}
type
TPosition = array[0..99] of Integer;
var
Consons : array[0..19] of string =
( 'b', 'c', 'd', 'f', 'g', 'j', 'k', 'l', 'm',
'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z',
'ç'
); //'h',
function IsConson(const C: Char):Boolean;
var
S: string;
begin
S := AnsiLowerCase(C);
Result := IndexStr(S, Consons) > -1
end;
function ProperQuoted(const ASt: string):string;
var
Positions : TPosition;
PosCount : Integer;
Buffer : string;
NextCar : Char;
SubStr : string;
ReplaceStr : string;
Len : Integer;
procedure AddPosition(const Value: Integer); begin
Positions[PosCount] := Value;
Inc(PosCount);
end;
procedure PosRetrieve; var i : Integer; begin
for i := 1 to Len do if Buffer[i] = '''' then
if i < Len then AddPosition(i)
end;
procedure Initialize; begin
PosCount := 0;
Buffer := AnsiLowerCase(ASt);
Len := Length(Buffer);
PosRetrieve;
end;
procedure CharFix(const P: Integer); begin
NextCar := Buffer[P + 1];
if IsConson(NextCar) then begin
SubStr := '''' + NextCar;
ReplaceStr := 'e ' + NextCar;
Buffer := System.StrUtils.ReplaceStr(Buffer, SubStr, ReplaceStr);
end
end;
function Process:string; var i : Integer; begin
for i := 0 to Pred(PosCount) do CharFix( Positions[i] );
Result := Buffer;
end;
begin
Initialize;
Result := Process;
end; {ProperQuoted}
{List Routines}
function ListFusion(const Src, New: string):string;
var
L, N: TStringList;
procedure Initialize; begin
L := TstringList.Create;
L.Sorted := True;
L.Text := Src;
N := TStringList.Create;
N.Sorted := True;
N.Text := New;
end;
procedure Finalize; begin
Result := L.Text;
L.Free;
N.Free;
end;
begin
Initialize;
try
with N.GetEnumerator do
try
while MoveNext do if Trim(Current) <> EmptyStr then
if L.IndexOf(Trim(Current)) = -1 then L.Add(Trim(Current));
Result := L.Text
finally
Free
end;
finally
Finalize;
end
end; {ListFusion}
function Belongs(const Src, AItem: string):Boolean;
begin
with TStringList.Create do
try
Text := Src;
Result := IndexOf(AItem) > -1
finally
Free
end
end;
function TextFileContent(const FileName: string):string;
begin
Result := EmptyStr;
if FileExists(FileName) then with TStringList.Create do
try
LoadFromFile(FileName);
Result := Text
finally
Free
end
end;
procedure SaveTextFile(const FileName, Content: string);
begin
with TStringList.Create do
try
Text := Trim( Content );
SaveToFile( FileName );
finally
Free
end
end;
function TextToList(const AText: string; const Sep: string;
AddSep: Boolean): string;
var
Buffer: string;
function Ins(const ASt: string): string; begin
if AddSep then Result := Format('%s%s', [ASt, Sep]) else Result := ASt
end;
begin
with TStringList.Create do
try
Buffer := AText;
while Pos(Sep, Buffer) > 0 do begin
Add( Ins( GetBeforStr(Buffer, Sep) ) );
Buffer := GetAfterStr(Buffer, Sep)
end;
if Trim(Buffer) <> EmptyStr then Add( Ins( Buffer ) );
Result := Text
finally
Free
end
end; {StrToList}
end.