Program Recalcula_Apuntadores;
Uses crt;
type
 archivoentrada=record entbyte:byte; end;
 archivosalida=record salbyte:byte; end;
 apuntadores=array [0..32000] of word;

var
 archent:file of archivoentrada;
 archsal:file of archivosalida;
 entarch:archivoentrada;
 salarch:archivosalida;
 apunt:apuntadores;
 apununo,totalapun,cont1,cont2,apunor,apuncop:integer;
 bytel,byteh,mesbreak:byte;
 palabra,adra:word;
 largote,largote2:longint;
 charro,leeapunas:char;
 nomarchent,nomarchsal:string[100];



Begin
 apuncop:=30000;
 leeapunas:='L';

 clrscr;
 writeln('(all values must be in decimal)');
 writeln;
 write('Input text file : ');
 readln(nomarchent);
 write('Output IPS file : ');
 readln(nomarchsal);
 write('Address of the pointer table : ');
 readln(largote);
 write('Value of the first pointer : ');
 readln(apununo);
 write('Message break character : ');
 readln(mesbreak);
 write('Total number of pointers : ');
 readln(totalapun);
 totalapun:=totalapun-1;

 assign(archent,nomarchent);
 assign(archsal,nomarchsal);
 reset(archent);
 rewrite(archsal);


 for cont1:=0 to 32000 do apunt[cont1]:=0;
 cont2:=1;
 cont1:=0;
 apunt[0]:=apununo;
 while not eof(archent) do
 begin
  read(archent,entarch);
  inc(cont1);
  if entarch.entbyte=mesbreak then
  begin
   apunt[cont2]:=cont1+apununo;
   inc(cont2);
  end;
 end;
 close(archent);


 salarch.salbyte:=byte('P');
 write(archsal,salarch);
 salarch.salbyte:=byte('A');
 write(archsal,salarch);
 salarch.salbyte:=byte('T');
 write(archsal,salarch);
 salarch.salbyte:=byte('C');
 write(archsal,salarch);
 salarch.salbyte:=byte('H');
 write(archsal,salarch);

 largote2:=largote div 65536;
 byteh:=lo(largote2);
 salarch.salbyte:=byteh;
 write(archsal,salarch);
 byteh:=hi(largote);
 salarch.salbyte:=byteh;
 write(archsal,salarch);
 byteh:=lo(largote);
 salarch.salbyte:=byteh;
 write(archsal,salarch);

 palabra:=(totalapun+1)*2;
 byteh:=hi(palabra);
 bytel:=lo(palabra);
 salarch.salbyte:=byteh;
 write(archsal,salarch);
 salarch.salbyte:=bytel;
 write(archsal,salarch);

 writeln;
 writeln('Do you want to substitute some pointers? (Y/N) ');
 charro:=upcase(readkey);
 writeln;
 writeln;

 if charro='Y' then
 begin
  writeln('To end, write 0s');
  writeln;
  cont2:=0;
  for cont1:=1 to (totalapun+1) do
  begin
   if apuncop=cont1 then
   begin
    palabra:=apunt[apunor-1];
    leeapunas:='L';
   end
   else begin palabra:=apunt[cont2]; inc(cont2); end;

   byteh:=lo(palabra);
   bytel:=hi(palabra);
   salarch.salbyte:=byteh;
   write(archsal,salarch);
   salarch.salbyte:=bytel;
   write(archsal,salarch);

   if leeapunas='L' then
   begin
    write('Pointer no. (original count) ');
    read(apuncop);
    write('   will be redirected to (new count) ');
    readln(apunor);
    leeapunas:='D';
    if (apuncop=0) or (apunor=0) then leeapunas:='x';
   end;
  end;
 end

 else
 for cont1:=1 to (totalapun+1) do
  begin
   palabra:=apunt[cont1-1];
   byteh:=lo(palabra);
   bytel:=hi(palabra);
   salarch.salbyte:=byteh;
   write(archsal,salarch);
   salarch.salbyte:=bytel;
   write(archsal,salarch);
  end;




 salarch.salbyte:=byte('E');
 write(archsal,salarch);
 salarch.salbyte:=byte('O');
 write(archsal,salarch);
 salarch.salbyte:=byte('F');
 write(archsal,salarch);
 close(archsal);
 writeln;
 writeln;
 writeln('Done');
End.

