xref: /phasta/phSolver/common/syncio.cc (revision a93de25bae149ad19f75861f2d379ed7e8392d86)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "syncio.h"
4 #include "phio_sync.h"
5 #include "phio_base.h"
6 
7 static struct phio_ops sync_ops_write = {
8   sync_openfile_write,
9   sync_closefile,
10   sync_readheader,
11   sync_writeheader,
12   sync_readdatablock,
13   sync_writedatablock,
14   sync_constructname
15 };
16 
17 static struct phio_ops sync_ops_read = {
18   sync_openfile_read,
19   sync_closefile,
20   sync_readheader,
21   sync_writeheader,
22   sync_readdatablock,
23   sync_writedatablock,
24   sync_constructname
25 };
26 
27 void init(sync_fp f, char mode) {
28   if(mode == 'w')
29     f->ops = &sync_ops_write;
30   else if(mode == 'r')
31     f->ops = &sync_ops_read;
32   else {
33     fprintf(stderr, "ERROR unsupported file mode in %s on line %d"
34         "... exiting", __FILE__, __LINE__);
35     exit(EXIT_FAILURE);
36   }
37   f->file = (int*) malloc(sizeof(int*));
38   f->mode = mode;
39 }
40 
41 void syncio_setup_read(int nfiles, phio_fp* f) {
42   *f = (phio_fp) malloc(sizeof(struct syncio_file));
43   sync_fp sf = (sync_fp) *f;
44   init(sf, 'r');
45   sf->nfiles = nfiles;
46   sf->nfields = 0;
47   sf->nppf = 0;
48 }
49 
50 void syncio_setup_write(int nfiles, int nfields, int nppf, phio_fp* f) {
51   *f = (phio_fp) malloc(sizeof(struct syncio_file));
52   sync_fp sf = (sync_fp) *f;
53   init(sf, 'w');
54   sf->nfiles = nfiles;
55   sf->nfields = nfields;
56   sf->nppf = nppf;
57 }
58