OpenMD 3.2
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 following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48/**
49 * @file ifstrstream.hpp
50 * @author Teng Lin
51 * @date 10/14/2004
52 * @version 1.0
53 */
54
55#ifndef IO_IFSTRSTREAM_HPP
56#define IO_IFSTRSTREAM_HPP
57
58#include <cassert>
59#include <cstring>
60#include <fstream>
61#include <sstream>
62
63namespace OpenMD {
64
65 /**
66 * @class ifstrstream ifstrstream.hpp "io/ifstrstream.hpp"
67 * @brief ifstrstream class provides a stream interface to read data
68 * from files.
69 * <p>In single mode, it falls back to ifstream, as we don't need to
70 * read the whole file into memory. In parallel mode, the primary
71 * node will read the whole file and broadcast it to other secondary
72 * nodes. After broadcasting, every node will fall back to
73 * stringstream.</p>
74 *
75 * @code
76 * const int MAXLEN = 1024;
77 * char buffer[MAXLEN];
78 * ifstrstream in;
79 * in.open("Shapes.frc");
80 * if (in.is_open()) {
81 * in.getline(buffer, MAXLEN);
82 * }
83 * in.close();
84 * @endcode
85 */
86 class ifstrstream : public std::basic_istream<char, std::char_traits<char>> {
87 public:
88 // traits
89 using char_type = char;
90 using int_type = std::char_traits<char>::int_type;
91 using pos_type = std::char_traits<char>::pos_type;
92 using off_type = std::char_traits<char>::off_type;
93 using traits_type = std::char_traits<char>;
94
95 using _Basic_ios = std::basic_ios<char, std::char_traits<char>>;
96 using _Base = std::basic_istream<char, std::char_traits<char>>;
97 using _Buf = std::basic_streambuf<char, std::char_traits<char>>;
98 using _StringBuf = std::basic_stringbuf<char, std::char_traits<char>>;
99 using _FileBuf = std::basic_filebuf<char, std::char_traits<char>>;
100
101 static const int FileNotExists = -1;
102 static const int FileIOError = -2;
103
104 public:
105 /** Constructs an object of class ifstream. */
106 ifstrstream();
107
108 /**
109 * Explicit constructor
110 * @param filename String containing the name of the file to be opened
111 * @param mode Flags describing the requested i/o mode for the
112 * file, default value is ios_base::in
113 * @param checkFilename Flags indicating checking the file name in parallel
114 */
115 explicit ifstrstream(const char* filename,
116 std::ios_base::openmode mode = std::ios_base::in,
117 bool checkFilename = false);
118
119 /**
120 * virtual destructor will close the file(in single mode) and
121 * clear the stream buffer
122 */
123 ~ifstrstream();
124
125 /**
126 * Opens a file and associates a buffer with the specified file to
127 * perform the i/o operations (single mode). The primary node reads
128 * a file and broadcasts its content to the other secondary
129 * nodes. After broadcasting, all nodes fall back to stringstream
130 * (parallel mode).
131 * @param filename String containing the name of the file to be opened
132 * @param mode Flags describing the requested i/o mode for the file
133 * @param checkFilename Flags indicating checking the file name in parallel
134 */
135 void open(const char* filename,
136 std::ios_base::openmode mode = std::ios_base::in,
137 bool checkFilename = false);
138
139 /**
140 * Tests if the stream is currently associated with a valid buffer.
141 * @return true if a file has successfully been opened (single
142 * mode) or the whole file has been read and spread among all of
143 * the processors (parallel mode), otherwise false is returned
144
145 */
146 bool is_open();
147
148 /**
149 * In single mode, closes a file. The stream's file buffer is
150 * released from its association with the currently open file. In
151 * parallel mode, clean up.
152 */
153 void close();
154
155 /**
156 * Gets the stream buffer object associated with the stream
157 * @return A pointer to the stream buffer object (filebuf in
158 * single mode, stringbuf in parallel mode) associated with the
159 * stream.
160 */
161 _Buf* rdbuf();
162
163 private:
164 /**
165 * Internal function used to open the file
166 * @return true if succesfully opens a file (single mode) or gets the file
167 * content (parallel mode) otherwise returns false
168 * @param filename String containing the name of the file to be opened
169 * @param mode Flags describing the requested i/o mode for the file
170 * @param checkFilename Flags indicating checking the file name in parallel
171 * @todo use try - catch syntax to make the program more readable
172 */
173 bool internalOpen(const char* filename, std::ios_base::openmode mode,
174 bool checkFilename);
175
176 _StringBuf internalStringBuf_; /** internal stream buffer */
177 _FileBuf internalFileBuf_; /** internal stream buffer */
178 bool isRead; /** file opened flag */
179 };
180} // namespace OpenMD
181
182#endif
_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.