source: cprs/branches/tmg-cprs/dklang-package-3.01/Converter_v2/Main.pas@ 1035

Last change on this file since 1035 was 468, checked in by Kevin Toppenberg, 16 years ago

CPRS v1.0.26.76

File size: 4.4 KB
RevLine 
[468]1unit Main;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, ExtCtrls;
8
9type
10 TfMain = class(TForm)
11 bBrowseOutputFile: TButton;
12 bGo: TButton;
13 bProjectFileBrowse: TButton;
14 cbRemoveFromProject: TCheckBox;
15 eOutputFile: TEdit;
16 eProjectFile: TEdit;
17 lConversionLog: TLabel;
18 lInfo: TLabel;
19 lOutputFile: TLabel;
20 lProjectFile: TLabel;
21 mConversionLog: TMemo;
22 odProjectFile: TOpenDialog;
23 pMain: TPanel;
24 sdOutputFile: TSaveDialog;
25 procedure bBrowseOutputFileClick(Sender: TObject);
26 procedure bGoClick(Sender: TObject);
27 procedure bProjectFileBrowseClick(Sender: TObject);
28 procedure eOutputFileChange(Sender: TObject);
29 procedure eProjectFileChange(Sender: TObject);
30 private
31 procedure UpdateState;
32 end;
33
34var
35 fMain: TfMain;
36
37implementation
38{$R *.dfm}
39uses DKLang, DKL_ResFile;
40
41 procedure TfMain.bBrowseOutputFileClick(Sender: TObject);
42 begin
43 sdOutputFile.FileName := eOutputFile.Text;
44 if sdOutputFile.Execute then eOutputFile.Text := sdOutputFile.FileName;
45 end;
46
47 procedure TfMain.bGoClick(Sender: TObject);
48 var
49 sProjectFile, sOutputFile: String;
50 ResFileIn, ResFileOut: TDKLang_ResFile;
51 ConstEntryIn, ConstEntryOut: TDKLang_ResEntry;
52
53 procedure LogString(const s: String); overload;
54 begin
55 mConversionLog.Lines.Add(s);
56 end;
57
58 procedure LogString(const s: String; const aParams: Array of const); overload;
59 begin
60 LogString(Format(s, aParams));
61 end;
62
63 begin
64 try
65 // Check prerequisites
66 sProjectFile := Trim(eProjectFile.Text);
67 sOutputFile := Trim(eOutputFile.Text);
68 LogString('Project file: %s', [sProjectFile]);
69 LogString('Output file: %s', [sOutputFile]);
70 if AnsiSameText(sProjectFile, sOutputFile) then raise Exception.Create('You shoudn''t specify the same file for both project and output files');
71 // Try to load the file
72 ResFileIn := TDKLang_ResFile.Create;
73 try
74 ResFileIn.LoadFromFile(sProjectFile);
75 LogString('Project file loaded successfully.');
76 // Look for constant resource
77 ConstEntryIn := ResFileIn.FindEntry(IntToStr(Integer(RT_RCDATA)), SDKLang_ConstResourceName);
78 if ConstEntryIn=nil then
79 LogString('The file doesn''t contain constant resource ("%s"), no need to convert it.', [SDKLang_ConstResourceName])
80 else begin
81 LogString('Constant resource "%s" found (%d bytes data).', [SDKLang_ConstResourceName, Length(ConstEntryIn.RawData)]);
82 // Create output file
83 ResFileOut := TDKLang_ResFile.Create;
84 try
85 // Copy the resource
86 ConstEntryOut := ConstEntryIn.Clone;
87 ResFileOut.AddEntry(ConstEntryOut);
88 LogString('Constant entry cloned.');
89 // Save the file
90 ResFileOut.SaveToFile(sOutputFile);
91 LogString('Output resource file saved successfully.');
92 finally
93 ResFileOut.Free;
94 end;
95 // Remove the entry from the input file, if needed
96 if cbRemoveFromProject.Checked then begin
97 ResFileIn.RemoveEntry(ConstEntryIn);
98 LogString('Constant entry removed from the project file.');
99 // Save the project resource file
100 ResFileIn.SaveToFile(sProjectFile);
101 LogString('Project resource file saved successfully.');
102 end;
103 end;
104 finally
105 ResFileIn.Free;
106 end;
107 LogString('Finished.');
108 LogString('----------------------------------------');
109 except
110 on e: Exception do begin
111 LogString('Exception class: %s', [e.ClassName]);
112 LogString('Exception message: %s', [e.Message]);
113 raise;
114 end;
115 end;
116 end;
117
118 procedure TfMain.bProjectFileBrowseClick(Sender: TObject);
119 begin
120 odProjectFile.FileName := eProjectFile.Text;
121 if odProjectFile.Execute then eProjectFile.Text := odProjectFile.FileName;
122 end;
123
124 procedure TfMain.eOutputFileChange(Sender: TObject);
125 begin
126 UpdateState;
127 end;
128
129 procedure TfMain.eProjectFileChange(Sender: TObject);
130 begin
131 eOutputFile.Text := ChangeFileExt(eProjectFile.Text, '.dkl_const.res');
132 UpdateState;
133 end;
134
135 procedure TfMain.UpdateState;
136 begin
137 bGo.Enabled := (eProjectFile.Text<>'') and (eOutputFile.Text<>'');
138 end;
139
140end.
Note: See TracBrowser for help on using the repository browser.