unit uHex2Bin;
{
This code is part of the TiCodEd Application, designed to develop for the
TI-99/4a Homecomputer in a modern, structured way.

BSD License
Copyright (c) 2026, Stefan Bauch, TiCodEd <at> lizardware <dot> de

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

}
{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  Buttons, MaskEdit;

type

  { TFormHex2Bin }

  TFormHex2Bin = class(TForm)
    BbClose: TBitBtn;
    BbExec: TBitBtn;
    BbUndo: TBitBtn;
    BnClean: TButton;
    BnCondense: TButton;
    BnRemCol: TButton;
    BnGoReplace: TButton;
    CBData: TCheckBox;
    cbSibling: TCheckBox;
    cbIgnCRLF: TCheckBox;
    cbIgnTab: TCheckBox;
    cbUppercase: TCheckBox;
    EdReplFrom: TEdit;
    EdReplTo: TEdit;
    GBparam: TGroupBox;
    GBCleanPar: TGroupBox;
    GBreplall: TGroupBox;
    MEmax: TMaskEdit;
    MemoInput: TMemo;
    MemoOutput: TMemo;
    Panel1: TPanel;
    Panel2: TPanel;
    SB1MCshape: TSpeedButton;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    StaticText4: TStaticText;
    stRemCol: TStaticText;
    TimerSel: TTimer;
    procedure BbCloseClick(Sender: TObject);
    procedure BbExecClick(Sender: TObject);
    procedure BbUndoClick(Sender: TObject);
    procedure BnCleanClick(Sender: TObject);
    procedure BnCondenseClick(Sender: TObject);
    procedure BnGoReplaceClick(Sender: TObject);
    procedure BnRemColClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SB1MCshapeClick(Sender: TObject);
    procedure StaticText1DblClick(Sender: TObject);
    procedure TimerSelTimer(Sender: TObject);
  private

  public

  end;

var
  FormHex2Bin: TFormHex2Bin;

implementation

{$R *.lfm}

{ TFormHex2Bin }

uses Clipbrd;

const hexdig  = ['0'..'9','A'..'F','a'..'f'];
      crlftab = [#11,#13,#9];

var bak : AnsiString;
    rc1,rc2 : Integer;

procedure TFormHex2Bin.FormShow(Sender: TObject);
begin
  Panel1.Visible:=true;
  bak := '';
end;

procedure TFormHex2Bin.SB1MCshapeClick(Sender: TObject);
begin
  bak := MemoInput.Text;
  MemoInput.Text := stringreplace(MemoInput.Text,'byte','',[rfReplaceAll]);
  MemoInput.Text := stringreplace(MemoInput.Text,'        ','',[rfReplaceAll]);
  MemoInput.Text := stringreplace(MemoInput.Text,'>0','',[rfReplaceAll]);
  MemoInput.Text := stringreplace(MemoInput.Text,',','',[rfReplaceAll]);
end;

procedure TFormHex2Bin.BbCloseClick(Sender: TObject);
begin
  Clipboard.AsText:=MemoOutput.Text;
  FormHex2Bin.Hide;
end;

procedure TFormHex2Bin.BbExecClick(Sender: TObject);
var i,mc : integer;
    s,t : AnsiString;
    c : char;
begin
  t := MEmax.text;
  t := '';
  s := MemoInput.text;
  MemoOutput.Text:='';
  mc := 2 * StrToInt(trim(MEmax.text));
  for i:=0 to length(s) do begin
    c := s.Chars[i];
    if    ((c=#9) and not cbIgnTab.Checked)
       or (((c=#11) or (c=#13)) and not cbIgnCRLF.Checked)
       or (length(t) >= mc)
       or (i = length(s))
       then if trim(t)<>'' then begin
         t := '"' + t + '"';
         if CBData.Checked then t := 'DATA ' + t;
         MemoOutput.Lines.Add(t);
         t := '';
       end else else if c in hexdig then
          if cbUppercase.Checked then t := t + Uppercase(c)
                                 else t := t + c;
  end;
end;

procedure TFormHex2Bin.BbUndoClick(Sender: TObject);
begin
  if bak <> '' then MemoInput.Text := bak;
end;

procedure TFormHex2Bin.BnCleanClick(Sender: TObject);
var i : integer;
    s,t : AnsiString;
    c : char;
begin
  t := '';
  bak := MemoInput.Text;
  if cbSibling.Checked then begin
    s := ' '+MemoInput.Text+' ';
    for i:=0 to length(s) do begin
      c := s.Chars[i];
      if ((c in hexdig) and ((s.Chars[i-1] in hexdig) or (s.Chars[i+1]in hexdig))) or (c in crlftab)
        then t:=t+c else t:=t+' ';
    end;
    MemoInput.Text:= copy(t,2,length(t)-2);
  end else begin
    s := MemoInput.Text;
    for i:=1 to length(s) do begin
      c := s.Chars[i];
      if (c in hexdig) or (c in crlftab) then t:=t+c else t:=t+' ';
    end;
    MemoInput.Text:=t;
  end;
end;

procedure TFormHex2Bin.BnCondenseClick(Sender: TObject);
var i : integer;
begin
  bak := MemoInput.Text;
  MemoInput.Text := stringreplace(MemoInput.Text,'  ','',[rfReplaceAll]);
  for i:=MemoInput.Lines.Count-1 downto 0 do begin
    MemoInput.Lines.Strings[i] := trim(MemoInput.Lines.Strings[i]);
    if MemoInput.Lines.Strings[i]='' then MemoInput.Lines.Delete(i);
  end;
end;

procedure TFormHex2Bin.BnGoReplaceClick(Sender: TObject);
begin
  bak := MemoInput.Text;
  MemoInput.Text := stringreplace(MemoInput.Text,EdReplFrom.Text,EdReplTo.Text,[rfReplaceAll]);
end;

procedure TFormHex2Bin.BnRemColClick(Sender: TObject);
var i : integer;
begin
  if rc1=rc2 then Exit;
  bak := MemoInput.Text;
  for i:=MemoInput.Lines.Count-1 downto 0 do begin
    MemoInput.Lines.Strings[i] := copy(MemoInput.Lines.Strings[i],1,rc1)
                                + copy(MemoInput.Lines.Strings[i],rc2+1,255);
  end;
end;

procedure TFormHex2Bin.StaticText1DblClick(Sender: TObject);
begin
  Panel1.Visible:=false;
end;

procedure TFormHex2Bin.TimerSelTimer(Sender: TObject);
var tp : TPoint;
begin
   tp := MemoInput.CaretPos;
   if tp.y = 0 then begin
     rc1:=MemoInput.SelStart;
     rc2:=rc1+MemoInput.SelLength;
     if rc1=rc2 then
       stRemCol.Caption := 'Remove Cols: '
     else
       stRemCol.Caption := 'Remove Cols: '+inttostr(rc1)+ ' to ' + inttostr(rc2);
   end;
end;

end.

