{
  Copyright 1998-2026 PasDoc developers.

  This file is part of "PasDoc".

  "PasDoc" 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.

  "PasDoc" 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.

  You should have received a copy of the GNU General Public License
  along with "PasDoc"; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

  ----------------------------------------------------------------------------
}

{ Reads file $1 and writes Pascal source file $2,
  such that $2 file contains type and value of Pascal constant, i.e.
    array[0 .. Xxx]of Byte = ( ... )
  that has the contents of file $1.

  File is treated as binary file, so file contents are reflected exactly,
  byte by byte, in $2.

  Useful if you want to "embed" some binary file inside compiled
  Pascal program. }

program file_to_pascal_data;

{$ifdef MSWINDOWS}
  {$apptype CONSOLE}
{$endif}
{$ifdef FPC}
  {$mode objfpc}
  {$H+}
{$endif}

uses SysUtils, Classes;

var
  Src: TFileStream;
  Dest: TextFile;
  SrcFileName, DestFileName: string;
  B: Byte;
  i: Integer;
begin
  if ParamCount() <> 2 then
  begin
    Writeln(ErrOutput, 'file_to_pascal_data: Usage: file_to_pascal_data INPUT_FILE OUTPUT_FILE');
    Writeln(ErrOutput, 'file_to_pascal_data: Invalid number of arguments: expected 2, got ', ParamCount());
    Halt(1)
  end;
  SrcFileName := ParamStr(1);
  DestFileName := ParamStr(2);

  Src := TFileStream.Create(SrcFileName, fmOpenRead or fmShareDenyWrite);
  try
    Assign(Dest, DestFileName);
    Rewrite(Dest);
    try
      Writeln(Dest, '{ -*- buffer-read-only: t -*- }');
      Writeln(Dest, '{ DON''T EDIT -- this file was automatically generated from "'
        + SrcFileName + '" }');

      Writeln(Dest, 'array [0 .. ', Src.Size - 1, '] of Byte = (');

      for i := 0 to Src.Size - 2 do
      begin
        Src.ReadBuffer(B, 1);
        Write(Dest, '$', IntToHex(B, 2), ', ');
        if ((i + 1) mod 15) = 0 then
          Writeln(Dest);
      end;

      Src.ReadBuffer(B, 1);
      Write(Dest, '$', IntToHex(B, 2));
      Writeln(Dest, ')');
    finally CloseFile(Dest) end;
  finally Src.Free end;
end.
