Moved from AliTransbit to AliL3Transbit.
[u/mrichter/AliRoot.git] / HLT / comp / bitio.c
CommitLineData
f92ad96b 1/************************** Start of BITIO.C *************************
2 *
3 * This utility file contains all of the routines needed to impement
4 * bit oriented routines under either ANSI or K&R C. It needs to be
5 * linked with every program used in the entire book.
6 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include "bitio.h"
11#include "errhand.h"
12
13#define PACIFIER_COUNT 2047
14
15BIT_FILE *OpenOutputBitFile( char *name )
16{
17 BIT_FILE *bit_file;
18
19 bit_file = (BIT_FILE *) calloc( 1, sizeof( BIT_FILE ) );
20 if ( bit_file == NULL )
21 return( bit_file );
22 bit_file->file = fopen( name, "wb" );
23 bit_file->rack = 0;
24 bit_file->mask = 0x80;
25 bit_file->pacifier_counter = 0;
26 return( bit_file );
27}
28
29BIT_FILE *OpenInputBitFile( char *name )
30{
31 BIT_FILE *bit_file;
32
33 bit_file = (BIT_FILE *) calloc( 1, sizeof( BIT_FILE ) );
34 if ( bit_file == NULL )
35 return( bit_file );
36 bit_file->file = fopen( name, "rb" );
37 bit_file->rack = 0;
38 bit_file->mask = 0x80;
39 bit_file->pacifier_counter = 0;
40 return( bit_file );
41}
42
43void CloseOutputBitFile(BIT_FILE *bit_file )
44{
45 if ( bit_file->mask != 0x80 )
46 if ( putc( bit_file->rack, bit_file->file ) != bit_file->rack )
47 fatal_error( "Fatal error in CloseBitFile!\n" );
48 fclose( bit_file->file );
49 free( (char *) bit_file );
50}
51
52void CloseInputBitFile(BIT_FILE *bit_file )
53{
54 fclose( bit_file->file );
55 free( (char *) bit_file );
56}
57
58void OutputBit( BIT_FILE *bit_file, int bit )
59{
4a838220 60 if ( bit )
61 bit_file->rack |= bit_file->mask;
62 bit_file->mask >>= 1;
63 if ( bit_file->mask == 0 ) {
64 if ( putc( bit_file->rack, bit_file->file ) != bit_file->rack )
65 fatal_error( "Fatal error in OutputBit!\n" );
66 /*else
67 if ( ( bit_file->pacifier_counter++ & PACIFIER_COUNT ) == 0 )
68 putc( '.', stdout );*/
69 bit_file->rack = 0;
70 bit_file->mask = 0x80;
71 }
f92ad96b 72}
73
74void OutputBits(BIT_FILE *bit_file,unsigned long code, int count )
75{
76 unsigned long mask;
4a838220 77
f92ad96b 78 mask = 1L << ( count - 1 );
79 while ( mask != 0) {
4a838220 80 if ( mask & code )
81 bit_file->rack |= bit_file->mask;
82 bit_file->mask >>= 1;
83 if ( bit_file->mask == 0 ) {
84 if ( putc( bit_file->rack, bit_file->file ) != bit_file->rack )
85 fatal_error( "Fatal error in OutputBit!\n" );
86 /*else if ( ( bit_file->pacifier_counter++ & PACIFIER_COUNT ) == 0 )
87 putc( '.', stdout );*/
88 bit_file->rack = 0;
89 bit_file->mask = 0x80;
90 }
91 mask >>= 1;
f92ad96b 92 }
93}
94
95int InputBit( BIT_FILE *bit_file )
96{
4a838220 97 int value;
98
99 if ( bit_file->mask == 0x80 ) {
100 bit_file->rack = getc( bit_file->file );
101 if ( bit_file->rack == EOF )
102 fatal_error( "Fatal error in InputBit!\n" );
103 /*if ( ( bit_file->pacifier_counter++ & PACIFIER_COUNT ) == 0 )
104 putc( '.', stdout );*/
105 }
106 value = bit_file->rack & bit_file->mask;
107 bit_file->mask >>= 1;
108 if ( bit_file->mask == 0 )
109 bit_file->mask = 0x80;
110 return( value ? 1 : 0 );
f92ad96b 111}
112
113unsigned long InputBits( BIT_FILE *bit_file, int bit_count )
114{
115 unsigned long mask;
116 unsigned long return_value;
117
118 mask = 1L << ( bit_count - 1 );
119 return_value = 0;
120 while ( mask != 0) {
4a838220 121 if ( bit_file->mask == 0x80 ) {
122 bit_file->rack = getc( bit_file->file );
123 if ( bit_file->rack == EOF )
124 fatal_error( "Fatal error in InputBit!\n" );
125 /*if ( ( bit_file->pacifier_counter++ & PACIFIER_COUNT ) == 0 )
126 putc( '.', stdout );*/
127 }
128 if ( bit_file->rack & bit_file->mask )
129 return_value |= mask;
130 mask >>= 1;
131 bit_file->mask >>= 1;
132 if ( bit_file->mask == 0 )
133 bit_file->mask = 0x80;
f92ad96b 134 }
135 return( return_value );
136}
137
138void FilePrintBinary( FILE *file, unsigned int code, int bits )
139{
140 unsigned int mask;
141
142 mask = 1 << ( bits - 1 );
143 while ( mask != 0 ) {
144 if ( code & mask )
145 fputc( '1', file );
146 else
147 fputc( '0', file );
148 mask >>= 1;
149 }
150}
151
8f2aba92 152
f92ad96b 153/*************************** End of BITIO.C **************************/
154