
program A2P;

{$A+,B+,D-,E-,F-,I-,L-,N-,O-,R-,S-,V+}

uses
  crt, dos;

const
  ScreenWidth = 80;

type
  FileNameType = string[14];

var
  InFile, OutFile          : text;
  InString                 : string[ScreenWidth];
  Index                    : byte;
  InFileName, OutFileName  : FileNameType;
  FirstPart                : string[8];


  procedure NameOutFile(InFileName : FileNameType; var OutFileName : FileNameType);

  var
    DotMarker, Index : byte;

  begin
    DotMarker := pos('.', InFileName);
    if DotMarker = 0 then
      FirstPart := InFileName
    else
      FirstPart := copy(InFileName, 1, (DotMarker - 1));
    OutFileName := concat(FirstPart, '.A2P')
  end;



  procedure UserPrompt;

  begin
    writeln;
    writeln('  New file with ''A2P'' extension will be created.');
    writeln;
    if paramcount = 0 then
      begin
        write('  Enter name of file to be converted: ');
        readln(InFileName)
      end
    else
      InFileName := paramstr(1);
    NameOutFile(InFileName, OutFileName)
  end;


  procedure ConvertAscii;

  var
    LeadingBlanks  : boolean;
    Y_Axis, X_Axis : byte;
    TempString     : string[ScreenWidth];

  begin
    Y_Axis := 0;
    assign(InFile, InFileName);        (* Assign file to file variable *)
    reset(InFile);                     (* Point to beginning of disk file *)
    assign(OutFile, OutFileName);
    rewrite(OutFile);
    writeln(OutFile, '');
    writeln(OutFile, 'program ', FirstPart, ';');
    writeln(OutFile, '');
    writeln(OutFile, 'uses');
    writeln(OutFile, '  crt;');
    writeln(OutFile, '');
    writeln(OutFile, 'BEGIN');
    writeln(OutFile, '  clrscr;');
    repeat
      LeadingBlanks := true;
      X_Axis := 0;
      TempString := '';
      readln(InFile, InString);        (* Read in first line of disk file *)
      if length(InString) = 0 then
        inc(Y_Axis)
      else
        begin
          for Index := 1 to length(InString) do
            begin
              if (InString[Index] = ' ') and LeadingBlanks = true then
                inc(X_Axis)
              else
                begin
                  LeadingBlanks := false;
                  TempString := concat(TempString + InString[Index])
                end;
              if InString[Index] = '''' then (* Single quotation mark *)
                TempString := concat(TempString + '''')
            end;
          inc(Y_Axis);
          inc(X_Axis);
          writeln(OutFile, '  gotoxy(', X_Axis, ',', Y_Axis, ');');
          writeln(OutFile, '  writeln(''', TempString, ''');')
        end;
    until eof(InFile);
    writeln(OutFile, 'END.');
    close(InFile);
    close(OutFile)
  end;

BEGIN
  clrscr;
  UserPrompt;
  ConvertAscii;
  writeln;
  writeln(' Conversion complete!');
  writeln
END.
 