From e427fe4c0b2933badd73ac316f87e078f7754359 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 26 Nov 2022 14:31:18 +0100 Subject: [PATCH] working on actual unhacky implementation --- huffman/huffman.c | 113 ++++++++++++++++++++++------------------------ 1 file changed, 54 insertions(+), 59 deletions(-) diff --git a/huffman/huffman.c b/huffman/huffman.c index f0b6406..db7e64e 100644 --- a/huffman/huffman.c +++ b/huffman/huffman.c @@ -8,6 +8,8 @@ #include #include +#define BUFSIZE 512 + // a single node, only having information for a single byte. struct Node { uint8_t byte; @@ -22,6 +24,24 @@ struct Heap { bool isRoot; }; +// global vars for Heaps and Nodes. +struct Node* nodes; +struct Heap* heaps; + +// initialize our nodes +void initNodes(){ + nodes = malloc(sizeof(struct Node)*256); + heaps = malloc(sizeof(struct Heap)*256); // not sure if i might need more memory for heaps? + + for(int i = 0; i < 256; i++){ + nodes[i].byte = i; + nodes[i].occurences = 0; + nodes[i].frequencyPriority = 0; + heaps[i].isRoot = false; + } + +} + // stolen from stackoverflow // https://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c off_t fsize(const char *filename) { @@ -96,8 +116,8 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } filelen = fsize(filestring); - if(verbose) - printf("filesize: %ldB\n", filelen); + if(verbose) + printf("filesize: %ldB\n", filelen); } else { // empty filestring or filestring is NULL @@ -111,19 +131,20 @@ int main(int argc, char *argv[]) { } else { + // compress the file if (verbose) printf("compressing file...\n"); // frequency analysis - + uint8_t buf [BUFSIZE]; // dump start of file if debugging // FIXME add conditions if the part to print is smaller than 512B if(debug){ printf("[DEBUG]First 512 bytes are:\n"); - fread(buf, 1, 512, fptrR); - for(int i=0;i<512;i++){ + fread(buf, 1, BUFSIZE, fptrR); + for(int i=0;i= 0)){ + if(!feof(fptrR)) { + // no EOF, but didn't read full buffer. Assuming an error + printf("Error while reading file."); + exit(EXIT_FAILURE); + } + else{ + // reached EOF, finished + break; + } } - printf("file length(added up occurences):\t%lldB\n", addedUpOccurences); + else{ + printf("Undefined behaviour while reading file.\n"); + exit(EXIT_FAILURE); + } + fseek(fptrR, BUFSIZE, SEEK_CUR); } - if(verbose) - printf("\n\nDone calculating occurences of bytes.\n"); - - // TODO - // calculate the frequencies of the bytes. - double frequencies[256]; - for(int i=0;i<256;i++){ - frequencies[i]=((double)occurences[i]/(double)filelen)*100; // calculate frequencies of bytes in percent (example: 05.23 (%)) - } - if(debug){ - printf("Frequencies:\n"); - for(int i=0;i<256;i++){ - if(i%8==0) - printf("\n"); - printf("0x%02x: %05.02f%%\t", i, frequencies[i]); - } - double addedUpFrequencies = 0; - for(int i=0;i<256;i++){ - addedUpFrequencies += frequencies[i]; - } - printf("\n\nadded up frequencies: %05.02f%%\n",addedUpFrequencies); - } } + fclose(fptrR); printf("\n"); + if(debug) // wait for input to end. + getchar(); exit(EXIT_SUCCESS); }