1 /* IO.c */
2
3 #include "../DSTree.h"
4
5 static const char *suffixb = ".dstreeb" ;
6 static const char *suffixf = ".dstreef" ;
7
8 /*--------------------------------------------------------------------*/
9 /*
10 --------------------------------------------------
11 purpose -- to read in an DSTree object from a file
12
13 input --
14
15 fn -- filename, must be *.dstreeb or *.dstreef
16
17 return value -- 1 if success, 0 if failure
18
19 created -- 96mar10, cca
20 --------------------------------------------------
21 */
22 int
DSTree_readFromFile(DSTree * dstree,char * fn)23 DSTree_readFromFile (
24 DSTree *dstree,
25 char *fn
26 ) {
27 FILE *fp ;
28 int fnlength, rc, sulength ;
29 /*
30 ---------------
31 check the input
32 ---------------
33 */
34 if ( dstree == NULL || fn == NULL ) {
35 fprintf(stderr, "\n error in DSTree_readFromFile(%p,%s)"
36 "\n bad input\n", dstree, fn) ;
37 return(0) ;
38 }
39 /*
40 -------------
41 read the file
42 -------------
43 */
44 fnlength = strlen(fn) ;
45 sulength = strlen(suffixb) ;
46 if ( fnlength > sulength ) {
47 if ( strcmp(&fn[fnlength-sulength], suffixb) == 0 ) {
48 if ( (fp = fopen(fn, "rb")) == NULL ) {
49 fprintf(stderr, "\n error in DSTree_readFromFile(%p,%s)"
50 "\n unable to open file %s", dstree, fn, fn) ;
51 rc = 0 ;
52 } else {
53 rc = DSTree_readFromBinaryFile(dstree, fp) ;
54 fclose(fp) ;
55 }
56 } else if ( strcmp(&fn[fnlength-sulength], suffixf) == 0 ) {
57 if ( (fp = fopen(fn, "r")) == NULL ) {
58 fprintf(stderr, "\n error in DSTree_readFromFile(%p,%s)"
59 "\n unable to open file %s", dstree, fn, fn) ;
60 rc = 0 ;
61 } else {
62 rc = DSTree_readFromFormattedFile(dstree, fp) ;
63 fclose(fp) ;
64 }
65 } else {
66 fprintf(stderr, "\n error in DSTree_readFromFile(%p,%s)"
67 "\n bad DSTree file name %s,"
68 "\n must end in %s (binary) or %s (formatted)\n",
69 dstree, fn, fn, suffixb, suffixf) ;
70 rc = 0 ;
71 }
72 } else {
73 fprintf(stderr, "\n error in DSTree_readFromFile(%p,%s)"
74 "\n bad DSTree file name %s,"
75 "\n must end in %s (binary) or %s (formatted)\n",
76 dstree, fn, fn, suffixb, suffixf) ;
77 rc = 0 ;
78 }
79 return(rc) ; }
80
81 /*--------------------------------------------------------------------*/
82 /*
83 ---------------------------------------------------------
84 purpose -- to read an DSTree object from a formatted file
85
86 return value -- 1 if success, 0 if failure
87
88 created -- 96mar10, cca
89 ---------------------------------------------------------
90 */
91 int
DSTree_readFromFormattedFile(DSTree * dstree,FILE * fp)92 DSTree_readFromFormattedFile (
93 DSTree *dstree,
94 FILE *fp
95 ) {
96 IV *mapIV ;
97 Tree *tree ;
98 /*
99 ---------------
100 check the input
101 ---------------
102 */
103 if ( dstree == NULL || fp == NULL ) {
104 fprintf(stderr, "\n error in DSTree_readFromFormattedFile(%p,%p)"
105 "\n bad input\n", dstree, fp) ;
106 return(0) ;
107 }
108 /*
109 ---------------------
110 clear the data fields
111 ---------------------
112 */
113 DSTree_clearData(dstree) ;
114 /*
115 -----------------------
116 read in the Tree object
117 -----------------------
118 */
119 tree = Tree_new() ;
120 Tree_readFromFormattedFile(tree, fp) ;
121 /*
122 ---------------------
123 read in the IV object
124 ---------------------
125 */
126 mapIV = IV_new() ;
127 IV_readFromFormattedFile(mapIV, fp) ;
128 /*
129 ----------------------------
130 initialize the DSTree object
131 ----------------------------
132 */
133 DSTree_init2(dstree, tree, mapIV) ;
134
135 return(1) ; }
136
137 /*--------------------------------------------------------------------*/
138 /*
139 ------------------------------------------------------
140 purpose -- to read an DSTree object from a binary file
141
142 return value -- 1 if success, 0 if failure
143
144 created -- 96mar10, cca
145 ------------------------------------------------------
146 */
147 int
DSTree_readFromBinaryFile(DSTree * dstree,FILE * fp)148 DSTree_readFromBinaryFile (
149 DSTree *dstree,
150 FILE *fp
151 ) {
152 IV *mapIV ;
153 Tree *tree ;
154 /*
155 ---------------
156 check the input
157 ---------------
158 */
159 if ( dstree == NULL || fp == NULL ) {
160 fprintf(stderr, "\n fatal error in DSTree_readFromBinaryFile(%p,%p)"
161 "\n bad input\n", dstree, fp) ;
162 return(0) ;
163 }
164 /*
165 ---------------------
166 clear the data fields
167 ---------------------
168 */
169 DSTree_clearData(dstree) ;
170 /*
171 -----------------------
172 read in the Tree object
173 -----------------------
174 */
175 tree = Tree_new() ;
176 Tree_readFromBinaryFile(tree, fp) ;
177 /*
178 ---------------------
179 read in the IV object
180 ---------------------
181 */
182 mapIV = IV_new() ;
183 IV_readFromBinaryFile(mapIV, fp) ;
184 /*
185 ----------------------------
186 initialize the DSTree object
187 ----------------------------
188 */
189 DSTree_init2(dstree, tree, mapIV) ;
190
191 return(1) ; }
192
193 /*--------------------------------------------------------------------*/
194 /*
195 ----------------------------------------------
196 purpose -- to write an DSTree object to a file
197
198 input --
199
200 fn -- filename
201 *.dstreeb -- binary
202 *.dstreef -- formatted
203 anything else -- for human eye
204
205 return value -- 1 if success, 0 otherwise
206
207 created -- 96mar10, cca
208 ----------------------------------------------
209 */
210 int
DSTree_writeToFile(DSTree * dstree,char * fn)211 DSTree_writeToFile (
212 DSTree *dstree,
213 char *fn
214 ) {
215 FILE *fp ;
216 int fnlength, rc, sulength ;
217 /*
218 ---------------
219 check the input
220 ---------------
221 */
222 if ( dstree == NULL || fn == NULL ) {
223 fprintf(stderr, "\n fatal error in DSTree_writeToFile(%p,%s)"
224 "\n bad input\n", dstree, fn) ;
225 }
226 /*
227 ------------------
228 write out the file
229 ------------------
230 */
231 fnlength = strlen(fn) ;
232 sulength = strlen(suffixb) ;
233 if ( fnlength > sulength ) {
234 if ( strcmp(&fn[fnlength-sulength], suffixb) == 0 ) {
235 if ( (fp = fopen(fn, "wb")) == NULL ) {
236 fprintf(stderr, "\n error in DSTree_writeToFile(%p,%s)"
237 "\n unable to open file %s", dstree, fn, fn) ;
238 rc = 0 ;
239 } else {
240 rc = DSTree_writeToBinaryFile(dstree, fp) ;
241 fclose(fp) ;
242 }
243 } else if ( strcmp(&fn[fnlength-sulength], suffixf) == 0 ) {
244 if ( (fp = fopen(fn, "w")) == NULL ) {
245 fprintf(stderr, "\n error in DSTree_writeToFile(%p,%s)"
246 "\n unable to open file %s", dstree, fn, fn) ;
247 rc = 0 ;
248 } else {
249 rc = DSTree_writeToFormattedFile(dstree, fp) ;
250 fclose(fp) ;
251 }
252 } else {
253 if ( (fp = fopen(fn, "a")) == NULL ) {
254 fprintf(stderr, "\n error in DSTree_writeToFile(%p,%s)"
255 "\n unable to open file %s", dstree, fn, fn) ;
256 rc = 0 ;
257 } else {
258 rc = DSTree_writeForHumanEye(dstree, fp) ;
259 fclose(fp) ;
260 }
261 }
262 } else {
263 if ( (fp = fopen(fn, "a")) == NULL ) {
264 fprintf(stderr, "\n error in DSTree_writeToFile(%p,%s)"
265 "\n unable to open file %s", dstree, fn, fn) ;
266 rc = 0 ;
267 } else {
268 rc = DSTree_writeForHumanEye(dstree, fp) ;
269 fclose(fp) ;
270 }
271 }
272 return(rc) ; }
273
274 /*--------------------------------------------------------------------*/
275 /*
276 --------------------------------------------------------
277 purpose -- to write an DSTree object to a formatted file
278
279 return value -- 1 if success, 0 otherwise
280
281 created -- 96mar10, cca
282 --------------------------------------------------------
283 */
284 int
DSTree_writeToFormattedFile(DSTree * dstree,FILE * fp)285 DSTree_writeToFormattedFile (
286 DSTree *dstree,
287 FILE *fp
288 ) {
289 int rc ;
290 /*
291 ---------------
292 check the input
293 ---------------
294 */
295 if ( dstree == NULL || fp == NULL || dstree->tree == NULL ) {
296 fprintf(stderr,
297 "\n fatal error in DSTree_writeToFormattedFile(%p,%p)"
298 "\n bad input\n", dstree, fp) ;
299 exit(-1) ;
300 }
301 /*
302 ---------------------------------
303 write the Tree object to the file
304 ---------------------------------
305 */
306 rc = Tree_writeToFormattedFile(dstree->tree, fp) ;
307 if ( rc < 0 ) {
308 fprintf(stderr,
309 "\n fatal error in DSTree_writeToFormattedFile(%p,%p)"
310 "\n rc = %d, return from writing Tree to file\n",
311 dstree, fp, rc) ;
312 return(0) ;
313 }
314 /*
315 -------------------------------
316 write the IV object to the file
317 -------------------------------
318 */
319 rc = IV_writeToFormattedFile(dstree->mapIV, fp) ;
320 if ( rc < 0 ) {
321 fprintf(stderr,
322 "\n fatal error in DSTree_writeToFormattedFile(%p,%p)"
323 "\n rc = %d, return from writing IV to file\n",
324 dstree, fp, rc) ;
325 return(0) ;
326 }
327
328 return(1) ; }
329
330 /*--------------------------------------------------------------------*/
331 /*
332 -----------------------------------------------------
333 purpose -- to write an DSTree object to a binary file
334
335 return value -- 1 if success, 0 otherwise
336
337 created -- 96mar10, cca
338 -----------------------------------------------------
339 */
340 int
DSTree_writeToBinaryFile(DSTree * dstree,FILE * fp)341 DSTree_writeToBinaryFile (
342 DSTree *dstree,
343 FILE *fp
344 ) {
345 int rc ;
346 /*
347 ---------------
348 check the input
349 ---------------
350 */
351 if ( dstree == NULL || fp == NULL || dstree->tree == NULL ) {
352 fprintf(stderr, "\n fatal error in DSTree_writeToBinaryFile(%p,%p)"
353 "\n bad input\n", dstree, fp) ;
354 exit(-1) ;
355 }
356 /*
357 ---------------------------------
358 write the Tree object to the file
359 ---------------------------------
360 */
361 rc = Tree_writeToBinaryFile(dstree->tree, fp) ;
362 if ( rc < 0 ) {
363 fprintf(stderr, "\n fatal error in DSTree_writeToBinaryFile(%p,%p)"
364 "\n rc = %d, return from writing Tree to file\n",
365 dstree, fp, rc) ;
366 return(0) ;
367 }
368 /*
369 -------------------------------
370 write the IV object to the file
371 -------------------------------
372 */
373 rc = IV_writeToBinaryFile(dstree->mapIV, fp) ;
374 if ( rc < 0 ) {
375 fprintf(stderr, "\n fatal error in DSTree_writeToBinaryFile(%p,%p)"
376 "\n rc = %d, return from writing IV to file\n",
377 dstree, fp, rc) ;
378 return(0) ;
379 }
380
381 return(1) ; }
382
383 /*--------------------------------------------------------------------*/
384 /*
385 ----------------------------------------------------
386 purpose -- to write an DSTree object for a human eye
387
388 return value -- 1 if success, 0 otherwise
389
390 created -- 96mar10, cca
391 ----------------------------------------------------
392 */
393 int
DSTree_writeForHumanEye(DSTree * dstree,FILE * fp)394 DSTree_writeForHumanEye (
395 DSTree *dstree,
396 FILE *fp
397 ) {
398 int rc ;
399
400 if ( dstree == NULL || fp == NULL ) {
401 fprintf(stderr, "\n fatal error in DSTree_writeForHumanEye(%p,%p)"
402 "\n bad input\n", dstree, fp) ;
403 exit(-1) ;
404 }
405 if ( (rc = DSTree_writeStats(dstree, fp)) == 0 ) {
406 fprintf(stderr, "\n fatal error in DSTree_writeForHumanEye(%p,%p)"
407 "\n rc = %d, return from DSTree_writeStats(%p,%p)\n",
408 dstree, fp, rc, dstree, fp) ;
409 return(0) ;
410 }
411 fprintf(fp, "\n\n domain/separator tree") ;
412 Tree_writeForHumanEye(dstree->tree, fp) ;
413 fprintf(fp, "\n\n map from vertices to domains and separators") ;
414 IV_writeForHumanEye(dstree->mapIV, fp) ;
415
416 return(1) ; }
417
418 /*--------------------------------------------------------------------*/
419 /*
420 ------------------------------------------------------------
421 purpose -- to write out the statistics for the DSTree object
422
423 return value -- 1 if success, 0 otherwise
424
425 created -- 96mar10, cca
426 ------------------------------------------------------------
427 */
428 int
DSTree_writeStats(DSTree * dstree,FILE * fp)429 DSTree_writeStats (
430 DSTree *dstree,
431 FILE *fp
432 ) {
433 int rc ;
434 /*
435 ---------------
436 check the input
437 ---------------
438 */
439 if ( dstree == NULL || fp == NULL ) {
440 fprintf(stderr, "\n error in DSTree_writeStats(%p,%p)"
441 "\n bad input\n", dstree, fp) ;
442 exit(-1) ;
443 }
444 rc = fprintf(fp, "\n DSTree : dstree object") ;
445 if ( rc < 0 ) {
446 fprintf(stderr, "\n fatal error in DSTree_writeStats(%p,%p)"
447 "\n rc = %d, return from fprintf\n", dstree, fp, rc) ;
448 return(0) ;
449 }
450 if ( dstree->tree != NULL && dstree->mapIV != NULL ) {
451 rc = fprintf(fp,
452 "\n %d domains and separators, %d vertices, occupies %d bytes",
453 dstree->tree->n, IV_size(dstree->mapIV), DSTree_sizeOf(dstree)) ;
454 if ( rc < 0 ) {
455 fprintf(stderr, "\n fatal error in DSTree_writeStats(%p,%p)"
456 "\n rc = %d, return from fprintf\n", dstree, fp, rc) ;
457 return(0) ;
458 }
459 }
460 return(1) ; }
461
462 /*--------------------------------------------------------------------*/
463