unit fun_1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, cp866, cp1251, LConvEncoding;



  procedure save_win();
  procedure run_win();
  procedure console_win();
  procedure  compiling_win();
  function CP866_to_UTF8(var MyCP866: ansistring) : widestring;
  function UTF8_to_CP866(MyUtf8: widestring): ansistring;
  function UTF8_to_CP1251(MyUtf8: widestring): ansistring;
  function CP1251_to_UTF8(var MyCP1251: ansistring): widestring;


    var
    MyUtf8: widestring;
    MyCP866: ansistring;
    MyCP1251: ansistring;

implementation

uses  Unit1;



function CP1251_to_UTF8(var MyCP1251: ansistring): widestring;
begin
        MyUtf8:= ConvertEncoding(MyCP1251, EncodingCP1251, EncodingUTF8);
        CP1251_to_UTF8:= MyUtf8;
end;
function CP866_to_UTF8(var MyCP866: ansistring): widestring;
begin
        MyUtf8:= ConvertEncoding(MyCP866, EncodingCP866, EncodingUTF8);
        CP866_to_UTF8:= MyUtf8;
end;

function UTF8_to_CP866(MyUtf8: widestring): ansistring;
begin
          MyCP866:= ConvertEncoding(MyUtf8, EncodingUTF8, EncodingCP866);
          UTF8_to_CP866:= MyCP866;
end;
function UTF8_to_CP1251(MyUtf8: widestring): ansistring;
begin
          MyCP1251:= ConvertEncoding(MyUtf8, EncodingUTF8, EncodingCP1251);
          UTF8_to_CP1251:= MyCP1251;
end;




procedure save_win();
begin
     if Form1.RadioButton1.Checked then
     begin
          Unit1.ListCP866.Text:=UTF8_to_CP866(Unit1.Form1.SynEdit1.Text);
          Unit1.ListCP866.SaveToFile(fullFileName);
     end;
     if Form1.RadioButton2.Checked then
     begin
          Unit1.ListCP1251.Text:=UTF8_to_CP1251(Unit1.Form1.SynEdit1.Text);
          Unit1.ListCP1251.SaveToFile(fullFileName);
     end;
     if Form1.RadioButton3.Checked then
     begin
          Unit1.Form1.SynEdit1.Lines.SaveToFile(fullFileName);

     end;
end;


procedure run_win();
var str,sss,FileName : String;
i, vc: Integer;
begin
    ChDir(ExtractFileDir(fullFileName));
    FileName:='';
     FileName := ExtractFileName(fullFileName);
     i:= Length(FileName);
     vc:= 3;
     sss:=Copy(FileName,1,i-vc);
    str:='';
    str:= sss + 'exe';
    Form1.StatusBar1.SimpleText:=str;
    Form1.ProcessUTF8_1.CommandLine:=str;
    Form1.ProcessUTF8_1.Execute;
end;

procedure  compiling_win();
var str,sss,FileName : String;
i: Integer;
begin
    ChDir(ExtractFileDir(fullFileName));
    FileName:='';
     FileName := ExtractFileName(fullFileName);
     save_win();
         Form1.Caption:=FileName;
    i:= Length(FileName);
    sss:=Copy(FileName,1,i-3);
    str:='';
    if fpc_path<>'' then
       str:=fpc_path + st_p + 'fpc ' + FileName
    else
       str:='fpc ' + FileName;

    Form1.StatusBar1.SimpleText:=str;
    Form1.ProcessUTF8_1.CommandLine:=str;
    Form1.ProcessUTF8_1.Execute;
    Form1.ProcessUTF8_1.WaitOnExit;
    if Form1.CheckBox1.Checked then
    begin
     if fpc_path<>'' then
      str:= fpc_path + st_p + 'strip ' + sss + 'exe'
     else
       str:='strip ' + sss + 'exe';

      Form1.ProcessUTF8_1.CommandLine:=str;
      Form1.ProcessUTF8_1.Execute;
      Form1.ProcessUTF8_1.WaitOnExit;
    end;
    if Form1.CheckBox2.Checked then
    begin
      str:= pr_path_dir + 'upx ' + sss + 'exe';

      Form1.ProcessUTF8_1.CommandLine:=str;
      Form1.ProcessUTF8_1.Execute;
      Form1.ProcessUTF8_1.WaitOnExit;
    end;
end;

procedure console_win();
 var sss, FileName :String;
     i:  integer;
begin
    Form1.SynEdit1.Lines.SaveToFile(fullFileName);
    ChDir(ExtractFileDir(fullFileName));
    FileName := ExtractFileName(fullFileName);
    Form1.Caption:=FileName;
    i:= Length(FileName);
    sss:=Copy(FileName,1,i-3);


     list := TStringList.Create;
     list.Clear;
     list.Add('echo off');
     list.Add('');
     list.Add('echo ------------------------Компиляция программы----------- ');
     list.Add('echo. ');
     if fpc_path<>'' then
     list.Add(fpc_path + st_p + 'fpc ' + pgui + ' ' + FileName)
     else
     list.Add('fpc ' + FileName);
     list.Add('echo. ');
     if Form1.CheckBox1.Checked then
        begin
             if fpc_path<>'' then
             begin
                list.Add(fpc_path + st_p + 'strip ' + sss + 'exe');
                list.Add('echo. ');
             end
             else
                 begin
                 list.Add('strip ' + sss + 'exe');
                 list.Add('echo. ');
                 end;
        end;
     if Form1.CheckBox2.Checked then
      list.Add(pr_path_dir + 'upx ' + sss + 'exe');
     list.Add('echo ------------------------Конец компиляции------------');
     list.Add('echo ------------------------Запуск программы--------------');
     list.Add('echo. ');
     list.Add(sss + 'exe');
     list.Add('echo. ');
     list.Add('echo ------------------------Конец программы--------------');
     list.Add('echo. ');
     list.Add('pause');
     save_win();
     Unit1.ListCP866.Text:=UTF8_to_CP866(list.Text);
     Unit1.ListCP866.SaveToFile('compile_run.bat');
     list.Destroy;
end;



end.

