/* Author : Rick van der Zwet * S-number : 0433373 * Version : $Id: compressie.cc 457 2008-01-08 22:24:08Z rick $ * Copyright : FreeBSD Licence */ #include #include #include #include #include "trie.cc" #include "fileio.h" #include "constants.h" /* XXX: File are only checked at creation time, not during execution */ int main(int argc, char ** argv) { FILE * input; FILE * output; int knob = START_KNOB; int code = 0; // character storage /* input Letter */ int c; Trie * lwz; if (argc < 2) { fprintf(stderr, "Usage: %s []\n",argv[0]); return(EX_USAGE); } input = fopen(argv[1], "r"); if (input == NULL) { fprintf(stderr,"Error: Unable to use `%s', errno: %i\n", argv[1], errno); return(EX_NOINPUT); } if (argc == 3) { output = fopen(argv[2], "w"); if (output == NULL) { fprintf(stderr,"Error: Unable to use `%s', errno: %i\n", argv[2], errno); return(EX_CANTCREAT); } } else output = stdout; /* initialiseer Trie voor ZLW codering */ lwz = new Trie; for (code = 0; code <= 0xFF; code++) { lwz->GoRoot(); lwz->AddChild(code); lwz->GoChild(code); lwz->PutInfo(knob); knob++; } lwz->GoRoot(); /* while not eof (SourceFile) */ while ((c = fgetc(input)) != EOF) { if (not lwz->ExistsChild(c)) { tofile(output, lwz->GetInfo()); /* if not (maximaal aantal strings in Trie) */ if (knob < MAX_KNOB) { /* voes nieuwe code via tak Letter and Trie toe */ lwz->AddChild(c); lwz->GoChild(c); lwz->PutInfo(knob); knob++; } /* ga naar wortel Trie */ lwz->GoRoot(); } /* ga naar kind Letter in Trie */ lwz->GoChild(c); } tofile(output, lwz->GetInfo()); /* flush buffer */ closefile(output); /* delete trie */ delete lwz; fclose(input); fclose(output); return(EX_OK); }