forked from abc874/ca2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.pas
1535 lines (1329 loc) · 45.9 KB
/
Utils.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 Utils;
{$I Information.inc}
// basic review and reformatting: done
interface
uses
// Delphi
Winapi.Windows, Winapi.MMSystem, System.Classes, System.SysUtils, System.IniFiles, Vcl.Forms, Vcl.StdCtrls,
Vcl.Graphics, Vcl.Dialogs, Vcl.Controls,
// DSPack
DXSUtils,
// Indy
IdMultipartFormData;
const
Application_name = 'Cut_assistant.exe'; //for use in cutlist files etc.
const // Multimedia key codes
VK_BROWSER_BACK = $A6;
{$EXTERNALSYM VK_BROWSER_BACK}
VK_BROWSER_FORWARD = $A7;
{$EXTERNALSYM VK_BROWSER_FORWARD}
VK_BROWSER_REFRESH = $A8;
{$EXTERNALSYM VK_BROWSER_REFRESH}
VK_BROWSER_STOP = $A9;
{$EXTERNALSYM VK_BROWSER_STOP}
VK_BROWSER_SEARCH = $AA;
{$EXTERNALSYM VK_BROWSER_SEARCH}
VK_BROWSER_FAVORITES = $AB;
{$EXTERNALSYM VK_BROWSER_FAVORITES}
VK_BROWSER_HOME = $AC;
{$EXTERNALSYM VK_BROWSER_HOME}
VK_VOLUME_MUTE = $AD;
{$EXTERNALSYM VK_VOLUME_MUTE}
VK_VOLUME_DOWN = $AE;
{$EXTERNALSYM VK_VOLUME_DOWN}
VK_VOLUME_UP = $AF;
{$EXTERNALSYM VK_VOLUME_UP}
VK_MEDIA_NEXT_TRACK = $B0;
{$EXTERNALSYM VK_MEDIA_NEXT_TRACK}
VK_MEDIA_PREV_TRACK = $B1;
{$EXTERNALSYM VK_MEDIA_PREV_TRACK}
VK_MEDIA_STOP = $B2;
{$EXTERNALSYM VK_MEDIA_STOP}
VK_MEDIA_PLAY_PAUSE = $B3;
{$EXTERNALSYM VK_MEDIA_PLAY_PAUSE}
VK_LAUNCH_MAIL = $B4;
{$EXTERNALSYM VK_LAUNCH_MAIL}
VK_LAUNCH_MEDIA_SELECT = $B5;
{$EXTERNALSYM VK_LAUNCH_MEDIA_SELECT}
VK_LAUNCH_APP1 = $B6;
{$EXTERNALSYM VK_LAUNCH_APP1}
VK_LAUNCH_APP2 = $B7;
{$EXTERNALSYM VK_LAUNCH_APP2}
// global Vars
var
batchmode: Boolean;
type
ARFileVersion = array[0 .. 3] of Word;
RCutAppSettings = record
CutAppName: string;
PreferredSourceFilter: TGUID;
CodecName: string;
CodecFourCC: FOURCC;
CodecVersion: DWORD;
CodecSettingsSize: Integer;
CodecSettings: string;
end;
THttpRequest = class(TObject)
private
FUrl: string;
FHandleRedirects: Boolean;
FResponse: string;
FErrorMessage: string;
FPostData: TIdMultiPartFormDataStream;
FIsPost: Boolean;
protected
procedure SetIsPost(const Value: Boolean);
public
constructor Create(const Url: string; const handleRedirects: Boolean; const Error_message: string); overload;
destructor Destroy; override;
property IsPostRequest: Boolean read FIsPost write SetIsPost;
property Url: string read FUrl write FUrl;
property HandleRedirects: Boolean read FHandleRedirects write FHandleRedirects;
property Response: string read FResponse write FResponse;
property ErrorMessage: string read FErrorMessage write FErrorMessage;
property PostData: TIdMultiPartFormDataStream read FPostData;
end;
{ TGUIDList - A strong typed list for TGUID }
TGUIDList = class
private
FGUIDList: array of TGUID;
FCount: Integer;
function GetItem(Index: Integer): TGUID;
procedure SetItem(Index: Integer; const Value: TGUID);
function GetItemString(Index: Integer): string;
procedure SetItemString(Index: Integer; const Value: string);
public
constructor Create; virtual;
destructor Destroy; override;
property Item[Index: Integer]: TGUID read GetItem write SetItem; DEFAULT;
property ItemString[Index: Integer]: string read GetItemString write SetItemString;
procedure Clear;
function Add(aGUID: TGUID): Integer;
function AddFromString(aGUIDString: string): Integer;
procedure Delete(Index: Integer); overload;
procedure Delete(Item: TGUID); overload;
function IndexOf(aGUID: TGUID): Integer; overload;
function IndexOf(aGUIDString: string): Integer; overload;
function IsInList(aGUID: TGUID): Boolean; overload;
function IsInList(aGUIDString: string): Boolean; overload;
property Count: Integer read FCount;
end;
{ TMemIniFileEx - An enhanced version of TMemIniFile that has more
strong typed read and write methods. if FileName is empty, the file
will not get saved to disk. }
TMemIniFileEx = class(TMemIniFile)
private
FFormatSettings: TFormatSettings;
FVolatile: Boolean;
function GetIsVolatile: Boolean;
public
constructor Create(const FileName: string); overload;
constructor Create(const FileName: string; const formatSettings: TFormatSettings); overload;
function ReadFloat(const Section, Name: string; Default: Double): Double; override;
procedure WriteFloat(const Section, Name: string; Value: Double); override;
function ReadDate(const Section, Name: string; Default: TDateTime): TDateTime; override;
procedure WriteDate(const Section, Name: string; Value: TDateTime); override;
function ReadTime(const Section, Name: string; Default: TDateTime): TDateTime; override;
procedure WriteTime(const Section, Name: string; Value: TDateTime); override;
function ReadDateTime(const Section, Name: string; Default: TDateTime): TDateTime; override;
procedure WriteDateTime(const Section, Name: string; Value: TDateTime); override;
function ReadRect(const Section, Prefix: string; const Default: TRect): TRect; virtual;
procedure WriteRect(const Section, Prefix: string; const Value: TRect); virtual;
function ReadGuid(const Section, Name: string; const Default: TGUID): TGUID; virtual;
procedure WriteGuid(const Section, Name: string; const Value: TGUID); virtual;
procedure ReadCutAppSettings(const Section: string; var CutAppSettings: RCutAppSettings);
procedure WriteCutAppSettings(const Section: string; var CutAppSettings: RCutAppSettings);
procedure WriteStrings(const section, name: string; const writeCount: Boolean; const value: string); overload;
procedure WriteStrings(const section, name: string; const writeCount: Boolean; const value: TStrings); overload;
procedure UpdateFile; override;
function GetDataString: string;
procedure LoadFromStream(const Stream: TStream);
procedure LoadFromString(const data: string);
procedure SaveToStream(const Stream: TStream);
procedure SaveToFile(const FileName: string);
property Volatile: Boolean read FVolatile write FVolatile DEFAULT False;
property IsVolatile: Boolean read GetIsVolatile;
end;
function rand_string: string;
function Get_File_Version(const FileName: string): string; overload;
function Get_File_Version(const FileName: string; var FileVersionMS, FileVersionLS: DWORD): Boolean; overload;
function Application_version: string;
function Application_Dir: string;
function Application_File: string;
function Application_Friendly_Name: string;
function UploadData_Path(useCSV: Boolean): string;
function cleanURL(aURL: string): string;
function cleanFileName(const FileName: string): string;
procedure ListBoxToClipboard(ListBox: TListBox; CopyAll: Boolean);
function STO_ShellExecute(const AppName, AppArgs: string; const Wait: Cardinal;
const Hide: Boolean; var ExitCode: DWORD): Boolean;
function STO_ShellExecute_Capture(const AppName, AppArgs: string; const Wait: Cardinal;
const Hide: Boolean; var ExitCode: DWORD; AMemo: TMemo): Boolean;
function CallApplication(AppPath, Command: string; var ErrorString: string): Boolean;
function secondsToTimeString(t: Double): string;
function fcc2String(fcc: DWORD): string;
function SaveBitmapAsJPEG(ABitmap: TBitmap; FileName: string): Boolean;
function IsPathRooted(const Path: string): Boolean;
function PathCombine(const aPath, otherPath: string): string;
function CtrlDown: Boolean;
function ShiftDown: Boolean;
function AltDown: Boolean;
function ValidRect(const ARect: TRect): Boolean;
// Use in Create event of form to fix scaling when screen resolution changes.
procedure ScaleForm(const F: TForm); overload;
procedure ScaleForm(const F: TForm; const ScreenWidthDev, ScreenHeightDev: Integer); overload;
// Fix Borland QC Report 13832: Constraints don't obey form Scaled property
procedure AdjustFormConstraints(form: TForm);
//ini.ReadString does work only up to 2047 characters due to restrictions in iniFiles.pas
function iniReadLargeString(
const ini: TCustomIniFile;
const BufferSize: Integer;
const section, name, default: string): string;
procedure ReadCutAppSettings(
const ini: TCustomIniFile;
const section: string;
var CutAppSettings: RCutAppSettings);
procedure WriteCutAppSettings(
const ini: TCustomIniFile;
const section: string;
var CutAppSettings: RCutAppSettings);
function FilterInfoToString(const filterInfo: TFilCatNode; FillToLen: Integer = 0): string;
function StringToFilterGUID(const s: string): TGUID;
procedure ShowExpectedException(E: Exception; const Header: string);
function iniReadRect(const ini: TCustomIniFile; const section, name: string; const default: TRect): TRect;
procedure iniWriteRect(const ini: TCustomIniFile; const section, name: string; const value: TRect);
procedure iniWriteStrings(const ini: TCustomIniFile; const section, name: string; const writeCount: Boolean; const value: string); overload;
procedure iniReadStrings(const ini: TCustomIniFile; const section, name: string; const readCount: Boolean; value: TStrings); overload;
procedure iniWriteStrings(const ini: TCustomIniFile; const section, name: string; const writeCount: Boolean; const value: TStrings); overload;
function MakeFourCC(const a, b, c, d: Char): DWORD;
function Parse_File_Version(const VersionStr: string): ARFileVersion;
function FloatToStrInvariant(Value: Extended): string;
function FilterStringFromExtArray(ExtArray: array of string): string;
function MakeFilterString(const description: string; const extensions: string): string;
function AppendFilterString(const filter: string; const description: string; const extensions: string): string;
function StrToFloatDefInv(const s: string; const d: extended; const sep: Char = '.'): extended;
type
RMediaSample = record
Active: Boolean;
SampleTime: Double;
IsKeyFrame: Boolean;
HasBitmap: Boolean;
Bitmap: TBitmap;
end;
function IntExt(const d: Double; const fraction: Double): Double;
function GetVersionRequestParams: string;
// func/proc from abc874
function ExtractBaseFileNameOTR(const S: string): string;
function FileNameToFormatName(const S: string): string;
function StringToken(var S: string; C: Char): string;
procedure ErrMsg(const S: string);
procedure ErrMsgFmt(const S: string; const Args: array of const);
procedure InfMsg(const S: string);
procedure InfMsgFmt(const S: string; const Args: array of const);
procedure WarnMsg(const S: string);
procedure WarnMsgFmt(const S: string; const Args: array of const);
function YesNoMsg(const S: string): Boolean;
function YesNoMsgFmt(const S: string; const Args: array of const): Boolean;
function YesNoWarnMsg(const S: string): Boolean;
function YesNoWarnMsgFmt(const S: string; const Args: array of const): Boolean;
function NoYesMsg(const S: string): Boolean;
function NoYesMsgFmt(const S: string; const Args: array of const): Boolean;
function NoYesWarnMsg(const S: string): Boolean;
function NoYesWarnMsgFmt(const S: string; const Args: array of const): Boolean;
function YesNoCancelNamed(const Msg: string; const YesCaption: string = ''; const NoCaption: string = '';
const CancelCaption: string = ''; DefaultButton: TMsgDlgBtn = mbCancel): TModalResult;
implementation
uses
// Delphi
Winapi.Messages, Winapi.ShellAPI, Winapi.DirectShow9, System.Variants, System.StrUtils, System.Types, System.Math,
System.UITypes, System.IOUtils, Vcl.Clipbrd, Vcl.Imaging.jpeg,
// Indy
IdUri,
// CA
CAResources;
const
ScreenWidthDev = 1280;
ScreenHeightDev = 1024;
var
invariantFormat: TFormatSettings;
function GetVersionRequestParams: string;
begin
Result := 'app=CutAssistant&version=' + TIdURI.ParamsEncode(Application_Version);
end;
function IntExt(const d: Double; const fraction: Double): Double;
begin
Result := Trunc(d / fraction) * fraction;
end;
function StrToFloatDefInv(const s: string; const d: extended; const sep: Char): extended;
var
Temp_DecimalSeparator: Char;
begin
Temp_DecimalSeparator := FormatSettings.DecimalSeparator;
FormatSettings.DecimalSeparator := sep;
try
Result := StrToFloatDef(s, d);
finally
FormatSettings.DecimalSeparator := Temp_DecimalSeparator;
end;
end;
function AppendFilterString(const filter: string; const description: string; const extensions: string): string;
begin
Result := filter;
if filter <> '' then
Result := Result + '|';
Result := Result + MakeFilterString(description, extensions);
end;
function MakeFilterString(const description: string; const extensions: string): string;
begin
Result := Format('%s (%s)|%s', [description, extensions, extensions]);
end;
function FilterStringFromExtArray(ExtArray: array of string): string;
var
I: Integer;
begin
Result := '';
for I := 0 to Pred(Length(ExtArray)) do
begin
if I > 0 then
Result := Result + ';';
Result := Result + '*' + ExtArray[I];
end;
end;
constructor TMemIniFileEx.Create(const FileName: string);
begin
inherited Create(FileName, TEncoding.UTF8);
FVolatile := False;
FFormatSettings := invariantFormat;
end;
constructor TMemIniFileEx.Create(const FileName: string; const formatSettings: TFormatSettings);
begin
inherited Create(FileName);
FVolatile := False;
FFormatSettings := formatSettings;
end;
function TMemIniFileEx.GetIsVolatile: Boolean;
begin
Result := FVolatile or (FileName = '');
end;
procedure TMemIniFileEx.UpdateFile;
var
List: TStringList;
begin
if (not Volatile) and (FileName <> '') then
begin
// inherited UpdateFile; writes BOM
List := TStringList.Create;
try
GetStrings(List);
List.WriteBOM := False;
List.SaveToFile(FileName, Encoding);
finally
List.Free;
end;
Modified := False;
end;
end;
function TMemIniFileEx.ReadFloat(const Section, Name: string; Default: Double): Double;
var
FloatStr: string;
begin
FloatStr := ReadString(Section, Name, '');
Result := Default;
if FloatStr <> '' then try
Result := StrToFloat(FloatStr, FFormatSettings);
except
on EConvertError do
// Ignore EConvertError exceptions
else
raise;
end;
end;
function TMemIniFileEx.ReadDate(const Section, Name: string; Default: TDateTime): TDateTime;
var
DateStr: string;
begin
DateStr := ReadString(Section, Name, '');
Result := Default;
if DateStr <> '' then
try
Result := StrToDate(DateStr, FFormatSettings);
except
on EConvertError do
// Ignore EConvertError exceptions
else
raise;
end;
end;
function TMemIniFileEx.ReadDateTime(const Section, Name: string; Default: TDateTime): TDateTime;
var
DateStr: string;
begin
DateStr := ReadString(Section, Name, '');
Result := Default;
if DateStr <> '' then
try
Result := StrToDateTime(DateStr, FFormatSettings);
except
on EConvertError do
// Ignore EConvertError exceptions
else
raise;
end;
end;
function TMemIniFileEx.ReadTime(const Section, Name: string; Default: TDateTime): TDateTime;
var
TimeStr: string;
begin
TimeStr := ReadString(Section, Name, '');
Result := Default;
if TimeStr <> '' then
try
Result := StrToTime(TimeStr, FFormatSettings);
except
on EConvertError do
// Ignore EConvertError exceptions
else
raise;
end;
end;
procedure TMemIniFileEx.WriteDate(const Section, Name: string; Value: TDateTime);
begin
WriteString(Section, Name, DateToStr(Value, FFormatSettings));
end;
procedure TMemIniFileEx.WriteDateTime(const Section, Name: string; Value: TDateTime);
begin
WriteString(Section, Name, DateTimeToStr(Value, FFormatSettings));
end;
procedure TMemIniFileEx.WriteFloat(const Section, Name: string; Value: Double);
begin
WriteString(Section, Name, FloatToStr(Value, FFormatSettings));
end;
procedure TMemIniFileEx.WriteTime(const Section, Name: string; Value: TDateTime);
begin
WriteString(Section, Name, TimeToStr(Value, FFormatSettings));
end;
function TMemIniFileEx.ReadRect(const Section, Prefix: string; const Default: TRect): TRect;
begin
Result.Left := ReadInteger(Section, Prefix + '_Left', Default.Left);
Result.Top := ReadInteger(Section, Prefix + '_Top', Default.Top);
Result.Right := Result.Left + ReadInteger(Section, Prefix + '_Width', Default.Right - Default.Left);
Result.Bottom := Result.Top + ReadInteger(Section, Prefix + '_Height', Default.Bottom - Default.Top);
end;
procedure TMemIniFileEx.WriteRect(const Section, Prefix: string; const Value: TRect);
begin
WriteInteger(Section, Prefix + '_Left', Value.Left);
WriteInteger(Section, Prefix + '_Top', Value.Top);
WriteInteger(Section, Prefix + '_Width', Value.Right - Value.Left);
WriteInteger(Section, Prefix + '_Height', Value.Bottom - Value.Top);
end;
function TMemIniFileEx.ReadGuid(const Section, Name: string; const Default: TGUID): TGUID;
var
GuidStr: string;
begin
GuidStr := ReadString(Section, Name, '');
Result := Default;
if GuidStr <> '' then
try
Result := StringToGUID(GuidStr);
except
on EConvertError do
// ignore EConvertError exceptions
else
raise
end;
end;
procedure TMemIniFileEx.WriteGuid(const Section, Name: string; const Value: TGUID);
begin
WriteString(Section, Name, GUIDToString(Value));
end;
procedure TMemIniFileEx.ReadCutAppSettings(const Section: string; var CutAppSettings: RCutAppSettings);
var
BufferSize: Integer;
begin
CutAppSettings.CutAppName := ReadString(Section, 'AppName', '');
CutAppSettings.PreferredSourceFilter := ReadGuid(Section, 'PreferredSourceFilter', GUID_NULL);
CutAppSettings.CodecName := ReadString(Section, 'CodecName', '');
CutAppSettings.CodecFourCC := ReadInteger(Section, 'CodecFourCC', 0);
CutAppSettings.CodecVersion := ReadInteger(Section, 'CodecVersion', 0);
CutAppSettings.CodecSettingsSize := ReadInteger(Section, 'CodecSettingsSize', 0);
CutAppSettings.CodecSettings := ReadString(Section, 'CodecSettings', '');
BufferSize := CutAppSettings.CodecSettingsSize div 3;
if (CutAppSettings.CodecSettingsSize mod 3) > 0 then
Inc(BufferSize);
BufferSize := BufferSize * 4 + 1; //+1 for terminating #0
if Length(CutAppSettings.CodecSettings) <> BufferSize - 1 then
begin
CutAppSettings.CodecSettings := '';
CutAppSettings.CodecSettingsSize := 0;
end;
end;
procedure TMemIniFileEx.WriteCutAppSettings(const Section: string; var CutAppSettings: RCutAppSettings);
begin
EraseSection(Section);
WriteString(Section, 'AppName', CutAppSettings.CutAppName);
WriteGuid(Section, 'PreferredSourceFilter', CutAppSettings.PreferredSourceFilter);
WriteString(Section, 'CodecName', CutAppSettings.CodecName);
WriteInteger(Section, 'CodecFourCC', CutAppSettings.CodecFourCC);
WriteInteger(Section, 'CodecVersion', CutAppSettings.CodecVersion);
WriteInteger(Section, 'CodecSettingsSize', CutAppSettings.CodecSettingsSize);
WriteString(Section, 'CodecSettings', CutAppSettings.CodecSettings);
end;
procedure TMemIniFileEx.WriteStrings(const section, name: string; const writeCount: Boolean; const value: string);
var
L: TStringList;
begin
L := TStringList.Create;
try
L.Text := value;
WriteStrings(section, name, writeCount, L);
finally
FreeAndNil(L);
end;
end;
procedure TMemIniFileEx.WriteStrings(const section, name: string; const writeCount: Boolean; const value: TStrings);
var
idx, cnt: Integer;
begin
if Assigned(value) then
cnt := value.Count
else
cnt := 0;
if writeCount then
WriteInteger(section, name + 'Count', cnt);
for idx := 0 to Pred(cnt) do
WriteString(section, name + IntToStr(idx + 1), value.Strings[idx]);
end;
function TMemIniFileEx.GetDataString: string;
var
List: TStringList;
begin
List := TStringList.Create;
try
GetStrings(List);
Result := List.Text;
finally
FreeAndNil(List);
end;
end;
procedure TMemIniFileEx.LoadFromString(const data: string);
var
List: TStringList;
begin
List := TStringList.Create;
try
List.Text := data;
SetStrings(List);
finally
FreeAndNil(List);
end;
end;
procedure TMemIniFileEx.LoadFromStream(const Stream: TStream);
var
List: TStringList;
begin
List := TStringList.Create;
try
List.LoadFromStream(Stream, TEncoding.UTF8);
SetStrings(List);
finally
FreeAndNil(List);
end;
end;
procedure TMemIniFileEx.SaveToStream(const Stream: TStream);
var
List: TStringList;
begin
List := TStringList.Create;
try
GetStrings(List);
List.WriteBOM := False;
List.SaveToStream(Stream, TEncoding.UTF8);
finally
List.Free;
end;
end;
procedure TMemIniFileEx.SaveToFile(const FileName: string);
var
fs: TFileStream;
begin
fs := TFileStream.Create(FileName, fmCreate, fmShareDenyWrite);
try
SaveToStream(fs);
finally
FreeAndNil(fs);
end;
end;
function FloatToStrInvariant(Value: Extended): string;
begin
Result := FloatToStr(Value, invariantFormat);
end;
function Parse_File_Version(const VersionStr: string): ARFileVersion;
var
S : string;
function NextWord(var S: string): Integer;
var
delimPos: Integer;
begin
delimPos := Pos('.', S);
if delimPos > 0 then
begin
Result := StrToIntDef(Copy(S, 1, delimPos - 1), -1);
Delete(S, 1, delimPos);
end else
Result := 0;
end;
begin
S := Copy(VersionStr, 1, MaxInt);
Result[0] := NextWord(S);
Result[1] := NextWord(S);
Result[2] := NextWord(S);
Result[3] := NextWord(S);
end;
procedure ShowExpectedException(E: Exception; const Header: string);
var
msg: string;
begin
msg := '';
with E do
begin
// SuspendThreads := True;
// ShowCpuRegisters := False;
// ShowStackDump := False;
// CreateScreenShot := False;
// ShowSetting := ssDetailBox;
// SendBtnVisible := False;
// CloseBtnVisible := False;
// FocusedButton := bContinueApplication;
if Header <> '' then
msg := Format(RsExpectedErrorHeader, [Header]);
ErrMsg(Format(RsExpectedErrorFormat, [msg, E.ClassName, E.Message]));
end;
end;
function iniReadRect(const ini: TCustomIniFile; const section, name: string; const default: TRect): TRect;
begin
Result.Left := ini.ReadInteger(section, name + '_Left', default.Left);
Result.Top := ini.ReadInteger(section, name + '_Top', default.Top);
Result.Right := Result.Left + ini.ReadInteger(section, name + '_Width', default.Right - default.Left);
Result.Bottom := Result.Top + ini.ReadInteger(section, name + '_Height', default.Bottom - default.Top);
end;
procedure iniWriteRect(const ini: TCustomIniFile; const section, name: string; const value: TRect);
begin
ini.WriteInteger(section, name + '_Left', value.Left);
ini.WriteInteger(section, name + '_Top', value.Top);
ini.WriteInteger(section, name + '_Width', value.Right - value.Left);
ini.WriteInteger(section, name + '_Height', value.Bottom - value.Top);
end;
procedure iniWriteStrings(const ini: TCustomIniFile; const section, name: string; const writeCount: Boolean; const value: string);
var
L: TStringList;
begin
L := TStringList.Create;
try
L.Text := value;
iniWriteStrings(ini, section, name, writeCount, L);
finally
FreeAndNil(L);
end;
end;
procedure iniWriteStrings(const ini: TCustomIniFile; const section, name: string; const writeCount: Boolean; const value: TStrings);
var
idx, cnt: Integer;
begin
if Assigned(value) then
cnt := value.Count
else
cnt := 0;
if writeCount then
ini.WriteInteger(section, name + 'Count', cnt);
for idx := 0 to Pred(cnt) do
ini.WriteString(section, name + IntToStr(idx + 1), value.Strings[idx]);
end;
procedure iniReadStrings(const ini: TCustomIniFile; const section, name: string; const readCount: Boolean; value: TStrings); overload;
var
idx, cnt: Integer;
begin
if Assigned(value) then
begin
value.Clear;
if readCount then
begin
cnt := ini.ReadInteger(section, name + 'Count', 0);
for idx := 1 to cnt - 1 do
value.Add(ini.ReadString(section, name + IntToStr(idx), ''));
end else
begin
idx := 1;
while ini.ValueExists(section, name + IntToStr(idx)) do
begin
value.Add(ini.ReadString(section, name + IntToStr(idx), ''));
Inc(idx);
end;
end;
end;
end;
function FilterInfoToString(const filterInfo: TFilCatNode; FillToLen: Integer = 0): string;
var
S: string;
begin
if FillToLen > 0 then
S := StringOfChar(' ', FillToLen - Length(filterInfo.FriendlyName))
else
S := '';
Result := filterInfo.FriendlyName + S + ' (' + GUIDToString(filterInfo.CLSID) + ')';
end;
function StringToFilterGUID(const s: string): TGUID;
var
L,R: Integer;
begin
L := LastDelimiter('(', s);
R := LastDelimiter(')', s);
if L > 0 then
Result := StringToGUID(Copy(s, Succ(L), IfThen(R > L, Pred(R - L), Length(s) - L)))
else
Result := GUID_NULL
end;
procedure WriteCutAppSettings(const ini: TCustomIniFile; const section: string; var CutAppSettings: RCutAppSettings);
begin
ini.WriteString(section, 'AppName', CutAppSettings.CutAppName);
ini.WriteString(section, 'PreferredSourceFilter', GUIDToString(CutAppSettings.PreferredSourceFilter));
ini.WriteString(section, 'CodecName', CutAppSettings.CodecName);
ini.WriteInteger(section, 'CodecFourCC', CutAppSettings.CodecFourCC);
ini.WriteInteger(section, 'CodecVersion', CutAppSettings.CodecVersion);
ini.WriteInteger(section, 'CodecSettingsSize', CutAppSettings.CodecSettingsSize);
ini.WriteString(section, 'CodecSettings', CutAppSettings.CodecSettings);
end;
procedure ReadCutAppSettings(const ini: TCustomIniFile; const section: string; var CutAppSettings: RCutAppSettings);
var
StrValue: string;
BufferSize: Integer;
begin
if Assigned(ini) then
begin
CutAppSettings.CutAppName := ini.ReadString(section, 'AppName', '');
StrValue := ini.ReadString(section, 'PreferredSourceFilter', GUIDToString(GUID_NULL));
try
CutAppSettings.PreferredSourceFilter := StringToGUID(StrValue);
except
on EConvertError do
CutAppSettings.PreferredSourceFilter := GUID_NULL;
end;
CutAppSettings.CodecName := ini.ReadString(section, 'CodecName', '');
CutAppSettings.CodecFourCC := ini.ReadInteger(section, 'CodecFourCC', 0);
CutAppSettings.CodecVersion := ini.ReadInteger(section, 'CodecVersion', 0);
CutAppSettings.CodecSettingsSize := ini.ReadInteger(section, 'CodecSettingsSize', 0);
BufferSize := CutAppSettings.CodecSettingsSize div 3;
if (CutAppSettings.CodecSettingsSize mod 3) > 0 then
Inc(BufferSize);
BufferSize := BufferSize * 4 + 1; //+1 for terminating #0
CutAppSettings.CodecSettings := iniReadLargeString(ini, BufferSize, section, 'CodecSettings', '');
if Length(CutAppSettings.CodecSettings) <> BufferSize - 1 then
begin
CutAppSettings.CodecSettings := '';
CutAppSettings.CodecSettingsSize := 0;
end;
end;
end;
//ini.ReadString does work only up to 2047 characters due to restrictions in iniFiles.pas
function iniReadLargeString(const ini: TCustomIniFile; const BufferSize: Integer; const section, name, default: string): string;
var
SizeRead: Integer;
Buffer: PChar;
begin
GetMem(Buffer, BufferSize * SizeOf(Char));
try
SizeRead := GetPrivateProfileString(PChar(Section), PChar(name), PChar(default), Buffer, BufferSize, PChar(ini.FileName));
if (SizeRead >= 0) and (SizeRead <= BufferSize - 1) then
SetString(Result, Buffer, SizeRead)
else
Result := default;
finally
freemem(Buffer, BufferSize * SizeOf(Char));
end;
end;
procedure ScaleForm(const F: TForm); overload;
begin
ScaleForm(F, ScreenWidthDev, ScreenHeightDev);
end;
procedure AdjustFormConstraints(form: TForm);
{$if compilerversion < 18}
var
FormDPI, ScreenDPI: Integer;
{$IFEND}
begin
if Assigned(form) then
begin
{$if compilerversion < 18}
if form.Scaled then
begin
FormDPI := form.PixelsPerInch;
ScreenDPI := Screen.PixelsPerInch;
if FormDPI <> ScreenDPI then
with form.Constraints do
begin
MinHeight := (MinHeight * ScreenDPI) div FormDPI;
MinWidth := (MinWidth * ScreenDPI) div FormDPI;
MaxHeight := (MinHeight * ScreenDPI) div FormDPI;
MaxWidth := (MinWidth * ScreenDPI) div FormDPI;
end;
end;
{$IFEND}
end;
end;
procedure ScaleForm(const F: TForm; const ScreenWidthDev, ScreenHeightDev: Integer);
var
x, y: Integer;
begin
if Assigned(F) then
begin
F.Scaled := True;
x := Screen.Width;
y := Screen.Height;
if (x <> ScreenWidthDev) or (y <> ScreenHeightDev) then
begin
F.Height := (F.ClientHeight * y div ScreenHeightDev) + F.Height - F.ClientHeight;
F.Width := (F.ClientWidth * y div ScreenWidthDev) + F.Width - F.ClientWidth;
F.ScaleBy(x, ScreenWidthDev);
end;
end;
end;
constructor THttpRequest.Create(const Url: string; const handleRedirects: Boolean; const Error_message: string);
begin
FUrl := Url;
FHandleRedirects := handleRedirects;
FErrorMessage := Error_message;
FPostData := PostData;
FResponse := '';
IsPostRequest := False;
end;
destructor THttpRequest.Destroy;
begin
IsPostRequest := False;
end;
procedure THttpRequest.SetIsPost(const Value: Boolean);
begin
FIsPost := Value;
if not Value and Assigned(FPostData) then
FreeAndNil(FPostData);
if Value and not Assigned(FPostData) then
FPostData := TIdMultiPartFormDataStream.Create;
end;
function ValidRect(const ARect: TRect): Boolean;
begin
Result := (ARect.Left > -1) and (ARect.Right > ARect.Left) and (ARect.Top > -1) and (ARect.Bottom > ARect.Top);
end;
function PathCombine(const aPath, otherPath: string): string;
begin
Result := otherPath;
if IsPathDelimiter(Result, 1) then
Delete(Result, 1, 1);
Result := IncludeTrailingPathDelimiter(aPath) + Result;
end;
function CtrlDown: Boolean;
var
State: TKeyboardState;
begin
GetKeyboardState(State);
Result := ((State[vk_Control] and 128) <> 0);
end;
function ShiftDown: Boolean;
var
State: TKeyboardState;
begin
GetKeyboardState(State);
Result := ((State[vk_Shift] and 128) <> 0);
end;
function AltDown: Boolean;
var
State: TKeyboardState;
begin
GetKeyboardState(State);
Result := ((State[vk_Menu] and 128) <> 0);
end;
function rand_string: string;
var
I: Integer;
begin
Result := '';
for I := 0 to 19 do begin
Result := Result + IntToHex(Round(random(16)), 1);
end;
end;
function Get_File_Version(const FileName: string): string;
var
dwFileVersionMS, dwFileVersionLS : DWORD;
begin
{ if FileName is not valid, return a string saying so and Exit}
if FileExists(FileName) then
begin
Result := '';
if Get_File_Version(FileName, dwFileVersionMS, dwFileVersionLS) then
Result := Format('%d.%d.%d.%d', [HiWord(dwFileVersionMS), LoWord(dwFileVersionMS), HiWord(dwFileVersionLS), LoWord(dwFileVersionLS)])
else
Result := Format(RsErrorFileVersionGetFileVersion, [FileName]);
end else
Result := Format(RsErrorFileVersionFileNotFound, [FileName]);
end;
function Get_File_Version(const FileName: string; var FileVersionMS, FileVersionLS: DWORD): Boolean; //True if successful