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 1583 by tim, Fri Oct 15 21:10:50 2004 UTC vs.
Revision 1930 by gezelter, Wed Jan 12 22:41:40 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 43 | 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 63 | 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 72 | 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),
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 <            basic_stringbuf<_CharT, _Traits, _Alloc>* stringBuffer = new  basic_stringbuf<_CharT, _Traits, _Alloc>();
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 <            basic_filebuf<_CharT, _Traits>* fileBuffer = new  basic_filebuf<_CharT, _Traits>();
116 >            std::basic_filebuf<_CharT, _Traits>* fileBuffer = new  std::basic_filebuf<_CharT, _Traits>();
117              internalBuf_ =  fileBuffer;
118   #endif            
119  
# Line 111 | Line 127 | class basic_ifstrstream : public basic_istream<_CharT,
127           * @mode Flags describing the requested i/o mode for the file, default value is ios_base::in      
128           * @checkFilename Flags indicating checking the file name in parallel
129           */
130 <        explicit basic_ifstrstream(const char* filename, ios_base::openmode mode = ios_base::in, bool checkFilename = false)
131 <            : basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0),
130 >        explicit basic_ifstrstream(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false)
131 >            : std::basic_ios<_CharT, _Traits>(),  std::basic_istream<_CharT, _Traits>(0),
132                internalBuf_(NULL), isRead(false) {
133  
134 <           isRead =  internalOpen(filename,  mode | ios_base::in, checkFilename);
134 >           isRead =  internalOpen(filename,  mode | std::ios_base::in, checkFilename);
135           }
136  
137          /**
# Line 135 | Line 151 | class basic_ifstrstream : public basic_istream<_CharT,
151           * @mode Flags describing the requested i/o mode for the file
152           * @checkFilename Flags indicating checking the file name in parallel
153           */
154 <        void open(const char* filename, ios_base::openmode mode = ios_base::in, bool checkFilename = false){
154 >        void open(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false){
155  
156              if (!isRead ) {
157                  isRead = internalOpen(filename, mode, checkFilename);
# Line 164 | Line 180 | class basic_ifstrstream : public basic_istream<_CharT,
180   #ifndef IS_MPI            
181              //single version fall back to ifstream
182              if (!this->rdbuf()->close())
183 <                this->setstate(ios_base::failbit);
183 >                this->setstate(std::ios_base::failbit);
184   #endif            
185  
186              isRead = false;
# Line 189 | Line 205 | class basic_ifstrstream : public basic_istream<_CharT,
205           * @mode Flags describing the requested i/o mode for the file
206           * @todo use try - catch syntax to make the program more readable
207           */
208 <        bool internalOpen(const char* filename, ios_base::openmode mode, bool checkFilename){
208 >        bool internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename){
209  
210   #ifdef IS_MPI        
211              int commStatus;
# Line 221 | Line 237 | class basic_ifstrstream : public basic_istream<_CharT,
237                          return false;  
238                  }
239                  
240 <                ifstream fin(filename, mode);
241 <                basic_stringbuf<_CharT, _Traits, _Alloc>* sbuf;
240 >                std::ifstream fin(filename, mode);
241 >                std::basic_stringbuf<_CharT, _Traits, _Alloc>* sbuf;
242  
243                  if (fin.is_open()) {
244                      
245 <                    fin.seekg(0, ios::end);
245 >                    fin.seekg(0, std::ios::end);
246                      fileSize = fin.tellg();
247 <                    fin.seekg(0, ios::beg);
247 >                    fin.seekg(0, std::ios::beg);
248                      
249                      // '\0' need one more char
250                      fbuf = new char[fileSize+1];
# Line 250 | Line 266 | class basic_ifstrstream : public basic_istream<_CharT,
266                          return false;
267                      }
268                      
269 <                    // make a c-style string and brocasting it
269 >                    // make a c-style  std::string and brocasting it
270                      fbuf[fileSize] = '\0';
271                      commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD);
272  
# Line 258 | Line 274 | class basic_ifstrstream : public basic_istream<_CharT,
274                      delete internalBuf_;
275  
276                      //initilaize istream
277 <                    internalBuf_  = new basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode);
277 >                    internalBuf_  = new std::basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode);
278                      assert(internalBuf_);
279                      this->init(internalBuf_);
280  
# Line 266 | Line 282 | class basic_ifstrstream : public basic_istream<_CharT,
282                      fin.close();                    
283                      delete fbuf;
284                  }else{
285 <                    fileSize = FileNoExists;
285 >                    fileSize = FileNotExists;
286                      commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD);  
287                      return false;
288                  }
289                
290 <             } else{
290 >             } else{ //slave nodes
291 >
292                      //check file name
293                      if (checkFilename) {
294                          commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD);    
# Line 305 | Line 322 | class basic_ifstrstream : public basic_istream<_CharT,
322                          delete internalBuf_;
323  
324                          //initilaize istream                        
325 <                        internalBuf_  = new basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode);
325 >                        internalBuf_  = new std::basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode);
326                          assert(internalBuf_);
327                          this->init(internalBuf_);
328  
329                          delete fbuf;
330  
331 <                    } else if (fileSize == FileNoExists ) {
331 >                    } else if (fileSize == FileNotExists ) {
332                          return false;
333  
334                      } else if (fileSize == FileIOError ) {
# Line 321 | Line 338 | class basic_ifstrstream : public basic_istream<_CharT,
338  
339   #else
340              //in single version, fall back to ifstream
341 <            basic_filebuf<_CharT, _Traits>* fileBuffer = new  basic_filebuf<_CharT, _Traits>();
341 >            std::basic_filebuf<_CharT, _Traits>* fileBuffer = new  std::basic_filebuf<_CharT, _Traits>();
342  
326            this->init(fileBuffer);
327            if (!fileBuffer->open(filename, mode))
328                this->setstate(ios_base::failbit);
329
343              //it is safe to delete null pointer
344 <            delete internalBuf_;
345 <
346 <           internalBuf_ =  fileBuffer;
344 >            delete internalBuf_;
345 >            internalBuf_ =  fileBuffer;
346 >
347 >            this->init(internalBuf_);
348 >            if (!fileBuffer->open(filename, mode)) {
349 >                this->setstate(std::ios_base::failbit);
350 >                return false;
351 >            }    
352 >
353   #endif
354  
355              return true;
356          }
357          
358 <        basic_streambuf<_CharT, _Traits>*  internalBuf_; /** internal stream buffer */        
358 >        std::basic_streambuf<_CharT, _Traits>*  internalBuf_; /** internal stream buffer */        
359          bool isRead;                                                                    /** file opened flag */
360   };
361  
362 < typedef basic_ifstrstream<char, char_traits<char>, allocator<char> > ifstrstream;
362 > typedef basic_ifstrstream<char, std::char_traits<char>, std::allocator<char> > ifstrstream;
363   }//namespace oopse
364   #endif //IO_IFSTRSTREAM_HPP

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines