xref: /phasta/phSolver/common/phio_posix.cc (revision a93de25bae149ad19f75861f2d379ed7e8392d86)
1064bab1dSCameron Smith #include "phIO.h"
2064bab1dSCameron Smith #include "phio_base.h"
3064bab1dSCameron Smith #include "phio_posix.h"
4064bab1dSCameron Smith #include "phComm.h"
5064bab1dSCameron Smith #include <stdio.h>
6064bab1dSCameron Smith #include <stdlib.h>
7064bab1dSCameron Smith #include <string.h>
8064bab1dSCameron Smith #include <assert.h>
9064bab1dSCameron Smith #include <phastaIO.h>
10064bab1dSCameron Smith #include <sstream>
11064bab1dSCameron Smith #include <string>
12064bab1dSCameron Smith 
13064bab1dSCameron Smith namespace {
14064bab1dSCameron Smith   std::string appendPosix(const char* phrase) {
15064bab1dSCameron Smith     std::stringstream ss;
16064bab1dSCameron Smith     ss << phrase << "?";
17064bab1dSCameron Smith     return ss.str();
18064bab1dSCameron Smith   }
19064bab1dSCameron Smith   std::string appendRank(const char* phrase) {
20064bab1dSCameron Smith     std::stringstream ss;
21064bab1dSCameron Smith     ss << phrase << phcomm_rank()+1;
22064bab1dSCameron Smith     return ss.str();
23064bab1dSCameron Smith   }
2457517afcSCameron Smith   void close(phio_fp f, const char* mode) {
2557517afcSCameron Smith     closefile(f->file, mode);
2657517afcSCameron Smith     free(f->file);
2757517afcSCameron Smith     free(f);
2857517afcSCameron Smith   }
29064bab1dSCameron Smith }
30064bab1dSCameron Smith 
31ab645d52SCameron Smith void posix_openfile( const char filename[], phio_fp f) {
32ab645d52SCameron Smith   assert(f->mode == 'r' || f->mode == 'w');
33ab645d52SCameron Smith   std::string posixName = appendRank(filename);
34ab645d52SCameron Smith   if(f->mode == 'r')
35ab645d52SCameron Smith     openfile(posixName.c_str(), "read", f->file);
36ab645d52SCameron Smith   else if(f->mode == 'w')
37ab645d52SCameron Smith     openfile(posixName.c_str(), "write", f->file);
38ab645d52SCameron Smith }
39ab645d52SCameron Smith 
40ab645d52SCameron Smith void posix_closefile(phio_fp f) {
41ab645d52SCameron Smith   assert(f->mode == 'r' || f->mode == 'w');
42ab645d52SCameron Smith   if(f->mode == 'r')
43ab645d52SCameron Smith     close(f, "read");
44ab645d52SCameron Smith   else if(f->mode == 'w')
45ab645d52SCameron Smith     close(f, "write");
46ab645d52SCameron Smith }
47064bab1dSCameron Smith 
48064bab1dSCameron Smith void posix_readheader(
49064bab1dSCameron Smith     int* fileDescriptor,
50064bab1dSCameron Smith     const  char keyphrase[],
51064bab1dSCameron Smith     void* valueArray,
52064bab1dSCameron Smith     int*  nItems,
53064bab1dSCameron Smith     const char  datatype[],
54064bab1dSCameron Smith     const char  iotype[] ) {
55064bab1dSCameron Smith   std::string posixPhrase = appendPosix(keyphrase);
56064bab1dSCameron Smith   readheader(fileDescriptor, posixPhrase.c_str(),
57064bab1dSCameron Smith       valueArray, nItems, datatype, iotype);
58064bab1dSCameron Smith }
59064bab1dSCameron Smith 
60064bab1dSCameron Smith void posix_writeheader(
61064bab1dSCameron Smith       const int* fileDescriptor,
62064bab1dSCameron Smith       const char keyphrase[],
63064bab1dSCameron Smith       const void* valueArray,
64064bab1dSCameron Smith       const int* nItems,
65064bab1dSCameron Smith       const int* ndataItems,
66064bab1dSCameron Smith       const char datatype[],
67064bab1dSCameron Smith       const char iotype[] ) {
683005da6cSCameron Smith   std::string posixPhrase = appendPosix(keyphrase);
693005da6cSCameron Smith   writeheader(fileDescriptor, posixPhrase.c_str(), valueArray,
7057517afcSCameron Smith       nItems, ndataItems, datatype, iotype);
71064bab1dSCameron Smith }
72064bab1dSCameron Smith 
73064bab1dSCameron Smith void posix_readdatablock(
74064bab1dSCameron Smith     int*  fileDescriptor,
75064bab1dSCameron Smith     const char keyphrase[],
76064bab1dSCameron Smith     void* valueArray,
77064bab1dSCameron Smith     int*  nItems,
78064bab1dSCameron Smith     const char  datatype[],
79064bab1dSCameron Smith     const char  iotype[] ) {
80064bab1dSCameron Smith   std::string posixPhrase = appendPosix(keyphrase);
81064bab1dSCameron Smith   readdatablock(fileDescriptor, posixPhrase.c_str(),
82064bab1dSCameron Smith       valueArray, nItems, datatype, iotype);
83064bab1dSCameron Smith }
84064bab1dSCameron Smith 
85064bab1dSCameron Smith void posix_writedatablock(
86064bab1dSCameron Smith     const int* fileDescriptor,
87064bab1dSCameron Smith     const char keyphrase[],
88064bab1dSCameron Smith     const void* valueArray,
89064bab1dSCameron Smith     const int* nItems,
90064bab1dSCameron Smith     const char datatype[],
91064bab1dSCameron Smith     const char iotype[]) {
923005da6cSCameron Smith   std::string posixPhrase = appendPosix(keyphrase);
933005da6cSCameron Smith   writedatablock(fileDescriptor, posixPhrase.c_str(), valueArray,
9457517afcSCameron Smith       nItems, datatype, iotype);
95064bab1dSCameron Smith }
96*a93de25bSCameron Smith 
97*a93de25bSCameron Smith void posix_constructname(
98*a93de25bSCameron Smith     const char* in,
99*a93de25bSCameron Smith     char* out) {
100*a93de25bSCameron Smith   std::string fullname(in);
101*a93de25bSCameron Smith   std::string gname("geombc");
102*a93de25bSCameron Smith   if( fullname.find(gname) != std::string::npos )
103*a93de25bSCameron Smith     fullname.append(".dat");
104*a93de25bSCameron Smith   fullname.append(".");
105*a93de25bSCameron Smith   sprintf(out, "%s", fullname.c_str());
106*a93de25bSCameron Smith }
107