Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kheesu committed Dec 10, 2022
1 parent 282c5c9 commit 5a6899c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC=gcc
CCFLAGS= -Wall -O2 -I.
CCFLAGS= -Wall -O2 -I. -s
HEAD = huff.h
O = main.o huff.o heap.o

Expand Down
2 changes: 1 addition & 1 deletion heap.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "huff.h"

//Swap two generic variable of same type
//Swap two variable of same type
void swap(void * a, void* b, size_t bytes) {
char *temp = calloc(sizeof(char), bytes);

Expand Down
1 change: 1 addition & 0 deletions huff.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ int searchhuffcode(node *hufftree, char *code, unsigned int len, unsigned char t
return result;
}

//Function for freeing entire tree
void cleantree(node *tree) {
if(tree == NULL) return;
if(tree->isleaf) {
Expand Down
14 changes: 13 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ int main(int argc, char** argv) {
FREADERR();
}

//Fill out freqtable
char_dist(text);
fclose(text);

Expand Down Expand Up @@ -178,6 +179,7 @@ int main(int argc, char** argv) {

fclose(input);
}
//If query flag -q is provided
else if(!strcmp("-q", argv[1])) {

long temp = -1;
Expand All @@ -188,7 +190,7 @@ int main(int argc, char** argv) {
fprintf(stderr, "shc: Incorrect argument format\n");
exit(-1);
}

//Query not in argument
if(argc == 3) {
fprintf(stdout, "Enter query in decimal : ");

Expand All @@ -200,8 +202,13 @@ int main(int argc, char** argv) {
fprintf(stderr, "shc: Invalid input\n");
exit(-1);
}

if(temp > 255) {
fprintf(stderr, "shc: Invalid input\n");
}
}
}
//Query in argument
else if(argc == 4) {
temp = strtol(argv[3], &err, 10);
if(temp < 0 || temp > 256 || errno == ERANGE) {
Expand All @@ -210,6 +217,8 @@ int main(int argc, char** argv) {
}
}

//Following code block is copied from decomp function

errno = 0;
FILE *input = fopen(argv[2], "rb");
if(errno != 0) {
Expand All @@ -227,10 +236,13 @@ int main(int argc, char** argv) {
if(identifier[4] == 0) maxchar = 256;
else maxchar = 128;

//Read original file size
fread(&fsize, sizeof(unsigned long long), 1, input);

//Read frequecny table
fread(freqtable, sizeof(unsigned int), maxchar, input);

//Build huffman tree from frequency table
node *tree = tbltoht();

char *code = calloc(MAXHCODE, sizeof(char));
Expand Down

0 comments on commit 5a6899c

Please sign in to comment.