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 {
appendRank(const char * phrase)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 }
close(phio_fp f,const char * mode)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
posix_openfile(const char filename[],phio_fp f)26ab645d52SCameron Smith void posix_openfile(const char filename[], phio_fp f) {
27ab645d52SCameron Smith std::string posixName = appendRank(filename);
28*d3a305dcSCameron Smith int err = posix_openfile_single(posixName.c_str(),f);
29*d3a305dcSCameron Smith assert(!err);
30560e081fSCameron Smith }
31560e081fSCameron Smith
posix_openfile_single(const char filename[],phio_fp f)32*d3a305dcSCameron Smith int posix_openfile_single(const char filename[], phio_fp f) {
33560e081fSCameron Smith assert(f->mode == 'r' || f->mode == 'w');
34560e081fSCameron Smith std::string posixName(filename);
35ab645d52SCameron Smith if(f->mode == 'r')
36ab645d52SCameron Smith openfile(posixName.c_str(), "read", f->file);
37ab645d52SCameron Smith else if(f->mode == 'w')
38ab645d52SCameron Smith openfile(posixName.c_str(), "write", f->file);
39*d3a305dcSCameron Smith if( ! *(f->file) )
40*d3a305dcSCameron Smith return 1;
41*d3a305dcSCameron Smith else
42*d3a305dcSCameron Smith return 0;
43ab645d52SCameron Smith }
44ab645d52SCameron Smith
posix_closefile(phio_fp f)45ab645d52SCameron Smith void posix_closefile(phio_fp f) {
46ab645d52SCameron Smith assert(f->mode == 'r' || f->mode == 'w');
47ab645d52SCameron Smith if(f->mode == 'r')
48ab645d52SCameron Smith close(f, "read");
49ab645d52SCameron Smith else if(f->mode == 'w')
50ab645d52SCameron Smith close(f, "write");
51ab645d52SCameron Smith }
52064bab1dSCameron Smith
posix_readheader(int * fileDescriptor,const char keyphrase[],void * valueArray,int * nItems,const char datatype[],const char iotype[])53064bab1dSCameron Smith void posix_readheader(
54064bab1dSCameron Smith int* fileDescriptor,
55064bab1dSCameron Smith const char keyphrase[],
56064bab1dSCameron Smith void* valueArray,
57064bab1dSCameron Smith int* nItems,
58064bab1dSCameron Smith const char datatype[],
59064bab1dSCameron Smith const char iotype[] ) {
60c74062d9SCameron Smith readheader(fileDescriptor, keyphrase,
61064bab1dSCameron Smith valueArray, nItems, datatype, iotype);
62064bab1dSCameron Smith }
63064bab1dSCameron Smith
posix_writeheader(const int * fileDescriptor,const char keyphrase[],const void * valueArray,const int * nItems,const int * ndataItems,const char datatype[],const char iotype[])64064bab1dSCameron Smith void posix_writeheader(
65064bab1dSCameron Smith const int* fileDescriptor,
66064bab1dSCameron Smith const char keyphrase[],
67064bab1dSCameron Smith const void* valueArray,
68064bab1dSCameron Smith const int* nItems,
69064bab1dSCameron Smith const int* ndataItems,
70064bab1dSCameron Smith const char datatype[],
71064bab1dSCameron Smith const char iotype[] ) {
72c74062d9SCameron Smith writeheader(fileDescriptor, keyphrase, valueArray,
7357517afcSCameron Smith nItems, ndataItems, datatype, iotype);
74064bab1dSCameron Smith }
75064bab1dSCameron Smith
posix_readdatablock(int * fileDescriptor,const char keyphrase[],void * valueArray,int * nItems,const char datatype[],const char iotype[])76064bab1dSCameron Smith void posix_readdatablock(
77064bab1dSCameron Smith int* fileDescriptor,
78064bab1dSCameron Smith const char keyphrase[],
79064bab1dSCameron Smith void* valueArray,
80064bab1dSCameron Smith int* nItems,
81064bab1dSCameron Smith const char datatype[],
82064bab1dSCameron Smith const char iotype[] ) {
83c74062d9SCameron Smith readdatablock(fileDescriptor, keyphrase,
84064bab1dSCameron Smith valueArray, nItems, datatype, iotype);
85064bab1dSCameron Smith }
86064bab1dSCameron Smith
posix_writedatablock(const int * fileDescriptor,const char keyphrase[],const void * valueArray,const int * nItems,const char datatype[],const char iotype[])87064bab1dSCameron Smith void posix_writedatablock(
88064bab1dSCameron Smith const int* fileDescriptor,
89064bab1dSCameron Smith const char keyphrase[],
90064bab1dSCameron Smith const void* valueArray,
91064bab1dSCameron Smith const int* nItems,
92064bab1dSCameron Smith const char datatype[],
93064bab1dSCameron Smith const char iotype[]) {
94c74062d9SCameron Smith writedatablock(fileDescriptor, keyphrase, valueArray,
9557517afcSCameron Smith nItems, datatype, iotype);
96064bab1dSCameron Smith }
97a93de25bSCameron Smith
posix_constructname(const char * in,char * out)98a93de25bSCameron Smith void posix_constructname(
99a93de25bSCameron Smith const char* in,
100a93de25bSCameron Smith char* out) {
101a93de25bSCameron Smith std::string fullname(in);
102a93de25bSCameron Smith std::string gname("geombc");
103a93de25bSCameron Smith if( fullname.find(gname) != std::string::npos )
104a93de25bSCameron Smith fullname.append(".dat");
105a93de25bSCameron Smith fullname.append(".");
106a93de25bSCameron Smith sprintf(out, "%s", fullname.c_str());
107a93de25bSCameron Smith }
108