/******************************** * HUFFMAN ENCODER * * Code Copyright 2000 * * Jonathan Sapan '03 * * (unless noted otherwise) * ********************************/ #include #include #include #include #include "tree.h" /* The following is in tree.h: #define CHARS #define BIT_BUFF_SIZE #define BYTE_BUFF_SIZE AND All function prototypes except qsort(), which is from stdlib.h, and all type definitions. */ FILE *inputFile; FILE *outputFile; extern int bitBuffer[BIT_BUFF_SIZE]; extern unsigned char byteBuffer[BYTE_BUFF_SIZE]; extern int bit_num; extern int byte_num; int main(int argc, char *argv[]) { int i, j, sum; unsigned char a, c, A; unsigned int x, B; letter char_array[CHARS]; /*--------------------------------------------------------------------*/ if ( argc != 3 ) /*Check to see if program was called properly*/ { fprintf(stdout, "TOO FEW ARGUMENTS\n" ); exit(EXIT_SUCCESS); } /*--------------------------------------------------------------------*/ for (i=0; i 0 ) { fwrite(byteBuffer, 1, byte_num, outputFile); byte_num = 0; } if ( bit_num > 0 ) { for ( j = bit_num; j < BIT_BUFF_SIZE; j++) bitBuffer[j] = 0; for ( j = 0, sum = 0; j < BIT_BUFF_SIZE; j++ ) sum += bitBuffer[j] * pow(2, (BIT_BUFF_SIZE - 1 - j) ); byteBuffer[0] = sum; fwrite(&byteBuffer[0], 1, 1, outputFile); } /*--------------------CLEAR BUFFERS------------------*/ fclose(inputFile); fclose(outputFile); return 0; }