-
Notifications
You must be signed in to change notification settings - Fork 11
/
CvtrObj.pas
873 lines (784 loc) · 26.8 KB
/
CvtrObj.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
unit CvtrObj;
interface
uses
PatchLib,
System.Classes,
System.Types,
System.SysUtils,
System.StrUtils,
Contnrs,
Winapi.Windows,
inifiles,
FMX.Objects,
System.Generics.Collections,
Image,
ImageList;
type
TLinkControl = record
DataSource : String;
FieldName : String;
Control : String;
end;
TLinkGridColumn = record
Caption : String;
FieldName : String;
Width : String;
end;
TLinkGrid = record
DataSource : String;
GridControl : String;
Columns : TArray<TLinkGridColumn>;
end;
TDfmToFmxObject = class(TObject)
private
FLinkControlList: TArray<TLinkControl>;
FLinkGridList: TArray<TLinkGrid>;
FDFMClass: String;
FObjName: String;
FOwnedObjs: TObjectList;
FOwnedItems: TObjectList;
FDepth: integer;
F2DPropertyArray: TTwoDArrayOfString;
FPropertyArraySz, FPropertyMax: integer;
FIniReplaceValues,
FIniIncludeValues,
FIniSectionValues,
FIniAddProperties,
FUsesTranslation,
FIniObjectTranslations: TStringlist;
function OwnedObjs: TObjectList;
function IniObjectTranslations: TStringList;
function IniSectionValues: TStringlist;
function UsesTranslation: TStringlist;
function IniReplaceValues: TStringlist;
function IniIncludeValues: TStringlist;
function IniAddProperties: TStringlist;
function PropertyArray(ARow: integer): TArrayOfStrings;
procedure UpdateUsesStringList(AUsesList: TStrings);
procedure ReadProperties(AData: String; AStm: TStream; var AIdx: Integer);
function ProcessUsesString(AOrigUsesArray: TArrayOfStrings): String;
function ProcessCodeBody(const ACodeBody: String): String;
procedure IniFileLoad(AIni: TIniFile);
procedure ReadItems(Prop: TTwoDArrayOfString; APropertyIdx: integer; AStm: TStream);
function FMXClass: String;
function TransformProperty(ACurrentName, ACurrentValue: String; APad: String = ''): String;
function AddArrayOfItemProperties(APropertyIdx: Integer; APad: String): String;
function FMXProperties(APad: String): String;
function FMXSubObjects(APad: String): String;
procedure ReadData(Prop: TTwoDArrayOfString; APropertyIdx: integer; AStm: TStream);
function GetFMXLiveBindings: String;
function GetPASLiveBindings: String;
procedure ReadText(Prop: TTwoDArrayOfString; APropertyIdx: integer; AStm: TStream);
public
constructor Create(ACreateText: String; AStm: TStream; ADepth: integer);
destructor Destroy; override;
procedure LoadInfileDefs(AIniFileName: String);
class function DFMIsTextBased(ADfmFileName: String): Boolean;
function GenPasFile(const APascalSourceFileName: String): AnsiString;
function FMXFile(APad: String = ''): String;
function WriteFMXToFile(const AFmxFileName: String): Boolean;
function WritePasToFile(const APasOutFileName, APascalSourceFileName: String): Boolean;
procedure LiveBindings(DfmObject: TObjectList = nil);
end;
TDfmToFmxListItem = class(TDfmToFmxObject)
FHasMore:Boolean;
FPropertyIndex:Integer;
FOwner:TDfmToFmxObject;
public
constructor Create(AOwner:TDfmToFmxObject;APropertyIdx: integer; AStm: TStream;ADepth: integer);
property HasMore: Boolean read FHasMore;
end;
implementation
const
ContinueCode: String = '#$Continue$#';
{ DfmToFmxObject }
{ Eduardo }
procedure TDfmToFmxObject.LiveBindings(DfmObject: TObjectList = nil);
var
I,J,K,L,M: Integer;
sProp: String;
sFields: String;
obj: TDfmToFmxObject;
bItem: Boolean;
sItem: String;
slItem: TStringDynArray;
begin
// Se não informou um objeto, obtem o inicial
if DfmObject = nil then
DfmObject := FOwnedObjs;
// Passa por todos objetos filhos
for I := 0 to Pred(DfmObject.Count) do
begin
// Se for de conversão
if DfmObject[I] is TDfmToFmxObject then
begin
// Obtem o objeto
obj := TDfmToFmxObject(DfmObject[I]);
// Se for uma grid
if obj.FDFMClass.Equals('TDBGrid') then
begin
// Inicializa
sFields := EmptyStr;
// Cria um novo item na lista de grids
SetLength(FLinkGridList, Succ(Length(FLinkGridList)));
// Insere o nome da grid
FLinkGridList[Pred(Length(FLinkGridList))].GridControl := obj.FObjName;
// Passa por todas propriedades da grid
for J := Low(F2DPropertyArray) to High(F2DPropertyArray) do
begin
// Obtem os dados do DataSource
if obj.F2DPropertyArray[J, 0].Equals('DataSource') then
FLinkGridList[Pred(Length(FLinkGridList))].DataSource := obj.F2DPropertyArray[J, 1];
// Se for as colunas
if obj.F2DPropertyArray[J, 0].Equals('Columns') then
begin
// Obtem os dados dos fields
bItem := False;
sFields := obj.F2DPropertyArray[J, 1];
slItem := SplitString(sFields, #13);
for sItem in slItem do
begin
if sItem = 'item' then
SetLength(FLinkGridList[Pred(Length(FLinkGridList))].Columns, Succ(Length(FLinkGridList[Pred(Length(FLinkGridList))].Columns)))
else
if Trim(SplitString(sItem, '=')[0]) = 'Title.Caption' then
FLinkGridList[Pred(Length(FLinkGridList))].Columns[Pred(Length(FLinkGridList[Pred(Length(FLinkGridList))].Columns))].Caption := Trim(SplitString(sItem, '=')[1])
else
if Trim(SplitString(sItem, '=')[0]) = 'FieldName' then
FLinkGridList[Pred(Length(FLinkGridList))].Columns[Pred(Length(FLinkGridList[Pred(Length(FLinkGridList))].Columns))].FieldName := Trim(SplitString(sItem, '=')[1])
else
if Trim(SplitString(sItem, '=')[0]) = 'Width' then
FLinkGridList[Pred(Length(FLinkGridList))].Columns[Pred(Length(FLinkGridList[Pred(Length(FLinkGridList))].Columns))].Width := Trim(SplitString(sItem, '=')[1]);
end;
end;
// Se ja encontrou tudo, sai do loop
if not FLinkGridList[Pred(Length(FLinkGridList))].DataSource.IsEmpty and not sFields.IsEmpty then
Break;
end;
end;
// Se for um dbedit
if obj.FDFMClass.Equals('TDBEdit') then
begin
// Cria um novo item na lista de dbedits
SetLength(FLinkControlList, Succ(Length(FLinkControlList)));
// Insere o nome do dbedit
FLinkControlList[Pred(Length(FLinkControlList))].Control := obj.FObjName;
// Passa por todas propriedades do dbedit
for J := Low(F2DPropertyArray) to High(F2DPropertyArray) do
begin
// Obtem os dados do DataSource
if obj.F2DPropertyArray[J, 0].Equals('DataSource') then
FLinkControlList[Pred(Length(FLinkControlList))].DataSource := obj.F2DPropertyArray[J, 1];
// Obtem os dados do field
if obj.F2DPropertyArray[J, 0].Equals('DataField') then
FLinkControlList[Pred(Length(FLinkControlList))].FieldName := GetArrayFromString(obj.F2DPropertyArray[J, 1], '=', True, True)[0];
// Se ja encontrou tudo, sai do loop
if not FLinkControlList[Pred(Length(FLinkControlList))].DataSource.IsEmpty and not FLinkControlList[Pred(Length(FLinkControlList))].FieldName.IsEmpty then
Break;
end;
end;
// Se o componente atual possui componentes nele, faz recursão
if Assigned(obj.FOwnedObjs) and (obj.FOwnedObjs.Count > 0) then
LiveBindings(obj.FOwnedObjs);
end;
end;
end;
{ Eduardo }
function TDfmToFmxObject.GetFMXLiveBindings: String;
var
I: Integer;
J: Integer;
begin
if (Length(FLinkControlList) = 0) and (Length(FLinkGridList) = 0) then
Exit(EmptyStr);
// Adiciona BindingsList
Result :=
' object BindingsList: TBindingsList '+
CRLF +' Methods = <> '+
CRLF +' OutputConverters = <> '+
CRLF +' Left = 20 '+
CRLF +' Top = 5 ';
// Passa pela lista de controles
for I := 0 to High(FLinkControlList) do
begin
Result := Result +
CRLF +' object LinkControlToField'+ I.ToString +': TLinkControlToField '+
CRLF +' Category = '+ QuotedStr('Quick Bindings') +
CRLF +' DataSource = '+ FLinkControlList[I].DataSource +
CRLF +' FieldName = '+ QuotedStr(FLinkControlList[I].FieldName) +
CRLF +' Control = '+ FLinkControlList[I].Control +
CRLF +' Track = False '+
CRLF +' end ';
end;
// Passa pela lista de grids
for I := 0 to High(FLinkGridList) do
begin
Result := Result +
CRLF +' object LinkGridToDataSourceBindSourceDB'+ I.ToString +': TLinkGridToDataSource '+
CRLF +' Category = '+ QuotedStr('Quick Bindings') +
CRLF +' DataSource = '+ FLinkGridList[I].DataSource +
CRLF +' GridControl = '+ FLinkGridList[I].GridControl +
CRLF +' Columns = < ';
// Passa pela lista de colunas da grid
for J := 0 to High(FLinkGridList[I].Columns) do
begin
Result := Result +
CRLF +' item '+
CRLF +' MemberName = '+ FLinkGridList[I].Columns[J].FieldName;
// Se tem Caption
if not FLinkGridList[I].Columns[J].Caption.IsEmpty then
begin
Result := Result +
CRLF +' Header = '+ FLinkGridList[I].Columns[J].Caption;
end;
// Se tem Width
if not FLinkGridList[I].Columns[J].Width.IsEmpty then
begin
Result := Result +
CRLF +' Width = '+ FLinkGridList[I].Columns[J].Width;
end;
Result := Result +
CRLF +' end ';
end;
Result := Result +
CRLF +' > '+
CRLF +' end ';
end;
Result := Result +
CRLF +' end ';
end;
{ Eduardo }
function TDfmToFmxObject.GetPASLiveBindings: String;
var
I: Integer;
begin
if (Length(FLinkControlList) = 0) and (Length(FLinkGridList) = 0) then
Exit(EmptyStr);
// Adiciona BindingsList
Result := ' BindingsList: TBindingsList; ';
// Passa pela lista de controles
for I := 0 to High(FLinkControlList) do
begin
Result := Result +
CRLF +' LinkControlToField'+ I.ToString +': TLinkControlToField; ';
end;
// Passa pela lista de grids
for I := 0 to High(FLinkGridList) do
begin
Result := Result +
CRLF +' LinkGridToDataSourceBindSourceDB'+ I.ToString +': TLinkGridToDataSource; ';
end;
end;
function TDfmToFmxObject.AddArrayOfItemProperties(APropertyIdx: Integer; APad: String): String;
begin
Result:=APad+' item'+ CRLF +
APad+ ' Prop1 = 6'+ CRLF +
APad+ ' end>'+ CRLF;
//Tempary patch
end;
constructor TDfmToFmxObject.Create(ACreateText: String; AStm: TStream; ADepth: integer);
var
InputArray: TArrayOfStrings;
Data: String;
NxtChr: PChar;
i: integer;
begin
i := 0;
FDepth := ADepth;
if Pos(AnsiString('object'), Trim(ACreateText)) = 1 then
begin
InputArray := GetArrayFromString(ACreateText, ' ');
NxtChr := @InputArray[1][1];
FObjName := FieldSep(NxtChr, ':');
FDFMClass := InputArray[2];
Data := Trim(ReadLineFrmStream(AStm));
while Data <> AnsiString('end') do
begin
if Pos(AnsiString('object'), Data) = 1 then
OwnedObjs.Add(TDfmToFmxObject.Create(Data, AStm, FDepth + 1))
else
ReadProperties(Data,AStm,i);
Data := Trim(ReadLineFrmStream(AStm));
end
end
else
raise Exception.Create('Bad Start::' + ACreateText);
SetLength(F2DPropertyArray, FPropertyMax + 1);
end;
destructor TDfmToFmxObject.Destroy;
begin
SetLength(F2DPropertyArray, 0);
FOwnedObjs.Free;
FOwnedItems.Free;
FIniReplaceValues.Free;
FIniIncludeValues.Free;
FIniSectionValues.Free;
FUsesTranslation.Free;
FIniAddProperties.Free;
end;
class function TDfmToFmxObject.DFMIsTextBased(ADfmFileName: String): Boolean;
var
Sz: Int64;
Idx: integer;
DFMFile: TFileStream;
TestString: AnsiString;
begin
Result := false;
if not FileExists(ADfmFileName) then
Exit;
DFMFile := TFileStream.Create(ADfmFileName, fmOpenRead);
try
Sz := DFMFile.Size;
if Sz > 20 then
begin
SetLength(TestString, 20);
Idx := DFMFile.Read(TestString[1], 20);
if Idx <> 20 then
raise Exception.Create('Error Dfm file read');
if PosNoCase('object', String(TestString)) > 0 then
Result := true;
if not Result then
begin
try
TestString := AnsiString(CompressedUnicode(String(TestString)));
if PosNoCase('object', String(TestString)) > 0 then
Result := true;
except
end;
end;
end;
finally
DFMFile.Free;
end;
end;
function TDfmToFmxObject.FMXClass: String;
begin
Result := FDFMClass;
end;
function TDfmToFmxObject.FMXFile(APad: String = ''): String;
begin
Result := APad +'object '+ FObjName +': '+ FMXClass + CRLF;
Result := Result + FMXProperties(APad);
Result := Result + FMXSubObjects(APad +' ');
if APad = EmptyStr then
Result := Result + GetFMXLiveBindings + CRLF + APad +'end' + CRLF
else
Result := Result + APad +'end' + CRLF;
end;
function TDfmToFmxObject.FMXProperties(APad: String): String;
var
i: Integer;
sProp: String;
begin
Result := EmptyStr;
for i := Low(F2DPropertyArray) to High(F2DPropertyArray) do
begin
if F2DPropertyArray[i, 1] = '<' then
Result := Result + APad +' '+ TransformProperty(F2DPropertyArray[i, 0], F2DPropertyArray[i, 1]) + CRLF + AddArrayOfItemProperties(i, APad +' ') + CRLF
else
if F2DPropertyArray[i, 1][1] = '{' then
begin
sProp := TransformProperty(F2DPropertyArray[i, 0], F2DPropertyArray[i, 1], APad);
if not sProp.IsEmpty then
Result := Result + APad +' '+ sProp + CRLF;
end
else
if F2DPropertyArray[i, 0] <> EmptyStr then
begin
sProp := TransformProperty(F2DPropertyArray[i, 0], F2DPropertyArray[i, 1]);
if not sProp.IsEmpty then
Result := Result + APad +' '+ sProp + CRLF;
end;
end;
if IniAddProperties.Count > 0 then
for i := 0 to Pred(FIniAddProperties.Count) do
Result := Result + APad +' '+ StringReplace(FIniAddProperties[i], '=', ' = ', []) + CRLF;
end;
function TDfmToFmxObject.FMXSubObjects(APad: String): String;
var
I: integer;
begin
Result := EmptyStr;
if FOwnedObjs = nil then
Exit;
for I := 0 to Pred(FOwnedObjs.Count) do
if FOwnedObjs[I] is TDfmToFmxObject then
Result := Result + TDfmToFmxObject(FOwnedObjs[I]).FMXFile(APad +' ');
end;
function TDfmToFmxObject.GenPasFile(const APascalSourceFileName: String): AnsiString;
var
PasFile: TFileStream;
PreUsesString, PostUsesString, UsesString: AnsiString;
UsesArray: TArrayOfStrings;
StartChr, EndChar: PAnsiChar;
Sz: integer;
Idx: integer;
s: String;
begin
Result := '';
PostUsesString := '';
UsesString := '';
if not FileExists(APascalSourceFileName) then
Exit;
PasFile := TFileStream.Create(APascalSourceFileName, fmOpenRead);
try
Sz := PasFile.Size;
if Sz > 20 then
begin
SetLength(PreUsesString, Sz);
Idx := PasFile.Read(PreUsesString[1], Sz);
if Idx <> Sz then
raise Exception.Create('Error Pas file read');
end
else
PreUsesString := '';
finally
PasFile.Free;
end;
if Sz > 20 then
begin
Idx := PosNoCase('uses', String(PreUsesString));
StartChr := @PreUsesString[Idx + 4];
s := ';';
EndChar := StrPos(StartChr, PAnsiChar(s));
UsesArray := GetArrayFromString(StringReplace(Copy(String(PreUsesString), Idx + 4, EndChar - StartChr), CRLF, '', [rfReplaceAll]), ',');
PostUsesString := Copy(PreUsesString, EndChar - StartChr + Idx + 4, Sz);
PostUsesString := AnsiString(ProcessCodeBody(String(PostUsesString)));
PostUsesString := AnsiString(Copy(String(PostUsesString), 1, Pos('TBindSourceDB', String(PostUsesString)) + 15) +
GetPASLiveBindings +
Copy(String(PostUsesString), Pos('TBindSourceDB', String(PostUsesString)) + 15));
SetLength(PreUsesString, Pred(Idx));
UsesString := AnsiString(ProcessUsesString(UsesArray));
end;
Result := PreUsesString + UsesString + PostUsesString;
end;
function TDfmToFmxObject.IniAddProperties: TStringlist;
begin
if FIniAddProperties = nil then
FIniAddProperties := TStringlist.Create;
Result := FIniAddProperties;
end;
procedure TDfmToFmxObject.IniFileLoad(AIni: TIniFile);
var
i: integer;
NewClassName: String;
begin
if AIni = nil then
Exit;
if FDepth < 1 then
begin
AIni.ReadSectionValues('ObjectChanges', IniObjectTranslations);
AIni.ReadSectionValues('TForm', IniSectionValues);
AIni.ReadSectionValues('TFormReplace', IniReplaceValues);
AIni.ReadSection('TFormInclude', IniIncludeValues);
end
else
begin
NewClassName := AIni.ReadString('ObjectChanges', FDFMClass, EmptyStr);
if NewClassName <> EmptyStr then
FDFMClass := NewClassName;
AIni.ReadSectionValues(FDFMClass, IniSectionValues);
AIni.ReadSectionValues(FDFMClass + 'Replace', IniReplaceValues);
AIni.ReadSection(FDFMClass + 'Include', IniIncludeValues);
AIni.ReadSectionValues(FDFMClass + 'AddProperty', IniAddProperties);
end;
for i := 0 to Pred(OwnedObjs.Count) do
if OwnedObjs[i] is TDfmToFmxObject then
TDfmToFmxObject(OwnedObjs[i]).IniFileLoad(AIni);
if FOwnedItems <> nil then
for i := 0 to Pred(fOwnedItems.Count) do
if fOwnedItems[i] is TDfmToFmxListItem then
TDfmToFmxListItem(fOwnedItems[i]).IniFileLoad(AIni);
if IniSectionValues.Count < 1 then
begin
AIni.WriteString(FDFMClass, 'Empty', 'Add Transformations');
AIni.WriteString(FDFMClass, 'Top', 'Position.Y');
AIni.WriteString(FDFMClass, 'Left', 'Position.X');
end;
if IniIncludeValues.Count < 1 then
AIni.WriteString(FDFMClass + 'Include', 'FMX.Controls', 'Empty Include');
end;
function TDfmToFmxObject.IniIncludeValues: TStringlist;
begin
if FIniIncludeValues = nil then
FIniIncludeValues := TStringlist.Create;
Result := FIniIncludeValues;
end;
function TDfmToFmxObject.IniObjectTranslations: TStringList;
begin
if FIniObjectTranslations = nil then
FIniObjectTranslations := TStringlist.Create;
Result := FIniObjectTranslations;
end;
function TDfmToFmxObject.IniReplaceValues: TStringlist;
begin
if FIniReplaceValues = nil then
FIniReplaceValues := TStringlist.Create;
Result := FIniReplaceValues;
end;
function TDfmToFmxObject.IniSectionValues: TStringlist;
begin
if FIniSectionValues = nil then
FIniSectionValues := TStringlist.Create;
Result := FIniSectionValues;
end;
procedure TDfmToFmxObject.LoadInfileDefs(AIniFileName: String);
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(AIniFileName);
IniFileLoad(Ini);
end;
function TDfmToFmxObject.OwnedObjs: TObjectList;
begin
if FOwnedObjs = nil then
begin
FOwnedObjs := TObjectList.Create;
FOwnedObjs.OwnsObjects := true;
end;
Result := FOwnedObjs;
end;
function TDfmToFmxObject.ProcessCodeBody(const ACodeBody: String): String;
var
BdyStr: String;
Idx: Integer;
TransArray: TArrayOfStrings;
begin
BdyStr := StringReplace(ACodeBody, AnsiString('{$R *.DFM}'), AnsiString('{$R *.FMX}'), [rfIgnoreCase]);
if FIniObjectTranslations <> nil then
begin
for Idx := 0 to FIniObjectTranslations.Count-1 do
begin
TransArray := GetArrayFromString(FIniObjectTranslations[Idx], '=');
if Length(TransArray) > 1 then
BdyStr := StringReplace(BdyStr, TransArray[0], TransArray[1], [rfReplaceAll,rfIgnoreCase]);
end;
end;
Result := BdyStr;
end;
function TDfmToFmxObject.ProcessUsesString(AOrigUsesArray: TArrayOfStrings): String;
var
i: integer;
begin
PopulateStringsFromArray(UsesTranslation, AOrigUsesArray);
UpdateUsesStringList(UsesTranslation);
Result := 'uses ';
for i := 0 to Pred(UsesTranslation.Count) do
if Trim(FUsesTranslation[i]) <> EmptyStr then
Result := Result + CRLF +' '+ FUsesTranslation[i] + ',';
SetLength(Result, Pred(Length(Result)));
end;
function TDfmToFmxObject.PropertyArray(ARow: integer): TArrayOfStrings;
begin
while ARow >= FPropertyArraySz do
begin
inc(FPropertyArraySz, 5);
SetLength(F2DPropertyArray, FPropertyArraySz);
end;
if ARow > FPropertyMax then
FPropertyMax := ARow;
Result := F2DPropertyArray[ARow];
end;
{ Eduardo }
procedure TDfmToFmxObject.ReadItems(Prop: TTwoDArrayOfString; APropertyIdx: integer; AStm: TStream);
var
Data: String;
saTemp: Array of String;
sTemp: String;
begin
Data := Trim(ReadLineFrmStream(AStm));
while not (Pos(AnsiString('>'), Data) > 0) do
begin
SetLength(saTemp, Succ(Length(saTemp)));
saTemp[Pred(Length(saTemp))] := Data;
Data := Trim(ReadLineFrmStream(AStm));
end;
SetLength(saTemp, Succ(Length(saTemp)));
saTemp[Pred(Length(saTemp))] := Data;
for sTemp in saTemp do
Prop[APropertyIdx, 1] := Prop[APropertyIdx, 1] + #13 + sTemp;
end;
{ Eduardo }
procedure TDfmToFmxObject.ReadData(Prop: TTwoDArrayOfString; APropertyIdx: integer; AStm: TStream);
var
Data: String;
begin
Data := Trim(ReadLineFrmStream(AStm));
while not (Pos(AnsiString('}'), Data) > 0) do
begin
Prop[APropertyIdx, 1] := Prop[APropertyIdx, 1] + Data;
Data := Trim(ReadLineFrmStream(AStm));
end;
Prop[APropertyIdx, 1] := Prop[APropertyIdx, 1] + Data;
end;
{ Eduardo }
procedure TDfmToFmxObject.ReadText(Prop: TTwoDArrayOfString; APropertyIdx: integer; AStm: TStream);
var
Data: String;
begin
Data := Trim(ReadLineFrmStream(AStm));
while (Pos(AnsiString('+'), Data) > 0) do
begin
Prop[APropertyIdx, 1] := Prop[APropertyIdx, 1] + Data;
Data := Trim(ReadLineFrmStream(AStm));
end;
Prop[APropertyIdx, 1] := Prop[APropertyIdx, 1] + Data;
end;
procedure TDfmToFmxObject.ReadProperties(AData: String; AStm: TStream; var AIdx: Integer);
begin
PropertyArray(AIdx);
F2DPropertyArray[AIdx] := GetArrayFromString(AData, '=');
if High(F2DPropertyArray[AIdx]) < 1 then
begin
SetLength(F2DPropertyArray[AIdx], 2);
F2DPropertyArray[AIdx, 0] := ContinueCode;
F2DPropertyArray[AIdx, 1] := AData;
end
else
if (F2DPropertyArray[AIdx,1] = '<') then
ReadItems(F2DPropertyArray, AIdx, AStm)
else
if (F2DPropertyArray[AIdx,1] = '{') then
ReadData(F2DPropertyArray, AIdx, AStm)
else
if (F2DPropertyArray[AIdx,1] = '') then
ReadText(F2DPropertyArray, AIdx, AStm);
Inc(AIdx);
end;
function TDfmToFmxObject.TransformProperty(ACurrentName, ACurrentValue: String; APad: String = ''): String;
var
s: String;
begin
if ACurrentName = ContinueCode then
Result := ACurrentValue
else
begin
s := FIniSectionValues.Values[ACurrentName];
if s = EmptyStr then
s := ACurrentName;
if s = '#Delete#' then
Result := EmptyStr
else
if Pos('#TAlign#', s) > 0 then
begin
ACurrentValue := StringReplace(ACurrentValue, 'al', EmptyStr, [rfReplaceAll]);
Result := ACurrentName +' = '+ ACurrentValue;
end
else
if Pos('#Class#', s) > 0 then
begin
if FDFMClass = 'TImage' then
Result := StringReplace(s, '#Class#', ProcessImage(ACurrentValue, APad), [])
else
if FDFMClass = 'TImageList' then
Result := StringReplace(s, '#Class#', ProcessImageList(ACurrentValue, APad), [])
end
else
Result := s +' = '+ ACurrentValue;
end;
end;
procedure TDfmToFmxObject.UpdateUsesStringList(AUsesList: TStrings);
var
i: integer;
Idx: integer;
begin
if FIniReplaceValues <> nil then
begin
for i := 0 to Pred(AUsesList.Count) do
begin
Idx := FIniReplaceValues.IndexOfName(AUsesList[i]);
if Idx >= 0 then
AUsesList[i] := FIniReplaceValues.ValueFromIndex[Idx];
end;
end;
for i := Pred(AUsesList.Count) downto 0 do
if Trim(AUsesList[i]) = EmptyStr then
AUsesList.Delete(i);
if FIniIncludeValues <> nil then
begin
for i := 0 to Pred(FIniIncludeValues.Count) do
begin
Idx := AUsesList.IndexOf(FIniIncludeValues[i]);
if Idx < 0 then
AUsesList.add(FIniIncludeValues[i]);
end;
end;
if FOwnedObjs = nil then
Exit;
for i := 0 to Pred(FOwnedObjs.Count) do
if FOwnedObjs[i] is TDfmToFmxObject then
TDfmToFmxObject(FOwnedObjs[i]).UpdateUsesStringList(AUsesList);
end;
function TDfmToFmxObject.UsesTranslation: TStringlist;
begin
if FUsesTranslation = nil then
FUsesTranslation := TStringlist.Create;
Result := FUsesTranslation;
end;
function TDfmToFmxObject.WriteFMXToFile(const AFmxFileName: String): Boolean;
var
OutFile: TFileStream;
s: AnsiString;
begin
s := AnsiString(FMXFile);
if String(s).IsEmpty then
raise Exception.Create('Não há dados para o arquivo FMX!');
if FileExists(AFmxFileName) then
RenameFile(AFmxFileName, ChangeFileExt(AFmxFileName, '.fbk'));
OutFile := TFileStream.Create(AFmxFileName, fmCreate);
try
OutFile.Write(s[1], Length(s));
Result := True;
finally
OutFile.Free;
end;
end;
function TDfmToFmxObject.WritePasToFile(const APasOutFileName, APascalSourceFileName: String): Boolean;
var
OutFile: TFileStream;
s: AnsiString;
begin
if not FileExists(APascalSourceFileName) then
raise Exception.Create('Pascal Source:' + APascalSourceFileName +
' Does not Exist');
s := GenPasFile(APascalSourceFileName);
if s = '' then
raise Exception.Create('No Data for Pas File');
s := AnsiString(StringReplace(String(s), ChangeFileExt(ExtractFileName(APascalSourceFileName), EmptyStr), ChangeFileExt(ExtractFileName(APasOutFileName), ''), [rfIgnoreCase]));
if FileExists(APasOutFileName) then
RenameFile(APasOutFileName, ChangeFileExt(APasOutFileName, '.bak'));
OutFile := TFileStream.Create(APasOutFileName, fmCreate);
try
OutFile.Write(s[1], Length(s));
Result := true;
finally
OutFile.Free;
end;
end;
{ TDfmToFmxListItem }
constructor TDfmToFmxListItem.Create(AOwner: TDfmToFmxObject; APropertyIdx: integer; AStm: TStream; ADepth: integer);
var
Data: String;
i,LoopCount: integer;
begin
FPropertyIndex := APropertyIdx;
FOwner := AOwner;
i := 0;
FDepth := ADepth;
Data := EmptyStr;
LoopCount := 55;
while (LoopCount > 0) and (Pos(AnsiString('end'),Data) <> 1) do
Begin
Dec(LoopCount);
if Pos(AnsiString('object'), Data) = 1 then
OwnedObjs.Add(TDfmToFmxObject.Create(Data, AStm, FDepth + 1))
else
ReadProperties(Data,AStm,i);
Data := Trim(ReadLineFrmStream(AStm));
if (Data <> EmptyStr) then
LoopCount := 55;
end;
SetLength(F2DPropertyArray, FPropertyMax + 1);
FHasMore := (Pos(AnsiString('end'),Data)=1) and not (Pos(AnsiString('end>'),Data) = 1);
end;
end.