Commit 0993657f by Rudolf

Remove EOF char

As I heard today, the compressor should do binary files.

NB! This EOF solution works perfectly fine, as 0xff is interpreted as ' '
(not space). Normal text files never have this character in them.
parent 52b38d73
Showing with 2 additions and 23 deletions
......@@ -31,9 +31,6 @@ void print_char(unsigned char c)
case 0:
printf("0\n");
break;
case EOF_CHAR:
printf("EOF\n");
break;
case '\n':
printf("\\n\n");
break;
......
......@@ -35,9 +35,8 @@ struct tree *create_tree(FILE *file)
if (c == EOF || c > MY_BUF-1)
break;
/* While theoretically these files *can* be supported, Huffman is meant
* for text encoding/decoding, so we don't give a sh. */
if (c > MY_BUF-1 || c == EOF_CHAR) {
/* Should never reach this but better safe than sorry. */
if (c > MY_BUF-1) {
fprintf(stderr, "File not supported\n");
return NULL;
}
......@@ -58,9 +57,6 @@ struct tree *create_tree(FILE *file)
}
}
/* Insert our special EOF char. */
forest[ntrees++] = insert_simple_tree(EOF_CHAR, 1);
/* Sort ascending */
for (int i = 0; i < ntrees; i++) {
for (int j = i+1; j < ntrees; j++) {
......@@ -140,9 +136,6 @@ int encode_tree(struct BIT_BUFFER *bitbuf, struct tree *parent, char *buf,
write_entry(bitbuf, parent, buf[i]);
}
/* Insert our special EOF char. */
write_entry(bitbuf, parent, EOF_CHAR);
return 0;
}
......@@ -153,20 +146,12 @@ static int read_entries(struct BIT_BUFFER *bitbuf, FILE *out,
struct tree *leaf = parent;
while ((bit = bb_read(bitbuf, 1)) != NULL) {
if (*bit == 1) {
if (leaf->right->ch == EOF_CHAR) {
free(bit);
break;
}
fwrite(&leaf->right->ch, 1, 1, out);
leaf->right->freq++;
leaf = parent; /* Reset */
} else {
leaf = leaf->left;
if (!leaf->left) {
if (leaf->ch == EOF_CHAR) {
free(bit);
break;
}
fwrite(&leaf->ch, 1, 1, out);
leaf->freq++;
leaf = parent; /* Reset */
......
......@@ -4,9 +4,6 @@
#include "bit-buffer.h"
/* Our special EOF character. */
#define EOF_CHAR 0xff
struct tree {
struct tree *left;
struct tree *right;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment