summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2023-03-22 22:58:12 +0200
committerkartofen <mladenovnasko0@gmail.com>2023-03-22 22:58:12 +0200
commit03afb5602acd88341232774c0ccd844c25e6e4d5 (patch)
tree61d2b3716a0f377f15b4a79b5cf809fe6b4eeb79
parent360f7996ed1777e1faab2e422079aa52cc8e42cb (diff)
more progressHEADmaster
-rwxr-xr-xbuild.sh3
-rw-r--r--encode.c18
-rw-r--r--text2.txt2
3 files changed, 16 insertions, 7 deletions
diff --git a/build.sh b/build.sh
index 94427a6..e35b44a 100755
--- a/build.sh
+++ b/build.sh
@@ -4,3 +4,6 @@ set -xe
gcc -o encode encode.c -Wall -Wextra -pedantic -g
gcc -o decode decode.c -Wall -Wextra -pedantic -g
+
+time ./encode < text2.txt > text2.huff
+time ./decode < text2.huff
diff --git a/encode.c b/encode.c
index 5a03f92..552f201 100644
--- a/encode.c
+++ b/encode.c
@@ -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) {
diff --git a/text2.txt b/text2.txt
new file mode 100644
index 0000000..428960f
--- /dev/null
+++ b/text2.txt
@@ -0,0 +1,2 @@
+One Two Tree Four Five Eight
+fff