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) ...@@ -31,9 +31,6 @@ void print_char(unsigned char c)
case 0: case 0:
printf("0\n"); printf("0\n");
break; break;
case EOF_CHAR:
printf("EOF\n");
break;
case '\n': case '\n':
printf("\\n\n"); printf("\\n\n");
break; break;
......
...@@ -35,9 +35,8 @@ struct tree *create_tree(FILE *file) ...@@ -35,9 +35,8 @@ struct tree *create_tree(FILE *file)
if (c == EOF || c > MY_BUF-1) if (c == EOF || c > MY_BUF-1)
break; break;
/* While theoretically these files *can* be supported, Huffman is meant /* Should never reach this but better safe than sorry. */
* for text encoding/decoding, so we don't give a sh. */ if (c > MY_BUF-1) {
if (c > MY_BUF-1 || c == EOF_CHAR) {
fprintf(stderr, "File not supported\n"); fprintf(stderr, "File not supported\n");
return NULL; return NULL;
} }
...@@ -58,9 +57,6 @@ struct tree *create_tree(FILE *file) ...@@ -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 */ /* Sort ascending */
for (int i = 0; i < ntrees; i++) { for (int i = 0; i < ntrees; i++) {
for (int j = i+1; j < ntrees; j++) { for (int j = i+1; j < ntrees; j++) {
...@@ -140,9 +136,6 @@ int encode_tree(struct BIT_BUFFER *bitbuf, struct tree *parent, char *buf, ...@@ -140,9 +136,6 @@ int encode_tree(struct BIT_BUFFER *bitbuf, struct tree *parent, char *buf,
write_entry(bitbuf, parent, buf[i]); write_entry(bitbuf, parent, buf[i]);
} }
/* Insert our special EOF char. */
write_entry(bitbuf, parent, EOF_CHAR);
return 0; return 0;
} }
...@@ -153,20 +146,12 @@ static int read_entries(struct BIT_BUFFER *bitbuf, FILE *out, ...@@ -153,20 +146,12 @@ static int read_entries(struct BIT_BUFFER *bitbuf, FILE *out,
struct tree *leaf = parent; struct tree *leaf = parent;
while ((bit = bb_read(bitbuf, 1)) != NULL) { while ((bit = bb_read(bitbuf, 1)) != NULL) {
if (*bit == 1) { if (*bit == 1) {
if (leaf->right->ch == EOF_CHAR) {
free(bit);
break;
}
fwrite(&leaf->right->ch, 1, 1, out); fwrite(&leaf->right->ch, 1, 1, out);
leaf->right->freq++; leaf->right->freq++;
leaf = parent; /* Reset */ leaf = parent; /* Reset */
} else { } else {
leaf = leaf->left; leaf = leaf->left;
if (!leaf->left) { if (!leaf->left) {
if (leaf->ch == EOF_CHAR) {
free(bit);
break;
}
fwrite(&leaf->ch, 1, 1, out); fwrite(&leaf->ch, 1, 1, out);
leaf->freq++; leaf->freq++;
leaf = parent; /* Reset */ leaf = parent; /* Reset */
......
...@@ -4,9 +4,6 @@ ...@@ -4,9 +4,6 @@
#include "bit-buffer.h" #include "bit-buffer.h"
/* Our special EOF character. */
#define EOF_CHAR 0xff
struct tree { struct tree {
struct tree *left; struct tree *left;
struct tree *right; 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