OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
ifstrstream.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45/**
46 * @file ifstrstream.hpp
47 * @author Teng Lin
48 * @date 10/14/2004
49 * @version 1.0
50 */
51
52#ifndef IO_IFSTRSTREAM_HPP
53#define IO_IFSTRSTREAM_HPP
54
55#include <cassert>
56#include <cstring>
57#include <fstream>
58#include <sstream>
59
60namespace OpenMD {
61
62 /**
63 * @class ifstrstream ifstrstream.hpp "io/ifstrstream.hpp"
64 * @brief ifstrstream class provides a stream interface to read data
65 * from files.
66 * <p>In single mode, it falls back to ifstream, as we don't need to
67 * read the whole file into memory. In parallel mode, the primary
68 * node will read the whole file and broadcast it to other secondary
69 * nodes. After broadcasting, every node will fall back to
70 * stringstream.</p>
71 *
72 * @code
73 * const int MAXLEN = 1024;
74 * char buffer[MAXLEN];
75 * ifstrstream in;
76 * in.open("Shapes.frc");
77 * if (in.is_open()) {
78 * in.getline(buffer, MAXLEN);
79 * }
80 * in.close();
81 * @endcode
82 */
83 class ifstrstream : public std::basic_istream<char, std::char_traits<char>> {
84 public:
85 // traits
86 using char_type = char;
87 using int_type = std::char_traits<char>::int_type;
88 using pos_type = std::char_traits<char>::pos_type;
89 using off_type = std::char_traits<char>::off_type;
90 using traits_type = std::char_traits<char>;
91
92 using _Basic_ios = std::basic_ios<char, std::char_traits<char>>;
93 using _Base = std::basic_istream<char, std::char_traits<char>>;
94 using _Buf = std::basic_streambuf<char, std::char_traits<char>>;
95 using _StringBuf = std::basic_stringbuf<char, std::char_traits<char>>;
96 using _FileBuf = std::basic_filebuf<char, std::char_traits<char>>;
97
98 static const int FileNotExists = -1;
99 static const int FileIOError = -2;
100
101 public:
102 /** Constructs an object of class ifstream. */
103 ifstrstream();
104
105 /**
106 * Explicit constructor
107 * @param filename String containing the name of the file to be opened
108 * @param mode Flags describing the requested i/o mode for the
109 * file, default value is ios_base::in
110 * @param checkFilename Flags indicating checking the file name in parallel
111 */
112 explicit ifstrstream(const char* filename,
113 std::ios_base::openmode mode = std::ios_base::in,
114 bool checkFilename = false);
115
116 /**
117 * virtual destructor will close the file(in single mode) and
118 * clear the stream buffer
119 */
120 ~ifstrstream();
121
122 /**
123 * Opens a file and associates a buffer with the specified file to
124 * perform the i/o operations (single mode). The primary node reads
125 * a file and broadcasts its content to the other secondary
126 * nodes. After broadcasting, all nodes fall back to stringstream
127 * (parallel mode).
128 * @param filename String containing the name of the file to be opened
129 * @param mode Flags describing the requested i/o mode for the file
130 * @param checkFilename Flags indicating checking the file name in parallel
131 */
132 void open(const char* filename,
133 std::ios_base::openmode mode = std::ios_base::in,
134 bool checkFilename = false);
135
136 /**
137 * Tests if the stream is currently associated with a valid buffer.
138 * @return true if a file has successfully been opened (single
139 * mode) or the whole file has been read and spread among all of
140 * the processors (parallel mode), otherwise false is returned
141
142 */
143 bool is_open();
144
145 /**
146 * In single mode, closes a file. The stream's file buffer is
147 * released from its association with the currently open file. In
148 * parallel mode, clean up.
149 */
150 void close();
151
152 /**
153 * Gets the stream buffer object associated with the stream
154 * @return A pointer to the stream buffer object (filebuf in
155 * single mode, stringbuf in parallel mode) associated with the
156 * stream.
157 */
158 _Buf* rdbuf();
159
160 private:
161 /**
162 * Internal function used to open the file
163 * @return true if succesfully opens a file (single mode) or gets the file
164 * content (parallel mode) otherwise returns false
165 * @param filename String containing the name of the file to be opened
166 * @param mode Flags describing the requested i/o mode for the file
167 * @param checkFilename Flags indicating checking the file name in parallel
168 * @todo use try - catch syntax to make the program more readable
169 */
170 bool internalOpen(const char* filename, std::ios_base::openmode mode,
171 bool checkFilename);
172
173 _StringBuf internalStringBuf_; /** internal stream buffer */
174 _FileBuf internalFileBuf_; /** internal stream buffer */
175 bool isRead; /** file opened flag */
176 };
177} // namespace OpenMD
178
179#endif
ifstrstream class provides a stream interface to read data from files.
_Buf * rdbuf()
Gets the stream buffer object associated with the stream.
void open(const char *filename, std::ios_base::openmode mode=std::ios_base::in, bool checkFilename=false)
Opens a file and associates a buffer with the specified file to perform the i/o operations (single mo...
~ifstrstream()
virtual destructor will close the file(in single mode) and clear the stream buffer
bool is_open()
Tests if the stream is currently associated with a valid buffer.
ifstrstream()
Constructs an object of class ifstream.
void close()
In single mode, closes a file.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.