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 {
appendRank(const char * phrase)14 std::string appendRank(const char* phrase) {
15 std::stringstream ss;
16 ss << phrase << phcomm_rank()+1;
17 return ss.str();
18 }
close(phio_fp f,const char * mode)19 void close(phio_fp f, const char* mode) {
20 closefile(f->file, mode);
21 free(f->file);
22 free(f);
23 }
24 }
25
posix_openfile(const char filename[],phio_fp f)26 void posix_openfile(const char filename[], phio_fp f) {
27 std::string posixName = appendRank(filename);
28 int err = posix_openfile_single(posixName.c_str(),f);
29 assert(!err);
30 }
31
posix_openfile_single(const char filename[],phio_fp f)32 int posix_openfile_single(const char filename[], phio_fp f) {
33 assert(f->mode == 'r' || f->mode == 'w');
34 std::string posixName(filename);
35 if(f->mode == 'r')
36 openfile(posixName.c_str(), "read", f->file);
37 else if(f->mode == 'w')
38 openfile(posixName.c_str(), "write", f->file);
39 if( ! *(f->file) )
40 return 1;
41 else
42 return 0;
43 }
44
posix_closefile(phio_fp f)45 void posix_closefile(phio_fp f) {
46 assert(f->mode == 'r' || f->mode == 'w');
47 if(f->mode == 'r')
48 close(f, "read");
49 else if(f->mode == 'w')
50 close(f, "write");
51 }
52
posix_readheader(int * fileDescriptor,const char keyphrase[],void * valueArray,int * nItems,const char datatype[],const char iotype[])53 void posix_readheader(
54 int* fileDescriptor,
55 const char keyphrase[],
56 void* valueArray,
57 int* nItems,
58 const char datatype[],
59 const char iotype[] ) {
60 readheader(fileDescriptor, keyphrase,
61 valueArray, nItems, datatype, iotype);
62 }
63
posix_writeheader(const int * fileDescriptor,const char keyphrase[],const void * valueArray,const int * nItems,const int * ndataItems,const char datatype[],const char iotype[])64 void posix_writeheader(
65 const int* fileDescriptor,
66 const char keyphrase[],
67 const void* valueArray,
68 const int* nItems,
69 const int* ndataItems,
70 const char datatype[],
71 const char iotype[] ) {
72 writeheader(fileDescriptor, keyphrase, valueArray,
73 nItems, ndataItems, datatype, iotype);
74 }
75
posix_readdatablock(int * fileDescriptor,const char keyphrase[],void * valueArray,int * nItems,const char datatype[],const char iotype[])76 void posix_readdatablock(
77 int* fileDescriptor,
78 const char keyphrase[],
79 void* valueArray,
80 int* nItems,
81 const char datatype[],
82 const char iotype[] ) {
83 readdatablock(fileDescriptor, keyphrase,
84 valueArray, nItems, datatype, iotype);
85 }
86
posix_writedatablock(const int * fileDescriptor,const char keyphrase[],const void * valueArray,const int * nItems,const char datatype[],const char iotype[])87 void posix_writedatablock(
88 const int* fileDescriptor,
89 const char keyphrase[],
90 const void* valueArray,
91 const int* nItems,
92 const char datatype[],
93 const char iotype[]) {
94 writedatablock(fileDescriptor, keyphrase, valueArray,
95 nItems, datatype, iotype);
96 }
97
posix_constructname(const char * in,char * out)98 void posix_constructname(
99 const char* in,
100 char* out) {
101 std::string fullname(in);
102 std::string gname("geombc");
103 if( fullname.find(gname) != std::string::npos )
104 fullname.append(".dat");
105 fullname.append(".");
106 sprintf(out, "%s", fullname.c_str());
107 }
108