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