QMCPACK
XmlStream Class Reference
+ Collaboration diagram for XmlStream:

Public Member Functions

 XmlStream (std::istream *is)
 
std::string getTag (const XmlElement &e) const
 
std::string getTag (int i) const
 
std::string getStreamSection (const std::streampos &start, const std::streampos &end) const
 
void findChildElements (int start, std::vector< int > &childIndices, int &podIndex) const
 
void listAll () const
 

Public Attributes

std::vector< XmlElementelements
 

Private Member Functions

int addNextTag ()
 
std::string getTagName (const std::string &tag, tagType type) const
 
int checkForPOD (const XmlElement &before, const XmlElement &after) const
 
int startComment (long position, long length) const
 
int endComment (long position, long length) const
 

Private Attributes

std::istream * stream_
 

Detailed Description

Definition at line 54 of file XmlRep.h.

Constructor & Destructor Documentation

◆ XmlStream()

XmlStream ( std::istream *  is)

Definition at line 358 of file XmlRep.cpp.

References addNextTag(), checkForPOD(), elements, XmlElement::endLoc, pod, XmlElement::startLoc, and XmlElement::type.

358  : stream_(is)
359 {
360  // on first pass, try to find all of the tags and encode their names
361  while (addNextTag() != -1) {}
362 
363  //now go through and look at space between live tags and see if there is POD there
364  std::vector<XmlElement> tags;
365  elements.swap(tags);
366 
367  elements.push_back(tags[0]);
368  for (int i = 1; i < tags.size(); i++)
369  {
370  if (checkForPOD(tags[i - 1], tags[i]) == 1)
371  {
372  // add POD element
373  XmlElement podElem;
374  podElem.startLoc = tags[i - 1].endLoc;
375  podElem.endLoc = tags[i].startLoc;
376  podElem.type = tagType::pod;
377  elements.push_back(podElem);
378  }
379  elements.push_back(tags[i]);
380  }
381 }
int checkForPOD(const XmlElement &before, const XmlElement &after) const
Definition: XmlRep.cpp:92
std::vector< XmlElement > elements
Definition: XmlRep.h:75
int addNextTag()
Definition: XmlRep.cpp:146
std::streampos endLoc
Definition: XmlRep.h:38
std::istream * stream_
Definition: XmlRep.h:72
std::streampos startLoc
Definition: XmlRep.h:37
tagType type
Definition: XmlRep.h:40

Member Function Documentation

◆ addNextTag()

int addNextTag ( )
private

Definition at line 146 of file XmlRep.cpp.

References closing, XmlElement::endLoc, XmlElement::name, opening, selfClosing, XmlElement::startLoc, and XmlElement::type.

Referenced by XmlStream().

147 {
148  std::streampos start;
149  std::streampos end;
150  int twoprev = 0;
151  int prev = 0;
152  int current = 0;
153 
154  int isProcessingInstruction = 0;
155  int isComment = 0;
156  int isClosingTag = 0;
157  int isSelfClosing = 0;
158 
159  int openCaretFound = 0;
160  int closeCaretFound = 0;
161 
162  int numSingleQuotes = 0;
163  int numDoubleQuotes = 0;
164  while ((current = stream_->get()) && current != EOF && closeCaretFound == 0)
165  {
166  if (current == '<')
167  {
168  if (prev != '\\')
169  {
170  // we found a live start string
171  stream_->unget();
172  start = stream_->tellg();
173  stream_->get();
174  openCaretFound = 1;
175  }
176  int one = stream_->get();
177  int two = stream_->get();
178  int three = stream_->get();
179  if (one == '\?')
180  {
181  isProcessingInstruction = 1;
182  }
183  if (one == '!' && two == '-' && three == '-')
184  {
185  isComment = 1;
186  }
187  if (one == '/')
188  {
189  isClosingTag = 1;
190  }
191  stream_->unget();
192  stream_->unget();
193  stream_->unget();
194  }
195  if (openCaretFound == 1)
196  {
197  if (current == '\'')
198  {
199  numSingleQuotes++;
200  }
201  else if (current == '\"')
202  {
203  numDoubleQuotes++;
204  }
205  else if (current == '>')
206  {
207  // check that we aren't currently in a quoted section
208  if (numSingleQuotes % 2 == 0 && numDoubleQuotes % 2 == 0)
209  {
210  // check that this close caret isn't escaped
211  if (prev != '\\')
212  {
213  if (isComment == 1)
214  {
215  if (prev == '-' && twoprev == '-')
216  {
217  closeCaretFound = 1;
218  end = stream_->tellg();
219  }
220  }
221  else
222  {
223  closeCaretFound = 1;
224  end = stream_->tellg();
225  if (prev == '/')
226  {
227  isSelfClosing = 1;
228  }
229  }
230  }
231  }
232  }
233  }
234  twoprev = prev;
235  prev = current;
236  }
237 
238  if (current == EOF)
239  {
240  stream_->clear();
241  stream_->seekg(0);
242  return -1;
243  }
244 
245  if (isProcessingInstruction == 0 && isComment == 0)
246  {
247  XmlElement elem;
248  elem.startLoc = start;
249  elem.endLoc = end;
250 
251  if (isSelfClosing == 1)
252  {
253  elem.type = tagType::selfClosing;
254  }
255  else if (isClosingTag == 1)
256  {
257  elem.type = tagType::closing;
258  }
259  else
260  {
261  elem.type = tagType::opening;
262  stream_->unget();
263  }
264  elem.name = getTagName(getTag(elem), elem.type);
265  elements.push_back(elem);
266  return 1;
267  }
268  if (isProcessingInstruction == 1 || isComment == 1)
269  {
270  stream_->unget();
271  return 0;
272  }
273  return -1;
274 }
std::vector< XmlElement > elements
Definition: XmlRep.h:75
std::string name
Definition: XmlRep.h:39
std::streampos endLoc
Definition: XmlRep.h:38
std::istream * stream_
Definition: XmlRep.h:72
std::streampos startLoc
Definition: XmlRep.h:37
tagType type
Definition: XmlRep.h:40
std::string getTagName(const std::string &tag, tagType type) const
Definition: XmlRep.cpp:18
std::string getTag(const XmlElement &e) const
Definition: XmlRep.cpp:384

◆ checkForPOD()

int checkForPOD ( const XmlElement before,
const XmlElement after 
) const
private

Definition at line 92 of file XmlRep.cpp.

References XmlElement::endLoc, and XmlElement::startLoc.

Referenced by XmlStream().

93 {
94  int foundPOD = 0;
95  std::streampos curLoc = stream_->tellg();
96 
97  stream_->seekg(before.endLoc);
98 
99  long length = after.startLoc - before.endLoc;
100  long position = 0;
101  int c;
102  int inComment = 0;
103  while (foundPOD == 0 && position < length)
104  {
105  c = stream_->get();
106  if (!isspace(c))
107  {
108  //if (!isspace(c) && c != '\n') {
109  stream_->unget();
110  // if we're in a comment, check that we are not ending it
111  if (inComment == 1)
112  {
113  if (endComment(position, length) == 1)
114  {
115  // position ourselves after the end comment tag
116  stream_->seekg(3, std::ios_base::cur);
117  position += 3;
118  inComment = 0;
119  }
120  }
121  else
122  {
123  // if we're not in a comment, check that we aren't starting one
124  if (endComment(position, length) == 1)
125  {
126  // position ourselves after the comment tag
127  stream_->seekg(4, std::ios_base::cur);
128  position += 4;
129  inComment = 1;
130  }
131  else
132  {
133  // we've found POD !!
134  foundPOD = 1;
135  }
136  }
137  }
138  position++;
139  }
140 
141  stream_->seekg(curLoc);
142  return foundPOD;
143 }
std::streampos endLoc
Definition: XmlRep.h:38
std::istream * stream_
Definition: XmlRep.h:72
std::streampos startLoc
Definition: XmlRep.h:37
int endComment(long position, long length) const
Definition: XmlRep.cpp:75

◆ endComment()

int endComment ( long  position,
long  length 
) const
private

Definition at line 75 of file XmlRep.cpp.

76 {
77  int isCommentEnd = 0;
78  std::streampos curLoc = stream_->tellg();
79  if ((length - position) > 3)
80  {
81  char buf[3];
82  stream_->read(buf, 3);
83  if (strncmp(buf, "-->", 3) == 0)
84  {
85  isCommentEnd = 1;
86  }
87  }
88  stream_->seekg(curLoc);
89  return isCommentEnd;
90 }
std::istream * stream_
Definition: XmlRep.h:72

◆ findChildElements()

void findChildElements ( int  start,
std::vector< int > &  childIndices,
int &  podIndex 
) const

Definition at line 290 of file XmlRep.cpp.

References closing, opening, pod, and selfClosing.

Referenced by XmlNode::createFromStream().

291 {
292  int index = start + 1;
293  int level = 1;
294 
295  while (index < elements.size() && level > 0)
296  {
297  const tagType tt = elements[index].type;
298  if (tt == tagType::opening)
299  {
300  if (level == 1)
301  {
302  childIndices.push_back(index);
303  }
304  level++;
305  }
306  else if (tt == tagType::closing)
307  {
308  level--;
309  }
310  else if (tt == tagType::selfClosing)
311  {
312  if (level == 1)
313  {
314  childIndices.push_back(index);
315  }
316  }
317  else if (tt == tagType::pod)
318  {
319  if (level == 1)
320  {
321  podIndex = index;
322  }
323  }
324  else
325  {
326  //cout << "There is an error, in findChildElements and the XmlElement at index: " << index << " did not have a recognized tag type" << endl;
327  exit(1);
328  }
329  index++;
330  }
331 }
std::vector< XmlElement > elements
Definition: XmlRep.h:75
tagType
Definition: XmlRep.h:24

◆ getStreamSection()

string getStreamSection ( const std::streampos &  start,
const std::streampos &  end 
) const

Definition at line 276 of file XmlRep.cpp.

Referenced by getTag().

277 {
278  // save current place in the stream
279  std::streampos curLoc = stream_->tellg();
280 
281  std::string result(end - start, '\0');
282  stream_->seekg(start);
283  stream_->read(result.data(), end - start);
284 
285  // go back to current place in the stream
286  stream_->seekg(curLoc);
287  return result;
288 }
std::istream * stream_
Definition: XmlRep.h:72

◆ getTag() [1/2]

string getTag ( const XmlElement e) const

Definition at line 384 of file XmlRep.cpp.

References qmcplusplus::Units::charge::e, and getStreamSection().

Referenced by getTag(), and XmlNode::handleTagString().

384 { return getStreamSection(e.startLoc, e.endLoc); }
std::string getStreamSection(const std::streampos &start, const std::streampos &end) const
Definition: XmlRep.cpp:276

◆ getTag() [2/2]

string getTag ( int  i) const

Definition at line 386 of file XmlRep.cpp.

References elements, and getTag().

387 {
388  if (i >= elements.size())
389  {
390  cerr << "requested a tag index past the end of the vector" << endl;
391  exit(1);
392  }
393  return getTag(elements[i]);
394 }
std::vector< XmlElement > elements
Definition: XmlRep.h:75
std::string getTag(const XmlElement &e) const
Definition: XmlRep.cpp:384

◆ getTagName()

string getTagName ( const std::string &  tag,
tagType  type 
) const
private

Definition at line 18 of file XmlRep.cpp.

References closing, and selfClosing.

19 {
20  string tagName;
21 
22  size_t spaceLoc = tagstr.find(" ");
23  size_t closeLoc = tagstr.find('>');
24  size_t slashLoc = string::npos;
25 
26  if (type == tagType::selfClosing)
27  {
28  slashLoc = tagstr.find("/");
29  }
30 
31  int endChar = tagstr.size();
32  if (spaceLoc != string::npos)
33  {
34  endChar = spaceLoc;
35  }
36  if (closeLoc < endChar && closeLoc != string::npos)
37  {
38  endChar = closeLoc;
39  }
40  if (slashLoc < endChar && slashLoc != string::npos)
41  {
42  endChar = slashLoc;
43  }
44 
45  if (type == tagType::closing)
46  {
47  endChar -= 2;
48  tagName = tagstr.substr(2, endChar);
49  }
50  else
51  {
52  endChar -= 1;
53  tagName = tagstr.substr(1, endChar);
54  }
55  return tagName;
56 }

◆ listAll()

void listAll ( ) const

Definition at line 333 of file XmlRep.cpp.

References closing, opening, pod, and selfClosing.

334 {
335  for (int i = 0; i < elements.size(); i++)
336  {
337  std::cout << i << " ";
338  tagType tt = elements[i].type;
339  if (tt == tagType::opening)
340  {
341  std::cout << "opening, name = " << elements[i].name << std::endl;
342  }
343  else if (tt == tagType::closing)
344  {
345  std::cout << "closing, name = " << elements[i].name << std::endl;
346  }
347  else if (tt == tagType::selfClosing)
348  {
349  std::cout << "selfClosing, name = " << elements[i].name << std::endl;
350  }
351  else if (tt == tagType::pod)
352  {
353  std::cout << "POD" << std::endl;
354  }
355  }
356 }
std::vector< XmlElement > elements
Definition: XmlRep.h:75
tagType
Definition: XmlRep.h:24

◆ startComment()

int startComment ( long  position,
long  length 
) const
private

Definition at line 58 of file XmlRep.cpp.

59 {
60  int isCommentStart = 0;
61  std::streampos curLoc = stream_->tellg();
62  if ((length - position) > 4)
63  {
64  char buf[4];
65  stream_->read(buf, 4);
66  if (strncmp(buf, "<!--", 4) == 0)
67  {
68  isCommentStart = 1;
69  }
70  }
71  stream_->seekg(curLoc);
72  return isCommentStart;
73 }
std::istream * stream_
Definition: XmlRep.h:72

Member Data Documentation

◆ elements

std::vector<XmlElement> elements

Definition at line 75 of file XmlRep.h.

Referenced by XmlNode::createFromStream(), getTag(), XmlNode::handleTagString(), and XmlStream().

◆ stream_

std::istream* stream_
private

Definition at line 72 of file XmlRep.h.


The documentation for this class was generated from the following files: