| 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 from files. |
| 63 |
* <p>In single mode, it falls back to ifstream. Don't need to read the whole file into memory. |
| 64 |
* In parallel mode, the master node will read the whole file and brocast it to other slave nodes. |
| 65 |
* After brocasting, every node will fall back to stringstream.</p> |
| 66 |
* |
| 67 |
* @code |
| 68 |
* const int MAXLEN = 1024; |
| 69 |
* char buffer[MAXLEN]; |
| 70 |
* ifstrstream in; |
| 71 |
* in.open("Shapes.frc"); |
| 72 |
* if (in.is_open()) { |
| 73 |
* in.getline(buffer, MAXLEN); |
| 74 |
* } |
| 75 |
* in.close(); |
| 76 |
* @endcode |
| 77 |
*/ |
| 78 |
class ifstrstream : public std::basic_istream<char, std::char_traits<char> > { |
| 79 |
public: |
| 80 |
//traits |
| 81 |
typedef char char_type; |
| 82 |
typedef std::char_traits<char>::int_type int_type; |
| 83 |
typedef std::char_traits<char>::pos_type pos_type; |
| 84 |
typedef std::char_traits<char>::off_type off_type; |
| 85 |
typedef std::char_traits<char> traits_type; |
| 86 |
|
| 87 |
typedef std::basic_ios<char, std::char_traits<char> > _Basic_ios; |
| 88 |
typedef std::basic_istream<char, std::char_traits<char> > _Base; |
| 89 |
typedef std::basic_streambuf<char, std::char_traits<char> > _Buf; |
| 90 |
typedef std::basic_stringbuf<char, std::char_traits<char> > _StringBuf; |
| 91 |
typedef std::basic_filebuf<char, std::char_traits<char> > _FileBuf; |
| 92 |
|
| 93 |
|
| 94 |
static const int FileNotExists = -1; |
| 95 |
static const int FileIOError = -2; |
| 96 |
|
| 97 |
public: |
| 98 |
|
| 99 |
/** Constructs an object of class ifstream. */ |
| 100 |
ifstrstream(); |
| 101 |
|
| 102 |
/** |
| 103 |
* Explicit constructor |
| 104 |
* @param filename String containing the name of the file to be opened |
| 105 |
* @param mode Flags describing the requested i/o mode for the file, default value is ios_base::in |
| 106 |
* @param checkFilename Flags indicating checking the file name in parallel |
| 107 |
*/ |
| 108 |
explicit ifstrstream(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false); |
| 109 |
|
| 110 |
/** |
| 111 |
* virtual destructor will close the file(in single mode) and clear the stream buffer |
| 112 |
*/ |
| 113 |
~ifstrstream(); |
| 114 |
|
| 115 |
/** |
| 116 |
* Opens a file and associats a buffer with the specified file to perform the i/o operations |
| 117 |
* (single mode). Master reads a file and brocasts its content to the other slave nodes. After |
| 118 |
* brocasting, every nodes fall back to stringstream (parallel mode). |
| 119 |
* @param filename String containing the name of the file to be opened |
| 120 |
* @param mode Flags describing the requested i/o mode for the file |
| 121 |
* @param checkFilename Flags indicating checking the file name in parallel |
| 122 |
*/ |
| 123 |
void open(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false); |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* Tests if the stream is currently associated with a valid buffer. |
| 128 |
* @return true if a file has successfully been opened (single mode) or the whole file is read |
| 129 |
* and spreaded among all of the processors (parallel mode), otherwise false is returned |
| 130 |
*/ |
| 131 |
bool is_open ( ); |
| 132 |
|
| 133 |
/** |
| 134 |
* In single mode, closes a file. The stream's file buffer is released from its association with |
| 135 |
* the currently open file. In parallel mode, clean the |
| 136 |
*/ |
| 137 |
void close(); |
| 138 |
|
| 139 |
/** |
| 140 |
* Gets the stream buffer object associated with the stream |
| 141 |
* @return A pointer to the stream buffer object(filebuf in single mode, |
| 142 |
* stringbuf in parallel mode) associated with the stream. |
| 143 |
*/ |
| 144 |
_Buf* rdbuf(); |
| 145 |
|
| 146 |
private: |
| 147 |
|
| 148 |
/** |
| 149 |
* Internal function used to open the file |
| 150 |
* @return true if succesfully opens a file (single mode) or gets the file content (parallel mode) |
| 151 |
* otherwise return false |
| 152 |
* @param filename String containing the name of the file to be opened |
| 153 |
* @param mode Flags describing the requested i/o mode for the file |
| 154 |
* @param checkFilename Flags indicating checking the file name in parallel |
| 155 |
* @todo use try - catch syntax to make the program more readable |
| 156 |
*/ |
| 157 |
bool internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename); |
| 158 |
|
| 159 |
_StringBuf internalStringBuf_; /** internal stream buffer */ |
| 160 |
_FileBuf internalFileBuf_; /** internal stream buffer */ |
| 161 |
bool isRead; /** file opened flag */ |
| 162 |
}; |
| 163 |
|
| 164 |
} |
| 165 |
#endif |