/**************************************************************************** * huffile.h * * Computer Science 50 * Problem Set 6 * * Declares a layer of abstraction for Huffman-coded files. ***************************************************************************/ #ifndef HUFFILE_H #define HUFFILE_H #include #include #include #include #include /* * SYMBOLS * * Number of symbols for which we might need codes. */ #define SYMBOLS 256 /* * MAGIC * * Magic number identifying a Huffman-coded file. */ #define MAGIC 0x46465548 /* * mode * * Allowed I/O modes for Huffman-coded files. */ typedef enum { READ, WRITE } mode; /* * Huffile * * In-memory representation of a Huffman-coded file. */ typedef struct { // 8-bit buffer unsigned char buffer; // actual file on disk FILE *stream; // current location in buffer unsigned char ith; // number of valid bits in file's second-to-last byte unsigned char zth; // I/O mode mode io; // size of file off_t size; } Huffile; /* * Huffeader * * Header for a Huffman-coded file. */ typedef struct { // magic number int magic; // symbols' frequencies int frequencies[SYMBOLS]; // checksum for frequencies int checksum; } Huffeader; /* * int * bread(Huffile *hf) * * Returns file's next bit (as 0 or 1) or EOF upon EOF or error. */ int bread(Huffile *hf); /* * bool * bwrite(int b, Huffile *hf) * * Lazily writes b (which must be 0 or 1) to Huffile as a single bit. * * Returns TRUE on success and FALSE on failure. */ bool bwrite(int b, Huffile *hf); /* * bool * hfclose(Huffile *hf) * * Closes file. * * Returns TRUE on success and FALSE on failure. */ bool hfclose(Huffile *hf); /* * Huffile * * hfopen(const char *path, const char *mode) * * Opens path as a Huffile, returning pointer thereto or NULL on error. * * Mode must be "r" or "w". */ Huffile * hfopen(const char *path, const char *mode); /* * bool * hread(Huffeader *h, Huffile *hf) * * Reads in header if still at start of file. * * Returns TRUE on success and FALSE on failure (e.g., not at start of file). */ bool hread(Huffeader *h, Huffile *hf); /* * bool * hwrite(Huffeader *h, Huffile *hf) * * Writes out header if still at start of file. * * Returns TRUE on success and FALSE on failure (e.g., not at start of file). */ bool hwrite(Huffeader *h, Huffile *hf); #endif