| 104 |
|
|
| 105 |
|
/** Constructs an object of class ifstream. */ |
| 106 |
|
basic_ifstrstream() |
| 107 |
< |
: std::basic_ios<_CharT, _Traits>(), std::basic_istream<_CharT, _Traits>(0), |
| 108 |
< |
internalBuf_(NULL), isRead(false) { |
| 109 |
< |
|
| 110 |
< |
#ifdef IS_MPI |
| 111 |
< |
//in parallel mode, fall back to istringstream |
| 112 |
< |
std::basic_stringbuf<_CharT, _Traits, _Alloc>* stringBuffer = new std::basic_stringbuf<_CharT, _Traits, _Alloc>(); |
| 113 |
< |
internalBuf_ = stringBuffer; |
| 114 |
< |
#else |
| 115 |
< |
//in single version, fall back to ifstream |
| 116 |
< |
std::basic_filebuf<_CharT, _Traits>* fileBuffer = new std::basic_filebuf<_CharT, _Traits>(); |
| 117 |
< |
internalBuf_ = fileBuffer; |
| 118 |
< |
#endif |
| 119 |
< |
|
| 120 |
< |
this->init(internalBuf_); |
| 121 |
< |
isRead = false; |
| 107 |
> |
: std::basic_istream<_CharT, _Traits>(0), |
| 108 |
> |
internalBuf_(), isRead(false) { |
| 109 |
> |
|
| 110 |
> |
this->init(&internalBuf_); |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
/** |
| 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_ios<_CharT, _Traits>(), std::basic_istream<_CharT, _Traits>(0), |
| 121 |
< |
internalBuf_(NULL), isRead(false) { |
| 120 |
> |
: std::basic_istream<_CharT, _Traits>(0), |
| 121 |
> |
internalBuf_(), isRead(false) { |
| 122 |
|
|
| 123 |
< |
isRead = internalOpen(filename, mode | std::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(); |
| 142 |
– |
delete internalBuf_; |
| 143 |
– |
internalBuf_ = NULL; |
| 132 |
|
} |
| 133 |
|
|
| 134 |
|
/** |
| 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()) |
| 170 |
> |
if (!internalBuf_.close()) |
| 171 |
|
this->setstate(std::ios_base::failbit); |
| 172 |
|
#endif |
| 173 |
|
|
| 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: |
| 226 |
|
} |
| 227 |
|
|
| 228 |
|
std::ifstream fin(filename, mode); |
| 241 |
– |
std::basic_stringbuf<_CharT, _Traits, _Alloc>* sbuf; |
| 229 |
|
|
| 230 |
|
if (fin.is_open()) { |
| 231 |
|
|
| 257 |
|
fbuf[fileSize] = '\0'; |
| 258 |
|
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 259 |
|
|
| 273 |
– |
//it is safe to delete null pointer |
| 274 |
– |
delete internalBuf_; |
| 275 |
– |
|
| 276 |
– |
//initilaize istream |
| 277 |
– |
internalBuf_ = new std::basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode); |
| 278 |
– |
assert(internalBuf_); |
| 279 |
– |
this->init(internalBuf_); |
| 280 |
– |
|
| 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 = FileNotExists; |
| 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 |
| 322 |
< |
delete internalBuf_; |
| 323 |
< |
|
| 324 |
< |
//initilaize istream |
| 325 |
< |
internalBuf_ = new std::basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode); |
| 326 |
< |
assert(internalBuf_); |
| 327 |
< |
this->init(internalBuf_); |
| 328 |
< |
|
| 301 |
> |
internalBuf_.str(fbuf); |
| 302 |
|
delete fbuf; |
| 303 |
|
|
| 304 |
|
} else if (fileSize == FileNotExists ) { |
| 311 |
|
|
| 312 |
|
#else |
| 313 |
|
//in single version, fall back to ifstream |
| 314 |
< |
std::basic_filebuf<_CharT, _Traits>* fileBuffer = new std::basic_filebuf<_CharT, _Traits>(); |
| 342 |
< |
|
| 343 |
< |
//it is safe to delete null pointer |
| 344 |
< |
delete internalBuf_; |
| 345 |
< |
internalBuf_ = fileBuffer; |
| 346 |
< |
|
| 347 |
< |
this->init(internalBuf_); |
| 348 |
< |
if (!fileBuffer->open(filename, mode)) { |
| 314 |
> |
if (!internalBuf_.open(filename, mode)) { |
| 315 |
|
this->setstate(std::ios_base::failbit); |
| 316 |
|
return false; |
| 317 |
|
} |
| 318 |
|
|
| 319 |
|
#endif |
| 320 |
< |
|
| 320 |
> |
this->clear(); |
| 321 |
|
return true; |
| 322 |
|
} |
| 323 |
|
|
| 324 |
< |
std::basic_streambuf<_CharT, _Traits>* internalBuf_; /** internal stream buffer */ |
| 324 |
> |
_Buf internalBuf_; /** internal stream buffer */ |
| 325 |
|
bool isRead; /** file opened flag */ |
| 326 |
|
}; |
| 327 |
|
|