1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

// ILLUSTRATION -- not working code
//
// This was part of a discussion after the 3:00 PM lecture, and not part of the lecture
// itself.

void _write(const char* path, const uchar* uncompressed_bytes) {
    FILE* fp = fopen(path, "w");
    for(int i = 0; uncompressed_bytes[i] != '\0'; i++) {
        fputc(uncompressed_bytes[i], fp);
    }
    fclose(fp);
}

int main(int argc, char* argv[]) {
    uchar uncompressed_bytes = "huffman fluffs many mums";

    const char* path_uncompressed = "uncompressed.txt";
    const char* path_compressed   = "compressed.txt";

    // Write the uncompressed (input) data to a file.
    _write(path_uncompressed, uncompressed_bytes);

    // Calculate frequencies
    Frequencies freqs;
    const char* error = NULL;
    if(calc_frequencies(freqs, path_uncompressed, &error) ) {
        Node* head = make_huffman_pq(freqs);

        // Make the Huffman tree
        TreeNode* root = make_huffman_tree(head);

        // Write compressed (output) data to an output file
        BitWriter writer = open_bit_writer(path_compressed);
        write_compressed(root, &writer, contents_of_file);
        close_bit_writer(writer);
    }

    destroy_huffman_tree(root);

    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2022 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.