huffman

huffman coding implementation in C
Log | Files | Refs | README | LICENSE

commit a3a211cc35db89ddd98f81202fbadc28e1a64588
parent 2a219554ce92704800ba237c253d822f2e1e0a6a
Author: tin <ichtinnotl@gmail.com>
Date:   Mon, 29 Oct 2018 22:12:57 +0100

enables piping a chunk of text, detecting the freq of the chars and printing stats

Diffstat:
huffman.c | 32++++++++++++++++++++++++++++----
huffman.h | 8++++++++
2 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/huffman.c b/huffman.c @@ -1,6 +1,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <ctype.h> #include "huffman.h" @@ -23,13 +24,36 @@ delete_node(node * n) { free(n); } +void +detect_frequency(int * byte_frequency, char * p) { + int len = strlen(p); + for (int i = 0; i < len; i++) { + byte_frequency[p[i]]++; + } +} + +void +print_stats(int * byte_frequency) { + for (int i = 0; i < NUM_OF_CHARACTERS; i++) { + if (byte_frequency[i] > 0) { if (isprint(i)) { printf(" %c : %d\n", i, byte_frequency[i]); } } + } +} + int main () { - int f = 1; - char c = 'a'; + char * p = calloc(BUFFER_SIZE, sizeof(char)); + + int byte_frequency[NUM_OF_CHARACTERS]; + memset(byte_frequency, 0, 256 * sizeof(int)); + + while(fgets(p, BUFFER_SIZE, stdin) != NULL) { + detect_frequency(byte_frequency, p); + } + + print_stats(byte_frequency); - node * n = create_node(c, f); - delete_node(n); + /* node * n = create_node(c, f); */ + /* delete_node(n); */ exit(0); } diff --git a/huffman.h b/huffman.h @@ -1,3 +1,6 @@ +#define BUFFER_SIZE 4096 +#define NUM_OF_CHARACTERS 256 + typedef struct { char character; int frequency; @@ -13,6 +16,11 @@ create_node(const char c, const int f); void delete_node(node * n); +void +print_stats(int * byte_frequency); + +void +detect_frequency(int * byte_frequency, char * p);