Created
December 1, 2016 06:52
-
-
Save harveyslash/5e7a881bdeaf48afa79771330eff55ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<string.h> | |
#include<stdbool.h> | |
#include<stdlib.h> | |
#include"HeapDT.h" | |
#include"Tree.h" | |
#define tableSize 256 | |
unsigned int freqList [tableSize]; | |
char codesTable [tableSize][tableSize]; | |
long decimalToBinary(long n) { | |
int remainder; | |
long binary = 0, i = 1; | |
while(n != 0) { | |
remainder = n%2; | |
n = n/2; | |
binary= binary + (remainder*i); | |
i = i*10; | |
} | |
return binary; | |
} | |
void printFrequenceTable(){ | |
printf("printing\n"); | |
for(int i=0;i<tableSize;i++){ | |
printf("%d",freqList[i]); | |
} | |
printf("\n"); | |
} | |
void updateFrequency(char x){ | |
freqList[(int)x]++; | |
} | |
void readFromFile(FILE* file){ | |
char currentChar ; | |
char buffer[256]; | |
while(1){ | |
currentChar= (char)fgetc(file); | |
if(currentChar==EOF)break; | |
printf("CURRENT CHAR ASCII IS %d\n",(int)currentChar); | |
updateFrequency((int)currentChar); | |
} | |
} | |
Heap populateHeap(){ | |
size_t nonzeroCount = 0; | |
for(int i=0;i<tableSize;i++){ | |
if(freqList[i]>0){ | |
nonzeroCount++; | |
printf("nonzero \n"); | |
} | |
} | |
Heap heap = createHeap(nonzeroCount | |
, compareNode | |
, printNode ) ; | |
for(int i=0;i<tableSize;i++){ | |
if(freqList[i]<=0)continue; | |
TreeNode* node = createTreeNode(); | |
node->frequency = freqList[i]; | |
node->symbol = (char)i; | |
insertHeapItem( heap, (void*)node ); | |
} | |
while(sizeHeap(heap)>1){ | |
TreeNode * node1 = (TreeNode*)removeTopHeap(heap); | |
/*removeTopHeap(heap);*/ | |
TreeNode * node2 = (TreeNode*)removeTopHeap(heap); | |
/*removeTopHeap(heap);*/ | |
TreeNode *cumulative = createTreeNode(); | |
cumulative->frequency = node1->frequency + node2->frequency; | |
cumulative-> left = node1; | |
cumulative-> right = node2; | |
cumulative-> symbol = '\0'; | |
insertHeapItem(heap,cumulative); | |
} | |
return heap; | |
} | |
void getCodes(TreeNode *node,int arr[],int top){ | |
if(node->left!=NULL){ | |
arr[top]=0; | |
getCodes(node->left,arr,top+1); | |
} | |
if(node->right!=NULL){ | |
arr[top]=1; | |
getCodes(node->right,arr,top+1); | |
} | |
if(node->symbol!='\0'){ | |
printf("%c: ",node->symbol); | |
int i =0; | |
for(i=0;i<top;i++){ | |
printf("%d",arr[i]); | |
if(arr[i]==1) | |
codesTable[(int)node->symbol][i] = '1'; | |
else codesTable[(int)node->symbol][i] = '0'; | |
} | |
codesTable[(int)node->symbol][i] = '\0'; | |
printf("\n"); | |
} | |
} | |
unsigned int bitStorageArray[tableSize]; | |
unsigned int getBitFromString(char string[]){ | |
unsigned int bit = 0; | |
for(int i=0;i<strlen(string);i++){ | |
if(string[i]=='0') | |
bit = bit <<1 | 0; | |
else | |
bit = bit <<1 | 1; | |
} | |
return bit; | |
} | |
int arraySize; | |
unsigned int totalBitsUsed; | |
unsigned int *generateBitArrayEncore(FILE *file){ | |
char currentChar ; | |
int currentArraySize = 1; | |
unsigned int *bitArray = (unsigned int*)malloc(sizeof(unsigned int)*currentArraySize); | |
int usedUpElements = 0; | |
int bitArrayIndex = 0; | |
unsigned int singleUIElement = 0; | |
int currentUIElementPosition = 31; | |
while(1){ | |
currentChar= (char)fgetc(file); | |
if(currentChar==EOF)break; | |
printf("bitt array indexxxxxxxxx %d",bitArrayIndex); | |
char *codeWordString = codesTable[currentChar]; | |
int codeWordLength = strlen(codeWordString); | |
printf("the char is %c \n",currentChar) ; | |
for(int i=0;i<codeWordLength;i++){ | |
if(codeWordString[i]=='0'){ | |
singleUIElement |= 0 << currentUIElementPosition; | |
}else{ | |
singleUIElement |= 1 << currentUIElementPosition; | |
} | |
currentUIElementPosition--; | |
totalBitsUsed++; | |
printf("OKay, im printing %u\n",singleUIElement); | |
if(currentUIElementPosition<0){ | |
printf("array is GROWING!!!!!!!!!!!!!%u",singleUIElement); | |
bitArray[bitArrayIndex] = singleUIElement; | |
bitArrayIndex++; | |
singleUIElement = 0; | |
unsigned int *tempArray ; | |
tempArray = (unsigned int*)realloc(bitArray,sizeof(unsigned int)*(currentArraySize+1)); | |
currentArraySize++; | |
bitArray = tempArray; | |
currentUIElementPosition = 31; | |
} | |
} | |
} | |
bitArray[bitArrayIndex]=singleUIElement; | |
printf("TOTAL BITS USED IS %u\n",totalBitsUsed); | |
return bitArray; | |
} | |
unsigned int* generateBitArray(FILE *file){ | |
char currentChar ; | |
char buffer[256]; | |
int currentSize = 1; | |
unsigned int *bitArray = (unsigned int*)malloc(sizeof(unsigned int)*currentSize); | |
int usedElements = 0; | |
int index = currentSize-1; | |
int numberOfBitsUsed = 0; | |
while(1){ | |
currentChar= (char)fgetc(file); | |
if(currentChar==EOF)break; | |
/*printf("CODES TABLE %s\n",codesTable[currentChar]);*/ | |
char *stringRepresentation = codesTable[currentChar]; | |
int codeRepIndex = strlen(stringRepresentation) -1; | |
while(codeRepIndex>=0){ | |
if(usedElements==currentSize){ | |
printf("array has grown"); | |
unsigned int *tempArray ; | |
tempArray = (unsigned int*)realloc(bitArray,sizeof(unsigned int)*(currentSize+1)); | |
bitArray = tempArray; | |
currentSize+=1; | |
arraySize = currentSize; | |
printf("array current size = %d\n",currentSize); | |
int counter = 0; | |
int tempIndex = usedElements-1; | |
while(tempIndex>=0){ | |
bitArray[currentSize-1-counter++] = bitArray[tempIndex] ; | |
tempIndex--; | |
} | |
index = currentSize-usedElements-1; | |
} | |
printf("index value is %d\n",index); | |
printf("current size is %d\n",currentSize); | |
if(stringRepresentation[codeRepIndex]=='1'){ | |
bitArray[index] |= 1 << 33-numberOfBitsUsed; | |
} | |
else{ | |
bitArray[index] |= 0 << 33-numberOfBitsUsed; | |
} | |
// printf("!!!!!!JUST SET BIT NUMBER %d\n",numberOfBitsUsed); | |
codeRepIndex--; | |
numberOfBitsUsed++; | |
// printf("I have come here \n"); | |
totalBitsUsed++; | |
if(numberOfBitsUsed==32){ | |
index--; | |
usedElements++; | |
numberOfBitsUsed= 0; | |
} | |
} | |
/*printf("total bits used is %d \n", totalBitsUsed);*/ | |
// printf("!!!!!!!!!!printing Bit %u \n", bitArray[index]); | |
} | |
/*bitArray[usedElements] = bitArray[usedElements] << (32-totalBitsUsed%32);*/ | |
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[20]);*/ | |
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[19]);*/ | |
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[18]);*/ | |
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[17]);*/ | |
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[16]);*/ | |
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[15]);*/ | |
/*printf("!!!!!!!!!!printing firest element %u \n", bitArray[14]);*/ | |
//printf("!!!!!!!!!!printing last element %u \n", bitArray[index]); | |
/*printf("total bits used is %u \n", totalBitsUsed);*/ | |
return bitArray; | |
} | |
const unsigned int magicNumber = 0x80F0; | |
int totalNumBitUsed2 = 0; | |
void encodeDirectToFile(FILE *input, FILE *output){ | |
while(1){ | |
/*currentChar= (char)fgetc(file);*/ | |
/*if(currentChar==EOF)break;*/ | |
/*char *codeWord = codesTable[currentChar];*/ | |
/*int codeLenght = strlen(codeWord);*/ | |
/*while()*/ | |
} | |
} | |
void printTree(TreeNode *root){ | |
if(root->left == NULL){ | |
printf("%c\n", root->symbol); | |
} | |
else { | |
printTree(root->left); | |
printTree(root->right); | |
} | |
} | |
void handleDecode(FILE *input,FILE *output){ | |
unsigned short header = 0; | |
fread(&header,1,sizeof(unsigned short),input); | |
TreeNode *huffman = createTreeNode(); | |
readTreeFromFile(&huffman,input); | |
/*printTree(huffman);*/ | |
int arr[256]; | |
fread(&totalBitsUsed, 1, sizeof(unsigned int), input); | |
// printf("total bits used: %u\n", totalBitsUsed); | |
int currentByte = 0; | |
int sizeOfArray = (totalBitsUsed/(8*sizeof(unsigned int))) + (totalBitsUsed%(8*sizeof(unsigned int))== 0 ? 0:1); | |
unsigned int buff=0; | |
int usedBits = 0; | |
TreeNode *tempTree = huffman; | |
// printf("array size: %d\n", sizeOfArray); | |
for (int j = 0; j < sizeOfArray; j++){ | |
fread(&buff, 1, sizeof(unsigned int), input); | |
// printf("PRINTING OUT BUFF %u\n",buff); | |
for(int i = 31; i >= 0; i--) { | |
usedBits ++; | |
if(usedBits>totalBitsUsed) { | |
// printf("used bits: %d\n", usedBits); | |
// printf("totalBitsUsed: %d\n", totalBitsUsed); | |
return ; | |
} | |
unsigned int bit = (buff & ( 1 << (i) )) >> (i); | |
// printf("bit value %u\n", bit); | |
if(bit==1) tempTree= tempTree->right; | |
else tempTree=tempTree->left; | |
if(tempTree->symbol!='\0'){ | |
// printf("GOT AN OUTPUT %c\n",tempTree->symbol) ; | |
printf("%c",tempTree->symbol) ; | |
tempTree = huffman; | |
} | |
} | |
} | |
} | |
int main(int agrc,char** argv){ | |
/*unsigned int dummyArray[3] ={1,4293918720,3} ;*/ | |
/*FILE *fp = fopen("dummyData.txt", "wb");*/ | |
/*fwrite(dummyArray, 3, sizeof(unsigned int), fp);*/ | |
/*printf("%u %u",dummyArray[0],dummyArray[1]);*/ | |
/*return 0;*/ | |
/*fclose(fp);*/ | |
/*return 0;*/ | |
handleDecode(fopen(argv[1],"rb"),NULL); | |
/*[>[>if(argc==2){<]<]*/ | |
/*[>[>}<]<]*/ | |
return 0; | |
/*FILE *fp = fopen(argv[1], "rb");*/ | |
/*handleDecode(fp, stdout);*/ | |
/*return 0;*/ | |
unsigned int x = 0; | |
for(int i=0;i<3000;i++){ | |
x = x <<1 |1; | |
} | |
/*FILE *input = fopen(argv[1],"r");*/ | |
/*unsigned int number;*/ | |
/*fread(&number,sizeof(unsigned int),1,input);*/ | |
/*printf("DONE 1\n");*/ | |
/*TreeNode *node ;*/ | |
/*printf("DONE 2\n");*/ | |
/*[>[>[>fread(node,sizeof(TreeNode),1,input);<]<]<]*/ | |
/*printf("DONE 3\n");*/ | |
/*fread(&arraySize,sizeof(arraySize),1,input);*/ | |
/*printf("Array size is %d\n",arraySize);*/ | |
/*unsigned int array [256];*/ | |
/*[>[>[>int arrayo[2];<]<]<]*/ | |
/*printf("DONE \n");*/ | |
/*[>[>[>getCodes(node,arrayo,0);<]<]<]*/ | |
/*fread(array,sizeof(array[0]),arraySize,input);*/ | |
/*for(int i =0;i<arraySize;i++){*/ | |
/*printf("%d \n",array[i]);*/ | |
/*} */ | |
/*fclose(input);*/ | |
/*return 0;*/ | |
//printf("okay"); | |
/*printFrequenceTable();*/ | |
FILE *test = fopen(argv[1],"r"); | |
readFromFile(test); | |
fclose(test); | |
Heap mainHeap =populateHeap(); | |
TreeNode * root = (TreeNode*)removeTopHeap(mainHeap); | |
int arr[256]; | |
getCodes(root,arr,0); | |
test = fopen(argv[1],"r"); | |
unsigned int *bitArr =generateBitArrayEncore(test); | |
FILE *output = fopen(argv[2],"wb"); | |
unsigned short magic = 0x80F0; | |
fwrite(&magic,1,sizeof(unsigned short),output); | |
writeTreeToFile(root,output,0); | |
fwrite(&totalBitsUsed, 1, sizeof(unsigned int), output); | |
//printf("FINAL PRINT 0,%u\n",bitArr[0]); | |
//printf("FINAL PRINT 1,%u\n",bitArr[1]); | |
int sizeOfArray = (totalBitsUsed/(sizeof(unsigned int)*8)) + (totalBitsUsed%(sizeof(unsigned int)*8) == 0 ? 0:1); | |
printf("SIZE OF ARRAY IS %d\n",sizeOfArray); | |
fwrite(bitArr, sizeOfArray, sizeof(unsigned int), output); | |
fclose(output); | |
/*fwrite(&magic,sizeof(unsigned int),1,output);*/ | |
/*[>fwrite(root,sizeof(TreeNode),1,output);<]*/ | |
/*fwrite(&arraySize,sizeof(arraySize),1,output);*/ | |
/*fwrite(bitArr,sizeof(bitStorageArray[0]),arraySize,output);*/ | |
/*fclose(output);*/ | |
/*printFrequenceTable();*/ | |
/*printf("PINRTING \n");*/ | |
/*printf("DONE \n");*/ | |
for(int i = 0 ; i <tableSize;i++){ | |
if(codesTable[i][0]=='\0')continue; | |
printf("%s %c \n",codesTable[i],i); | |
} | |
freeTree(root); | |
/*free(root);*/ | |
destroyHeap(mainHeap); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment