//Function takes frequencies from lists and creates list nodes ListNode * ListNode_build(Node * freqList) { ListNode * head = NULL; while(freqList != NULL) { TreeNode * tn = TreeNode_create(freqList -> char_val, freqList -> freq); ListNode * wn = ListNode_create(tn); head = ListNode_insert(head, wn); freqList = freqList -> next; } return head; }
END_TEST START_TEST (test_ListNode_create) { char *s = "foo"; ListNode_t *node = ListNode_create(s); /* fail_unless(node->item == s ); fail_unless(node->next == NULL); */ ListNode_free(node); }
//Running everything and saving it correctly. int main(int argc, char **argv) { if(argc < 2) { printf("Error, no input given.\n"); return(0); } int freq_array[127]; char * filename = argv[1]; int filename_size = strlen(filename); frequency_count(filename, freq_array); char * outfile = (char *)malloc(filename_size+5 * sizeof(char)); strcpy(outfile, filename); outfile = strcat(outfile, ".huff"); Node * list; list = List_build(freq_array); if (list == NULL) { return(0); } Node * temp; temp = list; while(temp != NULL) { printf("Frequency of %c = %d\n", temp -> char_val, temp -> freq); temp = temp -> next; } ListNode * tree_list; tree_list = ListNode_build(list); while(tree_list -> next != NULL) { ListNode * second = tree_list -> next; ListNode * third = second -> next; TreeNode * tn1 = tree_list -> tree_ptr; TreeNode * tn2 = second -> tree_ptr; free(tree_list); free(second); tree_list = third; TreeNode * fin = Tree_merge(tn1, tn2); ListNode * wn = ListNode_create(fin); tree_list = ListNode_insert(tree_list, wn); } Weight_print(tree_list); List_destroy(list); TreeNode * root = tree_list -> tree_ptr; free(tree_list); int numRow = Tree_leaf(root); int numCol = Tree_height(root); numCol++; int ** codebook = malloc(sizeof(int*) * numRow); int row; for(row = 0; row < numRow; row++) { codebook[row] = malloc(sizeof(int) * numCol); int col; for(col = 0; col < numCol; col++) { codebook[row][col] = -1; } } buildCodeBook(root, codebook); printCodeBook(codebook, numRow); unsigned int numChar = 0; int z; for(z = 0; z < 127; z++) { numChar += freq_array[z]; } printf("numChar: %d\n", numChar); int mapping[127]; int ind; for(ind = 0; ind < 127; ind++) { mapping[ind] = -1; int ind2; for(ind2 = 0; ind2 < numRow; ind2 ++) { if(codebook[ind2][0] == ind) { mapping[ind] = ind2; } } } Tree_header(root, numChar,outfile); compress(filename, outfile, codebook, mapping); return(0); }