source: liacs/da/opdr2b/decompressie.cc@ 54

Last change on this file since 54 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

File size: 4.8 KB
RevLine 
[2]1/* Author : Rick van der Zwet
2 * S-number : 0433373
3 * Version : $Id: decompressie.cc 457 2008-01-08 22:24:08Z rick $
4 * Copyright : FreeBSD Licence
5 */
6#include <stdio.h>
7#include <errno.h>
8#include <stdlib.h>
9#include <sysexits.h>
10#include <string>
11#include "trie.cc"
12#include "fileio.h"
13#include "constants.h"
14using namespace std;
15
16
17
18/* lookup table to quickly convert key into string */
19struct keypair {
20 int value;
21 int next;
22};
23keypair * lookup[MAX_KNOB];
24
25/* global trie */
26Trie<int> * lwz;
27
28
29/* find string of certain key, return empty if none found */
30void
31getstring (int * output, const int key) {
32 int temp[MAX_KNOB + 1];
33 for (int i = 0; i <= MAX_KNOB + 1; i++) {
34 output[i] = -2;
35 temp[i] = -2;
36 }
37
38 if (lookup[key] == NULL) {
39 output[0] = ROOT;
40 return;
41 }
42
43 int walker = key;
44 int count = 0;
45 while (walker != ROOT) {
46 temp[count] = lookup[walker]->value;
47 walker = lookup[walker]->next;
48 count++;
49 }
50
51 /* reverse the whole string */
52 count--;
53 walker = 0;
54 for (; count >= 0; count--) {
55 output[walker] = temp[count];
56 //fprintf(stderr, "Value %i\n", temp[count]);
57 walker++;
58 }
59 /* put a custom end on the string */
60 output[walker] = ROOT;
61}
62
63
64void decode (FILE * input, FILE *output)
65{
66 /* character storage */
67 int numbers[2];
68 /* string storage of word to find */
69 int string[2][MAX_KNOB + 1];
70
71 /* temp */
72 int count = 0; /* temp variable, loop counter */
73 int knob = START_KNOB; /* knob to be added */
74 int code = 0; /* temp variable, loop counter */
75 Trie<int> * lwz;
76
77 /* initialiseer Trie voor ZLW codering */
78 lwz = new Trie<int>;
79
80 for (code = 0; code <= 0xFF; code++) {
81 lwz->GoRoot();
82 lwz->AddChild(code);
83 lwz->GoChild(code);
84 lwz->PutInfo(knob);
85 /* Put data into lookup array */
86 lookup[knob] = new keypair;
87 lookup[knob]->value = code;
88 lookup[knob]->next = ROOT;
89
90 knob++;
91 }
92
93 /* initial input Letter */
94 numbers[1] = fetchnumber(input);
95 while (1) {
96 numbers[0] = numbers[1];
97 numbers[1] = fetchnumber(input);
98
99 /* no need to write any other strings, cause
100 * last knob exists for sure
101 */
102 if (numbers[1] == FILE_END) {
103 getstring(string[0], numbers[0]);
104 for (count = 0; string[0][count] != ROOT; count++)
105 fprintf(output,"%c",string[0][count]);
106 break;
107 }
108
109 /* find potential string */
110 #ifdef DEBUG
111 fprintf(stderr, "Find numbers %i and %i\n", numbers[0], numbers[1]);
112 #endif
113 getstring(string[0], numbers[0]);
114 getstring(string[1], numbers[1]);
115
116 /* string1 not exits, find special letter */
117 if (string[1][0] == ROOT) {
118 #ifdef DEBUG
119 fprintf(stderr, "Special find '%i'\n", string[0][0]);
120 #endif
121 string[1][0] = string[0][0];
122 }
123
124 /* find parent */
125 lwz->GoRoot();
126 for (count = 0; string[0][count] != ROOT; count++) {
127 lwz->GoChild(string[0][count]);
128 #ifdef DEBUG
129 fprintf(stderr, "Addon Position: %i\n", lwz->GetInfo());
130 #endif
131 }
132
133 /* add child */
134 if (knob < MAX_KNOB) {
135 lookup[knob] = new keypair;
136 lookup[knob]->value = string[1][0];
137 lookup[knob]->next = lwz->GetInfo();
138
139 #ifdef DEBUG
140 fprintf(stderr, "Knob add: %i, value: %i, next %i\n", knob,
141 string[1][0], lwz->GetInfo());
142 #endif
143
144 lwz->AddChild(string[1][0]);
145 lwz->GoChild(string[1][0]);
146 lwz->PutInfo(knob);
147 knob++;
148 }
149
150 for (count = 0; string[0][count] != ROOT; count++) {
151 fprintf(output,"%c",string[0][count]);
152 }
153 }
154
155 /* free trie memory */
156 delete lwz;
157
158 /* free keypair allocations */
159 for ( ; knob <= 0; knob--) {
160 delete lookup[knob];
161 }
162
163}
164
165
166
167/* XXX: File are only checked at creation time, not during execution */
168int
169main(int argc, char ** argv) {
170 FILE * input;
171 FILE * output;
172
173 if (argc < 2) {
174 fprintf(stderr, "Usage: %s <infile> [<outfile>]\n",argv[0]);
175 return(EX_USAGE);
176 }
177
178 input = fopen(argv[1], "r");
179 if (input == NULL) {
180 fprintf(stderr,"Error: Unable to use `%s', errno: %i\n",
181 argv[1], errno);
182 return(EX_NOINPUT);
183 }
184
185 if (argc == 3) {
186 output = fopen(argv[2], "w");
187 if (output == NULL)
188 {
189 fprintf(stderr,"Error: Unable to use `%s', errno: %i\n",
190 argv[2], errno);
191 return(EX_CANTCREAT);
192 }
193 } else
194 output = stdout;
195
196 /* call actual program */
197 decode(input,output);
198
199 fclose(input);
200 fclose(output);
201 return(EX_OK);
202}
Note: See TracBrowser for help on using the repository browser.