From 17ad2a3fd8933aa49bff4c8ea49963d15cc0db92 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 14 Mar 2013 10:36:16 +0200 Subject: Add support for peeking and getting current event in XML parser --- cutl/xml/parser.cxx | 22 +++++++++++----------- cutl/xml/parser.hxx | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 12 deletions(-) (limited to 'cutl') diff --git a/cutl/xml/parser.cxx b/cutl/xml/parser.cxx index 24a91ee..5724794 100644 --- a/cutl/xml/parser.cxx +++ b/cutl/xml/parser.cxx @@ -54,7 +54,7 @@ namespace cutl parser:: parser (istream& is, const string& name, feature_type f) : is_ (is), name_ (name), feature_ (f), - depth_ (0), event_ (eof), queue_ (eof), + depth_ (0), state_ (state_next), event_ (eof), queue_ (eof), pqname_ (&qname_), pvalue_ (&value_), attr_i_ (0), start_ns_i_ (0), end_ns_i_ (0) { @@ -156,9 +156,9 @@ namespace cutl }; parser::event_type parser:: - next () + next_ () { - event_type e (next_ ()); + event_type e (next_body ()); // Content-specific processing. Note that we handle characters in the // characters_() Expat handler for two reasons. Firstly, it is faster @@ -200,7 +200,7 @@ namespace cutl } parser::event_type parser:: - next_ () + next_body () { // See if we have any start namespace declarations we need to return. // @@ -230,7 +230,7 @@ namespace cutl default: { assert (false); - return eof; + return event_ = eof; } } } @@ -276,7 +276,7 @@ namespace cutl default: { assert (false); - return eof; + return event_ = eof; } } } @@ -316,9 +316,9 @@ namespace cutl // if (queue_ != eof) { - event_type r (queue_); + event_ = queue_; queue_ = eof; - return r; + return event_; } XML_ParsingStatus ps; @@ -334,11 +334,11 @@ namespace cutl case XML_PARSING: { assert (false); - return eof; + return event_ = eof; } case XML_FINISHED: { - return eof; + return event_ = eof; } case XML_SUSPENDED: { @@ -357,7 +357,7 @@ namespace cutl // unless this was the last chunk, in which case this is eof. // if (ps.finalBuffer) - return eof; + return event_ = eof; break; } diff --git a/cutl/xml/parser.hxx b/cutl/xml/parser.hxx index b61d26d..5dc2a83 100644 --- a/cutl/xml/parser.hxx +++ b/cutl/xml/parser.hxx @@ -106,7 +106,34 @@ namespace cutl }; event_type - next (); + next () + { + if (state_ == state_next) + return next_ (); + else + { + state_ = state_next; + return event_; + } + } + + event_type + peek () + { + if (state_ == state_peek) + return event_; + else + { + state_ = state_peek; + return next_ (); + } + } + + // Return the even that was last returned by the call to next() or + // peek(). + // + event_type + event () {return event_;} const qname_type& qname () const {return *pqname_;} @@ -167,6 +194,9 @@ namespace cutl event_type next_ (); + event_type + next_body (); + void handle_error (); @@ -177,6 +207,7 @@ namespace cutl XML_Parser p_; std::size_t depth_; + enum {state_next, state_peek} state_; event_type event_; event_type queue_; -- cgit v1.1