xref: /phasta/phSolver/common/phio_posix.cc (revision 560e081f137c5df861ce8be72b5bbca3320849a0)
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 appendRank(const char* phrase) {
15064bab1dSCameron Smith     std::stringstream ss;
16064bab1dSCameron Smith     ss << phrase << phcomm_rank()+1;
17064bab1dSCameron Smith     return ss.str();
18064bab1dSCameron Smith   }
1957517afcSCameron Smith   void close(phio_fp f, const char* mode) {
2057517afcSCameron Smith     closefile(f->file, mode);
2157517afcSCameron Smith     free(f->file);
2257517afcSCameron Smith     free(f);
2357517afcSCameron Smith   }
24064bab1dSCameron Smith }
25064bab1dSCameron Smith 
26ab645d52SCameron Smith void posix_openfile(const char filename[], phio_fp f) {
27ab645d52SCameron Smith   std::string posixName = appendRank(filename);
28*560e081fSCameron Smith   posix_openfile_single(posixName.c_str(),f);
29*560e081fSCameron Smith }
30*560e081fSCameron Smith 
31*560e081fSCameron Smith void posix_openfile_single(const char filename[], phio_fp f) {
32*560e081fSCameron Smith   assert(f->mode == 'r' || f->mode == 'w');
33*560e081fSCameron Smith   std::string posixName(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[] ) {
55c74062d9SCameron Smith   readheader(fileDescriptor, keyphrase,
56064bab1dSCameron Smith       valueArray, nItems, datatype, iotype);
57064bab1dSCameron Smith }
58064bab1dSCameron Smith 
59064bab1dSCameron Smith void posix_writeheader(
60064bab1dSCameron Smith       const int* fileDescriptor,
61064bab1dSCameron Smith       const char keyphrase[],
62064bab1dSCameron Smith       const void* valueArray,
63064bab1dSCameron Smith       const int* nItems,
64064bab1dSCameron Smith       const int* ndataItems,
65064bab1dSCameron Smith       const char datatype[],
66064bab1dSCameron Smith       const char iotype[] ) {
67c74062d9SCameron Smith   writeheader(fileDescriptor, keyphrase, valueArray,
6857517afcSCameron Smith       nItems, ndataItems, datatype, iotype);
69064bab1dSCameron Smith }
70064bab1dSCameron Smith 
71064bab1dSCameron Smith void posix_readdatablock(
72064bab1dSCameron Smith     int*  fileDescriptor,
73064bab1dSCameron Smith     const char keyphrase[],
74064bab1dSCameron Smith     void* valueArray,
75064bab1dSCameron Smith     int*  nItems,
76064bab1dSCameron Smith     const char  datatype[],
77064bab1dSCameron Smith     const char  iotype[] ) {
78c74062d9SCameron Smith   readdatablock(fileDescriptor, keyphrase,
79064bab1dSCameron Smith       valueArray, nItems, datatype, iotype);
80064bab1dSCameron Smith }
81064bab1dSCameron Smith 
82064bab1dSCameron Smith void posix_writedatablock(
83064bab1dSCameron Smith     const int* fileDescriptor,
84064bab1dSCameron Smith     const char keyphrase[],
85064bab1dSCameron Smith     const void* valueArray,
86064bab1dSCameron Smith     const int* nItems,
87064bab1dSCameron Smith     const char datatype[],
88064bab1dSCameron Smith     const char iotype[]) {
89c74062d9SCameron Smith   writedatablock(fileDescriptor, keyphrase, valueArray,
9057517afcSCameron Smith       nItems, datatype, iotype);
91064bab1dSCameron Smith }
92a93de25bSCameron Smith 
93a93de25bSCameron Smith void posix_constructname(
94a93de25bSCameron Smith     const char* in,
95a93de25bSCameron Smith     char* out) {
96a93de25bSCameron Smith   std::string fullname(in);
97a93de25bSCameron Smith   std::string gname("geombc");
98a93de25bSCameron Smith   if( fullname.find(gname) != std::string::npos )
99a93de25bSCameron Smith     fullname.append(".dat");
100a93de25bSCameron Smith   fullname.append(".");
101a93de25bSCameron Smith   sprintf(out, "%s", fullname.c_str());
102a93de25bSCameron Smith }
103