xref: /phasta/phSolver/common/phio_posix.cc (revision 712d3df0b59ebebaaeaea358162c8d2c043c6e08)
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   assert(f->mode == 'r' || f->mode == 'w');
28   std::string posixName = appendRank(filename);
29   if(f->mode == 'r')
30     openfile(posixName.c_str(), "read", f->file);
31   else if(f->mode == 'w')
32     openfile(posixName.c_str(), "write", f->file);
33 }
34 
35 void posix_closefile(phio_fp f) {
36   assert(f->mode == 'r' || f->mode == 'w');
37   if(f->mode == 'r')
38     close(f, "read");
39   else if(f->mode == 'w')
40     close(f, "write");
41 }
42 
43 void posix_readheader(
44     int* fileDescriptor,
45     const  char keyphrase[],
46     void* valueArray,
47     int*  nItems,
48     const char  datatype[],
49     const char  iotype[] ) {
50   readheader(fileDescriptor, keyphrase,
51       valueArray, nItems, datatype, iotype);
52 }
53 
54 void posix_writeheader(
55       const int* fileDescriptor,
56       const char keyphrase[],
57       const void* valueArray,
58       const int* nItems,
59       const int* ndataItems,
60       const char datatype[],
61       const char iotype[] ) {
62   writeheader(fileDescriptor, keyphrase, valueArray,
63       nItems, ndataItems, datatype, iotype);
64 }
65 
66 void posix_readdatablock(
67     int*  fileDescriptor,
68     const char keyphrase[],
69     void* valueArray,
70     int*  nItems,
71     const char  datatype[],
72     const char  iotype[] ) {
73   readdatablock(fileDescriptor, keyphrase,
74       valueArray, nItems, datatype, iotype);
75 }
76 
77 void posix_writedatablock(
78     const int* fileDescriptor,
79     const char keyphrase[],
80     const void* valueArray,
81     const int* nItems,
82     const char datatype[],
83     const char iotype[]) {
84   writedatablock(fileDescriptor, keyphrase, valueArray,
85       nItems, datatype, iotype);
86 }
87 
88 void posix_constructname(
89     const char* in,
90     char* out) {
91   std::string fullname(in);
92   std::string gname("geombc");
93   if( fullname.find(gname) != std::string::npos )
94     fullname.append(".dat");
95   fullname.append(".");
96   sprintf(out, "%s", fullname.c_str());
97 }
98