1 |
< |
/* |
2 |
< |
* Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project |
3 |
< |
* |
4 |
< |
* Contact: oopse@oopse.org |
5 |
< |
* |
6 |
< |
* This program is free software; you can redistribute it and/or |
7 |
< |
* modify it under the terms of the GNU Lesser General Public License |
8 |
< |
* as published by the Free Software Foundation; either version 2.1 |
9 |
< |
* of the License, or (at your option) any later version. |
10 |
< |
* All we ask is that proper credit is given for our work, which includes |
11 |
< |
* - but is not limited to - adding the above copyright notice to the beginning |
12 |
< |
* of your source code files, and to any copyright notice that you may distribute |
13 |
< |
* with programs based on this work. |
14 |
< |
* |
15 |
< |
* This program is distributed in the hope that it will be useful, |
16 |
< |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
< |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
< |
* GNU Lesser General Public License for more details. |
19 |
< |
* |
20 |
< |
* You should have received a copy of the GNU Lesser General Public License |
21 |
< |
* along with this program; if not, write to the Free Software |
22 |
< |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1 |
> |
/* |
2 |
> |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
* |
4 |
+ |
* The University of Notre Dame grants you ("Licensee") a |
5 |
+ |
* non-exclusive, royalty free, license to use, modify and |
6 |
+ |
* redistribute this software in source and binary code form, provided |
7 |
+ |
* that the following conditions are met: |
8 |
+ |
* |
9 |
+ |
* 1. Acknowledgement of the program authors must be made in any |
10 |
+ |
* publication of scientific results based in part on use of the |
11 |
+ |
* program. An acceptable form of acknowledgement is citation of |
12 |
+ |
* the article in which the program was described (Matthew |
13 |
+ |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
+ |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
+ |
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
+ |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
+ |
* |
18 |
+ |
* 2. Redistributions of source code must retain the above copyright |
19 |
+ |
* notice, this list of conditions and the following disclaimer. |
20 |
+ |
* |
21 |
+ |
* 3. Redistributions in binary form must reproduce the above copyright |
22 |
+ |
* notice, this list of conditions and the following disclaimer in the |
23 |
+ |
* documentation and/or other materials provided with the |
24 |
+ |
* distribution. |
25 |
+ |
* |
26 |
+ |
* This software is provided "AS IS," without a warranty of any |
27 |
+ |
* kind. All express or implied conditions, representations and |
28 |
+ |
* warranties, including any implied warranty of merchantability, |
29 |
+ |
* fitness for a particular purpose or non-infringement, are hereby |
30 |
+ |
* excluded. The University of Notre Dame and its licensors shall not |
31 |
+ |
* be liable for any damages suffered by licensee as a result of |
32 |
+ |
* using, modifying or distributing the software or its |
33 |
+ |
* derivatives. In no event will the University of Notre Dame or its |
34 |
+ |
* licensors be liable for any lost revenue, profit or data, or for |
35 |
+ |
* direct, indirect, special, consequential, incidental or punitive |
36 |
+ |
* damages, however caused and regardless of the theory of liability, |
37 |
+ |
* arising out of the use of or inability to use software, even if the |
38 |
+ |
* University of Notre Dame has been advised of the possibility of |
39 |
+ |
* such damages. |
40 |
|
*/ |
41 |
< |
|
41 |
> |
|
42 |
|
/** |
43 |
|
* @file basic_ifstrstream.hpp |
44 |
|
* @author Teng Lin |
59 |
|
#endif |
60 |
|
|
61 |
|
namespace oopse { |
62 |
< |
using namespace std; |
62 |
> |
|
63 |
|
/** |
64 |
|
* @class basic_ifstrstream basic_ifstrstream.hpp "io/basic_ifstrstream.hpp" |
65 |
< |
* @brief class provides a stream interface to read data from files. |
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> |
79 |
|
* @endcode |
80 |
|
*/ |
81 |
|
template <class _CharT, class _Traits, class _Alloc> |
82 |
< |
class basic_ifstrstream : public basic_istream<_CharT, _Traits> { |
82 |
> |
class basic_ifstrstream : public std::basic_istream<_CharT, _Traits> { |
83 |
|
public: |
84 |
|
//traits |
85 |
|
typedef _CharT char_type; |
88 |
|
typedef typename _Traits::off_type off_type; |
89 |
|
typedef _Traits traits_type; |
90 |
|
|
91 |
< |
typedef basic_ios<_CharT, _Traits> _Basic_ios; |
92 |
< |
typedef 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 basic_stringbuf<_CharT, _Traits, _Alloc> _Buf; |
95 |
> |
typedef std::basic_stringbuf<_CharT, _Traits, _Alloc> _Buf; |
96 |
|
#else |
97 |
< |
typedef basic_filebuf<_CharT, _Traits> _Buf; |
97 |
> |
typedef std::basic_filebuf<_CharT, _Traits> _Buf; |
98 |
|
#endif |
99 |
|
|
100 |
< |
static const int FileNoExists = -1; |
100 |
> |
static const int FileNotExists = -1; |
101 |
|
static const int FileIOError = -2; |
102 |
|
|
103 |
|
public: |
104 |
|
|
105 |
|
/** Constructs an object of class ifstream. */ |
106 |
|
basic_ifstrstream() |
107 |
< |
: basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), |
108 |
< |
internalBuf_(NULL), isRead(false) { |
109 |
< |
|
110 |
< |
#ifdef IS_MPI |
95 |
< |
//in parallel mode, fall back to istringstream |
96 |
< |
basic_stringbuf<_CharT, _Traits, _Alloc>* stringBuffer = new basic_stringbuf<_CharT, _Traits, _Alloc>(); |
97 |
< |
internalBuf_ = stringBuffer; |
98 |
< |
#else |
99 |
< |
//in single version, fall back to ifstream |
100 |
< |
basic_filebuf<_CharT, _Traits>* fileBuffer = new basic_filebuf<_CharT, _Traits>(); |
101 |
< |
internalBuf_ = fileBuffer; |
102 |
< |
#endif |
103 |
< |
|
104 |
< |
this->init(internalBuf_); |
105 |
< |
isRead = false; |
107 |
> |
: std::basic_istream<_CharT, _Traits>(0), |
108 |
> |
internalBuf_(), isRead(false) { |
109 |
> |
|
110 |
> |
this->init(&internalBuf_); |
111 |
|
} |
112 |
|
|
113 |
|
/** |
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, ios_base::openmode mode = ios_base::in, bool checkFilename = false) |
120 |
< |
: basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), |
121 |
< |
internalBuf_(NULL), isRead(false) { |
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 |
< |
isRead = internalOpen(filename, mode | ios_base::in, checkFilename); |
123 |
> |
this->init(&internalBuf_); |
124 |
> |
isRead = internalOpen(filename, mode | std::ios_base::in, checkFilename); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
129 |
|
*/ |
130 |
|
~basic_ifstrstream(){ |
131 |
|
close(); |
126 |
– |
delete internalBuf_; |
127 |
– |
internalBuf_ = NULL; |
132 |
|
} |
133 |
|
|
134 |
|
/** |
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, ios_base::openmode mode = ios_base::in, bool checkFilename = false){ |
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); |
156 |
|
return isRead; |
157 |
|
#else |
158 |
|
//single version fall back to ifstream |
159 |
< |
return this->rdbuf()->is_open(); |
159 |
> |
return internalBuf_.is_open(); |
160 |
|
#endif |
161 |
|
} |
162 |
|
|
167 |
|
void close() { |
168 |
|
#ifndef IS_MPI |
169 |
|
//single version fall back to ifstream |
170 |
< |
if (!this->rdbuf()->close()) |
171 |
< |
this->setstate(ios_base::failbit); |
170 |
> |
if (!internalBuf_.close()) |
171 |
> |
this->setstate(std::ios_base::failbit); |
172 |
|
#endif |
173 |
|
|
174 |
|
isRead = false; |
180 |
|
* parallel mode) associated with the stream. |
181 |
|
*/ |
182 |
|
_Buf* rdbuf() const{ |
183 |
< |
return static_cast<_Buf*>(internalBuf_); |
183 |
> |
return static_cast<_Buf*>(&internalBuf_); |
184 |
|
} |
185 |
|
|
186 |
|
private: |
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, ios_base::openmode mode, bool checkFilename){ |
196 |
> |
bool internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename){ |
197 |
|
|
198 |
|
#ifdef IS_MPI |
199 |
|
int commStatus; |
225 |
|
return false; |
226 |
|
} |
227 |
|
|
228 |
< |
ifstream fin(filename, mode); |
225 |
< |
basic_stringbuf<_CharT, _Traits, _Alloc>* sbuf; |
228 |
> |
std::ifstream fin(filename, mode); |
229 |
|
|
230 |
|
if (fin.is_open()) { |
231 |
|
|
232 |
< |
fin.seekg(0, ios::end); |
232 |
> |
fin.seekg(0, std::ios::end); |
233 |
|
fileSize = fin.tellg(); |
234 |
< |
fin.seekg(0, ios::beg); |
234 |
> |
fin.seekg(0, std::ios::beg); |
235 |
|
|
236 |
|
// '\0' need one more char |
237 |
|
fbuf = new char[fileSize+1]; |
253 |
|
return false; |
254 |
|
} |
255 |
|
|
256 |
< |
// make a c-style string and brocasting it |
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 |
|
|
257 |
– |
//it is safe to delete null pointer |
258 |
– |
delete internalBuf_; |
259 |
– |
|
260 |
– |
//initilaize istream |
261 |
– |
internalBuf_ = new basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode); |
262 |
– |
assert(internalBuf_); |
263 |
– |
this->init(internalBuf_); |
264 |
– |
|
260 |
|
//close the file and delete the buffer |
261 |
< |
fin.close(); |
261 |
> |
fin.close(); |
262 |
> |
internalBuf_.str(fbuf); |
263 |
|
delete fbuf; |
264 |
|
}else{ |
265 |
< |
fileSize = FileNoExists; |
265 |
> |
fileSize = FileNotExists; |
266 |
|
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
267 |
|
return false; |
268 |
|
} |
269 |
|
|
270 |
< |
} else{ |
270 |
> |
} else{ //slave nodes |
271 |
> |
|
272 |
|
//check file name |
273 |
|
if (checkFilename) { |
274 |
|
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
298 |
|
//receive file content |
299 |
|
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
300 |
|
|
301 |
< |
//it is safe to delete null pointer |
305 |
< |
delete internalBuf_; |
306 |
< |
|
307 |
< |
//initilaize istream |
308 |
< |
internalBuf_ = new basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode); |
309 |
< |
assert(internalBuf_); |
310 |
< |
this->init(internalBuf_); |
311 |
< |
|
301 |
> |
internalBuf_.str(fbuf); |
302 |
|
delete fbuf; |
303 |
|
|
304 |
< |
} else if (fileSize == FileNoExists ) { |
304 |
> |
} else if (fileSize == FileNotExists ) { |
305 |
|
return false; |
306 |
|
|
307 |
|
} else if (fileSize == FileIOError ) { |
311 |
|
|
312 |
|
#else |
313 |
|
//in single version, fall back to ifstream |
314 |
< |
basic_filebuf<_CharT, _Traits>* fileBuffer = new basic_filebuf<_CharT, _Traits>(); |
314 |
> |
if (!internalBuf_.open(filename, mode)) { |
315 |
> |
this->setstate(std::ios_base::failbit); |
316 |
> |
return false; |
317 |
> |
} |
318 |
|
|
326 |
– |
this->init(fileBuffer); |
327 |
– |
if (!fileBuffer->open(filename, mode)) |
328 |
– |
this->setstate(ios_base::failbit); |
329 |
– |
|
330 |
– |
//it is safe to delete null pointer |
331 |
– |
delete internalBuf_; |
332 |
– |
|
333 |
– |
internalBuf_ = fileBuffer; |
319 |
|
#endif |
320 |
< |
|
320 |
> |
this->clear(); |
321 |
|
return true; |
322 |
|
} |
323 |
|
|
324 |
< |
basic_streambuf<_CharT, _Traits>* internalBuf_; /** internal stream buffer */ |
324 |
> |
_Buf internalBuf_; /** internal stream buffer */ |
325 |
|
bool isRead; /** file opened flag */ |
326 |
|
}; |
327 |
|
|
328 |
< |
typedef basic_ifstrstream<char, char_traits<char>, allocator<char> > ifstrstream; |
328 |
> |
typedef basic_ifstrstream<char, std::char_traits<char>, std::allocator<char> > ifstrstream; |
329 |
|
}//namespace oopse |
330 |
|
#endif //IO_IFSTRSTREAM_HPP |