next up previous
Next: BitSetClass Up: simlib Previous: BitFiddleClass

BitPackClass


     NAME
	  bitpackclass - Routines to stuff bitpatterns into strings of
	  bytes

     DESCRIPTION
	  These	routines are used to provide unformatted convertion of
	  integers (or reals) to 'bits'. Used together with ByteFiles
	  (not ImageFiles!) it can serve as a facility for unformatted
	  I/O.

     EXAMPLE
	     external class BitPackClass;
	     ref(BitPackClass) Packer;
	     text t; integer i,j;
	     Packer:-new BitPackClass; t:-blanks(4);
	     Packer.PackInt(t,i); ! packs I into T and moves Pos of T;
	     t.setpos(1);
	     j:=Packer.UnPackInt(t);! restore the value	of i, advance
	  T.pos;

     AUTHOR
	  Boris	Magnusson, Lund	University.

     DETAILED INTERFACE
	     class bitpackclass;
	  This class is	used to	store Numbers as bytes of  bitpatterns
	  in a text string. It is usefull for storing or communicating
	  numbers in a compact form. The  number  of  bytes  a	number
	  occupies  is:	  short	 integer 2, integer 4, real 4 and long
	  real 8.

	  The bytes are	placed	from  pos  and	onwards,  the  pos  is
	  advanced  2/4/8  positions  as required.  If the text	is not
	  long enough a	runtimne Error is reported.

	  This class contains operations only.	 Data  is  stored  (or
	  fetched) from	a text parameter to each operation.

	  Supers: -
	  Kind:	Instantiable
	  Init:	none
	  Sequencing: (PackShort / UnPackShort / PackInt / UnPackInt /
	  PackReal / UnPackReal	/ PackLong / UnPackLong)*

     OPERATIONS
     PackShort
	     procedure PackShort(t,S);
	     name T; text T; ! Output buffer, pos incremented with 2.;
	     short integer S; !	Input value to pack into T.;
	  Put S	into T,	starting from pos, and increment T.pos with  2
	  If  the rest of T is less than 2 characters long the routine
	  will make a runtime error.
     UnPackShort
	     Short Integer procedure UnPackShort(T);
	     name T; text T; ! Input, next 2 bytes, T.pos
	     incremented.;
	  Extract 2 bytes from T, starting at pos, and increment T.pos
	  with	2.  The	2 bytes	are made into a	short (16-bit) integer
	  which	is returned.

     PackInt
	     procedure PackInt(T,Int);
	     name T; text T; ! Output buffer, pos incremented with 4.;
	     integer Int; ! Input value	to pack	into T.;
	  Put Int into T, starting from	pos, and increment T.pos  with
	  4  If	 the  rest  of	T  is  less than 4 characters long the
	  routine will make a runtime error.

     UnPackInt
	     Integer procedure UnPackInt(T);
	     name T; text T; ! Input, next 4 bytes, T.pos
	     incremented.;
	  Extract 4 bytes from T, starting at pos, and increment T.pos
	  with 4.  The 4 bytes are made	into a 32-bit integer which is
	  returned.

     PackReal
	     procedure PackReal(T,R);
	     name T; text T; ! Output buffer, pos incremented with 4.;
	     real R; ! Input value to pack into	T.;
	  Put R	into T,	starting from pos, and increment T.pos with  4
	  If  the rest of T is less than 4 characters long the routine
	  will make a runtime error.

     UnPackReal
	     Real procedure UnPackReal(T);
	     name T; text T; ! Input, next 4 bytes, T.pos
	     incremented.;
	  Extract 4 bytes from T, starting at pos, and increment T.pos
	  with	4.   The  4 bytes are made into	a 32-bit Real which is
	  returned.

     PackLong
	     procedure PackLong(T,L);
	     name T; text T; ! Output buffer, pos incremented with 8.;
	     long real L; ! Input value	to pack	into T.;
	  Put R	into T,	starting from pos, and increment T.pos with  8
	  If  the rest of T is less than 8 characters long the routine
	  will make a runtime error.

     UnPackLong
	     Long real procedure UnPackLong(T);
	     name T; text T; ! Input, next 8 bytes, T.pos
	     incremented.;

	  Extract 8 bytes from T, starting at pos, and increment T.pos
	  with	8.  The	8 bytes	are made into a	64-bit Long real which
	  is returned.