File: mainunit.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (288 lines) | stat: -rw-r--r-- 7,997 bytes parent folder | download | duplicates (2)
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
{
      Author: BARKO, OPINFOS d.o.o., SLOVENIA http://www.opinfos.com
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************
}
unit mainunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Dialogs, ExtCtrls, StdCtrls, ComCtrls, Menus, FileCtrl,
  LazFileUtils, LazUTF8, DefaultTranslator;

type

  { TLazConverterForm }

  TLazConverterForm = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    Edit1: TEdit;
    FileListBox1: TFileListBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label6: TLabel;
    MenuItem1: TMenuItem;
    Panel1: TPanel;
    PopupMenu1: TPopupMenu;
    ProgressBar1: TProgressBar;
    SelectDirectoryDialog1: TSelectDirectoryDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Form1Show(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
  private

  public
    procedure CountFiles;
  end; 

var
  LazConverterForm: TLazConverterForm;
  
const msgdone = 'Searching done... press CONVERT button! Files: ';

implementation

{$R *.lfm}

function replace (source: string; src, rep: string):string;
begin
  result:=StringReplace(source,src,rep,[rfReplaceAll]);
end;

function origpath:string;
var tmp:string;
begin
  tmp:=StrPas(argv[0]);
  tmp:=ExpandFileNameUTF8(tmp);
  tmp:=ExtractFileDir(tmp);
  tmp:=IncludeTrailingPathDelimiter(tmp);
  result:=tmp;
end;

procedure TLazConverterForm.CountFiles;
begin
  if FileListBox1.Items.Count-1<>-1 then
  begin
    label6.caption:=msgdone+inttostr(FileListBox1.Items.Count-1);
  end else label6.caption:=msgdone+'0';
end;

procedure TLazConverterForm.Button1Click(Sender: TObject);
begin
  if SelectDirectoryDialog1.Execute then
  begin
    edit1.text:=IncludeTrailingPathDelimiter(SelectDirectoryDialog1.FileName);
  end;
end;

procedure TLazConverterForm.Button2Click(Sender: TObject);
begin
  if trim(edit1.text)='' then
  begin
    button1click(sender);abort;
  end;
  FileListBox1.Mask:='';
  FileListBox1.Directory:='';
  FileListBox1.Clear;
  FileListBox1.Directory:=Edit1.text;
  FileListBox1.Mask:='*.pas;*.lfm;*.lrs;*.inc;*.lrt';
  CountFiles;
end;

procedure TLazConverterForm.Button3Click(Sender: TObject);
var tmp:tstringlist;
    i:integer;
    ok:boolean;

   function ConvertMe(var tmp:tstringlist):boolean;
   var cpin,cpout:tstringlist;
       ok:boolean;
       i:integer;
   begin
     ok:=true;result:=true;
     if trim(tmp.text)='' then result:=false else
     begin
       cpin:=tstringlist.create;
       cpout:=tstringlist.create;
       try
         try
           cpin.LoadFromFile(UTF8ToSys(origpath+combobox1.text));
         except
           ok:=false;
         end;
         try
           cpout.LoadFromFile(UTF8ToSys(origpath+combobox2.text));
         except
           ok:=false;
         end;
         if ok then
         begin
           if cpin.Count=cpout.Count then
           begin
             for i:=0 to cpin.Count-1 do
             begin
               cpin[i]:=trim(cpin[i]);
               cpout[i]:=trim(cpout[i]);
               if (cpin[i]<>'') and (cpout[i]<>'') then
               begin
                 tmp.text:=Replace(tmp.text,char(strtoint(cpin[i])),char(strtoint(cpout[i])));
               end;
             end;
           end else result:=false;
         end else result:=false;
       finally
         cpin.free;
         cpout.free;
       end;
     end;
   end;

   function ConvertMeLRS(var tmp:tstringlist):boolean;
   var cpin,cpout:tstringlist;
       ok:boolean;
       i:integer;
   begin
     result:=true;
     ok:=true;
     if trim(tmp.text)='' then result:=false else
     begin
       cpin:=tstringlist.create;
       cpout:=tstringlist.create;
       try
         try
           cpin.LoadFromFile(UTF8ToSys(origpath+combobox1.text));
         except
          ok:=false;
         end;
         try
           cpout.LoadFromFile(UTF8ToSys(origpath+combobox2.text));
         except
           ok:=false;
         end;
         if ok then
         begin
           if cpin.Count=cpout.Count then
           begin
             for i:=0 to cpin.Count-1 do
             begin
               cpin[i]:=trim(cpin[i]);
               cpout[i]:=trim(cpout[i]);
               if (cpin[i]<>'') and (cpout[i]<>'') then
               begin
                 tmp.text:=Replace(tmp.text,'#'+cpin[i],'#'+cpout[i]);
               end;
             end;
           end else result:=false;
         end else result:=false;
       finally
         cpin.free;
         cpout.free;
       end;
     end;
   end;

begin
  if FileListBox1.Items.count-1=-1 then abort;
  if trim(combobox1.text)='' then abort;
  if trim(combobox2.text)='' then abort;
  tmp:=tstringlist.create;
  try
    ProgressBar1.Min:=0;
    ProgressBar1.Max:=FileListBox1.Items.count;
    ProgressBar1.StepBy(1);
    for i:=0 to FileListBox1.Items.count-1 do
    begin
      ok:=true;
      try
        tmp.LoadFromFile(UTF8ToSys(FileListBox1.Directory+FileListBox1.Items[i]));
      except
        ok:=false;
      end;
      if ok then
      begin
        if uppercase(ExtractFileExt(FileListBox1.Items[i]))='.LRS' then
        begin
          if ConvertMeLRS(tmp) then
          begin
            ok:=true;
            try
              tmp.SaveToFile(UTF8ToSys(FileListBox1.Directory+FileListBox1.Items[i]));
            except ok:=false; end;
          end;
        end else
        begin
          if ConvertMe(tmp) then
          begin
            ok:=true;
            try
              tmp.SaveToFile(UTF8ToSys(FileListBox1.Directory+FileListBox1.Items[i]));
            except ok:=false; end;
          end;
        end;
      end;
      ProgressBar1.StepIt;
    end;
  finally
    ProgressBar1.Position:=0;
    tmp.free;
    label6.caption:='Done!';
    FileListBox1.Mask:='';
    FileListBox1.Directory:='';
    FileListBox1.Clear;
  end;
end;

procedure TLazConverterForm.Form1Show(Sender: TObject);
begin
  if paramcount<>0 then edit1.text:=ParamStrUTF8(1) else edit1.text:='';
  try
    ComboBox1.items.LoadFromFile(UTF8ToSys(origpath+'codepages.ini'));
  except end;
  try
    ComboBox2.items.LoadFromFile(UTF8ToSys(origpath+'codepages.ini'));
  except end;
  ComboBox1.text:='';
  ComboBox2.text:='';
  Edit1.SetFocus;
  FileListBox1.Mask:='';
  FileListBox1.Directory:='';
  FileListBox1.Clear;
end;

procedure TLazConverterForm.MenuItem1Click(Sender: TObject);
begin
 if FileListBox1.Items.count-1=-1 then abort;
 try
  FileListBox1.Items.Delete(FileListBox1.ItemIndex);
 except end;
 CountFiles;
end;

end.