-
Notifications
You must be signed in to change notification settings - Fork 24
/
define_types.pas
executable file
·1392 lines (1302 loc) · 36.3 KB
/
define_types.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 define_types;
{$D-,L-,O+,Q-,R-,Y-,S-}
interface
{$Include opts.inc}
{$H+}
{$DEFINE GUI}
uses
{$IFDEF FPC}
{$IFDEF GUI}LCLType, lclintf, {$ENDIF}
{$ENDIF}
{$IFDEF UNIX} BaseUnix, {$ELSE} Windows,{$ENDIF}
SysUtils,classes,graphics,userdir,math,
{$IFDEF GUI} forms,dialogs,controls;{$ELSE} dialogsx;{$ENDIF}
const
kVers = 'v1.0.20181112';
NaN : double = 1/0;
kMagicDouble : double = -111666222;
kTxtFilter = 'Text (*.txt)|*.txt;*.csv|Comma Separated (*.csv)|*.csv';
kNIIFilter = 'Neuroimaging (*.nii)|*.hdr;*.nii;*.nii.gz;*.voi;*.HEAD;*.mgh;*.mgz;*.mha;*.mhd;*.nhdr;*.nrrd';
kAnyFilter = 'Anything (*)|*';
kAnaHdrFilter = 'Analyze Header (*.hdr)|*.hdr';
kImgPlusVOIFilter = 'Neuroimaging/VOI|*.hdr;*.nii;*.nii.gz;*.voi;*.HEAD;*.mgh;*.mgz;*.mha;*.mhd;*.nhdr;*.nrrd|NIfTI/Analyze Header (*.hdr;*.nii)|*.hdr;*.nii;*.nii.gz|Volume of interest (*.voi)|*.voi|Anything (*.*)|*.*';
kImgFilter = 'Neuroimaging|*.hdr;*.nii;*.nii.gz;*.HEAD;*.mgh;*.mgz;*.mha;*.mhd;*.nhdr;*.nrrd|Volume of interest (*.voi)|*.voi';
kHistoBins = 256;//numbers of bins for histogram/image balance
PixelCountMax = 32768;
kTab = chr(9);
kEsc = chr(27);
kCR = chr (13);
kBS = #8 ; // Backspace
kDel = #127 ; // Delete
UNIXeoln = chr(10);
kVOI8bit = 1;//May07 100;
{$IFDEF unix}
PathDelim = '/';
{$ELSE}
PathDelim = '\';
{$ENDIF}
type
{$IFDEF COREGL}
TRGBA = packed record //Next: analyze Format Header structure
R,G,B,A : byte;
end;
TPoint3f = packed record
X: single;
Y: single;
Z: single
end;
TInts = array of integer;
{$ENDIF}
TStrRA = Array of String;
TPSPlot = RECORD //peristimulus plot
TRSec,BinWidthSec: single;
nNegBins,nPosBins,SPMDefaultsStatsFmriT,SPMDefaultsStatsFmriT0: integer;
TextOutput,GraphOutput,
SliceTime,SavePSVol,BaselineCorrect,PctSignal,RemoveRegressorVariability,TemporalDeriv,PlotModel,Batch: boolean
end;
{$IFDEF Unix}
TRGBquad = PACKED RECORD
rgbBlue,rgbGreen,rgbRed,rgbreserved: byte;
END;
{$ENDIF}
TLUTb = array[0..255] of byte;
glRGBaQUAD = packed record
rgbRed: Byte;
rgbGreen: Byte;
rgbBlue: Byte;
rgbReserved: Byte;
end;
TUnitRect = record
L,T,R,B: single;
end;
TGLRGBQuad = glRGBaQUAD;// Windows RGBquad is BGRA, GL is RGBA
GLRGBQuadRA0 = array [0..0] of TGLRGBQuad;
GLRGBQuadp0 = ^GLRGBQuadRA0;
TLUT = array[0..255] of TGLRGBQuad;
kStr20 = string[20];
TCutout = RECORD
Lo : array [1..3] of integer;
Hi : array [1..3] of integer;
end;
pRGBQuadArray = ^TRGBQuad;
TRGBQuadeArray = ARRAY[0..PixelCountMax-1] OF TRGBQuad;
RGBQuadRA = array [1..1] of TGLRGBQuad;
RGBQuadp = ^RGBQuadRA;
RGBQuadRA0 = array [0..0] of TGLRGBQuad;
RGBQuadp0 = ^RGBQuadRA0;
int32 = LongInt;
uint32 = Cardinal;
int16 = SmallInt;
uint16 = Word;
int8 = ShortInt;
uint8 = Byte;
SingleRA0 = array [0..0] of Single;
Singlep0 = ^SingleRA0;
ByteRA0 = array [0..0] of byte;
Bytep0 = ^ByteRA0;
WordRA0 = array [0..0] of Word;
Wordp0 = ^WordRA0;
SmallIntRA0 = array [0..0] of SmallInt;
SMallIntp0 = ^SmallIntRA0;
LongIntRA0 = array [0..0] of LongInt;
LongIntp0 = ^LongIntRA0;
DWordRA = array [1..1] of DWord;
DWordp = ^DWordRA;
ByteRA = array [1..1] of byte;
Bytep = ^ByteRA;
WordRA = array [1..1] of Word;
Wordp = ^WordRA;
SmallIntRA = array [1..1] of SmallInt;
SMallIntp = ^SmallIntRA;
LongIntRA = array [1..1] of LongInt;
LongIntp = ^LongIntRA;
SingleRA = array [1..1] of Single;
Singlep = ^SingleRA;
SingleRARA = array [1..1] of Singlep;
SingleRAp = ^SingleRARA;
DoubleRA = array [1..1] of Double;
Doublep = ^DoubleRA;
DoubleRA0 = array [0..0] of Double;
Doublep0 = ^DoubleRA0;
HistoRA = array [0..kHistoBins] of longint;
HistoDoubleRA = array [0..kHistoBins] of double;
function specialsingle (var s:single): boolean; //check if 32-bit float is Not-A-Number, infinity, etc
function FSize (lFName: String): int64;
function FileExistsEX(Name: String): Boolean;
function ParseFileName (lFilewExt:String): string;
function ParseFileFinalDir (lFileName:String): string;
function ExtractFileDirWithPathDelim(lInFilename: string): string;
function PadStr (lValIn, lPadLenIn: integer): string;
function ChangeFileExtX( lFilename: string; lExt: string): string;
function swap4r4i (s:single): longint; //swap and convert: endian-swap and then typecast 32-bit float as 32-bit integer
function conv4r4i (s:single): longint; //convert: typecast 32-bit float as 32-bit integer
function swap4r4u (s:single): longword; //swap and convert: endian-swap and then typecast 32-bit float as 32-bit unsigned integer
function conv4r4u (s:single): longword; //convert: typecast 32-bit float as 32-bit unsigned integer
function swap8r(s : double):double; //endian-swap 64-bit float
procedure pswap4i(var s : LongInt); //procedure to endian-swap 32-bit integer
procedure pswap4r ( var s:single); //procedure to endian-swap 32-bit integer
function swap64r(s : double):double;
function specialdouble (d:double): boolean;
function RealToStr(lR: double {was extended}; lDec: integer): string;
function UpCaseExt(lFileName: string): string;
function ExtGZ (lFilename: string): boolean;
procedure swap4(var s : LongInt);
procedure Xswap4r ( var s:single);
function Bool2Char (lBool: boolean): char;
function Char2Bool (lChar: char): boolean;
function Log(X, Base: single): single;
function ChangeFilePostfix(lInName,lPostfix: string): string;
{$IFNDEF FPC}
function DiskFreeEx (DriveStr: String): Int64;
{$ELSE}
function DiskFreeEx (DriveStr: String): Int64;
{$ENDIF}
procedure SortSingle(var lLo,lHi: single);
//procedure SortInteger(var lLo,lHi: integer);
procedure CopyFileEX (lInName,lOutName: string);
procedure CopyFileEXoverwrite (lInName,lOutName: string);
procedure fx (a: double); overload; //fx used to help debugging - reports number values
procedure fx (a,b: double); overload;
procedure fx (a,b,c: double); overload;
procedure fx (a,b,c,d: double); overload;
procedure msg(Str: String);
function Swap2(s: smallint): smallint;
function ChangeFilePostfixExt (lInName,lPostfix,lExt: string): string;
procedure SortCutout (var lCutout : TCutout); //ensure Lo < Hi
function freeRam: Int64;
function OKMsg(lMsg: string): boolean; //shows dialog with OK/Cancel returns true if user presses OK
function DirExists (lFolderName: String): boolean;
function FilenameParts (lInName: string; var lPath,lName,lExt: string): boolean;
function AddIndexToFilename (lInName: string; lIndex: integer): string;
//function GzExt(lFileName: string): boolean;
function ChangeFilePrefixExt (lInName,lPrefix,lExt: string): string;
function ChangeFilePrefix(lInName,lPrefix: string): string;
function makesmallint (b0,b1: byte): smallint;
function makesingle( b0,b1,b2,b3: byte): single;
procedure SortInt (var lMin,lMax: integer); overload;
procedure SortInt (var lMin,lMax: int64); overload;
function Bound (lDefault,lMin,lMax: integer): integer;
function IsNiftiExt(lStr: string): boolean;
function IsVOIExt(lStr: string): boolean;
function StrToUnitRect (lS: string; out lU: TUnitRect): boolean;
function UnitRectToStr (lU: TUnitRect) : string;
function CreateUnitRect (L,T,R,B: single) : TUnitRect;
procedure SensibleUnitRect (var U: TUnitRect);
function StrToRGBA(lS: string; out lU: glRGBaQUAD): boolean;
function RGBAToStr (lU: glRGBaQUAD) : string;
function RGBA2TColor ( lRGBA: TGLRGBQuad): TColor;
procedure TColor2RGBA (lColor: TColor; out lRGBA: TGLRGBQuad);
procedure EnsureDirExists (var lFilename: string);
function FloatMaxVal (lA,lB,lC: single): single;
function FloatMinVal (lA,lB,lC: single): single;
function ResetIniDefaults : boolean;
procedure ShowDebug (lS: AnsiString);
function float2str(Avalue:double; ADigits:integer):string; //e.g x:single=2.6; floattostrf(x,8,4);
//function RGBA(lR,lG,lB,lA: byte): TRGBA;
function RGBA(lR,lG,lB,lA: byte): TGLRGBQuad;
implementation
(*function RGBA(lR,lG,lB,lA: byte): TRGBA;
//set red,green,blue and alpha of a Quad
begin
result.r := lR;
result.g := lG;
result.b := lB;
result.a := lA;
end;*)
function float2str(Avalue:double; ADigits:integer):string; //e.g x:single=2.6; floattostrf(x,8,4);
//http://stackoverflow.com/questions/5650051/how-to-keep-2-decimal-places-in-delphi
var v:double; p:integer; e:string;
begin
if abs(Avalue)<1 then
begin
result:=floatTostr(Avalue);
p:=pos('E',result);
if p>0 then
begin
e:=copy(result,p,length(result));
setlength(result,p-1);
v:=RoundTo(StrToFloat(result),-Adigits);
result:=FloatToStr(v)+e;
end else
result:=FloatToStr(RoundTo(Avalue,-Adigits));
end
else
result:=FloatToStr(RoundTo(Avalue,-Adigits));
end;
procedure ShowDebug (lS: AnsiString);
begin
{$IFDEF UNIX}
writeln(lS);
//showmessage(lS); //OVA
{$ELSE}
showmessage(lS);
{$ENDIF}
end;
function ResetIniDefaults : boolean;
const
(* {$IFDEF LINUX}
kKey = 'Right button';
{$ELSE}
kKey = 'Shift key';
{$ENDIF} *)
kKey = 'Shift or control key ';
var
lKey: boolean;
begin
result := false;
if (paramcount > 0) then exit;
{$IFDEF GUI}
//{$IFDEF LINUX}
lKey := ((GetKeyState(VK_CONTROL) AND 128)=128) or (ssShift in KeyDataToShiftState(vk_Shift)) or ((GetKeyState(VK_RBUTTON) And $80)<>0) or ((GetKeyState(VK_SHIFT) And $80)<>0);
//{$ELSE}
//lKey := (ssShift in KeyDataToShiftState(vk_Shift));
//{$ENDIF}
if not lKey then
exit;
{$IFDEF GUI}
case MessageDlg(kKey+' down during launch: do you want to reset the default preferences?', mtConfirmation,
[mbYes, mbNo], 0) of { produce the message dialog box }
idYes: result := true;
end; //case
{$ENDIF}
{$ENDIF}
end;
const
kStrSep = '|';
(*function ColorToStr (lU: TColor) : string;
//floatrect values 0..1 convert to byte 0..1
begin
lU.
result := Inttostr(lU.rgbRed)+ kStrSep+Inttostr(lU.rgbGreen)+ kStrSep+Inttostr(lU.rgbBlue)+ kStrSep+Inttostr(lU.rgbReserved);
end;*)
function FloatMaxVal (lA,lB,lC: single): single;
//returns largest of three values
begin
if (lA > lB) and (lA > lC) then
result := lA
else if lB > lC then
result := lB
else
result := lC;
end; //func FloatMaxVal
function FloatMinVal (lA,lB,lC: single): single;
//returns largest of three values
begin
if (lA < lB) and (lA < lC) then
result := lA
else if lB < lC then
result := lB
else
result := lC;
end; //func FloatMaxVal
procedure EnsureDirExists (var lFilename: string);
begin
if direxists (extractfiledir(lFilename)) then
exit;
lFilename := DesktopFolder + extractfilename(lFilename);
end;
function RGBA2TColor ( lRGBA: TGLRGBQuad): TColor;
begin
result := lRGBA.rgbRed + lRGBA.rgbGreen shl 8 +lRGBA.rgbBlue shl 16;
end;
procedure TColor2RGBA (lColor: TColor; out lRGBA: TGLRGBQuad);
begin
lRGBA.rgbRed := lColor and 255;
lRGBA.rgbGreen := (lColor shr 8) and 255;
lRGBA.rgbBlue := (lColor shr 16) and 255;
end;
function RGBA(lR,lG,lB,lA: byte): TGLRGBQuad;
//set red,green,blue and alpha of a Quad
begin
result.rgbRed := lR;
result.rgbGreen := lG;
result.rgbBlue := lB;
result.rgbreserved := lA;
end;
function RGBAToStr (lU: glRGBaQUAD) : string;
//floatrect values 0..1 convert to byte 0..1
begin
result := Inttostr(lU.rgbRed)+ kStrSep+Inttostr(lU.rgbGreen)+ kStrSep+Inttostr(lU.rgbBlue)+ kStrSep+Inttostr(lU.rgbReserved);
end;
function ByteBound (lV: integer): Byte;
begin
if lV < 0 then
result := 0
else if lV > 255 then
result := 255
else
result := lV;
end;
function StrToRGBA(lS: string; out lU: glRGBaQUAD): boolean;
var
lV: string;
lI: byte;
lLen,lP,lN: integer;
begin
result := false;
lLen := length(lS);
if lLen < 7 then //shortest possible: 1|1|1|1 or 0|0|0|0
exit;
//read values
lV := '';
lP := 1;
lN := 0;
while (lP <= lLen) do begin
if lS[lP] in ['0'..'9'] then
lV := lV + lS[lP];
if (lV <> '') and ((lP = lLen) or (not (lS[lP] in ['0'..'9']))) then begin
inc(lN);
lI := ByteBound(strtoint(lV));
case lN of
1: lU.rgbRed := lI;
2: lU.rgbGreen := lI;
3: lU.rgbBlue := lI;
4: lU.rgbReserved := lI;
end;
lV := '';
end;
inc(lP);
end;
if lN >= 4 then
result := true;
end;
function UnitBound (lS: single): single;
begin
if lS < 0 then
result := 0
else if lS > 1 then
result := 1
else
result := lS;
end;
function CreateUnitRect (L,T,R,B: single) : TUnitRect;
begin
result.L := UnitBound(L);
result.T := UnitBound(T);
result.R := UnitBound(R);
result.B := UnitBound(B);
end;
procedure SensibleUnitRect (var U: TUnitRect);
begin
U.L := UnitBound(U.L);
U.T := UnitBound(U.T);
U.R := UnitBound(U.R);
U.B := UnitBound(U.B);
//left should be lower value than right
SortSingle(U.L,U.R);
if U.L = U.R then begin
if U.R < 0.1 then
U.R := 0.1
else
U.L := U.R -0.1;
end;
//bottom should lower value than top
SortSingle(U.B,U.T);
if U.B = U.T then begin
if U.T < 0.1 then
U.T := 0.1
else
U.B := U.T -0.1;
end;
end;
function UnitToByteStr (lS: single): string;
begin
result := inttostr(round(255 * UnitBound(lS)));
end;
function UnitRectToStr (lU: TUnitRect) : string;
//floatrect values 0..1 convert to byte 0..1
begin
result := UnitToByteStr(lU.L)+ kStrSep+UnitToByteStr(lU.T)+ kStrSep+UnitToByteStr(lU.R)+ kStrSep+UnitToByteStr(lU.B);
end;
function StrToUnitRect (lS: string; out lU: TUnitRect): boolean;
var
lQ : glRGBaQUAD;
begin
result := false;
if not StrToRGBA(lS,lQ) then
exit;
lU.L := lQ.rgbRed / 255;
lU.T := lQ.rgbGreen / 255;
lU.R := lQ.rgbBlue / 255;
lU.B := lQ.rgbReserved / 255;
result := true;
end;
procedure msg(Str: String);
begin
Showmessage(Str);
end;
function AddIndexToFilename (lInName: string; lIndex: integer): string;
var lPath,lName,lExt: string;
begin
result := '';
if not FilenameParts (lInName, lPath,lName,lExt) then exit;
result := lPath+lName+inttostr(lIndex)+lExt;
end;
function Bound (lDefault,lMin,lMax: integer): integer;
begin
result := lDefault;
if result < lMin then
result := lMin;
if result > lMax then
result := lMax;
end;
function IsVOIExt(lStr: string): boolean;
var
lExt: string;
begin
result := false;
lExt := UpCaseExt(lStr);
if (lExt = '.VOI') then
result := true;
end;
function IsNiftiExt(lStr: string): boolean;
var
lExt: string;
begin
result := false;
lExt := UpCaseExt(lStr);
if (lExt = '.NII') or (lExt = '.NII.GZ') then
result := true;
if (lExt = '.HDR') and (FSize(ChangeFileExt(lStr,'.img'))> 0) then
result := true;
if (lExt = '.IMG') and (FSize(ChangeFileExt(lStr,'.hdr'))> 0) then
result := true;
end;
procedure SortInt (var lMin,lMax: int64); overload;
var
lSwap: int64;
begin
if lMin <= lMax then
exit;
lSwap := lMax;
lMax := lMin;
lMin := lSwap;
end;
procedure SortInt (var lMin,lMax: integer); overload;
var
lSwap: integer;
begin
if lMin <= lMax then
exit;
lSwap := lMax;
lMax := lMin;
lMin := lSwap;
end;
function makesmallint (b0,b1: byte): smallint;
type
swaptype = packed record
case byte of
0:(b0,b1 : byte); //word is 16 bit
1:(s:smallint);
end;
swaptypep = ^swaptype;
var
outguy:swaptype;
begin
outguy.b0 := b0;
outguy.b1 := b1;
result:=outguy.s;
end;//makesmallint
function makesingle( b0,b1,b2,b3: byte): single;
type
swaptype = packed record
case byte of
0:(b0,b1,b2,b3 : byte); //word is 16 bit
1:(long:longint);
end;
swaptypep = ^swaptype;
var
outguy:swaptype;
begin
//inguy := @s; //assign address of s to inguy
outguy.b0 := b0;
outguy.b1 := b1;
outguy.b2 := b2;
outguy.b3 := b3;
result:=outguy.long;
end;//swap4r4i
function ChangeFilePrefix(lInName,lPrefix: string): string;
var
lC,lLen,lPos: integer;
lStr: string;
begin
//result := changefileext(lInName,lExt);
result := lInName;
lLen := length (result);
if lLen < 1 then exit;
lPos := lLen;
while (lPos > 1) and (result[lPos] <> pathdelim) do
dec(lPos);
lStr := '';
for lC := 1 to lPos do
lStr := lStr+result[lC];
lStr := lStr+lPrefix;
if lPos < lLen then
for lC := (lPos+1) to lLen do
lStr := lStr+result[lC];
result := lStr;
end;
function ChangeFilePrefixExt (lInName,lPrefix,lExt: string): string;
var
lC,lLen,lPos: integer;
lStr: string;
begin
result := changefileext(lInName,lExt);
lLen := length (result);
if lLen < 1 then exit;
lPos := lLen;
while (lPos > 1) and (result[lPos] <> pathdelim) do
dec(lPos);
lStr := '';
for lC := 1 to lPos do
lStr := lStr+result[lC];
lStr := lStr+lPrefix;
if lPos < lLen then begin
lC := lPos+1;
while (lC <= lLen) and (result[lC] <> '.') do begin
lStr := lStr + result[lC];
inc(lC);
end;
end;
lStr := lStr + lExt;
result := lStr;
end;
function FilenameParts (lInName: string; var lPath,lName,lExt: string): boolean;
var
lLen,lPos,lExtPos,lPathPos: integer;
begin
result := false;
lPath := '';
lName := '';
lExt := '';
lLen := length(lInName);
if lLen < 1 then exit;
//next find final pathdelim
lPathPos := lLen;
while (lPathPos > 0) and (lInName[lPathPos] <> '\') and (lInName[lPathPos] <> '/') do
dec(lPathPos);
if (lInName[lPathPos] = '\') or (lInName[lPathPos] = '/') then begin
for lPos := 1 to lPathPos do
lPath := lPath + lInName[lPos];
end;
// else
// dec(lPathPos);
inc(lPathPos);
//next find first ext
lExtPos := lPathPos;
while (lExtPos <= lLen) and (lInName[lExtPos] <> '.') do
inc(lExtPos);
if (lInName[lExtPos] = '.') then begin
for lPos := lExtPos to lLen do
lExt := lExt + lInName[lPos];
end;
// else
// inc(lExtPos);
dec(lExtPos);
//next extract filename
//fx(lPathPos,lExtPos);
if (lPathPos <= lExtPos) then
for lPos := lPathPos to lExtPos do
lName := lName + lInName[lPos];
result := true;
end;
function OKMsg(lMsg: string): boolean; //shows dialog with OK/Cancel returns true if user presses OK
begin
result := false;
{$IFDEF GUI}
case MessageDlg(lMsg, mtConfirmation,
[mbYes, mbCancel], 0) of
mrCancel: exit;
end; //case
{$ELSE}
case MsgDlg(lMsg, mtConfirmation,
[mbYes, mbCancel], 0) of
mrCancel: exit;
end; //case
{$ENDIF}
result := true;
end;
(*function DirExists (lDir: String): boolean;
var lSearchRec: TSearchRec;
begin
FindFirst(lDir, faAnyFile, lSearchRec);
if (faDirectory and lSearchRec.attr) = faDirectory then
DirExists := true
else
DirExists := false;
FindClose(lSearchRec);{}
end;*)
function DirExists (lFolderName: string): boolean;
{$IFDEF oldFPC} //bug now fixed
var
lSearchRec: TSearchRec;
begin
result := false;
if fileexists(lFoldername) then //File not folder
exit;
Filemode := 0; //readonly
if FindFirst(lFolderName, faDirectory, lSearchRec) = 0 then begin
result := true;
FindClose(lSearchRec);
end else
result := false; //some files found
Filemode := 2;
{$ELSE}
begin
result := DirectoryExists(lFolderName);
{$ENDIF}
end;
function freeRam: Int64;
{$IFDEF UNIX}
begin
result := maxint;
end;
{$ELSE}
var
memory:TMemoryStatus;
begin
memory.dwLength:=sizeof(memory);
GlobalMemoryStatus(memory);
result := memory.dwavailPhys;
//result := 1024;
end;
{$ENDIF}
procedure SortCutout (var lCutout : TCutout); //ensure Lo < Hi
var lInc,lSwap: integer;
begin
for lInc := 1 to 3 do
if lCutout.Lo[lInc] > lCutout.Hi[lInc] then begin
lSwap := lCutout.Lo[lInc];
lCutout.Lo[lInc] := lCutout.Hi[lInc];
lCutout.Hi[lInc] := lSwap;
end;
end;
function ChangeFilePostfix(lInName,lPostfix: string): string;
var
lPath,lName,lExt: string;
begin
FilenameParts (lInName, lPath,lName,lExt);
result := lPath+lName+lPostFix+lExt;
//showmessage(result);
end;
function ChangeFilePostfixExt (lInName,lPostfix,lExt: string): string;
var
lPath,lName,lExtIn: string;
begin
FilenameParts (lInName, lPath,lName,lExtIn);
result := lPath+lName+lPostFix+lExt;
//showmessage(result);
end;
(*var
lC,lLen,lPos: integer;
lStr: string;
begin
result := changefileext(lInName,lExt);
lLen := length (result);
if lLen < 1 then exit;
lPos := lLen;
while (lPos > 1) and (result[lPos] <> pathdelim) and (result[lPos] <> '.') do
dec(lPos);
if result[lPos] = '.' then
dec(lPos);
lStr := '';
for lC := 1 to lPos do
lStr := lStr+result[lC];
lStr := lStr+lPostfix;
if lPos < lLen then
for lC := (lPos+1) to lLen do
lStr := lStr+result[lC];
result := lStr;
end; *)
(*procedure ApplySaveDlgFilter (lSaveDlg: TSaveDialog);
var
lLen,lPos,lPipes,lPipesReq: integer;
lExt: string;
begin
lPipesReq := (lSaveDlg.FilterIndex * 2)-1;
if lPipesReq < 1 then exit;
lLen := length(lSaveDlg.Filter);
lPos := 1;
lPipes := 0;
while (lPos < lLen) and (lPipes < lPipesReq) do begin
if lSaveDlg.Filter[lPos] = '|' then
inc(lPipes);
inc(lPos);
end;
if (lPos >= lLen) or (lPipes < lPipesReq) then
exit;
lExt := '';
while (lPos <= lLen) and (lSaveDlg.Filter[lPos] <> '|') do begin
if lSaveDlg.Filter[lPos] <> '*' then
lExt := lExt + lSaveDlg.Filter[lPos];
inc(lPos);
end;
if lExt <> '' then
lSaveDlg.Filename := ChangeFileExt(lSaveDlg.Filename,lExt);
end; *)
(*function DefaultsDir (lSubFolder: string): string;
//for Linux: DefaultsDir is ~/appname/SubFolder/, e.g. /home/username/mricron/subfolder/
//for Windows: DefaultsDir is in the location of the executable, e.g. c:\program files\mricron\subfolder\
//Note: Final character is pathdelim
var
lBaseDir: string;
begin
{$IFDEF Unix}
lBaseDir := GetEnvironmentVariable ('HOME')+pathdelim+'.' +ParseFileName(ExtractFilename(paramstr(0) ) );
if not DirectoryExists(lBaseDir) then begin
{$I-}
MkDir(lBaseDir);
if IOResult <> 0 then begin
showmessage('Unble to create new folder '+lBaseDir);
end;
{$I+}
end;
lBaseDir := lBaseDir+pathdelim;
{$ELSE}
lBaseDir := extractfiledir(paramstr(0))+pathdelim;
{$ENDIF}
//if not DirectoryExists(extractfiledir(lBaseDir)) then
//mkDir(extractfiledir(lBaseDir));
if lSubFolder <> '' then begin
lBaseDir := lBaseDir + lSubFolder;
if not DirectoryExists(lBaseDir) then begin
{$I-}
MkDir(lBaseDir);
if IOResult <> 0 then begin
showmessage('Unable to create new folder '+lBaseDir);
end;
{$I+}
end;
result := lBaseDir + pathdelim;
end else
result := lBaseDir;
end; *)
function Swap2(s : SmallInt): smallint;
type
swaptype = packed record
case byte of
0:(Word1 : word); //word is 16 bit
1:(Small1: SmallInt);
end;
swaptypep = ^swaptype;
var
inguy:swaptypep;
outguy:swaptype;
begin
inguy := @s; //assign address of s to inguy
outguy.Word1 := swap(inguy^.Word1);
result :=outguy.Small1;
end;
procedure fx (a: double); overload; //fx used to help debugging - reports number values
begin
{$IFDEF GUI}
showmessage(floattostr(a));
{$ELSE}
msg(floattostr(a));
{$ENDIF}
end;
procedure fx (a,b: double); overload; //fx used to help debugging - reports number values
begin
{$IFDEF GUI}
showmessage(floattostr(a)+'x'+floattostr(b));
{$ELSE}
msg(floattostr(a)+'x'+floattostr(b));
{$ENDIF}
end;
procedure fx (a,b,c: double); overload; //fx used to help debugging - reports number values
begin
{$IFDEF GUI}
showmessage(floattostr(a)+'x'+floattostr(b)+'x'+floattostr(c));
{$ELSE}
msg(floattostr(a)+'x'+floattostr(b)+'x'+floattostr(c));
{$ENDIF}
end;
procedure fx (a,b,c,d: double); overload; //fx used to help debugging - reports number values
begin
{$IFDEF GUI}
showmessage(floattostr(a)+'x'+floattostr(b)+'x'+floattostr(c)+'x'+floattostr(d));
{$ELSE}
msg(floattostr(a)+'x'+floattostr(b)+'x'+floattostr(c)+'x'+floattostr(d));
{$ENDIF}
end;
procedure CopyFileEXoverwrite (lInName,lOutName: string);
var lFSize: Int64 ;
lBuff: bytep0;
lFData: file;
begin
lFSize := FSize(lInName);
if (lFSize < 1) then exit;
assignfile(lFdata,lInName);
filemode := 0;
reset(lFdata,lFSize{1});
GetMem( lBuff, lFSize);
BlockRead(lFdata, lBuff^, 1{lFSize});
closefile(lFdata);
assignfile(lFdata,lOutName);
filemode := 2;
Rewrite(lFdata,lFSize);
BlockWrite(lFdata,lBuff^, 1 {, NumWritten});
closefile(lFdata);
freemem(lBuff);
end;
procedure CopyFileEX (lInName,lOutName: string);
var lFSize: Int64;
begin
lFSize := FSize(lInName);
if (lFSize < 1) or (fileexistsEX(lOutName)) then exit;
CopyFileEXoverwrite (lInName,lOutName);
end;
(*procedure SortInteger(var lLo,lHi: integer);
var lSwap: integer;
begin
if lLo > lHi then begin
lSwap := lLo;
lLo := lHi;
lHi := lSwap;
end; //if Lo>Hi
end; //proc SortInteger*)
procedure SortSingle(var lLo,lHi: single);
var lSwap: single;
begin
if lLo > lHi then begin
lSwap := lLo;
lLo := lHi;
lHi := lSwap;
end; //if Lo>Hi
end; //proc SortSingle
{$IFDEF FPC}
{$IFDEF UNIX} //FPC and Unix
function DiskFreeEx (DriveStr: String): Int64;
begin
result := maxint;
end;
{$ELSE} //FPC and Windows
function DiskFreeEx (DriveStr: String): Int64;
var
lOutDisk: Integer;
begin
lOutDisk := ord(upcase(DriveStr[1]))+1-ord('A');
if (lOutDisk >= 0) and (lOutDisk <= 26) then
result := DiskFree(lOutDisk)
else
result := 0;
//showmessage(DriveStr+'->*'+inttostr(lOutDisk)+'* :'+inttostr(result));
//showmessage(inttostr(DiskFree(0){current drive})+' :'+inttostr(DiskFree(3) {C drive}));
end;
{$ENDIF}
{$ELSE} //Delphi Windows
function DiskFreeEx (DriveStr: String): Int64;
var
lOutDisk: Integer;
begin
lOutDisk := ord(upcase(DriveStr[1]))+1-ord('A');
if (lOutDisk >= 0) and (lOutDisk <= 26) then
result := DiskFree(lOutDisk)
else
result := 0;
//showmessage(DriveStr+'->*'+inttostr(lOutDisk)+'* :'+inttostr(result));
//showmessage(inttostr(DiskFree(0){current drive})+' :'+inttostr(DiskFree(3) {C drive}));
end;
{$ENDIF}
function Log(X, Base: single): single;
begin
if X = 0 then
result := 0
else
Log := Ln(X) / Ln(Base);
end;
function Bool2Char (lBool: boolean): char;
begin
if lBool then
result := '1'
else
result := '0';
end;
function Char2Bool (lChar: char): boolean;
begin
if lChar = '1' then
result := true
else
result := false;
end;
procedure Xswap4r ( var s:single);
type
swaptype = packed record
case byte of
0:(Word1,Word2 : word); //word is 16 bit
end;
swaptypep = ^swaptype;
var
inguy:swaptypep;
outguy:swaptype;
begin
inguy := @s; //assign address of s to inguy
outguy.Word1 := swap(inguy^.Word2);
outguy.Word2 := swap(inguy^.Word1);
inguy^.Word1 := outguy.Word1;