xref: /phasta/phSolver/common/phio_stream.cc (revision 98d6580a8ecca5add329d7adadb1985835e1e604)
1 #include <stdlib.h>
2 #include <string>
3 #include <sstream>
4 #include <phastaIO.h>
5 #include "phiotimer.h"  //phastaio_add[Open|Close]Time
6 #include "phio_stream.h"
7 
8 #define PHIO_STREAM_TRACING 0
9 namespace {
appendPosix(const char * phrase)10   std::string appendPosix(const char* phrase) {
11     std::stringstream ss;
12     ss << phrase << "?";
13     return ss.str();
14   }
traceEnter(const char * key,const char * aux="")15   void traceEnter(const char* key, const char* aux="") {
16     if(PHIO_STREAM_TRACING)
17       fprintf(stderr, "CAKE entering %s %s\n", key, aux);
18   }
traceExit(const char * key)19   void traceExit(const char* key) {
20     if(PHIO_STREAM_TRACING)
21       fprintf(stderr, "CAKE exiting %s\n", key);
22   }
23 }
24 
stream_openfile(const char filename[],phio_fp f)25 void stream_openfile(
26     const char filename[],
27     phio_fp f) {
28   traceEnter(__func__, filename);
29   stream_fp sf = (stream_fp) f;
30   phastaioTime t0,t1;
31   phastaio_time(&t0);
32   if(sf->mode == 'w' && sf->rs != NULL)
33     sf->file = (int*) openRStreamWrite(sf->rs);
34   else if(sf->mode == 'r' && sf->rs != NULL)
35     sf->file = (int*) openRStreamRead(sf->rs);
36   else if(sf->mode == 'r' && sf->grs != NULL)
37     sf->file = (int*) openGRStreamRead(sf->grs, filename);
38   else
39     fprintf(stderr,
40         "ERROR %s type of stream %s is unknown... exiting\n",
41         __func__, filename);
42   phastaio_time(&t1);
43   const size_t elapsed = phastaio_time_diff(&t0,&t1);
44   phastaio_addOpenTime(elapsed);
45   traceExit(__func__);
46 }
47 
stream_readheader(int * fileDescriptor,const char keyphrase[],void * valueArray,int * nItems,const char datatype[],const char iotype[])48 void stream_readheader(
49     int* fileDescriptor,
50     const  char keyphrase[],
51     void* valueArray,
52     int*  nItems,
53     const char  datatype[],
54     const char  iotype[] ) {
55   traceEnter(__func__, keyphrase);
56   readHeader((FILE*)fileDescriptor, keyphrase,
57       (int*)valueArray, *nItems, iotype);
58   traceExit(__func__);
59 }
60 
stream_writeheader(const int * fileDescriptor,const char keyphrase[],const void * valueArray,const int * nItems,const int * ndataItems,const char datatype[],const char iotype[])61 void stream_writeheader(
62     const int* fileDescriptor,
63     const char keyphrase[],
64     const void* valueArray,
65     const int* nItems,
66     const int* ndataItems,
67     const char datatype[],
68     const char iotype[] ) {
69   traceEnter(__func__, keyphrase);
70   writeHeader((FILE*)fileDescriptor, keyphrase,
71       (int*)valueArray, *nItems, *ndataItems, datatype, iotype);
72   traceExit(__func__);
73 }
74 
stream_readdatablock(int * fileDescriptor,const char *,void * valueArray,int * nItems,const char datatype[],const char iotype[])75 void stream_readdatablock(
76     int* fileDescriptor,
77     const  char*,
78     void* valueArray,
79     int*  nItems,
80     const char  datatype[],
81     const char  iotype[] ) {
82   traceEnter(__func__);
83   readDataBlock((FILE*)fileDescriptor, valueArray, *nItems,
84       datatype, iotype);
85   traceExit(__func__);
86 }
87 
stream_writedatablock(const int * fileDescriptor,const char *,const void * valueArray,const int * nItems,const char datatype[],const char iotype[])88 void stream_writedatablock(
89     const int* fileDescriptor,
90     const char*,
91     const void* valueArray,
92     const int* nItems,
93     const char datatype[],
94     const char iotype[]) {
95   traceEnter(__func__);
96   writeDataBlock((FILE*)fileDescriptor, valueArray,
97       *nItems, datatype, iotype);
98   traceExit(__func__);
99 }
100 
stream_closefile(phio_fp f)101 void stream_closefile(phio_fp f) {
102   traceEnter(__func__);
103   stream_fp sf = (stream_fp) f;
104   phastaioTime t0,t1;
105   phastaio_time(&t0);
106   fclose((FILE*)sf->file);
107   phastaio_time(&t1);
108   const size_t elapsed = phastaio_time_diff(&t0,&t1);
109   phastaio_addCloseTime(elapsed);
110   free(f);
111   traceExit(__func__);
112 }
113 
stream_constructname(const char * in,char * out)114 void stream_constructname(const char* in, char* out) {
115   traceEnter(__func__);
116   sprintf(out, "%s", in);
117   traceExit(__func__);
118 }
119