| 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. Redistributions of source code must retain the above copyright | 
| 10 | *    notice, this list of conditions and the following disclaimer. | 
| 11 | * | 
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 | *    notice, this list of conditions and the following disclaimer in the | 
| 14 | *    documentation and/or other materials provided with the | 
| 15 | *    distribution. | 
| 16 | * | 
| 17 | * This software is provided "AS IS," without a warranty of any | 
| 18 | * kind. All express or implied conditions, representations and | 
| 19 | * warranties, including any implied warranty of merchantability, | 
| 20 | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 | * be liable for any damages suffered by licensee as a result of | 
| 23 | * using, modifying or distributing the software or its | 
| 24 | * derivatives. In no event will the University of Notre Dame or its | 
| 25 | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 | * direct, indirect, special, consequential, incidental or punitive | 
| 27 | * damages, however caused and regardless of the theory of liability, | 
| 28 | * arising out of the use of or inability to use software, even if the | 
| 29 | * University of Notre Dame has been advised of the possibility of | 
| 30 | * such damages. | 
| 31 | * | 
| 32 | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 | * research, please cite the appropriate papers when you publish your | 
| 34 | * work.  Good starting points are: | 
| 35 | * | 
| 36 | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). | 
| 39 | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 | */ | 
| 42 |  | 
| 43 | /** | 
| 44 | * @file ifstrstream.hpp | 
| 45 | * @author Teng Lin | 
| 46 | * @date 10/14/2004 | 
| 47 | * @version 1.0 | 
| 48 | */ | 
| 49 |  | 
| 50 | #ifndef IO_IFSTRSTREAM_HPP | 
| 51 | #define IO_IFSTRSTREAM_HPP | 
| 52 |  | 
| 53 | #include <cassert> | 
| 54 | #include <cstring> | 
| 55 | #include <fstream> | 
| 56 | #include <sstream> | 
| 57 |  | 
| 58 | namespace OpenMD { | 
| 59 |  | 
| 60 | /** | 
| 61 | * @class ifstrstream ifstrstream.hpp "io/ifstrstream.hpp" | 
| 62 | * @brief ifstrstream class provides a stream interface to read data | 
| 63 | * from files. | 
| 64 | * <p>In single mode, it falls back to ifstream, as we don't need to | 
| 65 | * read the whole file into memory.  In parallel mode, the master | 
| 66 | * node will read the whole file and broadcast it to other slave | 
| 67 | * nodes.  After broadcasting, every node will fall back to | 
| 68 | * stringstream.</p> | 
| 69 | * | 
| 70 | * @code | 
| 71 | *       const int MAXLEN = 1024; | 
| 72 | *       char buffer[MAXLEN]; | 
| 73 | *       ifstrstream in; | 
| 74 | *       in.open("Shapes.frc"); | 
| 75 | *       if (in.is_open()) { | 
| 76 | *           in.getline(buffer, MAXLEN); | 
| 77 | *       } | 
| 78 | *       in.close(); | 
| 79 | * @endcode | 
| 80 | */ | 
| 81 | class ifstrstream : public std::basic_istream<char, std::char_traits<char> > { | 
| 82 | public: | 
| 83 | //traits | 
| 84 | typedef char                     char_type; | 
| 85 | typedef std::char_traits<char>::int_type int_type; | 
| 86 | typedef std::char_traits<char>::pos_type pos_type; | 
| 87 | typedef std::char_traits<char>::off_type off_type; | 
| 88 | typedef std::char_traits<char> traits_type; | 
| 89 |  | 
| 90 | typedef std::basic_ios<char, std::char_traits<char> >       _Basic_ios; | 
| 91 | typedef std::basic_istream<char, std::char_traits<char> >   _Base; | 
| 92 | typedef std::basic_streambuf<char, std::char_traits<char> > _Buf; | 
| 93 | typedef std::basic_stringbuf<char, std::char_traits<char> > _StringBuf; | 
| 94 | typedef std::basic_filebuf<char, std::char_traits<char> >   _FileBuf; | 
| 95 |  | 
| 96 | static const int FileNotExists = -1; | 
| 97 | static const int FileIOError = -2; | 
| 98 |  | 
| 99 | public: | 
| 100 |  | 
| 101 | /**  Constructs an object of class ifstream.  */ | 
| 102 | ifstrstream(); | 
| 103 |  | 
| 104 | /** | 
| 105 | * Explicit constructor | 
| 106 | * @param filename String containing the name of the file to be opened | 
| 107 | * @param mode Flags describing the requested i/o mode for the | 
| 108 | * file, default value is ios_base::in | 
| 109 | * @param checkFilename Flags indicating checking the file name in parallel | 
| 110 | */ | 
| 111 | explicit ifstrstream(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false); | 
| 112 |  | 
| 113 | /** | 
| 114 | * virtual destructor will close the file(in single mode) and | 
| 115 | * clear the stream buffer | 
| 116 | */ | 
| 117 | ~ifstrstream(); | 
| 118 |  | 
| 119 | /** | 
| 120 | * Opens a file and associates a buffer with the specified file to | 
| 121 | * perform the i/o operations (single mode). The master node reads | 
| 122 | * a file and broadcasts its content to the other slave | 
| 123 | * nodes. After broadcasting, all nodes fall back to stringstream | 
| 124 | * (parallel mode). | 
| 125 | * @param filename String containing the name of the file to be opened | 
| 126 | * @param mode Flags describing the requested i/o mode for the file | 
| 127 | * @param checkFilename Flags indicating checking the file name in parallel | 
| 128 | */ | 
| 129 | void open(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false); | 
| 130 |  | 
| 131 | /** | 
| 132 | * Tests if the stream is currently associated with a valid buffer. | 
| 133 | * @return true if a file has successfully been opened (single | 
| 134 | * mode) or the whole file has been read and spread among all of | 
| 135 | * the processors (parallel mode), otherwise false is returned | 
| 136 |  | 
| 137 | */ | 
| 138 | bool is_open ( ); | 
| 139 |  | 
| 140 | /** | 
| 141 | * In single mode, closes a file. The stream's file buffer is | 
| 142 | * released from its association with the currently open file. In | 
| 143 | * parallel mode, clean up. | 
| 144 | */ | 
| 145 | void close(); | 
| 146 |  | 
| 147 | /** | 
| 148 | * Gets the stream buffer object associated with the stream | 
| 149 | * @return A pointer to the stream buffer object (filebuf in | 
| 150 | * single mode, stringbuf in parallel mode) associated with the | 
| 151 | * stream. | 
| 152 | */ | 
| 153 | _Buf* rdbuf(); | 
| 154 |  | 
| 155 | private: | 
| 156 |  | 
| 157 | /** | 
| 158 | * Internal function used to open the file | 
| 159 | * @return true if succesfully opens a file (single mode) or gets the file content (parallel mode) | 
| 160 | * otherwise returns false | 
| 161 | * @param filename String containing the name of the file to be opened | 
| 162 | * @param mode Flags describing the requested i/o mode for the file | 
| 163 | * @param checkFilename Flags indicating checking the file name in parallel | 
| 164 | * @todo use try - catch syntax to make the program more readable | 
| 165 | */ | 
| 166 | bool internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename); | 
| 167 |  | 
| 168 | _StringBuf   internalStringBuf_; /** internal stream buffer */ | 
| 169 | _FileBuf     internalFileBuf_;    /** internal stream buffer */ | 
| 170 | bool isRead;        /** file opened flag */ | 
| 171 | }; | 
| 172 | } | 
| 173 | #endif |