Simpatico  v1.10
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 
12 namespace Util
13 {
14 
16  bool Label::isClear_ = true;
17  bool Label::isMatched_ = false;
18  std::string Label::input_;
19 
20  // Static member functions
21 
22  /*
23  * Clear static input buffer (static member function).
24  *
25  * Sets isClear = true and isMatched = false.
26  */
27  void Label::clear()
28  {
29  input_.clear();
30  isClear_ = true;
31  isMatched_ = false;
32  }
33 
34  /*
35  * Is the input buffer clear? (static member function).
36  */
38  { return isClear_; }
39 
40  // Non-static member functions
41 
42  /*
43  * Constructor.
44  */
46  : isRequired_(isRequired),
47  string_()
48  {}
49 
50  /*
51  * Constructor.
52  */
53  Label::Label(std::string string, bool isRequired)
54  : isRequired_(isRequired),
55  string_(string)
56  {}
57 
58  /*
59  * Constructor.
60  */
61  Label::Label(const char* string, bool isRequired)
62  : isRequired_(isRequired),
63  string_(string)
64  {}
65 
66  /*
67  * Copy constructor.
68  */
69  Label::Label(const Label& other)
70  : isRequired_(other.isRequired_),
71  string_(other.string_)
72  {}
73 
74  /*
75  * Destructor.
76  */
78  {}
79 
80  /*
81  * Set the label string.
82  */
83  void Label::setString(std::string string)
84  { string_ = string; }
85 
86  /*
87  * Return label string.
88  */
89  std::string Label::string() const
90  { return string_; }
91 
92  /*
93  * Attempt to match label and return true iff matched.
94  */
95  bool Label::match(std::istream& in)
96  {
97  in >> *this;
98  return isMatched_;
99  }
100 
101  /*
102  * Extract a label from an input stream.
103  */
104  std::istream& operator >> (std::istream& in, Label label)
105  {
106  UTIL_CHECK(label.string_.size() > 0);
107 
108  // If previous input value matched, try to read a new one.
109  if (label.isClear_) {
110  if (!in.eof()) {
111  if (in.fail()) {
112  UTIL_THROW("istream::fail() before reading Label");
113  }
114  skipws(in);
115  in >> label.input_;
116  if (label.input_.size() != 0) {
117  label.isClear_ = false;
118  } else {
119  if (label.isRequired()) {
120  Log::file() << "Empty required label" << std::endl;
121  Log::file() << "Expected: " << label.string_ << std::endl;
122  UTIL_THROW("Empty required label after read");
123  }
124  }
125  } else { // if is eof
126  if (label.isRequired()) {
127  Log::file() << "End-of-file before reading required Label"
128  << std::endl;
129  Log::file() << "Expected: " << label.string_ << std::endl;
130  UTIL_THROW("EOF before reading required label");
131  } else {
132  Label::input_.clear();
133  Label::isClear_ = true;
134  Label::isMatched_ = false;
135  return in;
136  }
137  }
138  }
139  if (label.input_ == label.string_) {
140  Label::input_.clear();;
141  Label::isClear_ = true;
142  Label::isMatched_ = true;
143  } else {
144  Label::isMatched_ = false;
145  if (label.isRequired_) {
146  Log::file() << "Error reading label" << std::endl;
147  Log::file() << "Expected: " << label.string_ << std::endl;
148  Log::file() << "Scanned: " << label.input_ << std::endl;
149  UTIL_THROW("Incorrect label");
150  }
151  };
152  return in;
153  }
154 
155  /*
156  * Insert a Label into an output stream.
157  */
158  std::ostream& operator << (std::ostream& out, Label label)
159  {
160  out << std::left << std::setw(Label::LabelWidth)
161  << label.string_;
162  out << std::right;
163  return out;
164  }
165 
166 }
static const int LabelWidth
Width of label field in file output format.
Definition: Label.h:44
bool isRequired() const
Is this the label for a required component?
Definition: Label.h:197
static bool isClear()
Is the input buffer clear?
Definition: Label.cpp:37
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
std::string string() const
Return label string.
Definition: Label.cpp:89
virtual ~Label()
Destructor.
Definition: Label.cpp:77
Utility classes for scientific computation.
Definition: accumulators.mod:1
friend std::ostream & operator<<(std::ostream &out, Label label)
Inserter for Label.
Definition: Label.cpp:158
void setString(std::string string)
Set the label string.
Definition: Label.cpp:83
A label string in a file format.
Definition: Label.h:36
static std::ostream & file()
Get log ostream by reference.
Definition: Log.cpp:57
bool match(std::istream &in)
Read and attempt to match next word in an input stream.
Definition: Label.cpp:95
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
friend std::istream & operator>>(std::istream &in, Label label)
Extractor for Label.
Definition: Label.cpp:104
Label(bool isRequired=true)
Constructor.
Definition: Label.cpp:45
static void clear()
Reset buffer and flags to initial state.
Definition: Label.cpp:27