source: liacs/da/opdr2b/compressie.cc@ 311

Last change on this file since 311 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: 2.2 KB
Line 
1/* Author : Rick van der Zwet
2 * S-number : 0433373
3 * Version : $Id: compressie.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 "trie.cc"
11#include "fileio.h"
12#include "constants.h"
13
14/* XXX: File are only checked at creation time, not during execution */
15int
16main(int argc, char ** argv) {
17 FILE * input;
18 FILE * output;
19 int knob = START_KNOB;
20 int code = 0;
21 // character storage
22 /* input Letter */
23 int c;
24 Trie<int> * lwz;
25
26 if (argc < 2) {
27 fprintf(stderr, "Usage: %s <infile> [<outfile>]\n",argv[0]);
28 return(EX_USAGE);
29 }
30
31 input = fopen(argv[1], "r");
32 if (input == NULL) {
33 fprintf(stderr,"Error: Unable to use `%s', errno: %i\n",
34 argv[1], errno);
35 return(EX_NOINPUT);
36 }
37
38 if (argc == 3) {
39 output = fopen(argv[2], "w");
40 if (output == NULL) {
41 fprintf(stderr,"Error: Unable to use `%s', errno: %i\n",
42 argv[2], errno);
43 return(EX_CANTCREAT);
44 }
45 } else
46 output = stdout;
47
48 /* initialiseer Trie voor ZLW codering */
49 lwz = new Trie<int>;
50 for (code = 0; code <= 0xFF; code++) {
51 lwz->GoRoot();
52 lwz->AddChild(code);
53 lwz->GoChild(code);
54 lwz->PutInfo(knob);
55 knob++;
56 }
57 lwz->GoRoot();
58
59 /* while not eof (SourceFile) */
60 while ((c = fgetc(input)) != EOF) {
61 if (not lwz->ExistsChild(c)) {
62 tofile(output, lwz->GetInfo());
63 /* if not (maximaal aantal strings in Trie) */
64 if (knob < MAX_KNOB) {
65 /* voes nieuwe code via tak Letter and Trie toe */
66 lwz->AddChild(c);
67 lwz->GoChild(c);
68 lwz->PutInfo(knob);
69 knob++;
70 }
71 /* ga naar wortel Trie */
72 lwz->GoRoot();
73 }
74 /* ga naar kind Letter in Trie */
75 lwz->GoChild(c);
76 }
77 tofile(output, lwz->GetInfo());
78
79 /* flush buffer */
80 closefile(output);
81
82 /* delete trie */
83 delete lwz;
84
85 fclose(input);
86 fclose(output);
87 return(EX_OK);
88}
Note: See TracBrowser for help on using the repository browser.