/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. }
  22. unit mainunit;
  23. {$mode objfpc}{$H+}
  24. interface
  25. uses
  26. Classes, SysUtils, Forms, Dialogs, ExtCtrls, StdCtrls, ComCtrls, Menus, FileCtrl,
  27. LazFileUtils, LazUTF8;
  28. type
  29. { TLazConverterForm }
  30. TLazConverterForm = class(TForm)
  31. Button1: TButton;
  32. Button2: TButton;
  33. Button3: TButton;
  34. ComboBox1: TComboBox;
  35. ComboBox2: TComboBox;
  36. Edit1: TEdit;
  37. FileListBox1: TFileListBox;
  38. Label1: TLabel;
  39. Label2: TLabel;
  40. Label3: TLabel;
  41. Label6: TLabel;
  42. MenuItem1: TMenuItem;
  43. Panel1: TPanel;
  44. PopupMenu1: TPopupMenu;
  45. ProgressBar1: TProgressBar;
  46. SelectDirectoryDialog1: TSelectDirectoryDialog;
  47. procedure Button1Click(Sender: TObject);
  48. procedure Button2Click(Sender: TObject);
  49. procedure Button3Click(Sender: TObject);
  50. procedure Form1Show(Sender: TObject);
  51. procedure MenuItem1Click(Sender: TObject);
  52. private
  53. public
  54. procedure CountFiles;
  55. end;
  56. var
  57. LazConverterForm: TLazConverterForm;
  58. const msgdone = 'Searching done... press CONVERT button! Files: ';
  59. implementation
  60. {$R *.lfm}
  61. function replace (source: string; src, rep: string):string;
  62. begin
  63. result:=StringReplace(source,src,rep,[rfReplaceAll]);
  64. end;
  65. function origpath:string;
  66. var tmp:string;
  67. begin
  68. tmp:=StrPas(argv[0]);
  69. tmp:=ExpandFileNameUTF8(tmp);
  70. tmp:=ExtractFileDir(tmp);
  71. tmp:=IncludeTrailingPathDelimiter(tmp);
  72. result:=tmp;
  73. end;
  74. procedure TLazConverterForm.CountFiles;
  75. begin
  76. if FileListBox1.Items.Count-1<>-1 then
  77. begin
  78. label6.caption:=msgdone+inttostr(FileListBox1.Items.Count-1);
  79. end else label6.caption:=msgdone+'0';
  80. end;
  81. procedure TLazConverterForm.Button1Click(Sender: TObject);
  82. begin
  83. if SelectDirectoryDialog1.Execute then
  84. begin
  85. edit1.text:=IncludeTrailingPathDelimiter(SelectDirectoryDialog1.FileName);
  86. end;
  87. end;
  88. procedure TLazConverterForm.Button2Click(Sender: TObject);
  89. begin
  90. if trim(edit1.text)='' then
  91. begin
  92. button1click(sender);abort;
  93. end;
  94. FileListBox1.Mask:='';
  95. FileListBox1.Directory:='';
  96. FileListBox1.Clear;
  97. FileListBox1.Directory:=Edit1.text;
  98. FileListBox1.Mask:='*.pas;*.lfm;*.lrs;*.inc;*.lrt';
  99. CountFiles;
  100. end;
  101. procedure TLazConverterForm.Button3Click(Sender: TObject);
  102. var tmp:tstringlist;
  103. i:integer;
  104. ok:boolean;
  105. function ConvertMe(var tmp:tstringlist):boolean;
  106. var cpin,cpout:tstringlist;
  107. ok:boolean;
  108. i:integer;
  109. begin
  110. ok:=true;result:=true;
  111. if trim(tmp.text)='' then result:=false else
  112. begin
  113. cpin:=tstringlist.create;
  114. cpout:=tstringlist.create;
  115. try
  116. try
  117. cpin.LoadFromFile(UTF8ToSys(origpath+combobox1.text));
  118. except
  119. ok:=false;
  120. end;
  121. try
  122. cpout.LoadFromFile(UTF8ToSys(origpath+combobox2.text));
  123. except
  124. ok:=false;
  125. end;
  126. if ok then
  127. begin
  128. if cpin.Count=cpout.Count then
  129. begin
  130. for i:=0 to cpin.Count-1 do
  131. begin
  132. cpin[i]:=trim(cpin[i]);
  133. cpout[i]:=trim(cpout[i]);
  134. if (cpin[i]<>'') and (cpout[i]<>'') then
  135. begin
  136. tmp.text:=Replace(tmp.text,char(strtoint(cpin[i])),char(strtoint(cpout[i])));
  137. end;
  138. end;
  139. end else result:=false;
  140. end else result:=false;
  141. finally
  142. cpin.free;
  143. cpout.free;
  144. end;
  145. end;
  146. end;
  147. function ConvertMeLRS(var tmp:tstringlist):boolean;
  148. var cpin,cpout:tstringlist;
  149. ok:boolean;
  150. i:integer;
  151. begin
  152. result:=true;
  153. ok:=true;
  154. if trim(tmp.text)='' then result:=false else
  155. begin
  156. cpin:=tstringlist.create;
  157. cpout:=tstringlist.create;
  158. try
  159. try
  160. cpin.LoadFromFile(UTF8ToSys(origpath+combobox1.text));
  161. except
  162. ok:=false;
  163. end;
  164. try
  165. cpout.LoadFromFile(UTF8ToSys(origpath+combobox2.text));
  166. except
  167. ok:=false;
  168. end;
  169. if ok then
  170. begin
  171. if cpin.Count=cpout.Count then
  172. begin
  173. for i:=0 to cpin.Count-1 do
  174. begin
  175. cpin[i]:=trim(cpin[i]);
  176. cpout[i]:=trim(cpout[i]);
  177. if (cpin[i]<>'') and (cpout[i]<>'') then
  178. begin
  179. tmp.text:=Replace(tmp.text,'#'+cpin[i],'#'+cpout[i]);
  180. end;
  181. end;
  182. end else result:=false;
  183. end else result:=false;
  184. finally
  185. cpin.free;
  186. cpout.free;
  187. end;
  188. end;
  189. end;
  190. begin
  191. if FileListBox1.Items.count-1=-1 then abort;
  192. if trim(combobox1.text)='' then abort;
  193. if trim(combobox2.text)='' then abort;
  194. tmp:=tstringlist.create;
  195. try
  196. ProgressBar1.Min:=0;
  197. ProgressBar1.Max:=FileListBox1.Items.count;
  198. ProgressBar1.StepBy(1);
  199. for i:=0 to FileListBox1.Items.count-1 do
  200. begin
  201. ok:=true;
  202. try
  203. tmp.LoadFromFile(UTF8ToSys(FileListBox1.Directory+FileListBox1.Items[i]));
  204. except
  205. ok:=false;
  206. end;
  207. if ok then
  208. begin
  209. if uppercase(ExtractFileExt(FileListBox1.Items[i]))='.LRS' then
  210. begin
  211. if ConvertMeLRS(tmp) then
  212. begin
  213. ok:=true;
  214. try
  215. tmp.SaveToFile(UTF8ToSys(FileListBox1.Directory+FileListBox1.Items[i]));
  216. except ok:=false; end;
  217. end;
  218. end else
  219. begin
  220. if ConvertMe(tmp) then
  221. begin
  222. ok:=true;
  223. try
  224. tmp.SaveToFile(UTF8ToSys(FileListBox1.Directory+FileListBox1.Items[i]));
  225. except ok:=false; end;
  226. end;
  227. end;
  228. end;
  229. ProgressBar1.StepIt;
  230. end;
  231. finally
  232. ProgressBar1.Position:=0;
  233. tmp.free;
  234. label6.caption:='Done!';
  235. FileListBox1.Mask:='';
  236. FileListBox1.Directory:='';
  237. FileListBox1.Clear;
  238. end;
  239. end;
  240. procedure TLazConverterForm.Form1Show(Sender: TObject);
  241. begin
  242. if paramcount<>0 then edit1.text:=ParamStrUTF8(1) else edit1.text:='';
  243. try
  244. ComboBox1.items.LoadFromFile(UTF8ToSys(origpath+'codepages.ini'));
  245. except end;
  246. try
  247. ComboBox2.items.LoadFromFile(UTF8ToSys(origpath+'codepages.ini'));
  248. except end;
  249. ComboBox1.text:='';
  250. ComboBox2.text:='';
  251. Edit1.SetFocus;
  252. FileListBox1.Mask:='';
  253. FileListBox1.Directory:='';
  254. FileListBox1.Clear;
  255. end;
  256. procedure TLazConverterForm.MenuItem1Click(Sender: TObject);
  257. begin
  258. if FileListBox1.Items.count-1=-1 then abort;
  259. try
  260. FileListBox1.Items.Delete(FileListBox1.ItemIndex);
  261. except end;
  262. CountFiles;
  263. end;
  264. end.