xref: /phasta/phSolver/common/phio_posix.cc (revision 3de22adcea938dcbea5f2b4ef2369694350775bd)
1 #include "phIO.h"
2 #include "phio_base.h"
3 #include "phio_posix.h"
4 #include "phComm.h"
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <phastaIO.h>
10 #include <sstream>
11 #include <string>
12 
13 namespace {
14   std::string appendPosix(const char* phrase) {
15     std::stringstream ss;
16     ss << phrase << "?";
17     return ss.str();
18   }
19   std::string appendRank(const char* phrase) {
20     std::stringstream ss;
21     ss << phrase << phcomm_rank()+1;
22     return ss.str();
23   }
24   void close(phio_fp f, const char* mode) {
25     closefile(f->file, mode);
26     free(f->file);
27     free(f);
28   }
29 }
30 
31 void posix_openfile( const char filename[], phio_fp f) {
32   assert(f->mode == 'r' || f->mode == 'w');
33   std::string posixName = appendRank(filename);
34   if(f->mode == 'r')
35     openfile(posixName.c_str(), "read", f->file);
36   else if(f->mode == 'w')
37     openfile(posixName.c_str(), "write", f->file);
38 }
39 
40 void posix_closefile(phio_fp f) {
41   assert(f->mode == 'r' || f->mode == 'w');
42   if(f->mode == 'r')
43     close(f, "read");
44   else if(f->mode == 'w')
45     close(f, "write");
46 }
47 
48 void posix_readheader(
49     int* fileDescriptor,
50     const  char keyphrase[],
51     void* valueArray,
52     int*  nItems,
53     const char  datatype[],
54     const char  iotype[] ) {
55   std::string posixPhrase = appendPosix(keyphrase);
56   readheader(fileDescriptor, posixPhrase.c_str(),
57       valueArray, nItems, datatype, iotype);
58 }
59 
60 void posix_writeheader(
61       const int* fileDescriptor,
62       const char keyphrase[],
63       const void* valueArray,
64       const int* nItems,
65       const int* ndataItems,
66       const char datatype[],
67       const char iotype[] ) {
68   std::string posixPhrase = appendPosix(keyphrase);
69   writeheader(fileDescriptor, posixPhrase.c_str(), valueArray,
70       nItems, ndataItems, datatype, iotype);
71 }
72 
73 void posix_readdatablock(
74     int*  fileDescriptor,
75     const char keyphrase[],
76     void* valueArray,
77     int*  nItems,
78     const char  datatype[],
79     const char  iotype[] ) {
80   std::string posixPhrase = appendPosix(keyphrase);
81   readdatablock(fileDescriptor, posixPhrase.c_str(),
82       valueArray, nItems, datatype, iotype);
83 }
84 
85 void posix_writedatablock(
86     const int* fileDescriptor,
87     const char keyphrase[],
88     const void* valueArray,
89     const int* nItems,
90     const char datatype[],
91     const char iotype[]) {
92   std::string posixPhrase = appendPosix(keyphrase);
93   writedatablock(fileDescriptor, posixPhrase.c_str(), valueArray,
94       nItems, datatype, iotype);
95 }
96 
97 void posix_constructname(
98     const char* in,
99     char* out) {
100   std::string fullname(in);
101   std::string gname("geombc");
102   if( fullname.find(gname) != std::string::npos )
103     fullname.append(".dat");
104   fullname.append(".");
105   sprintf(out, "%s", fullname.c_str());
106 }
107