1 |
< |
/* |
1 |
> |
/* |
2 |
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
* |
4 |
|
* The University of Notre Dame grants you ("Licensee") a |
60 |
|
|
61 |
|
namespace oopse { |
62 |
|
|
63 |
< |
/** |
64 |
< |
* @class basic_ifstrstream basic_ifstrstream.hpp "io/basic_ifstrstream.hpp" |
65 |
< |
* @brief basic_ifstrstream class provides a stream interface to read data from files. |
66 |
< |
* <p>In single mode, it falls back to ifstream. Don't need to read the whole file into memory. |
67 |
< |
* In parallel mode, the master node will read the whole file and brocast it to other slave nodes. |
68 |
< |
* After brocasting, every node will fall back to stringstream.</p> |
69 |
< |
* |
70 |
< |
* @code |
71 |
< |
* const int MAXLEN = 1024; |
72 |
< |
* char buffer[MAXLEN]; |
73 |
< |
* ifstrstream in; |
74 |
< |
* in.open("Shapes.frc"); |
75 |
< |
* if (in.is_open()) { |
76 |
< |
* in.getline(buffer, MAXLEN); |
77 |
< |
* } |
78 |
< |
* in.close(); |
79 |
< |
* @endcode |
80 |
< |
*/ |
81 |
< |
template <class _CharT, class _Traits, class _Alloc> |
82 |
< |
class basic_ifstrstream : public std::basic_istream<_CharT, _Traits> { |
83 |
< |
public: |
84 |
< |
//traits |
85 |
< |
typedef _CharT char_type; |
86 |
< |
typedef typename _Traits::int_type int_type; |
87 |
< |
typedef typename _Traits::pos_type pos_type; |
88 |
< |
typedef typename _Traits::off_type off_type; |
89 |
< |
typedef _Traits traits_type; |
63 |
> |
/** |
64 |
> |
* @class basic_ifstrstream basic_ifstrstream.hpp "io/basic_ifstrstream.hpp" |
65 |
> |
* @brief basic_ifstrstream class provides a stream interface to read data from files. |
66 |
> |
* <p>In single mode, it falls back to ifstream. Don't need to read the whole file into memory. |
67 |
> |
* In parallel mode, the master node will read the whole file and brocast it to other slave nodes. |
68 |
> |
* After brocasting, every node will fall back to stringstream.</p> |
69 |
> |
* |
70 |
> |
* @code |
71 |
> |
* const int MAXLEN = 1024; |
72 |
> |
* char buffer[MAXLEN]; |
73 |
> |
* ifstrstream in; |
74 |
> |
* in.open("Shapes.frc"); |
75 |
> |
* if (in.is_open()) { |
76 |
> |
* in.getline(buffer, MAXLEN); |
77 |
> |
* } |
78 |
> |
* in.close(); |
79 |
> |
* @endcode |
80 |
> |
*/ |
81 |
> |
template <class _CharT, class _Traits, class _Alloc> |
82 |
> |
class basic_ifstrstream : public std::basic_istream<_CharT, _Traits> { |
83 |
> |
public: |
84 |
> |
//traits |
85 |
> |
typedef _CharT char_type; |
86 |
> |
typedef typename _Traits::int_type int_type; |
87 |
> |
typedef typename _Traits::pos_type pos_type; |
88 |
> |
typedef typename _Traits::off_type off_type; |
89 |
> |
typedef _Traits traits_type; |
90 |
|
|
91 |
< |
typedef std::basic_ios<_CharT, _Traits> _Basic_ios; |
92 |
< |
typedef std::basic_istream<_CharT, _Traits> _Base; |
91 |
> |
typedef std::basic_ios<_CharT, _Traits> _Basic_ios; |
92 |
> |
typedef std::basic_istream<_CharT, _Traits> _Base; |
93 |
|
|
94 |
|
#ifdef IS_MPI |
95 |
< |
typedef std::basic_stringbuf<_CharT, _Traits, _Alloc> _Buf; |
95 |
> |
typedef std::basic_stringbuf<_CharT, _Traits, _Alloc> _Buf; |
96 |
|
#else |
97 |
< |
typedef std::basic_filebuf<_CharT, _Traits> _Buf; |
97 |
> |
typedef std::basic_filebuf<_CharT, _Traits> _Buf; |
98 |
|
#endif |
99 |
|
|
100 |
< |
static const int FileNotExists = -1; |
101 |
< |
static const int FileIOError = -2; |
100 |
> |
static const int FileNotExists = -1; |
101 |
> |
static const int FileIOError = -2; |
102 |
|
|
103 |
< |
public: |
103 |
> |
public: |
104 |
|
|
105 |
< |
/** Constructs an object of class ifstream. */ |
106 |
< |
basic_ifstrstream() |
107 |
< |
: std::basic_istream<_CharT, _Traits>(0), |
108 |
< |
internalBuf_(), isRead(false) { |
105 |
> |
/** Constructs an object of class ifstream. */ |
106 |
> |
basic_ifstrstream() |
107 |
> |
: std::basic_istream<_CharT, _Traits>(0), |
108 |
> |
internalBuf_(), isRead(false) { |
109 |
|
|
110 |
< |
this->init(&internalBuf_); |
111 |
< |
} |
110 |
> |
this->init(&internalBuf_); |
111 |
> |
} |
112 |
|
|
113 |
< |
/** |
114 |
< |
* Explicit constructor |
115 |
< |
* @filename String containing the name of the file to be opened |
116 |
< |
* @mode Flags describing the requested i/o mode for the file, default value is ios_base::in |
117 |
< |
* @checkFilename Flags indicating checking the file name in parallel |
118 |
< |
*/ |
119 |
< |
explicit basic_ifstrstream(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false) |
120 |
< |
: std::basic_istream<_CharT, _Traits>(0), |
121 |
< |
internalBuf_(), isRead(false) { |
113 |
> |
/** |
114 |
> |
* Explicit constructor |
115 |
> |
* @filename String containing the name of the file to be opened |
116 |
> |
* @mode Flags describing the requested i/o mode for the file, default value is ios_base::in |
117 |
> |
* @checkFilename Flags indicating checking the file name in parallel |
118 |
> |
*/ |
119 |
> |
explicit basic_ifstrstream(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false) |
120 |
> |
: std::basic_istream<_CharT, _Traits>(0), |
121 |
> |
internalBuf_(), isRead(false) { |
122 |
|
|
123 |
< |
this->init(&internalBuf_); |
124 |
< |
isRead = internalOpen(filename, mode | std::ios_base::in, checkFilename); |
125 |
< |
} |
123 |
> |
this->init(&internalBuf_); |
124 |
> |
isRead = internalOpen(filename, mode | std::ios_base::in, checkFilename); |
125 |
> |
} |
126 |
|
|
127 |
< |
/** |
128 |
< |
* virtual destructor will close the file(in single mode) and clear the stream buffer |
129 |
< |
*/ |
130 |
< |
~basic_ifstrstream(){ |
131 |
< |
close(); |
132 |
< |
} |
127 |
> |
/** |
128 |
> |
* virtual destructor will close the file(in single mode) and clear the stream buffer |
129 |
> |
*/ |
130 |
> |
~basic_ifstrstream(){ |
131 |
> |
close(); |
132 |
> |
} |
133 |
|
|
134 |
< |
/** |
135 |
< |
* Opens a file and associats a buffer with the specified file to perform the i/o operations |
136 |
< |
* (single mode). Master reads a file and brocasts its content to the other slave nodes. After |
137 |
< |
* brocasting, every nodes fall back to stringstream (parallel mode). |
138 |
< |
* @filename String containing the name of the file to be opened |
139 |
< |
* @mode Flags describing the requested i/o mode for the file |
140 |
< |
* @checkFilename Flags indicating checking the file name in parallel |
141 |
< |
*/ |
142 |
< |
void open(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false){ |
134 |
> |
/** |
135 |
> |
* Opens a file and associats a buffer with the specified file to perform the i/o operations |
136 |
> |
* (single mode). Master reads a file and brocasts its content to the other slave nodes. After |
137 |
> |
* brocasting, every nodes fall back to stringstream (parallel mode). |
138 |
> |
* @filename String containing the name of the file to be opened |
139 |
> |
* @mode Flags describing the requested i/o mode for the file |
140 |
> |
* @checkFilename Flags indicating checking the file name in parallel |
141 |
> |
*/ |
142 |
> |
void open(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false){ |
143 |
|
|
144 |
< |
if (!isRead ) { |
145 |
< |
isRead = internalOpen(filename, mode, checkFilename); |
146 |
< |
} |
147 |
< |
} |
144 |
> |
if (!isRead ) { |
145 |
> |
isRead = internalOpen(filename, mode, checkFilename); |
146 |
> |
} |
147 |
> |
} |
148 |
|
|
149 |
< |
/** |
150 |
< |
* Tests if the stream is currently associated with a valid buffer. |
151 |
< |
* @return true if a file has successfully been opened (single mode) or the whole file is read |
152 |
< |
* and spreaded among all of the processors (parallel mode), otherwise false is returned |
153 |
< |
*/ |
154 |
< |
bool is_open ( ) { |
149 |
> |
/** |
150 |
> |
* Tests if the stream is currently associated with a valid buffer. |
151 |
> |
* @return true if a file has successfully been opened (single mode) or the whole file is read |
152 |
> |
* and spreaded among all of the processors (parallel mode), otherwise false is returned |
153 |
> |
*/ |
154 |
> |
bool is_open ( ) { |
155 |
|
#ifdef IS_MPI |
156 |
< |
return isRead; |
156 |
> |
return isRead; |
157 |
|
#else |
158 |
< |
//single version fall back to ifstream |
159 |
< |
return internalBuf_.is_open(); |
158 |
> |
//single version fall back to ifstream |
159 |
> |
return internalBuf_.is_open(); |
160 |
|
#endif |
161 |
< |
} |
161 |
> |
} |
162 |
|
|
163 |
< |
/** |
164 |
< |
* In single mode, closes a file. The stream's file buffer is released from its association with |
165 |
< |
* the currently open file. In parallel mode, clean the |
166 |
< |
*/ |
167 |
< |
void close() { |
163 |
> |
/** |
164 |
> |
* In single mode, closes a file. The stream's file buffer is released from its association with |
165 |
> |
* the currently open file. In parallel mode, clean the |
166 |
> |
*/ |
167 |
> |
void close() { |
168 |
|
#ifndef IS_MPI |
169 |
< |
//single version fall back to ifstream |
170 |
< |
if (!internalBuf_.close()) |
171 |
< |
this->setstate(std::ios_base::failbit); |
169 |
> |
//single version fall back to ifstream |
170 |
> |
if (!internalBuf_.close()) |
171 |
> |
this->setstate(std::ios_base::failbit); |
172 |
|
#endif |
173 |
|
|
174 |
< |
isRead = false; |
175 |
< |
} |
174 |
> |
isRead = false; |
175 |
> |
} |
176 |
|
|
177 |
< |
/** |
178 |
< |
* Gets the stream buffer object associated with the stream |
179 |
< |
* @return A pointer to the stream buffe object(filebuf in single mode, stringbuf in |
180 |
< |
* parallel mode) associated with the stream. |
181 |
< |
*/ |
182 |
< |
_Buf* rdbuf() const{ |
183 |
< |
return static_cast<_Buf*>(&internalBuf_); |
184 |
< |
} |
177 |
> |
/** |
178 |
> |
* Gets the stream buffer object associated with the stream |
179 |
> |
* @return A pointer to the stream buffe object(filebuf in single mode, stringbuf in |
180 |
> |
* parallel mode) associated with the stream. |
181 |
> |
*/ |
182 |
> |
_Buf* rdbuf() const{ |
183 |
> |
return static_cast<_Buf*>(&internalBuf_); |
184 |
> |
} |
185 |
|
|
186 |
< |
private: |
186 |
> |
private: |
187 |
|
|
188 |
< |
/** |
189 |
< |
* Internal function used to open the file |
190 |
< |
* @return true if succesfully opens a file (single mode) or gets the file content (parallel mode) |
191 |
< |
* otherwise return false |
192 |
< |
* @filename String containing the name of the file to be opened |
193 |
< |
* @mode Flags describing the requested i/o mode for the file |
194 |
< |
* @todo use try - catch syntax to make the program more readable |
195 |
< |
*/ |
196 |
< |
bool internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename){ |
188 |
> |
/** |
189 |
> |
* Internal function used to open the file |
190 |
> |
* @return true if succesfully opens a file (single mode) or gets the file content (parallel mode) |
191 |
> |
* otherwise return false |
192 |
> |
* @filename String containing the name of the file to be opened |
193 |
> |
* @mode Flags describing the requested i/o mode for the file |
194 |
> |
* @todo use try - catch syntax to make the program more readable |
195 |
> |
*/ |
196 |
> |
bool internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename){ |
197 |
|
|
198 |
|
#ifdef IS_MPI |
199 |
< |
int commStatus; |
200 |
< |
long fileSize; |
201 |
< |
char* fbuf; |
202 |
< |
int filenameLen; |
203 |
< |
int diffFilename; |
204 |
< |
int error; |
205 |
< |
int myRank; |
206 |
< |
int masterNode; |
199 |
> |
int commStatus; |
200 |
> |
long fileSize; |
201 |
> |
char* fbuf; |
202 |
> |
int filenameLen; |
203 |
> |
int diffFilename; |
204 |
> |
int error; |
205 |
> |
int myRank; |
206 |
> |
int masterNode; |
207 |
|
|
208 |
< |
commStatus = MPI_Comm_rank(MPI_COMM_WORLD, &myRank); |
209 |
< |
masterNode = 0; |
208 |
> |
commStatus = MPI_Comm_rank(MPI_COMM_WORLD, &myRank); |
209 |
> |
masterNode = 0; |
210 |
|
|
211 |
< |
if (myRank == masterNode) { |
211 |
> |
if (myRank == masterNode) { |
212 |
|
|
213 |
< |
if (checkFilename) { |
213 |
> |
if (checkFilename) { |
214 |
|
|
215 |
< |
//check the filename is the same |
216 |
< |
filenameLen = strlen(filename); |
217 |
< |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
218 |
< |
commStatus = MPI_Bcast((void*)filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
215 |
> |
//check the filename is the same |
216 |
> |
filenameLen = strlen(filename); |
217 |
> |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
218 |
> |
commStatus = MPI_Bcast((void*)filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
219 |
|
|
220 |
< |
diffFilename = 0; |
221 |
< |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
220 |
> |
diffFilename = 0; |
221 |
> |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
222 |
|
|
223 |
< |
//if file names are different just return false |
224 |
< |
if (error > 0) |
225 |
< |
return false; |
226 |
< |
} |
223 |
> |
//if file names are different just return false |
224 |
> |
if (error > 0) |
225 |
> |
return false; |
226 |
> |
} |
227 |
|
|
228 |
< |
std::ifstream fin(filename, mode); |
228 |
> |
std::ifstream fin(filename, mode); |
229 |
|
|
230 |
< |
if (fin.is_open()) { |
230 |
> |
if (fin.is_open()) { |
231 |
|
|
232 |
< |
fin.seekg(0, std::ios::end); |
233 |
< |
fileSize = fin.tellg(); |
234 |
< |
fin.seekg(0, std::ios::beg); |
232 |
> |
fin.seekg(0, std::ios::end); |
233 |
> |
fileSize = fin.tellg(); |
234 |
> |
fin.seekg(0, std::ios::beg); |
235 |
|
|
236 |
< |
// '\0' need one more char |
237 |
< |
fbuf = new char[fileSize+1]; |
236 |
> |
// '\0' need one more char |
237 |
> |
fbuf = new char[fileSize+1]; |
238 |
|
|
239 |
< |
assert(fbuf != 0); |
239 |
> |
assert(fbuf != 0); |
240 |
|
|
241 |
< |
fin.read(fbuf, fileSize); |
241 |
> |
fin.read(fbuf, fileSize); |
242 |
|
|
243 |
< |
if (fin.fail()) |
244 |
< |
fileSize = FileIOError; |
243 |
> |
if (fin.fail()) |
244 |
> |
fileSize = FileIOError; |
245 |
|
|
246 |
< |
//brocasting the file size |
247 |
< |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
246 |
> |
//brocasting the file size |
247 |
> |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
248 |
|
|
249 |
< |
if (fileSize < 0) { |
250 |
< |
fin.close(); |
251 |
< |
delete fbuf; |
249 |
> |
if (fileSize < 0) { |
250 |
> |
fin.close(); |
251 |
> |
delete fbuf; |
252 |
|
|
253 |
< |
return false; |
254 |
< |
} |
253 |
> |
return false; |
254 |
> |
} |
255 |
|
|
256 |
< |
// make a c-style std::string and brocasting it |
257 |
< |
fbuf[fileSize] = '\0'; |
258 |
< |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
256 |
> |
// make a c-style std::string and brocasting it |
257 |
> |
fbuf[fileSize] = '\0'; |
258 |
> |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
259 |
|
|
260 |
< |
//close the file and delete the buffer |
261 |
< |
fin.close(); |
262 |
< |
internalBuf_.str(fbuf); |
263 |
< |
delete fbuf; |
264 |
< |
}else{ |
265 |
< |
fileSize = FileNotExists; |
266 |
< |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
267 |
< |
return false; |
268 |
< |
} |
260 |
> |
//close the file and delete the buffer |
261 |
> |
fin.close(); |
262 |
> |
internalBuf_.str(fbuf); |
263 |
> |
delete [] fbuf; |
264 |
> |
}else{ |
265 |
> |
fileSize = FileNotExists; |
266 |
> |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
267 |
> |
return false; |
268 |
> |
} |
269 |
|
|
270 |
< |
} else{ //slave nodes |
270 |
> |
} else{ //slave nodes |
271 |
|
|
272 |
< |
//check file name |
273 |
< |
if (checkFilename) { |
274 |
< |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
272 |
> |
//check file name |
273 |
> |
if (checkFilename) { |
274 |
> |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
275 |
|
|
276 |
< |
char * masterFilename = new char[filenameLen]; |
277 |
< |
commStatus = MPI_Bcast(masterFilename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
276 |
> |
char * masterFilename = new char[filenameLen]; |
277 |
> |
commStatus = MPI_Bcast(masterFilename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
278 |
|
|
279 |
< |
if( strcmp(masterFilename, filename) == 0) |
280 |
< |
diffFilename = 0; |
281 |
< |
else |
282 |
< |
diffFilename = 1; |
279 |
> |
if( strcmp(masterFilename, filename) == 0) |
280 |
> |
diffFilename = 0; |
281 |
> |
else |
282 |
> |
diffFilename = 1; |
283 |
|
|
284 |
< |
delete masterFilename; |
284 |
> |
delete masterFilename; |
285 |
|
|
286 |
< |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
286 |
> |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
287 |
|
|
288 |
< |
if (error > 0) |
289 |
< |
return false; |
290 |
< |
} |
291 |
< |
//get file size |
292 |
< |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
288 |
> |
if (error > 0) |
289 |
> |
return false; |
290 |
> |
} |
291 |
> |
//get file size |
292 |
> |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
293 |
|
|
294 |
< |
if (fileSize >= 0 ) { |
295 |
< |
fbuf = new char[fileSize+1]; |
296 |
< |
assert(fbuf); |
294 |
> |
if (fileSize >= 0 ) { |
295 |
> |
fbuf = new char[fileSize+1]; |
296 |
> |
assert(fbuf); |
297 |
|
|
298 |
< |
//receive file content |
299 |
< |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
298 |
> |
//receive file content |
299 |
> |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
300 |
|
|
301 |
< |
internalBuf_.str(fbuf); |
302 |
< |
delete fbuf; |
301 |
> |
internalBuf_.str(fbuf); |
302 |
> |
delete [] fbuf; |
303 |
|
|
304 |
< |
} else if (fileSize == FileNotExists ) { |
305 |
< |
return false; |
304 |
> |
} else if (fileSize == FileNotExists ) { |
305 |
> |
return false; |
306 |
|
|
307 |
< |
} else if (fileSize == FileIOError ) { |
308 |
< |
return false; |
309 |
< |
} |
310 |
< |
} |
307 |
> |
} else if (fileSize == FileIOError ) { |
308 |
> |
return false; |
309 |
> |
} |
310 |
> |
} |
311 |
|
|
312 |
|
#else |
313 |
< |
//in single version, fall back to ifstream |
314 |
< |
if (!internalBuf_.open(filename, mode)) { |
315 |
< |
this->setstate(std::ios_base::failbit); |
316 |
< |
return false; |
317 |
< |
} |
313 |
> |
//in single version, fall back to ifstream |
314 |
> |
if (!internalBuf_.open(filename, mode)) { |
315 |
> |
this->setstate(std::ios_base::failbit); |
316 |
> |
return false; |
317 |
> |
} |
318 |
|
|
319 |
|
#endif |
320 |
< |
this->clear(); |
321 |
< |
return true; |
322 |
< |
} |
320 |
> |
this->clear(); |
321 |
> |
return true; |
322 |
> |
} |
323 |
|
|
324 |
< |
_Buf internalBuf_; /** internal stream buffer */ |
325 |
< |
bool isRead; /** file opened flag */ |
326 |
< |
}; |
324 |
> |
_Buf internalBuf_; /** internal stream buffer */ |
325 |
> |
bool isRead; /** file opened flag */ |
326 |
> |
}; |
327 |
|
|
328 |
< |
typedef basic_ifstrstream<char, std::char_traits<char>, std::allocator<char> > ifstrstream; |
328 |
> |
typedef basic_ifstrstream<char, std::char_traits<char>, std::allocator<char> > ifstrstream; |
329 |
|
}//namespace oopse |
330 |
|
#endif //IO_IFSTRSTREAM_HPP |