-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsupportfuncs.pas
1049 lines (971 loc) · 25.2 KB
/
supportfuncs.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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ Ovotext - simple text editor
Copyright (C) 2015 Marco Caselli <marcocas@gmail.com>
This source is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
}
{$I codegen.inc}
unit SupportFuncs;
interface
uses
Classes, SysUtils, RegExpr, LazFileUtils
{$IFDEF UNIX}
, BaseUnix
{$ENDIF} ;
const
// TODO: read it from config file
NUMBEROFSPACEFORTAB = 2;
//Remove invalid char from highlighters name and attributes
function CleanupName(aName: string): string;
//Split a delimited string in a Stringlist
procedure StrToStrings(S, Sep: string; const List: TStrings; const AllowEmptyString: boolean = True);
{$IFDEF UNIX}
function isRoot: boolean;
{$ENDIF}
function RemoveSpacesInExcess(const s: string): string;
function TabsToSpace(const S: string): string;
function FormatXML(const S: string): string;
function CompactXML(const S: string): string;
function FormatJson(const S: string): string;
function CompactJson(const S: string): string;
function FormatSQL(const S: string): string;
function BuildFolderList(const Path: string; const List: TStrings): boolean;
function BuildFileList(const Path: string; const Attr: integer; const List: TStrings; Recurring: boolean): boolean;
function DecodeExtendedSearch(S: string): string;
implementation
uses Math, lazutf8;
const
Aligner = #31;
CRLF = #13#10;
CR = #13;
LF = #10;
DOUBLEQUOTE = #34;
QUOTE = #39;
SQUARE_OPEN = #91;
SQUARE_CLOSE = #93;
ROUND_OPEN = #40;
ROUND_CLOSE = #41;
COMMA = #44;
SEMICOLON = #59;
SPACE = #32;
SQLKEYWORDMAX = 21;
var
sqlKeyWord: array[1..SQLKEYWORDMAX] of string = (' INNER JOIN', ' LEFT JOIN', ' RIGHT JOIN', ' WHERE', ' LEFT OUTER JOIN', ' GROUP BY',
' ORDER BY', ' HAVING', ' FROM', ' SELECT', ' AND', ' FOR', ' INSERT INTO', ' OR',
' UPDATE', ' SET', ' DELETE', ' ALTER ', ' JOIN', ' DROP', ' VALUES');
function DecodeExtendedSearch(S: string): string;
const
Escape = ['b', 't', 'n', 'v', 'f', 'r'];
var
C: PChar;
R: string;
I, J: Integer;
H: string;
begin
C := PChar(S);
I := Length(S);
if I < 1 then
Exit('');
R := '';
SetLength(R, I);
I := 1;
while C^ <> #00 do
begin
if C^ = '\' then
begin
Inc(C);
if C^ in Escape then
case C^ of
'b': R[I] := #8;
't': R[I] := #9;
'n': R[I] := #10;
'v': R[I] := #11;
'f': R[I] := #12;
'r': R[I] := #13;
end
else if C^ = 'u' then
begin
H := UnicodeToUTF8(StrToInt('$' + C[1] + C[2] + C[3] + C[4]));
for J := 1 to Length(H) - 1 do
begin
R[I] := H[J];
Inc(I);
end;
R[I] := H[Length(H)];
Inc(C, 4);
end
else
R[I] := C^;
end
else
R[I] := C^;
Inc(C);
Inc(I);
end;
SetLength(r,i-1);
Result := R;
end;
function CleanupName(aName: string): string;
var
c: integer;
begin
Result := aName;
for c := 1 to Length(aName) do
if not (upcase(aName[c]) in ['A'..'Z', '0'..'9', '_']) then
Result[c] := '_';
end;
function RemoveSpacesInExcess(const s: string): string;
var
p: integer;
begin
Result := trim(s);
p := pos(#32#32, Result);
while p > 0 do
begin
Result := StringReplace(Result, #32#32, #32, [rfReplaceAll]);
p := pos(#32#32, Result);
end;
end;
function TabsToSpace(const S: string): string;
begin
Result := StringReplace(s, #9, StringOfChar(#32, NUMBEROFSPACEFORTAB), [rfReplaceAll]);
end;
procedure StrToStrings(S, Sep: string; const List: TStrings; const AllowEmptyString: boolean = True);
var
I, L: integer;
Left: string;
begin
Assert(List <> nil);
List.BeginUpdate;
try
List.Clear;
L := Length(Sep);
I := Pos(Sep, S);
while I > 0 do
begin
Left := LeftStr(S, I - 1);
if (Left <> '') or AllowEmptyString then
List.Add(Left);
Delete(S, 1, I + L - 1);
I := Pos(Sep, S);
end;
if S <> '' then
List.Add(S);
finally
List.EndUpdate;
end;
end;
function FormatXML(const S: string): string;
const
bHighlightCloseTag = False;
CDATA = '![CDATA[';
COMMENT = '!--';
var
st: TMemoryStream;
bDoIndent: boolean;
szCheckTag: string;
bTagBuilding: boolean;
szCurrentTag: string;
c, cNextChar: char;
bQuoteActive: boolean;
cChar: char;
bCheckIndent: boolean;
bSkipping: boolean;
bTagActive: boolean;
nStringLoop: integer;
nIndent: integer;
procedure OutputIndent;
var
nIndentLoop: integer;
begin
if st.Size > 0 then
st.Write(string(sLineBreak)[1], Length(sLineBreak));
if nIndent < 0 then
begin
nIndent := 0; // fix negative indents due to bad XML
// result := result + '[NEGINDENT]';
end;
c := ' ';
for nIndentLoop := 0 to nIndent - 1 do
begin
st.Write(c, SizeOf(char));
end;
end;
procedure SkipCDATAorComment;
var
MaybeDone: boolean;
begin
Inc(nStringLoop);
bSkipping := False;
if copy(s, nStringLoop, Length(CDATA)) = CDATA then
begin
MaybeDone := False;
while nStringLoop <= Length(S) do
begin
if s[nStringLoop] = ']' then
begin
if MaybeDone then
begin
c := ']';
st.Write(c, SizeOf(char));
szCurrentTag := CDATA;
bSkipping := True;
break;
end
else
MaybeDone := True;
end
else
MaybeDone := False;
c := s[nStringLoop];
st.Write(c, SizeOf(char));
Inc(nStringLoop);
end;
end
else
begin
if copy(s, nStringLoop, Length(COMMENT)) = COMMENT then
begin
MaybeDone := False;
while nStringLoop <= Length(S) do
begin
if s[nStringLoop] = '-' then
begin
if MaybeDone then
begin
c := '-';
st.Write(c, SizeOf(char));
szCurrentTag := COMMENT;
bSkipping := True;
break;
end
else
MaybeDone := True;
end
else
MaybeDone := False;
c := s[nStringLoop];
st.Write(c, SizeOf(char));
Inc(nStringLoop);
end;
end;
end;
end;
begin
bTagBuilding := False;
bQuoteActive := False;
bTagActive := False;
Result := '';
nIndent := 0;
bSkipping := False;
bCheckIndent := False;
szCurrentTag := '';
St := TMemoryStream.Create;
nStringLoop := 1;
while nStringLoop <= Length(S) do
begin
cChar := S[nStringLoop];
if nStringLoop < Length(S) then
begin
cNextChar := S[nStringLoop + 1];
end
else
begin
cNextChar := ' '; // safe char
end;
case cChar of
'<':
begin
bDoIndent := False;
bTagActive := True;
if cNextChar = '/' then
begin
Dec(nIndent, NUMBEROFSPACEFORTAB);
bTagBuilding := False;
bCheckIndent := False;
szCheckTag := Copy(S, nStringLoop + 2, Length(szCurrentTag));
if szCheckTag <> szCurrentTag then
bDoIndent := True;
end
else
begin
bTagBuilding := True;
szCurrentTag := '';
bCheckIndent := True;
if not bHighlightCloseTag then
bDoIndent := True;
end;
if bDoIndent then
OutputIndent;
c := '<';
st.Write(c, SizeOf(char));
if cNextChar = '!' then
SkipCDATAorComment;
end;
'>':
begin
bTagActive := False;
bTagBuilding := False;
c := '>';
st.Write(c, SizeOf(char));
if bCheckIndent then
Inc(nIndent, NUMBEROFSPACEFORTAB);
if bSkipping then
begin
Dec(nIndent, NUMBEROFSPACEFORTAB);
bSkipping := False;
end;
end;
'"':
begin
st.Write(cChar, 1);
if bTagActive then
bQuoteActive := not bQuoteActive;
end;
'/':
begin
if (bTagActive) and (not bQuoteActive) then
begin
if bCheckIndent then
begin
if cNextChar = '>' then
Dec(nIndent, NUMBEROFSPACEFORTAB);
end;
end;
c := '/';
st.Write(c, SizeOf(char));
end;
#13, #10, #9:
begin
end;
else
begin
if bTagBuilding then
begin
if cChar <> ' ' then
begin
szCurrentTag := szCurrentTag + cChar;
end
else
begin
bTagBuilding := False;
end;
end;
st.Write(cChar, SizeOf(char));
end;
end; // case
Inc(nStringLoop);
end;
SetString(Result, st.Memory, st.Size);
st.Free;
end;
function Spaces(Num: integer): string;
begin
Result := StringOfChar(' ', num);
end;
{
Json formatting code taken and adapted from delphi-xe-json by Nils Achterholt
https://bitbucket.org/Gloegg/delphi-xe-json
}
function RemoveWhiteSpace(const aInput: string): string;
const
whitespace = [#0, #8, #9, #10, #12, #13, ' '];
var
i: integer;
insideString: boolean;
begin
i := 1;
Result := '';
insideString := False;
while i <= length(aInput) do
begin
if (aInput[i] = '\') then
begin
Result := Result + aInput[i] + aInput[i + 1];
Inc(i, 2);
end
else if aInput[i] = '"' then
begin
Result := Result + aInput[i];
insideString := not insideString;
Inc(i);
end
else if not insideString and (aInput[i] in whitespace) then
Inc(i)
else
begin
Result := Result + aInput[i];
Inc(i);
end;
end;
end;
function Indent(const aInput: string): string;
var
sl: TStringList;
i: integer;
lvl: integer;
begin
lvl := 0;
sl := TStringList.Create;
try
sl.Text := aInput;
for i := 0 to sl.Count - 1 do
begin
case sl[i][Length(sl[i])] of
'{':
begin
sl[i] := Spaces(lvl * NUMBEROFSPACEFORTAB) + sl[i];
Inc(lvl);
if (Length(sl[i]) > 1) and (sl[i][2] = '}') then
Dec(lvl);
end;
'[':
begin
sl[i] := Spaces(lvl * NUMBEROFSPACEFORTAB) + sl[i];
Inc(lvl);
if (Length(sl[i]) > 1) and (sl[i][2] = ']') then
Dec(lvl);
end;
else
case sl[i][1] of
'}', ']':
begin
Dec(lvl);
lvl := max(lvl, 0);
sl[i] := Spaces(lvl * NUMBEROFSPACEFORTAB) + sl[i];
end
else
sl[i] := Spaces(lvl * NUMBEROFSPACEFORTAB) + sl[i];
end;
end;
end;
Result := sl.Text;
finally
sl.Free;
end;
end;
function InsertLineBreaks(const aInput: string): string;
var
i: integer;
insideString: boolean;
s: string;
begin
s := '';
i := 1;
insideString := False;
while i <= length(aInput) do
begin
if insideString then
begin
s := s + aInput[i];
if aInput[i] = '\' then
begin
s := s + aInput[i + 1];
Inc(i, 2);
end
else
begin
if aInput[i] = '"' then
insideString := False;
Inc(i);
end;
end
else
begin
case aInput[i] of
'\':
begin
s := s + aInput[i] + aInput[i + 1];
Inc(i, 2);
end;
'"':
begin
s := s + aInput[i];
insideString := not insideString;
Inc(i);
end;
'{':
begin
if aInput[i + 1] = '}' then
begin
s := s + '{}';
Inc(i, 2);
end
else
begin
s := s + aInput[i] + sLineBreak;
Inc(i);
end;
end;
'[':
begin
if aInput[i + 1] = ']' then
begin
s := s + '[]';
Inc(i, 2);
end
else
begin
s := s + aInput[i] + sLineBreak;
Inc(i);
end;
end;
'}', ']':
begin
if (length(aInput) > i) and (aInput[i + 1] = ',') then
begin
s := s + sLineBreak + aInput[i] + ',' + sLineBreak;
Inc(i, 2);
end
else
begin
s := s + sLineBreak + aInput[i] + sLineBreak;
Inc(i);
end;
end;
',':
begin
s := s + aInput[i] + sLineBreak;
Inc(i);
end;
else
begin
s := s + aInput[i];
Inc(i);
end;
end;
end;
end;
Result := s;
end;
function RemoveEmptyLines(const aInput: string): string;
var
sl: TStringList;
i: integer;
begin
sl := TStringList.Create;
try
sl.Text := aInput;
for i := sl.Count - 1 downto 0 do
begin
if sl[i] = '' then
sl.Delete(i);
end;
Result := sl.Text;
finally
sl.Free;
end;
end;
function CompactXML(const S: string): string;
begin
Result := ReplaceRegExpr('>\s{0,}<', S, '><', True);
end;
function FormatJSON(const S: string): string;
begin
// Clean the input from previous formatting
Result := RemoveWhiteSpace(S);
// Split up logical units of JSON
Result := InsertLineBreaks(Result);
// It's easier to clean up empty lines then preventing them
Result := RemoveEmptyLines(Result);
// Indent each line with the correct space
Result := Indent(Result);
end;
{$IFDEF UNIX}
function isRoot: boolean;
begin
Result := FpGetuid = 0;
end;
{$ENDIF}
// Derived from "Like" by Michael Winter
function StrMatches(const Substr, S: string; const Index: SizeInt = 1): boolean;
var
StringPtr: PChar;
PatternPtr: PChar;
StringRes: PChar;
PatternRes: PChar;
begin
Result := SubStr = '*';
if Result or (S = '') then
Exit;
StringPtr := PChar(@S[Index]);
PatternPtr := PChar(SubStr);
StringRes := nil;
PatternRes := nil;
repeat
repeat
case PatternPtr^ of
#0:
begin
Result := StringPtr^ = #0;
if Result or (StringRes = nil) or (PatternRes = nil) then
Exit;
StringPtr := StringRes;
PatternPtr := PatternRes;
Break;
end;
'*':
begin
Inc(PatternPtr);
PatternRes := PatternPtr;
Break;
end;
'?':
begin
if StringPtr^ = #0 then
Exit;
Inc(StringPtr);
Inc(PatternPtr);
end;
else
begin
if StringPtr^ = #0 then
Exit;
if StringPtr^ <> PatternPtr^ then
begin
if (StringRes = nil) or (PatternRes = nil) then
Exit;
StringPtr := StringRes;
PatternPtr := PatternRes;
Break;
end
else
begin
Inc(StringPtr);
Inc(PatternPtr);
end;
end;
end;
until False;
repeat
case PatternPtr^ of
#0:
begin
Result := True;
Exit;
end;
'*':
begin
Inc(PatternPtr);
PatternRes := PatternPtr;
end;
'?':
begin
if StringPtr^ = #0 then
Exit;
Inc(StringPtr);
Inc(PatternPtr);
end;
else
begin
repeat
if StringPtr^ = #0 then
Exit;
if StringPtr^ = PatternPtr^ then
Break;
Inc(StringPtr);
until False;
Inc(StringPtr);
StringRes := StringPtr;
Inc(PatternPtr);
Break;
end;
end;
until False;
until False;
end;
function IsFileNameMatch(FileName: string; const Mask: string; const CaseSensitive: boolean): boolean;
begin
Result := True;
{$IFDEF MSWINDOWS}
if (Mask = '') or (Mask = '*') or (Mask = '*.*') then
Exit;
if Pos('.', FileName) = 0 then
FileName := FileName + '.'; // file names w/o extension match '*.'
{$ENDIF MSWINDOWS}
{$IFDEF UNIX}
if (Mask = '') or (Mask = '*') then
Exit;
{$ENDIF UNIX}
if CaseSensitive then
Result := StrMatches(Mask, FileName)
else
Result := StrMatches(AnsiUpperCase(Mask), AnsiUpperCase(FileName));
end;
function BuildFolderList(const Path: string; const List: TStrings): boolean;
var
SearchRec: TSearchRec;
Directory: string;
begin
Assert(List <> nil);
{* extract the Directory *}
Directory := ExtractFileDir(Path);
{* files can be searched in the current directory *}
if Directory <> '' then
begin
Directory := IncludeTrailingPathDelimiter(Directory);
end;
{* search all files in the directory *}
Result := FindFirstUTF8(Directory + AllFilesMask, faDirectory, SearchRec) = 0;
List.BeginUpdate;
try
while Result do
begin
if (SearchRec.Name <> '.') and
(SearchRec.Name <> '..') and
((SearchRec.Attr and faDirectory) = faDirectory) then
begin
List.Add(Directory + SearchRec.Name);
end;
case FindNextUTF8(SearchRec) of
0: ;
2: //ERROR_NO_MORE_FILES:
Break;
else
Result := False;
end;
end;
finally
FindCloseUTF8(SearchRec);
List.EndUpdate;
end;
end;
function BuildFileList(const Path: string; const Attr: integer; const List: TStrings; Recurring: boolean): boolean;
var
SearchRec: TSearchRec;
IndexMask: integer;
MaskList: TStringList;
Masks, Directory: string;
begin
Assert(List <> nil);
MaskList := TStringList.Create;
try
{* extract the Directory *}
Directory := ExtractFileDir(Path);
{* files can be searched in the current directory *}
if Directory <> '' then
begin
Directory := IncludeTrailingPathDelimiter(Directory);
{* extract the FileMasks portion out of Path *}
Masks := copy(Path, Length(Directory) + 1, Length(Path));
end
else
Masks := Path;
{* put the Masks into TStringlist *}
StrToStrings(Masks, ';', MaskList, False);
{* search all files in the directory *}
Result := FindFirstUTF8(Directory + AllFilesMask, faAnyFile, SearchRec) = 0;
List.BeginUpdate;
try
while Result do
begin
{* if the filename matches any mask then it is added to the list *}
if Recurring and ((searchrec.Attr and faDirectory) <> 0) and (SearchRec.Name <> '.') and
(SearchRec.Name <> '..') then
BuildFileList(IncludeTrailingPathDelimiter(Directory + SearchRec.Name) + masks,
Attr, list, Recurring);
for IndexMask := 0 to MaskList.Count - 1 do
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') and
((SearchRec.Attr and Attr) = (SearchRec.Attr and faAnyFile)) and
((searchrec.Attr and faDirectory) <> faDirectory) and
IsFileNameMatch(SearchRec.Name, MaskList.Strings[IndexMask], False) then
begin
List.Add(Directory + SearchRec.Name);
Break;
end;
case FindNext(SearchRec) of
0: ;
2: //ERROR_NO_MORE_FILES:
Break;
else
Result := False;
end;
end;
finally
FindClose(SearchRec);
List.EndUpdate;
end;
finally
MaskList.Free;
end;
end;
function CharReplace(var S: string; const Search, Replace: char): integer;
var
P: PChar;
begin
Result := 0;
if Search <> Replace then
begin
UniqueString(S);
P := PChar(S);
while P^ <> #0 do
begin
if P^ = Search then
begin
P^ := Replace;
Inc(Result);
end;
Inc(P);
end;
end;
end;
function Match(OpenChar, CloseChar: char; Text: string): longint;
var
Last, First: longint;
begin
First := 1;
Last := Pos(CloseChar, Text);
if Last = 0 then
Last := Length(Text);
repeat
repeat
Inc(First);
until (First = last) or (Text[First] = openchar);
if First < last then
repeat
Inc(Last);
until (last >= Length(Text)) or (Text[Last] = closeChar);
until First = last;
Result := Last;
end;
function CompactJson(const S: string): string;
begin
Result := RemoveWhiteSpace(S);
end;
function FormatSQL(const S: string): string;
var
strSQL: string;
blnkeyWord: boolean;
intKeyWord: integer;
lngChar: longint;
lngEnd: longint;
strOut: string;
maxKeyword: integer;
cntSpaces: integer;
After: string;
Before: string;
SL: TStringList;
procedure EmitStatement;
var
idRow: integer;
AlignerPos: integer;
begin
SL := TStringList.Create;
try
SL.Text := strOut;
for idRow := 0 to SL.Count - 1 do
begin
AlignerPos := pos(Aligner, SL[idRow]);
if AlignerPos = 0 then
Continue;
cntSpaces := (maxKeyword - AlignerPos);
Before := Trim(copy(SL[idRow], 1, AlignerPos - 1));
After := sl[idRow];
Delete(After, 1, AlignerPos);
SL[idRow] := spaces(cntSpaces) + Before + ' ' + After;
end;
Result := Result + SL.Text;
maxKeyword := 0;
strOut := '';
finally
SL.Free;
end;
end;
begin
strout := '';
maxKeyword := 0;
Result := '';
if S <> '' then
begin
strSQL := S;
strSQL := ' ' + strSQL + ' ';
CharReplace(strSQL, CR, SPACE);
CharReplace(strSQL, LF, SPACE);
strSQL := RemoveSpacesInExcess(strSQL);
lngChar := 1;
while lngChar <= Length(strSQL) do
begin
blnkeyWord := False;
for intKeyWord := 1 to SQLKEYWORDMAX do
if UpperCase(Copy(strSQL, lngChar, Length(sqlKeyWord[intKeyWord]))) = (sqlKeyWord[intKeyWord]) then
begin
blnkeyWord := True;
Maxkeyword := max(Length(sqlKeyWord[intKeyWord]), maxKeyword);
Break;
end;
if blnkeyWord then
begin
after := Aligner;
before := CRLF;
cntSpaces := 0;
strOut := strOut + Before + trim(LowerCase(sqlKeyWord[intKeyWord])) + After + Spaces(cntSpaces);
lngChar := lngChar + Length(sqlKeyWord[intKeyWord]);
end
else
if (strSQL[lngChar] = CR) or (strSQL[lngChar] = LF) then
lngChar := lngChar + 1
else
case strSQL[lngChar] of
QUOTE, DOUBLEQUOTE:
begin
lngEnd :=
pos(strSQL[lngChar], Copy(strSQL, lngChar + 1, Length(strsql)));
strOut := strOut + Copy(strSQL, lngChar, lngEnd + 1);
lngChar := lngChar + lngEnd + 1;
end;
SQUARE_OPEN:
begin
lngEnd :=