-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtilsUnit.pas
1025 lines (949 loc) · 26.1 KB
/
UtilsUnit.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
unit UtilsUnit;
{$MODE Delphi}
interface
uses SysUtils, DateUtils, Dialogs, FileUtil, LCLIntf, LCLType, LMessages, Math, Registry,
{$IFDEF Windows}
ShlObj,
ActiveX,
{$ENDIF}
db, Classes, typinfo;
function ValidateID(ID: String): Boolean;
function WorkoutAge(StoreDate: TDateTime): Integer;
function IntDatetoString(i: integer): String;
function DatetoIntDate(d: TDateTime): integer;
function Decrypt(pr: String): String;
function Encrypt(tr: String): String;
function TrimSpaces(stemp: AnsiString): AnsiString;
function TrimDash(stemp: AnsiString): AnsiString;
function MyRoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;
function ValidEmail(email: AnsiString): boolean;
function FixMySQLString(stemp: String): String;
function FixSQLString(stemp: String): String;
function ChangeEnterforSMS(stemp: AnsiString): AnsiString;
function UrlEncode(const DecodedStr: AnsiString; Pluses: Boolean): AnsiString;
function UrlDecode(const EncodedStr: AnsiString): AnsiString;
function HexToInt(HexStr: AnsiString): Int64;
function filenamespescharfix(stemp:string): AnsiString;
function GetSpecialFolder(const CSIDL : integer) : String;
function AddCR(stemp: AnsiString): AnsiString;
function RemoveLF(stemp: String): String;
function RemoveCRLF(stemp: String): String;
function IsInteger(S: AnsiString): Boolean;
function TestSwitchString(SwitchString,TestValue: AnsiString): AnsiString;
function UpdateSwitchString(SwitchString,TestValue,Value: AnsiString): AnsiString;
function CreateEasyPayNumber(GroupNo,MemNo,BranchNo:Integer):String;
function replacedoublequotewithsinglequote(stemp: AnsiString): AnsiString;
//function CreateGuid: string;
function ConvertFieldtoSQLString(FieldStore: TField): String;
procedure Split(const Delimiter: Char; Input: string; const Strings: TStrings);
function CalculateBMI(LenghtValue,WeightValue: Extended; WeightUnit, LenghtUnit:String): Extended;
function FloatToLocaleIndependantString(const v: Extended): string;
function GetRegistryData(RootKey: HKEY; Key, Value: String): variant;
procedure SetRegistryData(RootKey: HKEY; Key, Value: String;
RegDataType: TRegDataType; Data: variant);
function FieldTypeToString(AFieldType: TFieldType): String;
function StringToFieldType(Str: String): TFieldType;
const
// Sets UnixStartDate to TDateTime of 01/01/1970
// UnixStartDate: TDateTime = 25569.0;
DELPHI_DATE_01_01_1970 = 25569;
var
myGlobalFormatSettings : TFormatSettings;
implementation
function ValidateID(ID: String): Boolean;
var
i1, i2, i3, i4, i5: Integer;
s: String;
begin
ValidateID := True;
If Length(ID) = 13 then
begin
try
i1 := StrtoInt(copy(ID,1,1)) + StrtoInt(copy(ID,3,1)) + StrtoInt(copy(ID,5,1)) + StrtoInt(copy(ID,7,1)) + StrtoInt(copy(ID,9,1)) + StrtoInt(copy(ID,11,1));
i2 := StrtoInt(copy(ID,2,1) + copy(ID,4,1) + copy(ID,6,1) + copy(ID,8,1) + copy(ID,10,1) + copy(ID,12,1)) * 2;
If Length(InttoStr(i2)) = 5 then
s := '0' + InttoStr(i2)
else
begin
If Length(InttoStr(i2)) = 4 then
s := '00' + InttoStr(i2)
else
begin
If Length(InttoStr(i2)) = 3 then
s := '000' + InttoStr(i2)
else
s := InttoStr(i2);
end;
end;
If Length(s) = 6 then
i3 := StrtoInt(copy(s,1,1)) + StrtoInt(copy(s,2,1)) + StrtoInt(copy(s,3,1)) + StrtoInt(copy(s,4,1)) + StrtoInt(copy(s,5,1)) + StrtoInt(copy(s,6,1))
else
i3 := StrtoInt(copy(s,1,1)) + StrtoInt(copy(s,2,1)) + StrtoInt(copy(s,3,1)) + StrtoInt(copy(s,4,1)) + StrtoInt(copy(s,5,1)) + StrtoInt(copy(s,6,1)) + StrtoInt(copy(s,7,1));
i4 := i1 + i3;
i5 := 10 - strtoint(copy(InttoStr(i4),2,1));
If i5 > 9 then
i5 := 0;
If copy(ID,13,1) <> InttoStr(i5) then
ValidateID := False;
except
ValidateID := False;
end;
end
else
ValidateID := False;
end;
function WorkoutAge(StoreDate: TDateTime): Integer;
begin
try
WorkoutAge := YearsBetween(Today,StoreDate);
except
WorkoutAge := 0;
end;
end;
function DatetoIntDate(d: TDateTime): integer;
var
// yy, mm, dd: String;
s: String;
begin
// If ShortDateFormat = 'dd/MM/yyyy' then
// begin
// dd := copy(datetostr(d),1,2);
// mm := copy(datetostr(d),4,2);
// yy := copy(datetostr(d),7,4);
// end
// else
// begin
// If ShortDateFormat = 'dd/MM/yy' then
// begin
// dd := copy(datetostr(d),1,2);
// mm := copy(datetostr(d),4,2);
// yy := copy(datetostr(d),7,2);
// Insert('20',yy,1);
// end
// else
// begin
// if ShortDateFormat = 'MM/dd/yyyy' then
// begin
// mm := copy(datetostr(d),1,2);
// dd := copy(datetostr(d),4,2);
// yy := copy(datetostr(d),7,4);
// end
// else
// begin
// yy := copy(datetostr(d),1,4);
// mm := copy(datetostr(d),6,2);
// dd := copy(datetostr(d),9,2);
// end;
// end;
// end;
// s := yy + mm + dd;
DateTimeToString(s, 'yyyymmdd', d);;
DatetoIntDate := strtoint(s);
end;
function IntDatetoString(i: integer): String;
var
yy, mm, dd: String;
d: TDatetime;
begin
if i = 0 then
IntDatetostring := ''
else
begin
yy := copy(inttostr(i),1,4);
mm := copy(inttostr(i),5,2);
dd := copy(inttostr(i),7,2);
try
d := EncodeDate(strtoint(yy), strtoint(mm), strtoint(dd));
except
begin
IntDatetoString := '';
exit;
end;
end;
IntDatetoString := datetostr(d);
// If ShortDateFormat = 'dd/MM/yyyy' then
// begin
// IntDatetostring := dd + '/' + mm + '/' + yy;
// end
// else
// begin
// If ShortDateFormat = 'dd/MM/yy' then
// begin
// delete(yy,1,2);
// IntDatetostring := dd + '/' + mm + '/' + yy;
// end
// else
// begin
// IntDatetostring := yy + '/' + mm + '/' + dd;
// end;
// end;
// If IsValidDate(StrtoInt(yy),StrtoInt(mm),StrtoInt(dd)) = False then
// IntDatetoString := '';
end;
end;
//Encrypt
function Encrypt(tr: String): String;
var
i: Integer;
Temp: String;
begin
for i := 1 to Length(tr) do
begin
Temp := Temp + Chr(Ord(tr[i]) + 3);
end;
Encrypt := Temp;
end;
//Decrypt
function Decrypt(pr: String): String;
var
i: Integer;
Temp: String;
begin
for i := 1 to Length(pr) do
begin
Temp := Temp + Chr(Ord(pr[i]) - 3);
end;
Decrypt := Temp;
end;
function TrimSpaces(stemp: String): String;
const Remove = [' ', #13, #10];
var
i: integer;
begin
result := '';
for i := 1 to length(stemp) do begin
if not (stemp[i] in remove) then
result := result+stemp[i];
end;
end;
function TrimDash(stemp: AnsiString): AnsiString;
const Remove = ['-', '/', #13, #10];
var
i: integer;
begin
result := '';
for i := 1 to length(stemp) do begin
if not (stemp[i] in remove) then
result := result+stemp[i];
end;
end;
function MyRoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;
var
LFactor: Double;
begin
LFactor := IntPower(10, ADigit);
Result := Round((AValue + 0.001) / LFactor) * LFactor;
end;
function ValidEmail(email: AnsiString): boolean;
// Returns True if the email address is valid
// Author: Ernesto D'Spirito
const
// Valid characters in an "atom"
atom_chars = [#33..#255] - ['(', ')', '<', '>', '@', ',', ';', ':',
'\', '/', '"', '.', '[', ']', #127];
// Valid characters in a "quoted-string"
quoted_string_chars = [#0..#255] - ['"', #13, '\'];
// Valid characters in a subdomain
letters = ['A'..'Z', 'a'..'z'];
letters_digits = ['0'..'9', 'A'..'Z', 'a'..'z'];
subdomain_chars = ['-', '0'..'9', 'A'..'Z', 'a'..'z'];
type
States = (STATE_BEGIN, STATE_ATOM, STATE_QTEXT, STATE_QCHAR,
STATE_QUOTE, STATE_LOCAL_PERIOD, STATE_EXPECTING_SUBDOMAIN,
STATE_SUBDOMAIN, STATE_HYPHEN);
var
State: States;
i, n, subdomains: integer;
c: ansichar;
begin
State := STATE_BEGIN;
n := Length(email);
i := 1;
subdomains := 1;
while (i <= n) do begin
c := email[i];
case State of
STATE_BEGIN:
if c in atom_chars then
State := STATE_ATOM
else if c = '"' then
State := STATE_QTEXT
else
break;
STATE_ATOM:
if c = '@' then
State := STATE_EXPECTING_SUBDOMAIN
else if c = '.' then
State := STATE_LOCAL_PERIOD
else if not (c in atom_chars) then
break;
STATE_QTEXT:
if c = '\' then
State := STATE_QCHAR
else if c = '"' then
State := STATE_QUOTE
else if not (c in quoted_string_chars) then
break;
STATE_QCHAR:
State := STATE_QTEXT;
STATE_QUOTE:
if c = '@' then
State := STATE_EXPECTING_SUBDOMAIN
else if c = '.' then
State := STATE_LOCAL_PERIOD
else
break;
STATE_LOCAL_PERIOD:
if c in atom_chars then
State := STATE_ATOM
else if c = '"' then
State := STATE_QTEXT
else
break;
STATE_EXPECTING_SUBDOMAIN:
if c in letters then
State := STATE_SUBDOMAIN
else
break;
STATE_SUBDOMAIN:
if c = '.' then begin
inc(subdomains);
State := STATE_EXPECTING_SUBDOMAIN
end else if c = '-' then
State := STATE_HYPHEN
else if not (c in letters_digits) then
break;
STATE_HYPHEN:
if c in letters_digits then
State := STATE_SUBDOMAIN
else if c <> '-' then
break;
end;
inc(i);
end;
if i <= n then
Result := False
else
Result := (State = STATE_SUBDOMAIN) and (subdomains >= 2);
end;
function FixMySQLString(stemp: String): String;
const SpecialChar = ['%','&', '\',#39,'"','/'];
var
i: integer;
Skiploop:Boolean;
begin
result := '';
skiploop := false;
for i := 1 to length(stemp) do
begin
if skiploop = true then
begin
skiploop := false;
continue;
end;
// showmessage(stemp[i]);
if not (stemp[i] in SpecialChar) then
begin
result := result+stemp[i];
end
else
begin
if stemp[i] = #39 then
result := result+#39+stemp[i]
else
result := result+'\'+stemp[i];
end;
end;
end;
function FixSQLString(stemp: String): String;
const SpecialChar = ['%', '\',#39];
var
i: integer;
Skiploop:Boolean;
begin
result := '';
skiploop := false;
for i := 1 to length(stemp) do
begin
if skiploop = true then
begin
skiploop := false;
continue;
end;
// showmessage(stemp[i]);
if not (stemp[i] in SpecialChar) then
begin
result := result+stemp[i];
end
else
begin
if stemp[i] = #39 then
result := result+#39+stemp[i]
else
result := result+'\'+stemp[i];
end;
end;
end;
function ChangeEnterforSMS(stemp: AnsiString): AnsiString;
const Remove = [#13, #10];
var
i: integer;
begin
result := '';
for i := 1 to length(stemp) do begin
if not (stemp[i] in remove) then
result := result+stemp[i]
else
if stemp[i] = #13 then
result := result + '|';
end;
end;
function UrlEncode(const DecodedStr: AnsiString; Pluses: Boolean): AnsiString;
var
I: Integer;
begin
Result := '';
if Length(DecodedStr) > 0 then
for I := 1 to Length(DecodedStr) do
begin
if not (DecodedStr[I] in ['0'..'9', 'a'..'z',
'A'..'Z', ' ']) then
Result := Result + '%' + IntToHex(Ord(DecodedStr[I]), 2)
else if not (DecodedStr[I] = ' ') then
Result := Result + DecodedStr[I]
else
begin
if not Pluses then
Result := Result + '%20'
else
Result := Result + '+';
end;
end;
end;
function UrlDecode(const EncodedStr: AnsiString): AnsiString;
var
I: Integer;
begin
Result := '';
if Length(EncodedStr) > 0 then
begin
I := 1;
while I <= Length(EncodedStr) do
begin
if EncodedStr[I] = '%' then
begin
Result := Result + Chr(HexToInt(EncodedStr[I+1]
+ EncodedStr[I+2]));
I := Succ(Succ(I));
end
else if EncodedStr[I] = '+' then
Result := Result + ' '
else
Result := Result + EncodedStr[I];
I := Succ(I);
end;
end;
end;
function HexToInt(HexStr: AnsiString): Int64;
var RetVar : Int64;
i : byte;
begin
HexStr := UpperCase(HexStr);
if HexStr[length(HexStr)] = 'H' then
Delete(HexStr,length(HexStr),1);
RetVar := 0;
for i := 1 to length(HexStr) do begin
RetVar := RetVar shl 4;
if HexStr[i] in ['0'..'9'] then
RetVar := RetVar + (byte(HexStr[i]) - 48)
else
if HexStr[i] in ['A'..'F'] then
RetVar := RetVar + (byte(HexStr[i]) - 55)
else begin
Retvar := 0;
break;
end;
end;
Result := RetVar;
end;
function filenamespescharfix(stemp:string): AnsiString;
const SpecialChar = ['&','\','''','"','/',':'];
var
i: integer;
begin
result := '';
for i := 1 to length(stemp) do
begin
if not (stemp[i] in SpecialChar) then
result := result+stemp[i]
else
result := result + '_';
end;
end;
function GetSpecialFolder(const CSIDL : integer) : String;
var RecPath : PChar;
begin
{$IFDEF Windows}
RecPath := StrAlloc(MAX_PATH);
try
FillChar(RecPath^,MAX_PATH,0);
if SHGetSpecialFolderPath(0,RecPath,CSIDL,false) then begin
result := RecPath;
end else result := '';
finally
StrDispose(RecPath);
end;
{$ENDIF}
end;
function AddCR(stemp: AnsiString): AnsiString;
const FindStr = [#10];
var
i: integer;
begin
result := '';
for i := 1 to length(stemp) do begin
if not (stemp[i] in FindStr) then
result := result+stemp[i]
else
result := result+#13+stemp[i];
end;
end;
function RemoveLF(stemp: String): String;
const FindStr = [#10];
var
i: integer;
begin
result := '';
for i := 1 to length(stemp) do begin
if not (stemp[i] in FindStr) then
result := result+stemp[i];
end;
end;
function RemoveCRLF(stemp: String): String;
const FindStr = [#10,#13];
var
i: integer;
begin
result := '';
for i := 1 to length(stemp) do begin
if not (stemp[i] in FindStr) then
result := result+stemp[i]
else
result := result+' ';
end;
end;
function IsInteger(S: AnsiString): Boolean;
begin
try
Result := True;
StrToInt(copy(S,1,9));
except on E: EConvertError do
Result := False;
end;
end;
function TestSwitchString(SwitchString,TestValue: AnsiString): AnsiString;
var
i,I2,pos1,pos2: integer;
begin
result := 'False';
for i := 1 to length(SwitchString) do
begin
if copy(SwitchString,i,length(TestValue)) = TestValue then
begin
result := 'True';
if copy(SwitchString,i+length(TestValue),1) = '(' then
begin
for I2 := i to length(SwitchString) do
begin
if copy(SwitchString,I2,1) = '(' then
pos1 := I2;
if copy(SwitchString,I2,1) = ')' then
begin
pos2 := I2;
break;
end;
end;
result := copy(SwitchString,pos1 + 1,(pos2 - pos1)-1);
end;
end;
end;
end;
function UpdateSwitchString(SwitchString,TestValue,Value: AnsiString): AnsiString;
var
i,I2,pos1,pos2: integer;
s: AnsiString;
begin
result := 'False';
s := SwitchString;
for i := 1 to length(SwitchString) do
begin
if copy(SwitchString,i,length(TestValue)) = TestValue then
begin
result := 'True';
if (copy(SwitchString,i+length(TestValue),1) = '(') or (Value <> '') then
begin
pos1 := 0;
pos2 := 0;
for I2 := i + 1 to length(SwitchString) do
begin
if copy(SwitchString,I2,1) = '(' then
pos1 := I2;
if copy(SwitchString,I2,1) = ')' then
begin
pos2 := I2;
break;
end;
if copy(SwitchString,I2,1) = '-' then
begin
if (pos1 = 0) and (pos2 = 0) and (Value <> '') then
begin
insert('(' + Value + ')',s,i+length(TestValue));
end;
break;
end;
end;
if (pos1 <> 0) or (pos2 <> 0) then
begin
delete(s,pos1+1,(pos2-pos1)-1);
insert(Value,s,pos1+1);
end;
// result := copy(SwitchString,pos1 + 1,(pos2 - pos1)-1);
end;
end;
end;
if result = 'False' then
begin
if Value <> '' then
insert(TestValue+'('+Value+')',s,length(SwitchString)+1)
else
insert(TestValue,s,length(SwitchString));
end;
result := s;
end;
function CreateEasyPayNumber(GroupNo,MemNo,BranchNo:Integer):String;
var
s,s2,s3,s4:String;
i,i2,i3:Integer;
begin
s := '2540' + Formatfloat('0000',GroupNo) + Formatfloat('00',BranchNo) + Formatfloat('00000000',MemNo);
i2 := 1;
s2 := '';
for I := 0 to Length(s) - 1 do
begin
if i2 = 2 then
begin
i3 := StrtoInt(copy(s,I+1,1)) * 2;
// showmessage('2=item' + copy(s,I+1,1));
if i3 > 9 then
i3 := i3 - 9;
s2 := s2 + InttoStr(i3);
// showmessage('2=' + s2);
i2 := 1;
end
else
begin
s2 := s2 + copy(s,I+1,1);
// showmessage('1=' + s2);
i2 := 2;
end;
end;
i3 := 0;
for I := 0 to Length(s2) - 1 do
begin
i3 := i3 + StrtoInt(copy(s2,I+1,1));
end;
i2 := i3 + 10;
s3 := InttoStr(i2);
s4 := '';
for I := 0 to Length(s3) - 1 do
begin
if I = Length(s3) - 1 then
s4 := s4 + '0'
else
s4 := s4 + copy(s3,I,1);
end;
// showmessage(s4 + '=' + InttoStr(i3));
i2 := StrtoInt(s4);
s3 := InttoStr(i2 - i3);
if s3 = '10' then
s3 := '0';
CreateEasyPayNumber := '9' + s + s3;
end;
function replacedoublequotewithsinglequote(stemp: AnsiString): AnsiString;
const Remove = ['"', #13, #10];
var
i: integer;
begin
result := '';
for i := 1 to length(stemp) do begin
if not (stemp[i] in remove) then
result := result+stemp[i]
else
begin
if stemp[i] = '"' then
result := result+'''';
end;
end;
end;
//function CreateGuid: string;
//var
// ID: TGUID;
//begin
// Result := '';
// {$IFDEF Windows}
// if CoCreateGuid(ID) = S_OK then
// Result := GUIDToString(ID);
// {$ENDIF}
//
//end;
Function ConvertFieldtoSQLString(FieldStore: TField):String;
var
temp:String;
begin
temp := '';
case TFieldType(Ord(FieldStore.DataType)) of
ftString, ftGuid, ftWideString, ftMemo:
begin
if (FieldStore.AsString = '') or (FieldStore.IsNull) or (LowerCase(FieldStore.AsString) = 'null') then
temp := 'null' //Put a default string
else
begin
temp := '''' + trim(FixSQLString(FieldStore.AsString)) + '''';
end;
end;
ftInteger, ftWord, ftSmallint, ftAutoinc, ftLargeint:
begin
if FieldStore.AsInteger > 0 then
temp := IntToStr(FieldStore.AsInteger)
else
begin
if FieldStore.IsNull then
temp := 'null'
else
temp := '0';
end;
end;
ftFloat, ftCurrency, ftBCD:
begin
// Showmessage(FloattoStr(FieldStore.AsFloat) + '==' + FloatToLocaleIndependantString(FieldStore.AsFloat));
if FieldStore.AsFloat <> 0 then
temp := FloatToLocaleIndependantString(FieldStore.AsFloat)
else
begin
if FieldStore.IsNull then
temp := 'null'
else
temp := '0';
end;
end;
ftBoolean:
begin
if not FieldStore.isnull then
begin
if FieldStore.Value then
temp:= '1'
else
temp:= '0';
end
else
temp := 'null';
end;
ftDate:
begin
if (not FieldStore.IsNull) or
(Length(Trim(FieldStore.AsString)) > 0) then
temp := 'convert(datetime, ''' + FormatDateTime('yyyy/MM/dd',
FieldStore.AsDateTime) + ''',111)'
else
begin
if FieldStore.IsNull then
temp := 'null'
else
temp:= 'convert(datetime, ''1900/01/01'',111)'; //put some valid default date
end;
end;
ftDateTime, ftTimeStamp:
begin
if (not FieldStore.IsNull) or
(Length(Trim(FieldStore.AsString)) > 0) then
temp := 'convert(datetime, ''' + FormatDateTime('yyyy-MM-dd hh:mm:ss',
FieldStore.AsDateTime) + ''',120)'
else
begin
if FieldStore.IsNull then
temp := 'null'
else
temp := 'convert(datetime, ''1900/01/01 00:00:00'',120)';//Put some valid default date and time
end;
end;
ftTime:
begin
if (not FieldStore.IsNull) or
(Length(Trim(FieldStore.AsString)) > 0) then
temp := 'convert(datetime, ''' + FormatDateTime('hh:mm:ss',
FieldStore.AsDateTime) + ''',8)'
else
begin
if FieldStore.IsNull then
temp := 'null'
else
temp := 'convert(datetime, ''00:00:00'',8)'; //Put some valid default time
end;
end;
else
showmessage('Field not found' + FieldTypeToString(FieldStore.DataType));
end;
ConvertFieldtoSQLString := temp;
end;
procedure Split
(const Delimiter: Char;
Input: string;
const Strings: TStrings) ;
begin
Assert(Assigned(Strings)) ;
Strings.Clear;
Strings.Delimiter := Delimiter;
Strings.StrictDelimiter := True;
Strings.DelimitedText := Input;
end;
Function CalculateBMI(LenghtValue,WeightValue: Extended; WeightUnit, LenghtUnit:String): Extended;
var
WeightConv,LenghtConv:Extended;
begin
if (LenghtValue <> 0) and (WeightValue <> 0) then
begin
if WeightUnit = 'lbs' then
WeightConv := WeightValue / 2.2046
else
WeightConv := WeightValue;
if LenghtUnit = 'feet' then
LenghtConv := (LenghtValue * 30.48) / 100
else
LenghtConv := LenghtValue;
CalculateBMI := WeightConv / (LenghtConv * LenghtConv);
end
else
CalculateBMI := 0;
end;
function FloatToLocaleIndependantString(const v: Extended): string;
//var
// oldDecimalSeparator: Char;
begin
{$IFDEF Windows}
GetLocaleFormatSettings(0,myGlobalFormatSettings);
myGlobalFormatSettings.DecimalSeparator := '.';
// oldDecimalSeparator := SysUtils.DecimalSeparator;
// SysUtils.DecimalSeparator := '.'; //Windows formatting functions assume single decimal point
try
Result := FloatToStrF(v, ffFixed,
18, //Precision: "should be 18 or less for values of type Extended"
9, //Scale 0..18. Sure...9 digits before decimal mark, 9 digits after. Why not
myGlobalFormatSettings
);
finally
// SysUtils.DecimalSeparator := oldDecimalSeparator;
end;
{$ELSE}
try
Result := FloatToStrF(v, ffFixed,
18,
9
);
finally
end;
{$ENDIF}
end;
function GetRegistryData(RootKey: HKEY; Key, Value: String): variant;
var
Reg: TRegistry;
RegDataType: TRegDataType;
DataSize, Len: integer;
s: String;
label cantread;
begin
{$IFDEF Windows}
Reg := nil;
try
Reg := TRegistry.Create(KEY_READ);
// Reg := TRegistry.Create(KEY_QUERY_VALUE);
Reg.RootKey := RootKey;
if Reg.OpenKeyReadOnly(Key) then begin
try
RegDataType := Reg.GetDataType(Value);
if (RegDataType = rdString) or
(RegDataType = rdExpandString) then
Result := Reg.ReadString(Value)
else if RegDataType = rdInteger then
Result := Reg.ReadInteger(Value)
else if RegDataType = rdBinary then begin
DataSize := Reg.GetDataSize(Value);
if DataSize = -1 then goto cantread;
SetLength(s, DataSize);
Len := Reg.ReadBinaryData(Value, PChar(s)^, DataSize);
if Len <> DataSize then goto cantread;
Result := s;
end else
cantread:
raise Exception.Create('Problem');
except
s := ''; // Deallocates memory if allocated
Reg.CloseKey;
raise;
end;
Reg.CloseKey;
end else
raise Exception.Create('Problem');
except
Reg.Free;
raise;
end;
Reg.Free;
{$ENDIF}
{$IFDEF Linux}
Result := '';
{$ENDIF}
end;
procedure SetRegistryData(RootKey: HKEY; Key, Value: String;
RegDataType: TRegDataType; Data: variant);
var
Reg: TRegistry;
s: String;
begin
Reg := TRegistry.Create(KEY_WRITE);
try
Reg.RootKey := RootKey;
if Reg.OpenKey(Key, True) then begin
try
if RegDataType = rdUnknown then
RegDataType := Reg.GetDataType(Value);
if RegDataType = rdString then
Reg.WriteString(Value, Data)
else if RegDataType = rdExpandString then
Reg.WriteExpandString(Value, Data)
else if RegDataType = rdInteger then
Reg.WriteInteger(Value, Data)
else if RegDataType = rdBinary then begin
s := Data;
Reg.WriteBinaryData(Value, PChar(s)^, Length(s));