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