home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / pslib / bitio.c next >
C/C++ Source or Header  |  1998-06-08  |  6KB  |  202 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: f:/miner/source/pslib/rcs/bitio.c $
  15.  * $Revision: 1.9 $
  16.  * $Author: yuan $
  17.  * $Date: 1993/10/22 17:51:09 $
  18.  *
  19.  * Contains all the necessary bit routines.
  20.  * The file handling has been optimized and the pacifier has been removed.
  21.  *
  22.  * $Log: bitio.c $
  23.  * Revision 1.9  1993/10/22  17:51:09  yuan
  24.  * No major revisions
  25.  * 
  26.  * Revision 1.8  1993/10/18  17:59:55  yuan
  27.  * Fixed memory alloc errors
  28.  * 
  29.  * Revision 1.7  1993/09/21  17:22:17  yuan
  30.  * *** empty log message ***
  31.  * 
  32.  * Revision 1.6  1993/09/21  17:16:19  yuan
  33.  * cleaning up
  34.  * 
  35.  * Revision 1.5  1993/09/20  12:25:39  yuan
  36.  * ReadFile, WriteFile, AppendFile removed to readfile.lib
  37.  * 
  38.  * Revision 1.4  1993/09/14  13:06:25  yuan
  39.  * CloseOutputBitFile no longer frees the buffer.
  40.  * 
  41.  * Revision 1.3  1993/09/09  17:42:02  yuan
  42.  * tab added to ERROR messages
  43.  * 
  44.  * Revision 1.2  1993/09/09  12:38:28  yuan
  45.  * WriteFile and AppendFile fixed.
  46.  * 
  47.  * Revision 1.1  1993/09/08  16:14:04  yuan
  48.  * Initial revision
  49.  * 
  50.  * Revision 1.3  1993/07/24  19:04:52  yuan
  51.  * *** empty log message ***
  52.  * 
  53.  * Revision 1.2  1993/07/22  11:25:35  yuan
  54.  * No change
  55.  * 
  56.  * Revision 1.1  1993/07/21  15:28:13  matt
  57.  * Initial revision
  58.  * 
  59.  *
  60.  */
  61.  
  62. #pragma off (unreferenced)
  63. static char rcsid[] = "$Id: bitio.c 1.9 1993/10/22 17:51:09 yuan Exp $";
  64. #pragma on (unreferenced)
  65.  
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <fcntl.h>
  69. #include <io.h>
  70. #include <sys\types.h>
  71. #include <sys\stat.h>
  72.  
  73. #include "library.h"
  74. #include "mem.h"
  75. //#include "mem2.h"
  76.  
  77. #define PACIFIER_COUNT 2047
  78.  
  79. BIT_BUF *OpenOutputBitBuf( ) {
  80.     BIT_BUF *bit_buf;
  81.  
  82.     //MALLOC( bit_buf, BIT_BUF, 1 );//Compile hack again -KRB
  83.     bit_buf = (BIT_BUF *)malloc(1*sizeof(BIT_BUF));
  84.     if ( bit_buf == NULL )
  85.         return( bit_buf );
  86.     bit_buf->current_byte = 0;
  87.     bit_buf->rack = 0;
  88.     bit_buf->mask = 0x80;
  89.     bit_buf->pacifier_counter = 0;
  90.     return( bit_buf );
  91. }
  92.  
  93. BIT_BUF *OpenInputBitBuf( ubyte *buffer ) {
  94.     BIT_BUF *bit_buf;
  95.  
  96.     //bit_buf = (BIT_BUF *) calloc( 1, sizeof( BIT_BUF ) );
  97.   //  MALLOC(bit_buf, BIT_BUF, 1);//Compile hack again -KRB
  98.     bit_buf = (BIT_BUF *)malloc(1*sizeof(BIT_BUF));
  99.     if ( bit_buf == NULL )
  100.         return( bit_buf );
  101.     bit_buf->buf = buffer;
  102.     bit_buf->current_byte = 0;
  103.     bit_buf->rack = 0;
  104.     bit_buf->mask = 0x80;
  105.     bit_buf->pacifier_counter = 0;
  106.     return( bit_buf );
  107. }
  108.  
  109. void CloseOutputBitBuf( BIT_BUF *bit_buf ) {
  110.     if ( bit_buf->mask != 0x80 )
  111.         bit_buf->buf[bit_buf->current_byte++] = bit_buf->rack;
  112.     free( bit_buf );
  113. }
  114.  
  115. void CloseInputBitBuf( BIT_BUF *bit_buf )
  116. {
  117.     free( bit_buf->buf );
  118.     free( bit_buf );
  119. }
  120.  
  121. void OutputBit( BIT_BUF *bit_buf, int bit ) {
  122.     if ( bit )
  123.         bit_buf->rack |= bit_buf->mask;
  124.     bit_buf->mask >>= 1;
  125.     if ( bit_buf->mask == 0 ) {
  126.         bit_buf->buf[bit_buf->current_byte++] = bit_buf->rack;
  127.         bit_buf->rack = 0;
  128.         bit_buf->mask = 0x80;
  129.     }
  130. }
  131.  
  132. void OutputBits( BIT_BUF *bit_buf, unsigned int code, int count ) {
  133.     unsigned int mask;
  134.  
  135.     mask = 1L << ( count - 1 );
  136.     while ( mask != 0) {
  137.         if ( mask & code )
  138.             bit_buf->rack |= bit_buf->mask;
  139.         bit_buf->mask >>= 1;
  140.         if ( bit_buf->mask == 0 ) {
  141.             bit_buf->buf[bit_buf->current_byte++] = bit_buf->rack;
  142.             bit_buf->rack = 0;
  143.             bit_buf->mask = 0x80;
  144.         }
  145.         mask >>= 1;
  146.     }
  147. }
  148.  
  149. int InputBit( BIT_BUF *bit_buf ) {
  150.     int value;
  151.  
  152.     if ( bit_buf->mask == 0x80 ) {
  153.         bit_buf->rack = bit_buf->buf[bit_buf->current_byte++];
  154.         if ( bit_buf->rack == EOF ) {
  155.             printf( "    ERROR : Fatal error in InputBit!\n" );
  156.             exit(4);
  157.         }
  158.     }
  159.     value = bit_buf->rack & bit_buf->mask;
  160.     bit_buf->mask >>= 1;
  161.     if ( bit_buf->mask == 0 )
  162.     bit_buf->mask = 0x80;
  163.     return( value ? 1 : 0 );
  164. }
  165.  
  166. unsigned int InputBits( BIT_BUF *bit_buf, int bit_count ) {
  167.     unsigned int mask;
  168.     unsigned int return_value;
  169.  
  170.     mask = 1L << ( bit_count - 1 );
  171.     return_value = 0;
  172.     while ( mask != 0) {
  173.     if ( bit_buf->mask == 0x80 ) {
  174.         bit_buf->rack = bit_buf->buf[bit_buf->current_byte++];
  175.         if ( bit_buf->rack == EOF ) {
  176.             printf( "    ERROR : Fatal error in InputBits!\n" );
  177.             exit(5);
  178.         }
  179.     }
  180.     if ( bit_buf->rack & bit_buf->mask )
  181.             return_value |= mask;
  182.         mask >>= 1;
  183.         bit_buf->mask >>= 1;
  184.         if ( bit_buf->mask == 0 )
  185.             bit_buf->mask = 0x80;
  186.     }
  187.     return( return_value );
  188. }
  189. /*
  190. void FilePrintBinary( FILE *file, unsigned int code, int bits ) {
  191.     unsigned int mask;
  192.  
  193.     mask = 1 << ( bits - 1 );
  194.     while ( mask != 0 ) {
  195.         if ( code & mask )
  196.             fputc( '1', file );
  197.         else
  198.             fputc( '0', file );
  199.         mask >>= 1;
  200.     }
  201. } */
  202.