Commit | Line | Data |
---|---|---|
54fea4c1 C |
1 | /* build_trees.c - build decode trees */ |
2 | ||
3 | #ifndef lint | |
4 | static char *rcsid = "$Header: /f/osi/others/quipu/photo/RCS/build_trees.c,v 7.1 90/09/24 15:36:37 mrose Exp $"; | |
5 | #endif | |
6 | ||
7 | /* | |
8 | * $Header: /f/osi/others/quipu/photo/RCS/build_trees.c,v 7.1 90/09/24 15:36:37 mrose Exp $ | |
9 | * | |
10 | * | |
11 | * $Log: build_trees.c,v $ | |
12 | * Revision 7.1 90/09/24 15:36:37 mrose | |
13 | * update | |
14 | * | |
15 | * Revision 7.0 89/11/23 22:01:33 mrose | |
16 | * Release 6.0 | |
17 | * | |
18 | */ | |
19 | ||
20 | /* | |
21 | * NOTICE | |
22 | * | |
23 | * Acquisition, use, and distribution of this module and related | |
24 | * materials are subject to the restrictions of a license agreement. | |
25 | * Consult the Preface in the User's Manual for the full terms of | |
26 | * this agreement. | |
27 | * | |
28 | */ | |
29 | ||
30 | ||
31 | ||
32 | #include <stdio.h> | |
33 | #include "quipu/photo.h" | |
34 | ||
35 | static int built = 0; | |
36 | char * malloc (); | |
37 | ||
38 | extern node * bl_tree_top; /* pointers to the decode trees */ | |
39 | extern node * wt_tree_top; | |
40 | extern node * two_tree_top; | |
41 | ||
42 | /* ROUTINE: get_node */ | |
43 | /* */ | |
44 | /* SYNOPSIS: creates a new node. */ | |
45 | /* */ | |
46 | /* DESCRIPTION: Uses malloc to allocate sufficient memory for a node to */ | |
47 | /* be stored, and the sets all the pointer fields of the */ | |
48 | /* node to NULL, and initialises the other fields */ | |
49 | ||
50 | static node * | |
51 | get_node () | |
52 | ||
53 | { | |
54 | node * mem; | |
55 | ||
56 | if ((mem = (node *) malloc (sizeof (node))) == NULL) | |
57 | return NULL; | |
58 | mem->n_type = INTERNAL; /* The most common node type */ | |
59 | mem->zero = NULL; | |
60 | mem->one = NULL; | |
61 | ||
62 | return (mem); | |
63 | } | |
64 | ||
65 | ||
66 | static char * file_path (faxdir,file) | |
67 | char *faxdir, *file; | |
68 | { | |
69 | static char buf [200]; | |
70 | char * sprintf (); | |
71 | ||
72 | (void) sprintf (buf,"%s/%s",faxdir,file); | |
73 | return (buf); | |
74 | } | |
75 | ||
76 | /* ROUTINE: build_trees. | |
77 | /* | |
78 | /* SYNOPSIS: build the decode tree. | |
79 | /* | |
80 | /* DESCRIPTION: For each of the three trees, read the data in from a file | |
81 | /* in string form, convert this into an integer, then add this integer to the | |
82 | /* tree. | |
83 | /* Also contained in the node is the value associated with the string read in, | |
84 | /* so we need to count each string as it is read in. | |
85 | */ | |
86 | ||
87 | build_trees (faxdir) | |
88 | char * faxdir; | |
89 | { | |
90 | FILE * fptr; | |
91 | register i; | |
92 | char buffer [15]; | |
93 | char * string = buffer; | |
94 | char * file; | |
95 | ||
96 | if (built) | |
97 | return (0); | |
98 | ||
99 | /* create the tops of the trees */ | |
100 | bl_tree_top = get_node (); | |
101 | wt_tree_top = get_node (); | |
102 | two_tree_top = get_node (); | |
103 | if (bl_tree_top == NULL || wt_tree_top == NULL || two_tree_top == NULL) { | |
104 | if (bl_tree_top) | |
105 | free (bl_tree_top); | |
106 | if (wt_tree_top) | |
107 | free (wt_tree_top); | |
108 | if (two_tree_top) | |
109 | free (two_tree_top); | |
110 | (void) fprintf (stderr, "PHOTO: out of memory"); | |
111 | return (-1); | |
112 | } | |
113 | ||
114 | /* Add the white terminals to the white tree */ | |
115 | file = file_path (faxdir,"wt_term"); | |
116 | if ((fptr = fopen (file,"r")) == NULL) | |
117 | { | |
118 | (void) fprintf(stderr,"Can't open data file %s\n",file); | |
119 | return(-1); | |
120 | } | |
121 | i=0; | |
122 | while ( fscanf(fptr,"%s",string) != EOF ) | |
123 | add_tree (string,i++,WT_TERM,wt_tree_top); | |
124 | (void) fclose (fptr); | |
125 | ||
126 | /* Add the black terminals to the black tree */ | |
127 | file = file_path (faxdir,"bl_term"); | |
128 | if ((fptr = fopen (file,"r")) == NULL) | |
129 | { | |
130 | (void) fprintf(stderr,"Can't open data file %s\n",file); | |
131 | return(-1); | |
132 | } | |
133 | i=0; | |
134 | while ( fscanf(fptr,"%s",string) != EOF ) | |
135 | add_tree (string,i++,BL_TERM,bl_tree_top); | |
136 | (void) fclose (fptr); | |
137 | ||
138 | /* Add the white make codes to the white tree */ | |
139 | file = file_path (faxdir,"wt_make"); | |
140 | if ((fptr = fopen (file,"r")) == NULL) | |
141 | { | |
142 | (void) fprintf(stderr,"Can't open data file %s\n",file); | |
143 | return(-1); | |
144 | } | |
145 | i = 64; | |
146 | while ( fscanf(fptr,"%s",string) != EOF ) { | |
147 | add_tree (string,i,MAKE,wt_tree_top); | |
148 | i += 64; | |
149 | } | |
150 | (void) fclose (fptr); | |
151 | ||
152 | /* Add the black make up codes to the black tree */ | |
153 | file = file_path (faxdir,"bl_make"); | |
154 | if ((fptr = fopen (file,"r")) == NULL) | |
155 | { | |
156 | (void) fprintf(stderr,"Can't open data file %s\n",file); | |
157 | return(-1); | |
158 | } | |
159 | i = 64; | |
160 | while ( fscanf(fptr,"%s",string) != EOF ) { | |
161 | add_tree (string,i,MAKE,bl_tree_top); | |
162 | i += 64; | |
163 | } | |
164 | (void) fclose (fptr); | |
165 | ||
166 | /* make the two dimensional decode tree */ | |
167 | file = file_path (faxdir,"two_dim"); | |
168 | if ((fptr = fopen (file,"r")) == NULL) | |
169 | { | |
170 | (void) fprintf(stderr,"Can't open data file %s\n",file); | |
171 | return(-1); | |
172 | } | |
173 | i = 1; | |
174 | while ( fscanf(fptr,"%s",string) != EOF ) | |
175 | add_tree (string,i++,MAKE,two_tree_top); | |
176 | (void) fclose (fptr); | |
177 | ||
178 | /* put end of line markers on all three trees */ | |
179 | add_tree ("00000000000",EOLN,EOLN,bl_tree_top); | |
180 | add_tree ("00000000000",EOLN,EOLN,wt_tree_top); | |
181 | add_tree ("00000000000",EOLN,EOLN,two_tree_top); | |
182 | ||
183 | built = 1; | |
184 | return(0); | |
185 | } | |
186 | ||
187 | /* ROUTINE: add_tree */ | |
188 | /* */ | |
189 | /* SYNOPSIS: adds a run to the tree */ | |
190 | /* */ | |
191 | ||
192 | static add_tree (string,run,mode,root) | |
193 | ||
194 | char * string; /* string containing the bit sequence */ | |
195 | int run; /* the run length associated with the sequence */ | |
196 | char mode; /* the type of data we are entering */ | |
197 | node * root; /* top of the tree sting should be added to */ | |
198 | { | |
199 | ||
200 | char * ptr; | |
201 | node * treeptr; | |
202 | register i; | |
203 | ||
204 | ptr = string; | |
205 | treeptr = root; | |
206 | ||
207 | for ( i=0; i< strlen(string); i++,ptr++) | |
208 | if (*ptr == '0') { | |
209 | if (treeptr->zero == NULL) | |
210 | treeptr->zero = get_node (); | |
211 | treeptr = treeptr->zero; | |
212 | ||
213 | } else { | |
214 | if (treeptr->one == NULL) | |
215 | treeptr->one = get_node (); | |
216 | treeptr = treeptr->one; | |
217 | } | |
218 | ||
219 | treeptr->n_type = mode; | |
220 | treeptr->value = run; | |
221 | } | |
222 |