xref: /phasta/phSolver/common/phio_posix.cc (revision 4d60bba28c1e1f3ca80b42756ae9dcbcd5c4bc48)
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 appendRank(const char* phrase) {
15     std::stringstream ss;
16     ss << phrase << phcomm_rank()+1;
17     return ss.str();
18   }
19   void close(phio_fp f, const char* mode) {
20     closefile(f->file, mode);
21     free(f->file);
22     free(f);
23   }
24 }
25 
26 void posix_openfile(const char filename[], phio_fp f) {
27   std::string posixName = appendRank(filename);
28   posix_openfile_single(posixName.c_str(),f);
29 }
30 
31 void posix_openfile_single(const char filename[], phio_fp f) {
32   assert(f->mode == 'r' || f->mode == 'w');
33   std::string posixName(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   readheader(fileDescriptor, keyphrase,
56       valueArray, nItems, datatype, iotype);
57 }
58 
59 void posix_writeheader(
60       const int* fileDescriptor,
61       const char keyphrase[],
62       const void* valueArray,
63       const int* nItems,
64       const int* ndataItems,
65       const char datatype[],
66       const char iotype[] ) {
67   writeheader(fileDescriptor, keyphrase, valueArray,
68       nItems, ndataItems, datatype, iotype);
69 }
70 
71 void posix_readdatablock(
72     int*  fileDescriptor,
73     const char keyphrase[],
74     void* valueArray,
75     int*  nItems,
76     const char  datatype[],
77     const char  iotype[] ) {
78   readdatablock(fileDescriptor, keyphrase,
79       valueArray, nItems, datatype, iotype);
80 }
81 
82 void posix_writedatablock(
83     const int* fileDescriptor,
84     const char keyphrase[],
85     const void* valueArray,
86     const int* nItems,
87     const char datatype[],
88     const char iotype[]) {
89   writedatablock(fileDescriptor, keyphrase, valueArray,
90       nItems, datatype, iotype);
91 }
92 
93 void posix_constructname(
94     const char* in,
95     char* out) {
96   std::string fullname(in);
97   std::string gname("geombc");
98   if( fullname.find(gname) != std::string::npos )
99     fullname.append(".dat");
100   fullname.append(".");
101   sprintf(out, "%s", fullname.c_str());
102 }
103