ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/io/basic_ifstrstream.hpp
(Generate patch)

Comparing trunk/OOPSE-4/src/io/basic_ifstrstream.hpp (file contents):
Revision 1582 by tim, Fri Oct 15 20:42:25 2004 UTC vs.
Revision 2165 by tim, Mon Apr 11 21:58:05 2005 UTC

# Line 1 | Line 1
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
# Line 33 | Line 49
49   #ifndef IO_IFSTRSTREAM_HPP
50   #define IO_IFSTRSTREAM_HPP
51  
52 + #include <cassert>
53   #include <cstring>
54   #include <fstream>
55   #include <sstream>
# Line 42 | Line 59 | using namespace std;
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>
# Line 62 | Line 79 | class basic_ifstrstream : public basic_istream<_CharT,
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;
# Line 71 | Line 88 | class basic_ifstrstream : public basic_istream<_CharT,
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        
94 <            //in parallel mode, fall back to istringstream
95 <            basic_stringbuf<_CharT, _Traits, _Alloc>* stringBuffer = new  basic_stringbuf<_CharT, _Traits, _Alloc>();
96 <            internalBuf_ =  stringBuffer;
97 < #else
98 <            //in single version, fall back to ifstream
99 <            basic_filebuf<_CharT, _Traits>* fileBuffer = new  basic_filebuf<_CharT, _Traits>();
100 <            internalBuf_ =  fileBuffer;
101 < #endif            
102 <
103 <            this->init(internalBuf_);
104 <            isRead = false;
107 >            :  std::basic_istream<_CharT, _Traits>(0),
108 >              internalBuf_(), isRead(false)  {
109 >    
110 >            this->init(&internalBuf_);
111          }
112          
113          /**
# Line 110 | Line 116 | class basic_ifstrstream : public basic_istream<_CharT,
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          /**
# Line 122 | Line 129 | class basic_ifstrstream : public basic_istream<_CharT,
129           */
130          ~basic_ifstrstream(){
131              close();
125            delete internalBuf_;
126            internalBuf_ = NULL;
132          }
133  
134          /**
# Line 134 | Line 139 | class basic_ifstrstream : public basic_istream<_CharT,
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);
# Line 151 | Line 156 | class basic_ifstrstream : public basic_istream<_CharT,
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  
# Line 162 | Line 167 | class basic_ifstrstream : public basic_istream<_CharT,
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;
# Line 175 | Line 180 | class basic_ifstrstream : public basic_istream<_CharT,
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:
# Line 188 | Line 193 | class basic_ifstrstream : public basic_istream<_CharT,
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;
# Line 209 | Line 214 | class basic_ifstrstream : public basic_istream<_CharT,
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(filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD);    
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_COMM_WORLD);            
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                  }
227                  
228 <                ifstream fin(filename, mode);
224 <                basic_stringbuf<_CharT, _Traits, _Alloc>* sbuf;
228 >                std::ifstream fin(filename, mode);
229  
230                  if (fin.is_open()) {
231                      
232 <                    fin.in.seekg(0, ios::end);
232 >                    fin.seekg(0, std::ios::end);
233                      fileSize = fin.tellg();
234 <
234 >                    fin.seekg(0, std::ios::beg);
235 >                    
236                      // '\0' need one more char
237                      fbuf = new char[fileSize+1];
238                      
# Line 248 | Line 253 | class basic_ifstrstream : public basic_istream<_CharT,
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  
255                    //it is safe to delete null pointer
256                    delete internalBuf_;
257
258                    //initilaize istream
259                    internalBuf_  = new basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode);
260                    assert(internalBuf_);
261                    this->init(internalBuf_);
262
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);    
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);    
# Line 284 | Line 283 | class basic_ifstrstream : public basic_istream<_CharT,
283  
284                          delete masterFilename;
285                          
286 <                        commStatus = MPI_Allreduce(diffFilename, error, 1,  MPI_INT,  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;                        
# Line 299 | Line 298 | class basic_ifstrstream : public basic_istream<_CharT,
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
303 <                        delete internalBuf_;
304 <
305 <                        //initilaize istream                        
306 <                        internalBuf_  = new basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode);
307 <                        assert(internalBuf_);
308 <                        this->init(internalBuf_);
309 <
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 ) {
# Line 319 | Line 311 | class basic_ifstrstream : public basic_istream<_CharT,
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  
324            this->init(fileBuffer);
325            if (!fileBuffer->open(filename, mode))
326                this->setstate(ios_base::failbit);
327
328            //it is safe to delete null pointer
329            delete internalBuf_;
330
331           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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines