ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/io/ifstrstream.hpp
Revision: 1962
Committed: Wed Jan 15 22:26:18 2014 UTC (11 years, 3 months ago) by gezelter
File size: 6898 byte(s)
Log Message:
Some potential bug fixes.

File Contents

# User Rev Content
1 gezelter 1627 /*
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 gezelter 1879 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 gezelter 1665 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40     * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 gezelter 1627 */
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 gezelter 1962 * @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 gezelter 1627 *
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 gezelter 1962 class ifstrstream : public std::basic_istream<char, std::char_traits<char> > {
82 gezelter 1627 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 gezelter 1962 typedef std::char_traits<char> traits_type;
89    
90 gezelter 1627 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 gezelter 1879 * @param filename String containing the name of the file to be opened
107 gezelter 1962 * @param mode Flags describing the requested i/o mode for the
108     * file, default value is ios_base::in
109 gezelter 1879 * @param checkFilename Flags indicating checking the file name in parallel
110 gezelter 1627 */
111     explicit ifstrstream(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false);
112    
113     /**
114 gezelter 1962 * virtual destructor will close the file(in single mode) and
115     * clear the stream buffer
116 gezelter 1627 */
117     ~ifstrstream();
118    
119     /**
120 gezelter 1962 * 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 gezelter 1879 * @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 gezelter 1627 */
129     void open(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false);
130    
131 gezelter 1962 /**
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 gezelter 1627
137     */
138     bool is_open ( );
139    
140     /**
141 gezelter 1962 * 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 gezelter 1627 */
145     void close();
146    
147     /**
148     * Gets the stream buffer object associated with the stream
149 gezelter 1962 * @return A pointer to the stream buffer object (filebuf in
150     * single mode, stringbuf in parallel mode) associated with the
151     * stream.
152 gezelter 1627 */
153     _Buf* rdbuf();
154    
155     private:
156    
157     /**
158     * Internal function used to open the file
159 gezelter 1962 * @return true if succesfully opens a file (single mode) or gets the file content (parallel mode)
160     * otherwise returns false
161 gezelter 1879 * @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 gezelter 1627 * @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 gezelter 1962
168     _StringBuf internalStringBuf_; /** internal stream buffer */
169     _FileBuf internalFileBuf_; /** internal stream buffer */
170     bool isRead; /** file opened flag */
171     };
172 gezelter 1627 }
173     #endif

Properties

Name Value
svn:eol-style native