PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/examples/codepageconverter/mainunit.pas

http://github.com/graemeg/lazarus
Pascal | 288 lines | 240 code | 24 blank | 24 comment | 29 complexity | bfec68ca881f1ff174a02141ccae28fe MD5 | raw file
  1{
  2      Author: BARKO, OPINFOS d.o.o., SLOVENIA http://www.opinfos.com
  3 ***************************************************************************
  4 *                                                                         *
  5 *   This source is free software; you can redistribute it and/or modify   *
  6 *   it under the terms of the GNU General Public License as published by  *
  7 *   the Free Software Foundation; either version 2 of the License, or     *
  8 *   (at your option) any later version.                                   *
  9 *                                                                         *
 10 *   This code is distributed in the hope that it will be useful, but      *
 11 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 13 *   General Public License for more details.                              *
 14 *                                                                         *
 15 *   A copy of the GNU General Public License is available on the World    *
 16 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 17 *   obtain it by writing to the Free Software Foundation,                 *
 18 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 19 *                                                                         *
 20 ***************************************************************************
 21}
 22unit mainunit;
 23
 24{$mode objfpc}{$H+}
 25
 26interface
 27
 28uses
 29  Classes, SysUtils, Forms, Dialogs, ExtCtrls, StdCtrls, ComCtrls, Menus, FileCtrl,
 30  LazFileUtils, LazUTF8;
 31
 32type
 33
 34  { TLazConverterForm }
 35
 36  TLazConverterForm = class(TForm)
 37    Button1: TButton;
 38    Button2: TButton;
 39    Button3: TButton;
 40    ComboBox1: TComboBox;
 41    ComboBox2: TComboBox;
 42    Edit1: TEdit;
 43    FileListBox1: TFileListBox;
 44    Label1: TLabel;
 45    Label2: TLabel;
 46    Label3: TLabel;
 47    Label6: TLabel;
 48    MenuItem1: TMenuItem;
 49    Panel1: TPanel;
 50    PopupMenu1: TPopupMenu;
 51    ProgressBar1: TProgressBar;
 52    SelectDirectoryDialog1: TSelectDirectoryDialog;
 53    procedure Button1Click(Sender: TObject);
 54    procedure Button2Click(Sender: TObject);
 55    procedure Button3Click(Sender: TObject);
 56    procedure Form1Show(Sender: TObject);
 57    procedure MenuItem1Click(Sender: TObject);
 58  private
 59
 60  public
 61    procedure CountFiles;
 62  end; 
 63
 64var
 65  LazConverterForm: TLazConverterForm;
 66  
 67const msgdone = 'Searching done... press CONVERT button! Files: ';
 68
 69implementation
 70
 71{$R *.lfm}
 72
 73function replace (source: string; src, rep: string):string;
 74begin
 75  result:=StringReplace(source,src,rep,[rfReplaceAll]);
 76end;
 77
 78function origpath:string;
 79var tmp:string;
 80begin
 81  tmp:=StrPas(argv[0]);
 82  tmp:=ExpandFileNameUTF8(tmp);
 83  tmp:=ExtractFileDir(tmp);
 84  tmp:=IncludeTrailingPathDelimiter(tmp);
 85  result:=tmp;
 86end;
 87
 88procedure TLazConverterForm.CountFiles;
 89begin
 90  if FileListBox1.Items.Count-1<>-1 then
 91  begin
 92    label6.caption:=msgdone+inttostr(FileListBox1.Items.Count-1);
 93  end else label6.caption:=msgdone+'0';
 94end;
 95
 96procedure TLazConverterForm.Button1Click(Sender: TObject);
 97begin
 98  if SelectDirectoryDialog1.Execute then
 99  begin
100    edit1.text:=IncludeTrailingPathDelimiter(SelectDirectoryDialog1.FileName);
101  end;
102end;
103
104procedure TLazConverterForm.Button2Click(Sender: TObject);
105begin
106  if trim(edit1.text)='' then
107  begin
108    button1click(sender);abort;
109  end;
110  FileListBox1.Mask:='';
111  FileListBox1.Directory:='';
112  FileListBox1.Clear;
113  FileListBox1.Directory:=Edit1.text;
114  FileListBox1.Mask:='*.pas;*.lfm;*.lrs;*.inc;*.lrt';
115  CountFiles;
116end;
117
118procedure TLazConverterForm.Button3Click(Sender: TObject);
119var tmp:tstringlist;
120    i:integer;
121    ok:boolean;
122
123   function ConvertMe(var tmp:tstringlist):boolean;
124   var cpin,cpout:tstringlist;
125       ok:boolean;
126       i:integer;
127   begin
128     ok:=true;result:=true;
129     if trim(tmp.text)='' then result:=false else
130     begin
131       cpin:=tstringlist.create;
132       cpout:=tstringlist.create;
133       try
134         try
135           cpin.LoadFromFile(UTF8ToSys(origpath+combobox1.text));
136         except
137           ok:=false;
138         end;
139         try
140           cpout.LoadFromFile(UTF8ToSys(origpath+combobox2.text));
141         except
142           ok:=false;
143         end;
144         if ok then
145         begin
146           if cpin.Count=cpout.Count then
147           begin
148             for i:=0 to cpin.Count-1 do
149             begin
150               cpin[i]:=trim(cpin[i]);
151               cpout[i]:=trim(cpout[i]);
152               if (cpin[i]<>'') and (cpout[i]<>'') then
153               begin
154                 tmp.text:=Replace(tmp.text,char(strtoint(cpin[i])),char(strtoint(cpout[i])));
155               end;
156             end;
157           end else result:=false;
158         end else result:=false;
159       finally
160         cpin.free;
161         cpout.free;
162       end;
163     end;
164   end;
165
166   function ConvertMeLRS(var tmp:tstringlist):boolean;
167   var cpin,cpout:tstringlist;
168       ok:boolean;
169       i:integer;
170   begin
171     result:=true;
172     ok:=true;
173     if trim(tmp.text)='' then result:=false else
174     begin
175       cpin:=tstringlist.create;
176       cpout:=tstringlist.create;
177       try
178         try
179           cpin.LoadFromFile(UTF8ToSys(origpath+combobox1.text));
180         except
181          ok:=false;
182         end;
183         try
184           cpout.LoadFromFile(UTF8ToSys(origpath+combobox2.text));
185         except
186           ok:=false;
187         end;
188         if ok then
189         begin
190           if cpin.Count=cpout.Count then
191           begin
192             for i:=0 to cpin.Count-1 do
193             begin
194               cpin[i]:=trim(cpin[i]);
195               cpout[i]:=trim(cpout[i]);
196               if (cpin[i]<>'') and (cpout[i]<>'') then
197               begin
198                 tmp.text:=Replace(tmp.text,'#'+cpin[i],'#'+cpout[i]);
199               end;
200             end;
201           end else result:=false;
202         end else result:=false;
203       finally
204         cpin.free;
205         cpout.free;
206       end;
207     end;
208   end;
209
210begin
211  if FileListBox1.Items.count-1=-1 then abort;
212  if trim(combobox1.text)='' then abort;
213  if trim(combobox2.text)='' then abort;
214  tmp:=tstringlist.create;
215  try
216    ProgressBar1.Min:=0;
217    ProgressBar1.Max:=FileListBox1.Items.count;
218    ProgressBar1.StepBy(1);
219    for i:=0 to FileListBox1.Items.count-1 do
220    begin
221      ok:=true;
222      try
223        tmp.LoadFromFile(UTF8ToSys(FileListBox1.Directory+FileListBox1.Items[i]));
224      except
225        ok:=false;
226      end;
227      if ok then
228      begin
229        if uppercase(ExtractFileExt(FileListBox1.Items[i]))='.LRS' then
230        begin
231          if ConvertMeLRS(tmp) then
232          begin
233            ok:=true;
234            try
235              tmp.SaveToFile(UTF8ToSys(FileListBox1.Directory+FileListBox1.Items[i]));
236            except ok:=false; end;
237          end;
238        end else
239        begin
240          if ConvertMe(tmp) then
241          begin
242            ok:=true;
243            try
244              tmp.SaveToFile(UTF8ToSys(FileListBox1.Directory+FileListBox1.Items[i]));
245            except ok:=false; end;
246          end;
247        end;
248      end;
249      ProgressBar1.StepIt;
250    end;
251  finally
252    ProgressBar1.Position:=0;
253    tmp.free;
254    label6.caption:='Done!';
255    FileListBox1.Mask:='';
256    FileListBox1.Directory:='';
257    FileListBox1.Clear;
258  end;
259end;
260
261procedure TLazConverterForm.Form1Show(Sender: TObject);
262begin
263  if paramcount<>0 then edit1.text:=ParamStrUTF8(1) else edit1.text:='';
264  try
265    ComboBox1.items.LoadFromFile(UTF8ToSys(origpath+'codepages.ini'));
266  except end;
267  try
268    ComboBox2.items.LoadFromFile(UTF8ToSys(origpath+'codepages.ini'));
269  except end;
270  ComboBox1.text:='';
271  ComboBox2.text:='';
272  Edit1.SetFocus;
273  FileListBox1.Mask:='';
274  FileListBox1.Directory:='';
275  FileListBox1.Clear;
276end;
277
278procedure TLazConverterForm.MenuItem1Click(Sender: TObject);
279begin
280 if FileListBox1.Items.count-1=-1 then abort;
281 try
282  FileListBox1.Items.Delete(FileListBox1.ItemIndex);
283 except end;
284 CountFiles;
285end;
286
287end.
288