xref: /phasta/phSolver/common/phio_posix.cc (revision c74062d9650951aac4d7462bb155f84b1531d149)
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   assert(f->mode == 'r' || f->mode == 'w');
28ab645d52SCameron Smith   std::string posixName = appendRank(filename);
29ab645d52SCameron Smith   if(f->mode == 'r')
30ab645d52SCameron Smith     openfile(posixName.c_str(), "read", f->file);
31ab645d52SCameron Smith   else if(f->mode == 'w')
32ab645d52SCameron Smith     openfile(posixName.c_str(), "write", f->file);
33ab645d52SCameron Smith }
34ab645d52SCameron Smith 
35ab645d52SCameron Smith void posix_closefile(phio_fp f) {
36ab645d52SCameron Smith   assert(f->mode == 'r' || f->mode == 'w');
37ab645d52SCameron Smith   if(f->mode == 'r')
38ab645d52SCameron Smith     close(f, "read");
39ab645d52SCameron Smith   else if(f->mode == 'w')
40ab645d52SCameron Smith     close(f, "write");
41ab645d52SCameron Smith }
42064bab1dSCameron Smith 
43064bab1dSCameron Smith void posix_readheader(
44064bab1dSCameron Smith     int* fileDescriptor,
45064bab1dSCameron Smith     const  char keyphrase[],
46064bab1dSCameron Smith     void* valueArray,
47064bab1dSCameron Smith     int*  nItems,
48064bab1dSCameron Smith     const char  datatype[],
49064bab1dSCameron Smith     const char  iotype[] ) {
50*c74062d9SCameron Smith   readheader(fileDescriptor, keyphrase,
51064bab1dSCameron Smith       valueArray, nItems, datatype, iotype);
52064bab1dSCameron Smith }
53064bab1dSCameron Smith 
54064bab1dSCameron Smith void posix_writeheader(
55064bab1dSCameron Smith       const int* fileDescriptor,
56064bab1dSCameron Smith       const char keyphrase[],
57064bab1dSCameron Smith       const void* valueArray,
58064bab1dSCameron Smith       const int* nItems,
59064bab1dSCameron Smith       const int* ndataItems,
60064bab1dSCameron Smith       const char datatype[],
61064bab1dSCameron Smith       const char iotype[] ) {
62*c74062d9SCameron Smith   writeheader(fileDescriptor, keyphrase, valueArray,
6357517afcSCameron Smith       nItems, ndataItems, datatype, iotype);
64064bab1dSCameron Smith }
65064bab1dSCameron Smith 
66064bab1dSCameron Smith void posix_readdatablock(
67064bab1dSCameron Smith     int*  fileDescriptor,
68064bab1dSCameron Smith     const char keyphrase[],
69064bab1dSCameron Smith     void* valueArray,
70064bab1dSCameron Smith     int*  nItems,
71064bab1dSCameron Smith     const char  datatype[],
72064bab1dSCameron Smith     const char  iotype[] ) {
73*c74062d9SCameron Smith   readdatablock(fileDescriptor, keyphrase,
74064bab1dSCameron Smith       valueArray, nItems, datatype, iotype);
75064bab1dSCameron Smith }
76064bab1dSCameron Smith 
77064bab1dSCameron Smith void posix_writedatablock(
78064bab1dSCameron Smith     const int* fileDescriptor,
79064bab1dSCameron Smith     const char keyphrase[],
80064bab1dSCameron Smith     const void* valueArray,
81064bab1dSCameron Smith     const int* nItems,
82064bab1dSCameron Smith     const char datatype[],
83064bab1dSCameron Smith     const char iotype[]) {
84*c74062d9SCameron Smith   writedatablock(fileDescriptor, keyphrase, valueArray,
8557517afcSCameron Smith       nItems, datatype, iotype);
86064bab1dSCameron Smith }
87a93de25bSCameron Smith 
88a93de25bSCameron Smith void posix_constructname(
89a93de25bSCameron Smith     const char* in,
90a93de25bSCameron Smith     char* out) {
91a93de25bSCameron Smith   std::string fullname(in);
92a93de25bSCameron Smith   std::string gname("geombc");
93a93de25bSCameron Smith   if( fullname.find(gname) != std::string::npos )
94a93de25bSCameron Smith     fullname.append(".dat");
95a93de25bSCameron Smith   fullname.append(".");
96a93de25bSCameron Smith   sprintf(out, "%s", fullname.c_str());
97a93de25bSCameron Smith }
98