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