440 Intermediate C Programming
freque ncies [ ind ]. freq );74
ListNode * ln = L istNod e_crea te ( tn) ;75
head = List_i nsert ( head , ln , SORTED ) ;76
ind ++;77
}78
return head ;79
}80
void List_pri n t ( L i stNode * head )81
{82
i f ( head == NULL )83
{84
return ;85
}86
Tree_pr i nt ( head -> tnptr , 0) ;87
List_pr i nt ( head -> next );88
}89
// utility .h1
#i f n d e f UTILITY_H2
#d ef in e UTILITY_ H3
#in clude < stdio .h >4
// write one bit to a file5
6
// whichbi t indicat e s which bit this is written to (0 means7
// leftmost , 7 means right m ost )8
// curbyte is the c u r rent byte9
//10
// if whichbit is zero , curbyte is reset and bit is put11
// to the leftmost bit12
//13
// when which bit r e a ches 7, this byte is written to the14
// file and whichbit is reset15
//16
// the functi o n returns 1 if a byte is written to the file17
// returns 0 if no byte is written18
// -1 if it tries to write and fails19
int writeBit ( FILE * fptr , unsigned char bit ,20
unsigned char * whichbit , unsigned char *21
curbyte ) ;22
// if * w hichbit is not 0, some bits of * curbyte are not used23
// fill these bits by 0 and write the byte to the file24
int padZero ( FILE * fptr , unsigned char * whichbit ,25
unsigned char * curbyte );26
27
int readBit ( FILE * fptr , unsigned char * bit ,28
unsigned char * whichbit ,29
unsigned char * curbyte );30
#e ndif31
// utility .c1
#in clude < stdio .h >2
Huffman Compression 441
#in clude " utility . h"3
int padZero ( FILE * fptr , unsigned char * whichbit ,4
unsigned char * curbyte )5
{6
int rtv ;7
while ((* whichbit ) != 0)8
{9
rtv = wri t eB it ( fptr , 0 , whichbit , c u r b y t e ) ;10
i f ( rtv == -1)11
{12
return -1;13
}14
}15
return rtv ;16
}17
int readBit ( FILE * fptr , unsigned char * bit ,18
unsigned char * whichbit , unsigned char * curbyte )19
20
{21
int ret = 1;22
i f ((* whichbit ) == 0)23
{24
// read a byte from the file25
ret = fread ( curbyte , s i z e o f ( unsigned char ) , 1, fptr );26
}27
i f ( ret != 1)28
{29
// read fail30
return -1;31
}32
// shift the bit to the correct location33
unsigned char temp = (* curbyte ) >> (7 - (* whichbit ) );34
temp = temp & 0 X01 ; // get only 1 bit , ignore the others35
// increas e by 136
* whichbit = ((* whic h bit ) + 1) % 8;37
* bit = temp ;38
return 1;39
}40
This is the Makefile for the program.
CFLAGS = -g - Wall - Wshadow1
GCC = gcc $ ( CFLAGS )2
SRCS = main .c encode .c freq .c tree .c list .c utility . c3
OBJS = $ ( SRCS :%. c =%. o)4
VALGRIND = valgrind -- leak - check = full -- tool = memcheck5
-- verbose --log - file6
7
code : $ ( OBJS )8
$( GCC ) $ ( OBJS ) -o code9
10
test1 : code11
442 Intermediate C Programming
./ code e input1 comp r ess112
$( VALGRIND )= logenc1 ./ code e input1 compr e ss113
./ code d compre s s1 output114
$( VALGRIND )= logdec1 ./ code d compres s 1 output115
echo # add a blank line16
diff input1 output117
18
test2 : code19
./ code e input2 comp r ess220
$( VALGRIND )= logenc2 ./ code e input2 compr e ss221
./ code d compre s s2 output222
$( VALGRIND )= logdec2 ./ code d compres s 2 output223
echo # add a blank line24
diff input2 output225
26
test3 : code27
./ code e input3 comp ress328
$( VALGRIND )= logenc3 ./ code e input3 compr e ss329
./ code d compre s s3 output330
$( VALGRIND )= logdec3 ./ code d compres s 3 output331
echo # add a blank line32
diff input3 output333
34
.c .o:35
$( GCC ) $ ( CFLAGS ) -c $ *. c36
37
clean :38
rm -f *. o a. out code log *39
This is the most complex program in this book. It integrates almost all topics in this
book. Please study this program carefully. It is a bridge for you from being an intermediate
programmer to becoming an advanced programmer.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset