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