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

Comparing:
trunk/OOPSE-4/src/io/basic_ifstrstream.hpp (file contents), Revision 1581 by tim, Fri Oct 15 20:35:01 2004 UTC vs.
branches/new_design/OOPSE-4/src/io/basic_ifstrstream.hpp (file contents), Revision 1832 by tim, Thu Dec 2 16:04:19 2004 UTC

# Line 33 | Line 33
33   #ifndef IO_IFSTRSTREAM_HPP
34   #define IO_IFSTRSTREAM_HPP
35  
36 + #include <cassert>
37   #include <cstring>
38   #include <fstream>
39   #include <sstream>
# Line 42 | Line 43 | using namespace std;
43   #endif
44  
45   namespace oopse {
46 < using namespace std;
46 >
47   /**
48   * @class basic_ifstrstream basic_ifstrstream.hpp "io/basic_ifstrstream.hpp"
49 < * @brief class provides a stream interface to read data from files.
49 > * @brief basic_ifstrstream class provides a stream interface to read data from files.
50   * <p>In single mode, it falls back to ifstream. Don't need to read the whole file into memory.
51   * In parallel mode, the master node will read the whole file and brocast it to other slave nodes.
52   * After brocasting, every node will fall back to stringstream.</p>
# Line 62 | Line 63 | class basic_ifstrstream : public basic_istream<_CharT,
63   * @endcode
64   */
65   template <class _CharT, class _Traits,  class _Alloc>
66 < class basic_ifstrstream : public basic_istream<_CharT, _Traits> {
66 > class basic_ifstrstream : public std::basic_istream<_CharT, _Traits> {
67      public:
68          //traits
69          typedef _CharT                     char_type;
# Line 71 | Line 72 | class basic_ifstrstream : public basic_istream<_CharT,
72          typedef typename _Traits::off_type off_type;
73          typedef _Traits                    traits_type;
74  
75 <        typedef basic_ios<_CharT, _Traits>                _Basic_ios;
76 <        typedef basic_istream<_CharT, _Traits>            _Base;
75 >        typedef std::basic_ios<_CharT, _Traits>                _Basic_ios;
76 >        typedef std::basic_istream<_CharT, _Traits>            _Base;
77  
78   #ifdef IS_MPI
79 <         typedef basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;        
79 >         typedef std::basic_stringbuf<_CharT, _Traits, _Alloc>  _Buf;        
80   #else
81 <        typedef basic_filebuf<_CharT, _Traits>            _Buf;
81 >        typedef std::basic_filebuf<_CharT, _Traits>            _Buf;
82   #endif
83  
84 <        static  const int FileNoExists = -1;
84 >        static  const int FileNotExists = -1;
85          static const int FileIOError = -2;
86          
87      public:
88          
89          /**  Constructs an object of class ifstream.  */
90          basic_ifstrstream()
91 <            : basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0),
91 >            : std::basic_ios<_CharT, _Traits>(),  std::basic_istream<_CharT, _Traits>(0),
92                internalBuf_(NULL), isRead(false)  {
93  
94   #ifdef IS_MPI        
95              //in parallel mode, fall back to istringstream
96 <            basic_stringbuf<_CharT, _Traits, _Alloc>* stringBuffer = new  basic_stringbuf<_CharT, _Traits, _Alloc>();
96 >            std::basic_stringbuf<_CharT, _Traits, _Alloc>* stringBuffer = new  std::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>();
100 >            std::basic_filebuf<_CharT, _Traits>* fileBuffer = new  std::basic_filebuf<_CharT, _Traits>();
101              internalBuf_ =  fileBuffer;
102   #endif            
103  
# Line 110 | Line 111 | class basic_ifstrstream : public basic_istream<_CharT,
111           * @mode Flags describing the requested i/o mode for the file, default value is ios_base::in      
112           * @checkFilename Flags indicating checking the file name in parallel
113           */
114 <        explicit basic_ifstrstream(const char* filename, ios_base::openmode mode = ios_base::in, bool checkFilename = false)
115 <            : basic_ios<_CharT, _Traits>(),  basic_istream<_CharT, _Traits>(0),
114 >        explicit basic_ifstrstream(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false)
115 >            : std::basic_ios<_CharT, _Traits>(),  std::basic_istream<_CharT, _Traits>(0),
116                internalBuf_(NULL), isRead(false) {
117  
118 <           isRead =  internalOpen(filename,  mode | ios_base::in, checkFilename);
118 >           isRead =  internalOpen(filename,  mode | std::ios_base::in, checkFilename);
119           }
120  
121          /**
# Line 134 | Line 135 | class basic_ifstrstream : public basic_istream<_CharT,
135           * @mode Flags describing the requested i/o mode for the file
136           * @checkFilename Flags indicating checking the file name in parallel
137           */
138 <        void open(const char* filename, ios_base::openmode mode = ios_base::in, bool checkFilename = false){
138 >        void open(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false){
139  
140              if (!isRead ) {
141                  isRead = internalOpen(filename, mode, checkFilename);
# Line 175 | Line 176 | class basic_ifstrstream : public basic_istream<_CharT,
176           * parallel mode) associated with the stream.
177           */
178          _Buf* rdbuf() const{
179 <            return const_cast<_Buf*>(internalBuf_);
179 >            return static_cast<_Buf*>(internalBuf_);
180          }
181  
182      private:
# Line 188 | Line 189 | class basic_ifstrstream : public basic_istream<_CharT,
189           * @mode Flags describing the requested i/o mode for the file
190           * @todo use try - catch syntax to make the program more readable
191           */
192 <        bool internalOpen(const char* filename, ios_base::openmode mode, bool checkFilename){
192 >        bool internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename){
193  
194   #ifdef IS_MPI        
195              int commStatus;
# Line 209 | Line 210 | class basic_ifstrstream : public basic_istream<_CharT,
210  
211                      //check the filename is the same
212                      filenameLen = strlen(filename);
213 <                    commStatus = MPI_Bcast(filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD);    
214 <                    commStatus = MPI_Bcast(filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD);    
213 >                    commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD);    
214 >                    commStatus = MPI_Bcast((void*)filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD);    
215  
216                      diffFilename = 0;
217 <                    commStatus = MPI_Allreduce(diffFilename, error, 1,  MPI_INT,  MPI_COMM_WORLD);            
217 >                    commStatus = MPI_Allreduce(&diffFilename, &error, 1,  MPI_INT, MPI_SUM,  MPI_COMM_WORLD);            
218  
219                      //if file names are different just return false
220                      if (error > 0)
221                          return false;  
222                  }
223                  
224 <                ifstream fin(filename, mode);
225 <                basic_stringbuf<_CharT, _Traits, _Alloc>* sbuf;
224 >                std::ifstream fin(filename, mode);
225 >                std::basic_stringbuf<_CharT, _Traits, _Alloc>* sbuf;
226  
227                  if (fin.is_open()) {
228                      
229 <                    fin.in.seekg(0, ios::end);
229 >                    fin.seekg(0, ios::end);
230                      fileSize = fin.tellg();
231 <
231 >                    fin.seekg(0, ios::beg);
232 >                    
233                      // '\0' need one more char
234                      fbuf = new char[fileSize+1];
235                      
# Line 248 | Line 250 | class basic_ifstrstream : public basic_istream<_CharT,
250                          return false;
251                      }
252                      
253 <                    // make a c-style string and brocasting it
253 >                    // make a c-style  std::string and brocasting it
254                      fbuf[fileSize] = '\0';
255                      commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD);
256  
# Line 256 | Line 258 | class basic_ifstrstream : public basic_istream<_CharT,
258                      delete internalBuf_;
259  
260                      //initilaize istream
261 <                    internalBuf_  = new basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode);
261 >                    internalBuf_  = new std::basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode);
262                      assert(internalBuf_);
263                      this->init(internalBuf_);
264  
# Line 264 | Line 266 | class basic_ifstrstream : public basic_istream<_CharT,
266                      fin.close();                    
267                      delete fbuf;
268                  }else{
269 <                    fileSize = FileNoExists;
269 >                    fileSize = FileNotExists;
270                      commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD);  
271                      return false;
272                  }
273                
274 <             } else{
274 >             } else{ //slave nodes
275 >
276                      //check file name
277                      if (checkFilename) {
278 <                        commStatus = MPI_Bcast(filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD);    
278 >                        commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD);    
279  
280                          char * masterFilename = new char[filenameLen];
281                          commStatus = MPI_Bcast(masterFilename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD);    
# Line 284 | Line 287 | class basic_ifstrstream : public basic_istream<_CharT,
287  
288                          delete masterFilename;
289                          
290 <                        commStatus = MPI_Allreduce(diffFilename, error, 1,  MPI_INT,  MPI_COMM_WORLD);    
290 >                        commStatus = MPI_Allreduce(&diffFilename, &error, 1,  MPI_INT,  MPI_SUM, MPI_COMM_WORLD);    
291  
292                          if (error > 0)
293                              return false;                        
# Line 303 | Line 306 | class basic_ifstrstream : public basic_istream<_CharT,
306                          delete internalBuf_;
307  
308                          //initilaize istream                        
309 <                        internalBuf_  = new basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode);
309 >                        internalBuf_  = new std::basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode);
310                          assert(internalBuf_);
311                          this->init(internalBuf_);
312  
313                          delete fbuf;
314  
315 <                    } else if (fileSize == FileNoExists ) {
315 >                    } else if (fileSize == FileNotExists ) {
316                          return false;
317  
318                      } else if (fileSize == FileIOError ) {
# Line 319 | Line 322 | class basic_ifstrstream : public basic_istream<_CharT,
322  
323   #else
324              //in single version, fall back to ifstream
325 <            basic_filebuf<_CharT, _Traits>* fileBuffer = new  basic_filebuf<_CharT, _Traits>();
325 >            std::basic_filebuf<_CharT, _Traits>* fileBuffer = new  std::basic_filebuf<_CharT, _Traits>();
326  
327              this->init(fileBuffer);
328              if (!fileBuffer->open(filename, mode))
# Line 334 | Line 337 | class basic_ifstrstream : public basic_istream<_CharT,
337              return true;
338          }
339          
340 <        basic_streambuf<_CharT, _Traits>*  internalBuf_; /** internal stream buffer */        
340 >        std::basic_streambuf<_CharT, _Traits>*  internalBuf_; /** internal stream buffer */        
341          bool isRead;                                                                    /** file opened flag */
342   };
343  
344 < typedef basic_ifstrstream<char, char_traits<char>, allocator<char> > ifstrstream;
344 > typedef basic_ifstrstream<char, std::char_traits<char>, std::allocator<char> > ifstrstream;
345   }//namespace oopse
346   #endif //IO_IFSTRSTREAM_HPP

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines