diff options
Diffstat (limited to 'encode.c')
-rw-r--r-- | encode.c | 18 |
1 files changed, 11 insertions, 7 deletions
@@ -36,6 +36,8 @@ size_t write_tree_shape(node_t *root); size_t write_encoded(node_t *root); void encode(); +size_t MMAP_SIZE_CAP = 1024; // 1 kibibyte + FILE *in; FILE *out; @@ -48,7 +50,7 @@ node_t *encoding_table[256] = {0}; void encode() { // write tree // - wb_flush(1); // flush amount of bits (written in gen_tree()) + wb_flush(1); // flush amount of leafs in tree (written in gen_tree()) wb_flush(write_tree_data(root)); wb_flush(write_tree_shape(root)/8 + 1); @@ -61,13 +63,15 @@ void encode() // encode // char ch; while(fread(&ch, sizeof(char), 1, in) > 0) { - if(encoding_table[ch] == NULL) { - fprintf(stderr, "Character %c not found encoding table\n", ch); - exit(1); - } + // fprintf(stderr, "%c\n", ch); write_encoded(encoding_table[ch]); } - wb_flush(write_encoded(NULL)/8+1); + // fprintf(stderr, "%d\n", write_encoded(NULL)/8); + wb_flush((write_encoded(NULL) + 7)/8); + + // write amout of extra bits + write_buf[0] = write_encoded(NULL) % 8; + wb_flush(1); } int main(void) @@ -145,7 +149,7 @@ size_t write_encoded(node_t *root) static size_t bits = 0; if(root == NULL || root->parent == NULL) return bits; - write_buf[bits/8] |= root->direction << (bits % 8); + write_buf[bits/8] |= root->direction << (7 - (bits % 8)); bits++; if(bits/8 == WRITE_BUF_CAP) { |