aboutsummaryrefslogtreecommitdiff
path: root/cutl/xml
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2013-03-14 10:36:16 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2013-03-14 10:36:16 +0200
commit17ad2a3fd8933aa49bff4c8ea49963d15cc0db92 (patch)
treee848ab6a86aa64b0be987d0cdc61fe1940280cf6 /cutl/xml
parentce3dfdff0d03697bf0c9e0cc9fb45dc6ef7c4ba9 (diff)
Add support for peeking and getting current event in XML parser
Diffstat (limited to 'cutl/xml')
-rw-r--r--cutl/xml/parser.cxx22
-rw-r--r--cutl/xml/parser.hxx33
2 files changed, 43 insertions, 12 deletions
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_;