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