program duptext;{duplica un file testo in due colonne
{$C-,I-,K-}
CONST
  {$i \pascal\toolcons}
TYPE
  {$i \pascal\tooltype}
  str255=string[255];
VAR
  {$i \pascal\toolvar}
  filein,fileout:text;
  name:str12;
  line:str255;
  ch:char;
  tasto:char;
  space,times,len:integer;

{$I \pascal\TOOL}
procedure findmaxline(name:str12;var i:integer);
var filein:text;
    line:str80;
begin
  assign(filein,name);
  if esiste(name) then begin
    reset(filein);
    i:=0;
    while not(eof(filein)) do begin
      readln(filein,line);
      if length(line)>i then
        i:=length(line);
    end;
    close(filein);
  end
  else begin
    mid('Non esiste il file testo specificato');
    stand_by;
  end;
end;
procedure addspace(max:integer;var line:str255);
var i,n:integer;
begin
  i:=length(line);
  for n:=i to max do
    line:=line+' ';
end;
procedure writeline(times,space:integer;line:str255);
var n:integer;
    sp:str80;
begin
  sp:='';
  for n:=1 to space do
    sp:=sp+' ';
  for n:=1 to times do
    write(fileout,line+sp);
  writeln(fileout);
end;

BEGIN {program}
  {$i \pascal\toolinit}
  menu(1,'DUPLICAZIONE FILE TESTO IN PIU`COLONE');
  name:='';
  get_NAME(10,10,'Nome dell`archivio di input?',name);
  findmaxline(name,len);
  message('La linea pi— lunga Š di caratteri '+sts(len),1,0);
  if len<>0 then begin
    assign(filein,name);
    get_NAME(10,12,'Nome dell`archivio di output?',name);
    if esiste(name) then begin
      tasto:='s';
      get_chr(10,14,'L`archivio di output esiste gi….Cancello?',tasto);
    end;
    if (upcase(tasto)='S') or not(esiste(name)) then begin
      times:=2;
      get_int(10,14,'Quante colonne?',times);
      get_int(10,16,'Quante caratteri bianchi fra due colonne vicine?',space);
      assign(fileout,name);
      rewrite(fileout);
      reset(filein);
      while not(eof(filein)) do begin
        readln(filein,line);
        addspace(len,line);
        writeline(times,space,line);
      end;
      close(filein);
      close(fileout);
    end;
  end
  else begin
    mid('archivio vuoto');
    stand_by;
  end;
  mid('Arrivederci');
end.


