HuffNode * HuffBuildBit(FILE * fptr) { unsigned char ch = fgetc(fptr); int cmd_loc = 1; int count = 0; unsigned char masks[] = {0x80, 0x40, 0x20,0x10,0x08,0x04,0x02,0x01}; unsigned char command = 0; Stack * st = NULL; unsigned char next; unsigned char value; unsigned char next_temp; while (1) { command = (ch & masks[count]); if (command != 0) { next = fgetc(fptr); value = ch << cmd_loc; next_temp = next >> (8 - cmd_loc); value = value | next_temp; HuffNode * TreeNode = HuffNode_create(value); st = StackPush(st, TreeNode); ch = next; } else {
//This function reads the char-based input file and then creates a huffman tree. // It Returns a structure of HuffNode type. HuffNode *Huff_CharRead(char * filename) { int command = 0; FILE *fptr = NULL; fptr = fopen(filename,"r"); if(fptr == NULL) { return NULL; } Stack *st = NULL; while((command = fgetc(fptr)) != EOF)// This loop opens the file till its end. { if(command == '1')// THis statement checks whether the command is 1 or not. { command = fgetc(fptr); if(command == EOF) { return NULL; } st = Stack_push(st,HuffNode_create(command)); } if(command == '0') { HuffNode * A = st -> node; st = Stack_pop(st); if (st == NULL) { return A;// Tree is complete here } else { HuffNode * B = st -> node; st = Stack_pop(st); HuffNode * par = malloc(sizeof(HuffNode)); par -> value = ' '; // doesn't matter par -> right = A; par -> left = B; st = Stack_push(st, par); } } } return NULL; }
HuffNode * HuffBuildTree(FILE * fptr) { Stack * st = NULL; unsigned char command = fgetc(fptr); do { if (command == '1') { unsigned char value = fgetc(fptr); HuffNode * leaf = HuffNode_create(value); st = StackPush(st, leaf); } if (command == '0') { HuffNode * A = st -> node; st = StackPop(st); if (st == NULL) { return A; } else { HuffNode * B = st -> node; st = StackPop(st); HuffNode * parent = malloc(sizeof(HuffNode)); parent -> value = ' '; parent -> right = A; parent -> left = B; st = StackPush(st,parent); } } command = fgetc(fptr); }while(st != NULL); return NULL; }
//This function reads the bit-based input file and then creates the function. //It returns a structure HuffNode type HuffNode *Huff_BitRead(char *filename) { unsigned char command = 0; unsigned char ch1 = 0; unsigned char ch2 = 0; unsigned char ch2_temp = 0; unsigned char character = 0; unsigned char mask[] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; int position = 1; int lcv = 0; int sw = -1; FILE *fptr = NULL; fptr = fopen(filename,"r"); if(fptr == NULL) { return NULL; } Stack *st = NULL; ch1 = fgetc(fptr); while(!feof(fptr))// This loop opens the file till its end. { command = ch1 &mask[lcv]; if((command) == mask[lcv]) { ch1 = ch1 << position; // THis left shifts my first charater ch2 = fgetc(fptr); // THis gets my second character ch2_temp = ch2; ch2_temp = ch2_temp >> (8-position);// THis left shifts my second character. character = ch1 | ch2_temp; // THis gets me the chracter after the command is 1. st = Stack_push(st,HuffNode_create(character)); ch1 = ch2; } if((command) != mask[lcv]) { HuffNode * A = st -> node; st = Stack_pop(st); if (st == NULL) { sw = 1; return A;// Tree is complete here } else { HuffNode * B = st -> node; st = Stack_pop(st); HuffNode * par = malloc(sizeof(HuffNode)); par -> value = ' '; // doesn't matter par -> right = A; par -> left = B; st = Stack_push(st, par); } } if(position == 8) { ch1 = fgetc(fptr); position = 0; lcv = -1; } position++; lcv++; }