[fpc-pascal] Unit crt

Roger Rivero Jr. (SAMPA WebMaster) sampa at caonao.cu
Thu Sep 24 11:48:29 CEST 2015


El 23/9/2015 12:25 PM, Rainer Stratmann escribió:

Rather than saving the state in a static way, I prefer implementing a 
Push/Pop (LIFO queue) approach to allow for nested pairs of setting and 
restoring, like this:

Type IOsCollection = {Any sort of collection, array, list, or record 
with linking pointers};

var oldios : IOsCollection;

procedure PushTio(tio : termios);
begin
//Stores tio in the oldios: IOsCollection, and advances a pointer, index, etc., if necessary
end;
funtion PopTio: termios;
begin
//Retrieves and removes last pushed tio from the oldios: IOsCollection
end;

> procedure crt_output_set_normal;
> var tio : termios;
> begin
>   tcgetattr( 1 , tio );

   PushTio(tio);

>   // !
>   tio.c_oflag := tio.c_oflag or OPOST or ONLCR;
>   // !
>   tcsetattr( 1 , TCSANOW , tio );
> end;
>
> procedure crt_output_restore;
> begin
>   // tcsetattr( 1 , TCSANOW , oldio ); //old suggestion

  tcsetattr( 1 , TCSANOW , PopTio );

> end;
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>

-----------------------------------------------------
I repeat it here for the sake of clarity
-----------------------------------------------------
Type IOsCollection = {Any sort of collection, array, list, or record 
with linking pointers};

var oldios : IOsCollection;

procedure PushTio(tio : termios);
begin
//Stores tio in the IOsCollection, and advances a pointer, index, etc., if necessary
end;
funtion PopTio: termios;
begin
//Retrieves and removes last pushed tio from the IOsCollection
end;


procedure crt_output_set_normal;
var tio : termios;
begin
  tcgetattr( 1 , tio );
  PushTio(tio);
  // !
  tio.c_oflag := tio.c_oflag or OPOST or ONLCR;
  // !
  tcsetattr( 1 , TCSANOW , tio );
end;

procedure crt_output_restore;
begin
  tcsetattr( 1 , TCSANOW , PopTio );
end;

---------------------------------------------
Now you can nest them
---------------------------------------------

uses AnyUnit {Unit that implements crt_output_set_any_other_tio_state and any_other_tio_modifyng_routine};

procedure any_tio_modifyng_routine; forward;

procedure MyNestingExample;
begin
crt_output_set_normal;
// Do some outputs

any_tio_modifyng_routine;
//keep working;
crt_output_restore
end;

procedure any_tio_modifyng_routine;
begin
crt_output_set_any_other_tio_state;
// Do some outputs
any_other_tio_modifyng_routine;
//keep working;
crt_output_restore
end;

Best regards,
Roger Rivero
  





More information about the fpc-pascal mailing list