program translate_ascii_to_vector; { trasforma un file asii in un file
                                     di array 80x24}


const
      stlen=25;
      const n_tab=20;
      int_len=12;
      r_u=6; r_d=4;
      n_campi=0;

TYPE
        strc=string[stlen];
        str8=string[8];
        str10=string[10];
        str12=string[12];
        str20=string[20];
        str25=string[25];
        str30=string[30];
        str50=string[50];
        str80=string[80];
        str160=string[160];
        str320=string[240];

        vet_scr=array[0..n_campi]of str20;

        boo=boolean;

      pagev=array[1..80,1..24] of char;

var aiuto:text;
    fileout:file of pagev;
    key,key2,tasto:char;
    video1,video2,video3:pagev;
    linee,s,max,code,i:integer;
    testo,testo2:str80;
    tommorow,yesterday,nop:real;
    carta,ff,finish:boolean;
    namein,nameout:str12;
    car,ris:char;


    tab                       :array[1..n_tab] of strc;
    editor                    :file;
    data,articolo             :strc;
    tastog                    :char;
    ok,secret,level3,level4   :boo;
    debug                     :boo;

{$C-,I-,K-}
{$i tool}

procedure azzera(var video:pagev);
var x,y:integer;
begin
  for y:=1 to 24 do
    for x:=1 to 80 do
      video[x,y]:=' ';
end;


procedure get_page(var video:pagev;var np:real);
var key:char;
    quit:boolean;
    x,y:integer;

procedure new_line;
begin
  y:=y+1;
  x:=1;
end;

begin
  azzera(video);
  y:=2;
  x:=1;
  if not(eof(aiuto)) then
    quit:=false;
  nop:=0;
  repeat
    readln(aiuto,testo);
    val(testo,nop,code);
    message('letto numero pagina='+sts_r(nop),1,0);
  until (code=0) or (testo='');
  if testo<>'' then begin
  str(nop:2:2,testo);
  readln(aiuto,testo2);
  message(testo2,1,0);
  testo2:=copy(testo2,6,length(testo2)-5);
  message(testo2,1,0);
  max:=length(testo2);
  s:=40-((max div 2));
  message(sts(max)+' '+sts(s),1,0);
  for i:=1 to max do
    video[s+i,1]:=copy(testo2,i,1);

  testo:='Pag: '+testo;
  for i:=1 to length(testo) do
    video[i,2]:=copy(testo,i,1);
  repeat
    read(aiuto,key);
    case ord(key) of
      13:new_line;
       128:begin
           quit:=true;
           y:=25;
         end;
       9:if x<73 then
           x:=x+8
         else
           new_line;
     else {case}
        begin
          if x>80 then
            new_line;
           if y<25 then
             begin
               video[x,y]:=key;
               x:=x+1;
             end; {if}
        end; {else case}
    end;{case}
  until eof(aiuto) or quit or (y>24);
  write(fileout,video);
  if eof(aiuto) then
    finish:=true;
  end
  else
    tommorow:=0;
end;

procedure print_page(video:pagev);
var x,y:integer;
begin
  for y:=1 to 24 do
    begin
      for x:=1 to 80 do
        write(lst,video[x,y]);
      if video[x,y]<>chr(13) then
        writeln(lst);
    end;
  if ff then
    page(1)
  else
    writeln(lst);
end;

procedure display_page(video:pagev);
var x,y:integer;
begin
  clrscr;
  for y:=1 to 24 do
    for x:=1 to 80 do
      writexy(x,y,video[x,y]);
  if carta then
    print_page(video);
end;



procedure no_help;
begin
            mid('no help done');
            get_chr(30,20,'Non  disponibile nessun men di aiuto',key);
end;

begin
  debug:=false;
  carta:=false;
  ff:=false;
  menu(1,'TRANSLATE 1.0 - PROGRAMMA DI TRASFORMAZIONE FILES ASCII IN ARRAY 80X24');
  namein:='help.asc';
  get_chr(10,6,'Stampa su carta?',car);
  if upcase(car)='S' then
    begin
      carta:=true;
      get_chr(10,8,'Uso form feed ad ogni pagina?',car);
      if upcase(car)='S' then
        ff:=true;
      if ff then
        begin
          linee:=66;
          get_int(5,10,'numero di righe reali che compongono il foglio(max 127) ',linee);
          {settagio numero righe}
          write(lst,chr(27)+chr(67)+chr(linee));
        end;
    end;
  get_name(10,10,'Nome del file da convertire?',namein);
  assign(aiuto,namein);
  if esiste(namein) then
    begin
      ris:='s';
      repeat
        nameout:='help.pag';
        get_name(10,12,'Nome del file convertito?',nameout);
        if esiste(nameout) then
          begin
            get_chr(10,14,'Esiste gi un archivio con lo stesso nome; scrivo di seguito?',ris);
            if upcase(ris)='S' then
              begin
                get_chr(10,16,'Scrivo di sopra?',ris);
                if upcase(ris)='S' then
                  begin
                    assign(fileout,nameout);
                    rewrite(fileout);
                  end
                else
                  begin
                    ris:='s';
                    assign(fileout,nameout);
                    reset(fileout);
                    seek(fileout,filesize(fileout));
                  end;
              end;
          end
        else
           begin
             assign(fileout,nameout);
             rewrite(fileout);
           end;
      until  upcase(ris)='S';
      reset(aiuto);
      if not(eof(aiuto))then
        begin
          nop:=0;
          yesterday:=0;
          finish:=false;
          azzera(video1);
          azzera(video2);
          get_page(video3,tommorow);
          repeat

            yesterday:=nop;
            video1:=video2;
            nop:=tommorow;
            video2:=video3;
            azzera(video3);
            display_page(video2);
            if not(tommorow=0) then
            get_page(video3,nop);
            get_real2(1,25,4,'Inserire: 0 per uscire, enter o numero pagina per contiuare. Pag:?',tommorow);
            if finish and not(tommorow=0) then
              begin
                video2:=video3;
                display_page(video2);
                get_real2(1,25,4,'Premere: 0 per uscire, enter per contiuare. ?',tommorow);
                if tommorow<>0 then
                  begin
                     close(aiuto);
                     reset(aiuto);
                     finish:=false;
                     get_page(video3,tommorow);
                  end;
              end;
          until tommorow=0;
          close(aiuto);
          close(fileout);
          end
        else
          no_help;
    end
  else
    no_help;
end.

