/* Author : Rick van der Zwet * S-number : 0433373 * Version : $Id: decompressie.cc 457 2008-01-08 22:24:08Z rick $ * Copyright : FreeBSD Licence */ #include #include #include #include #include #include "trie.cc" #include "fileio.h" #include "constants.h" using namespace std; /* lookup table to quickly convert key into string */ struct keypair { int value; int next; }; keypair * lookup[MAX_KNOB]; /* global trie */ Trie * lwz; /* find string of certain key, return empty if none found */ void getstring (int * output, const int key) { int temp[MAX_KNOB + 1]; for (int i = 0; i <= MAX_KNOB + 1; i++) { output[i] = -2; temp[i] = -2; } if (lookup[key] == NULL) { output[0] = ROOT; return; } int walker = key; int count = 0; while (walker != ROOT) { temp[count] = lookup[walker]->value; walker = lookup[walker]->next; count++; } /* reverse the whole string */ count--; walker = 0; for (; count >= 0; count--) { output[walker] = temp[count]; //fprintf(stderr, "Value %i\n", temp[count]); walker++; } /* put a custom end on the string */ output[walker] = ROOT; } void decode (FILE * input, FILE *output) { /* character storage */ int numbers[2]; /* string storage of word to find */ int string[2][MAX_KNOB + 1]; /* temp */ int count = 0; /* temp variable, loop counter */ int knob = START_KNOB; /* knob to be added */ int code = 0; /* temp variable, loop counter */ Trie * lwz; /* 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); /* Put data into lookup array */ lookup[knob] = new keypair; lookup[knob]->value = code; lookup[knob]->next = ROOT; knob++; } /* initial input Letter */ numbers[1] = fetchnumber(input); while (1) { numbers[0] = numbers[1]; numbers[1] = fetchnumber(input); /* no need to write any other strings, cause * last knob exists for sure */ if (numbers[1] == FILE_END) { getstring(string[0], numbers[0]); for (count = 0; string[0][count] != ROOT; count++) fprintf(output,"%c",string[0][count]); break; } /* find potential string */ #ifdef DEBUG fprintf(stderr, "Find numbers %i and %i\n", numbers[0], numbers[1]); #endif getstring(string[0], numbers[0]); getstring(string[1], numbers[1]); /* string1 not exits, find special letter */ if (string[1][0] == ROOT) { #ifdef DEBUG fprintf(stderr, "Special find '%i'\n", string[0][0]); #endif string[1][0] = string[0][0]; } /* find parent */ lwz->GoRoot(); for (count = 0; string[0][count] != ROOT; count++) { lwz->GoChild(string[0][count]); #ifdef DEBUG fprintf(stderr, "Addon Position: %i\n", lwz->GetInfo()); #endif } /* add child */ if (knob < MAX_KNOB) { lookup[knob] = new keypair; lookup[knob]->value = string[1][0]; lookup[knob]->next = lwz->GetInfo(); #ifdef DEBUG fprintf(stderr, "Knob add: %i, value: %i, next %i\n", knob, string[1][0], lwz->GetInfo()); #endif lwz->AddChild(string[1][0]); lwz->GoChild(string[1][0]); lwz->PutInfo(knob); knob++; } for (count = 0; string[0][count] != ROOT; count++) { fprintf(output,"%c",string[0][count]); } } /* free trie memory */ delete lwz; /* free keypair allocations */ for ( ; knob <= 0; knob--) { delete lookup[knob]; } } /* XXX: File are only checked at creation time, not during execution */ int main(int argc, char ** argv) { FILE * input; FILE * output; 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; /* call actual program */ decode(input,output); fclose(input); fclose(output); return(EX_OK); }