PSCF v1.5.0
Label.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, David Morse (morse012@um.edu)
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "Label.h"
9#ifdef UTIL_MPI
10#include <util/mpi/MpiSendRecv.h>
11#endif
12
13#include <iomanip>
14
15namespace Util
16{
17
19 bool Label::isClear_ = true;
20 bool Label::isMatched_ = false;
21 std::string Label::buffer_;
22
23 // Static member functions
24
25 /*
26 * Clear static input buffer and set isClear = true (static).
27 */
29 {
30 buffer_.clear();
31 isClear_ = true;
32 }
33
34 /*
35 * Extract string from input stream and store in the buffer without
36 * attempting to match (static).
37 */
38 void Label::read(std::istream& in)
39 {
40 if (isClear_) {
41 if (in.eof()) {
42 Label::isMatched_ = false;
43 } else {
44 if (in.fail()) {
45 UTIL_THROW("istream::fail() before reading Label");
46 }
47 skipws(in);
48 in >> buffer_;
49 if (buffer_.size() == 0) {
50 // If the string is empty
51 Label::isMatched_ = false;
52 } else {
53 isClear_ = false;
54 }
55 }
56 }
57 }
58
59 #ifdef UTIL_MPI
60 /*
61 * Broadcast the buffer from processor 0 and set isClear (static).
62 */
63 void Label::bcast(MPI::Intracomm& communicator)
64 {
65 Util::bcast<std::string>(communicator, buffer_, 0);
66 isClear_ = buffer_.empty();
67 }
68 #endif
69
70 // Non-static member functions
71
72 /*
73 * Constructor.
74 */
76 : string_(),
77 isRequired_(isRequired)
78 {}
79
80 /*
81 * Constructor.
82 */
83 Label::Label(std::string string, bool isRequired)
84 : string_(string),
85 isRequired_(isRequired)
86 {}
87
88 /*
89 * Constructor.
90 */
91 Label::Label(const char* string, bool isRequired)
92 : string_(string),
93 isRequired_(isRequired)
94 {}
95
96 /*
97 * Copy constructor.
98 */
99 Label::Label(const Label& other)
100 : string_(other.string_),
101 isRequired_(other.isRequired_)
102 {}
103
104 /*
105 * Destructor.
106 */
109
110 /*
111 * Set the label string.
112 */
113 void Label::setString(std::string string)
114 { string_ = string; }
115
116 /*
117 * Set the isRequired flag.
118 */
120 { isRequired_ = isRequired; }
121
122 /*
123 * Return label string.
124 */
125 std::string Label::string() const
126 { return string_; }
127
128 /*
129 * Attempt to match label and return true iff matched.
130 */
131 bool Label::match(std::istream& in)
132 {
133 in >> *this;
134 return isMatched_;
135 }
136
137 /*
138 * Extract a label from an input stream.
139 */
140 std::istream& operator >> (std::istream& in, Label label)
141 {
142 UTIL_CHECK(label.string_.size() > 0);
143
144 // End-of-file means that the label is absent only if the buffer
145 // is clear. A string already in the buffer, such as a closing
146 // bracket that is the last character of the file and that was
147 // read by a failed attempt to match an optional label, must
148 // still be compared to the expected label.
149 if (Label::isClear_ && in.eof()) {
150 if (label.isRequired()) {
151 Log::file() << "End-of-file before reading required Label"
152 << std::endl;
153 Log::file() << "Expected: " << label.string_ << std::endl;
154 UTIL_THROW("EOF before reading required label");
155 } else {
156 Label::isMatched_ = false;
157 return in;
158 }
159 } else {
160 Label::read(in);
161
162 if (label.isRequired() && label.isClear()) {
163 Log::file() << "Empty required label" << std::endl;
164 Log::file() << "Expected: " << label.string_ << std::endl;
165 UTIL_THROW("Empty required label after read");
166 }
167 }
168
169 // Attempt to match input string to expected string.
170 if (label.buffer_ == label.string_) {
171 Label::clear();
172 Label::isMatched_ = true;
173 } else {
174 Label::isMatched_ = false;
175 if (label.isRequired_) {
176 Log::file() << "Error reading required label" << std::endl;
177 Log::file() << "Expected: " << label.string_ << std::endl;
178 Log::file() << "Scanned: " << label.buffer_ << std::endl;
179 UTIL_THROW("Incorrect required label");
180 }
181 };
182 return in;
183 }
184
185 /*
186 * Insert a Label into an output stream.
187 */
188 std::ostream& operator << (std::ostream& out, Label label)
189 {
190 out << std::left << std::setw(Label::LabelWidth)
191 << label.string_;
192 out << std::right;
193 return out;
194 }
195
196}
Label(bool isRequired=true)
Constructor.
Definition Label.cpp:75
std::string string() const
Return label string.
Definition Label.cpp:125
static const int LabelWidth
Width of label field in file output format.
Definition Label.h:80
static void read(std::istream &in)
Read a string without checking its value.
Definition Label.cpp:38
static void clear()
Clear the input buffer.
Definition Label.cpp:28
virtual ~Label()
Destructor.
Definition Label.cpp:107
bool match(std::istream &in)
Read and attempt to match next word in an input stream.
Definition Label.cpp:131
void setString(std::string string)
Set the label string.
Definition Label.cpp:113
void setIsRequired(bool isRequired)
Set the isRequired boolean flag.
Definition Label.cpp:119
bool isRequired() const
Is this the label for a required component?
Definition Label.h:273
static bool isClear()
Is the input buffer clear?
Definition Label.h:279
static std::ostream & file()
Get log ostream by reference.
Definition Log.cpp:59
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
Utility classes for scientific computation.
std::istream & operator>>(std::istream &in, Pair< Data > &pair)
Input a Pair from an istream.
Definition Pair.h:44
std::ostream & operator<<(std::ostream &out, const Pair< Data > &pair)
Output a Pair to an ostream, without line breaks.
Definition Pair.h:57