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