xref: /phasta/phSolver/common/phIO.cc (revision 9a6935e58d1bec7fbe804a813bfa0382dfd762c1)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <cstring>
4 #include <string>
5 #include <sstream>
6 #include "phIO.h"
7 #include "phComm.h"
8 #include "phio_base.h"
9 #include "phio_sync.h"
10 #include "phio_posix.h"
11 
12 void phio_readheader(
13     phio_fp f,
14     const  char keyphrase[],
15     void* valueArray,
16     int*  nItems,
17     const char  datatype[],
18     const char  iotype[] ) {
19   f->ops->readheader(f->file, keyphrase, valueArray,
20       nItems, datatype, iotype);
21 }
22 void phio_writeheader(
23     phio_fp f,
24     const char keyphrase[],
25     const void* valueArray,
26     const int* nItems,
27     const int* ndataItems,
28     const char datatype[],
29     const char iotype[] ) {
30   f->ops->writeheader(f->file, keyphrase, valueArray,
31       nItems, ndataItems, datatype, iotype);
32 }
33 void phio_readdatablock(
34     phio_fp f,
35     const  char keyphrase[],
36     void* valueArray,
37     int*  nItems,
38     const char  datatype[],
39     const char  iotype[] ) {
40   f->ops->readdatablock(f->file, keyphrase, valueArray,
41       nItems, datatype, iotype);
42 }
43 void phio_writedatablock(
44     phio_fp f,
45     const char keyphrase[],
46     const void* valueArray,
47     const int* nItems,
48     const char datatype[],
49     const char iotype[]) {
50   f->ops->writedatablock(f->file, keyphrase, valueArray,
51       nItems, datatype, iotype);
52 }
53 void phio_openfile_read(
54     const char filename[],
55     int* numFiles,
56     phio_fp* fileDescriptor) {
57   std::string fn(filename);
58   std::string syncSuffix("-dat");
59   std::string posixSuffix(".dat");
60   if( fn.find(syncSuffix) != std::string::npos )
61     sync_openfile_read(fn.c_str(), numFiles, fileDescriptor);
62   else if( fn.find(posixSuffix) != std::string::npos )
63     posix_openfile_read(fn.c_str(), fileDescriptor);
64   else {
65     fprintf(stderr,
66         "type of file %s is unknown... exiting\n", filename);
67     exit(1);
68   }
69 }
70 void phio_openfile_write(
71     const char filename[],
72     int* numFiles,
73     int* numFields,
74     int* numPPF,
75     phio_fp* fileDescriptor) {
76   std::string fn(filename);
77   std::string syncSuffix("-dat");
78   std::string posixSuffix(".dat");
79   if( fn.find(syncSuffix) != std::string::npos )
80     sync_openfile_write(filename, numFiles, numFields, numPPF, fileDescriptor);
81   else if( fn.find(posixSuffix) != std::string::npos )
82     posix_openfile_write(filename, fileDescriptor);
83   else {
84     fprintf(stderr,
85         "type of file %s is unknown... exiting\n", filename);
86     exit(1);
87   }
88 }
89 void phio_closefile_read(phio_fp f) {
90   f->ops->closefile_read(f);
91 }
92 void phio_closefile_write(phio_fp f) {
93   f->ops->closefile_write(f);
94 }
95 void phio_appendStep(char* dest, int v) {
96   std::stringstream ss;
97   ss << dest << v << '.';
98   std::string s = ss.str();
99   strcpy(dest, s.c_str());
100 }
101