PSCF v1.1
Label.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "Label.h"
9
10#include <iomanip>
11
12namespace Util
13{
14
16 bool Label::isClear_ = true;
17 bool Label::isMatched_ = false;
18 std::string Label::buffer_;
19
20 // Static member functions
21
22 /*
23 * Clear static input buffer and set isClear = true (static).
24 */
26 {
27 buffer_.clear();
28 isClear_ = true;
29 }
30
31 /*
32 * Extract string from input stream and store in the buffer without
33 * attempting to match (static).
34 */
35 void Label::read(std::istream& in)
36 {
37 if (isClear_) {
38 if (in.eof()) {
39 Label::isMatched_ = false;
40 } else {
41 if (in.fail()) {
42 UTIL_THROW("istream::fail() before reading Label");
43 }
44 skipws(in);
45 in >> buffer_;
46 if (buffer_.size() == 0) {
47 // If the string is empty
48 Label::isMatched_ = false;
49 } else {
50 isClear_ = false;
51 }
52 }
53 }
54 }
55
56 // Non-static member functions
57
58 /*
59 * Constructor.
60 */
61 Label::Label(bool isRequired)
62 : string_(),
63 isRequired_(isRequired)
64 {}
65
66 /*
67 * Constructor.
68 */
69 Label::Label(std::string string, bool isRequired)
70 : string_(string),
71 isRequired_(isRequired)
72 {}
73
74 /*
75 * Constructor.
76 */
77 Label::Label(const char* string, bool isRequired)
78 : string_(string),
79 isRequired_(isRequired)
80 {}
81
82 /*
83 * Copy constructor.
84 */
85 Label::Label(const Label& other)
86 : string_(other.string_),
87 isRequired_(other.isRequired_)
88 {}
89
90 /*
91 * Destructor.
92 */
94 {}
95
96 /*
97 * Set the label string.
98 */
99 void Label::setString(std::string string)
100 { string_ = string; }
101
102 /*
103 * Set the isRequired flag.
104 */
105 void Label::setIsRequired(bool isRequired)
106 { isRequired_ = isRequired; }
107
108 /*
109 * Return label string.
110 */
111 std::string Label::string() const
112 { return string_; }
113
114 /*
115 * Attempt to match label and return true iff matched.
116 */
117 bool Label::match(std::istream& in)
118 {
119 in >> *this;
120 return isMatched_;
121 }
122
123 /*
124 * Extract a label from an input stream.
125 */
126 std::istream& operator >> (std::istream& in, Label label)
127 {
128 UTIL_CHECK(label.string_.size() > 0);
129
130 if (in.eof()) {
131 if (label.isRequired()) {
132 Log::file() << "End-of-file before reading required Label"
133 << std::endl;
134 Log::file() << "Expected: " << label.string_ << std::endl;
135 UTIL_THROW("EOF before reading required label");
136 } else {
137 Label::isMatched_ = false;
138 return in;
139 }
140 } else {
141 Label::read(in);
142
143 if (label.isRequired() && label.isClear()) {
144 Log::file() << "Empty required label" << std::endl;
145 Log::file() << "Expected: " << label.string_ << std::endl;
146 UTIL_THROW("Empty required label after read");
147 }
148 }
149
150 // Attempt to match input string to expected string.
151 if (label.buffer_ == label.string_) {
152 Label::clear();
153 Label::isMatched_ = true;
154 } else {
155 Label::isMatched_ = false;
156 if (label.isRequired_) {
157 Log::file() << "Error reading required label" << std::endl;
158 Log::file() << "Expected: " << label.string_ << std::endl;
159 Log::file() << "Scanned: " << label.buffer_ << std::endl;
160 UTIL_THROW("Incorrect required label");
161 }
162 };
163 return in;
164 }
165
166 /*
167 * Insert a Label into an output stream.
168 */
169 std::ostream& operator << (std::ostream& out, Label label)
170 {
171 out << std::left << std::setw(Label::LabelWidth)
172 << label.string_;
173 out << std::right;
174 return out;
175 }
176
177}
A label string in a file format.
Definition: Label.h:73
Label(bool isRequired=true)
Constructor.
Definition: Label.cpp:61
std::string string() const
Return label string.
Definition: Label.cpp:111
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:35
static void clear()
Clear the input buffer.
Definition: Label.cpp:25
virtual ~Label()
Destructor.
Definition: Label.cpp:93
bool match(std::istream &in)
Read and attempt to match next word in an input stream.
Definition: Label.cpp:117
void setString(std::string string)
Set the label string.
Definition: Label.cpp:99
void setIsRequired(bool isRequired)
Set the isRequired boolean flag.
Definition: Label.cpp:105
bool isRequired() const
Is this the label for a required component?
Definition: Label.h:261
static bool isClear()
Is the input buffer clear?
Definition: Label.h:267
static std::ostream & file()
Get log ostream by reference.
Definition: Log.cpp:57
#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:51
Utility classes for scientific computation.
Definition: accumulators.mod:1
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