PSCF v1.1
XmlAttribute.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 "XmlAttribute.h"
9#include <util/misc/ioUtil.h>
10
11namespace Util
12{
13
15 : XmlBase(),
16 label_(""),
17 value_()
18 {
19 value_.str("");
20 value_.clear();
21 }
22
24 {}
25
26 bool XmlAttribute::match(const std::string& line, int begin)
27 {
28 // Clear members
29 label_ = "";
30 value_.str("");
31 value_.clear();
32
33 setString(line, begin);
34
35 // Skip white space
36 if (isEnd()) return false;
37 skip();
38 if (isEnd()) return false;
39
40 // Read label
41 int beginLabel, endLabel;
42 if (c() == '=') return false;
43 if (c() == '>') return false;
44 beginLabel = cursor();
45 while (c() != '=') {
46 next();
47 if (isEnd()) return false;
48 if (c() == '>') return false;
49 }
50 endLabel = cursor();
51
52 // Advance past '=' and white space
53 next();
54 skip();
55 if (isEnd()) return false;
56
57 // Read value
58 int beginValue, endValue;
59 char quote;
60 if (c() == '\'' || c() == '\"') {
61 quote = c();
62 } else {
63 return false;
64 }
65 next();
66 if (isEnd()) return false;
67 beginValue = cursor();
68 while (c() != quote) {
69 next();
70 if (isEnd()) return false;
71 }
72 endValue = cursor();
73 next();
74
75 label_ = string().substr(beginLabel, endLabel - beginLabel);
76 rStrip(label_);
77 value_.str(string().substr(beginValue, endValue - beginValue));
78
79 return true;
80 }
81
83 {
84 bool status = match(parser.string(), parser.cursor());
85 if (status) {
86 parser.setCursor(cursor());
87 }
88 return status;
89 }
90
91}
XmlAttribute()
Constructor.
virtual ~XmlAttribute()
Destructor.
bool match(const std::string &string, int begin)
Return true if an attribute is found, false otherwise.
Base class for classes that parse XML markup tags.
Definition: XmlBase.h:23
void skip()
Skip leading white space, if any.
Definition: XmlBase.h:88
const std::string & string() const
Return the associated string.
Definition: XmlBase.h:118
void setCursor(int cursor)
Set cursor.
Definition: XmlBase.cpp:42
void next()
Advance to the next character.
Definition: XmlBase.h:104
int cursor() const
Return the index of the current character.
Definition: XmlBase.h:124
void setString(const std::string &string, int cursor=0)
Initialize string and cursor.
Definition: XmlBase.cpp:32
int c() const
Return the current character.
Definition: XmlBase.h:130
bool isEnd() const
Has the cursor reached the end of the string?
Definition: XmlBase.h:136
int rStrip(std::string &str)
Strip trailing whitespace from a string.
Definition: ioUtil.cpp:18
Utility classes for scientific computation.
Definition: accumulators.mod:1