27 #ifndef _SAXPARSER_HPP_
28 #define _SAXPARSER_HPP_
33 #include "boost/iostreams/positioning.hpp"
82 memcpy(
data(),rhs.c_str(),rhs.length());
83 (*this)[rhs.length()] = 0;
87 if (strchr(
c_str(),
'&')) {
106 init(rhs ? strlen(rhs) : 0);
115 size_t oldsize =
length();
123 size_t rhslen = rhs?strlen(rhs):0;
125 size_t oldsize =
length();
127 strcpy(
data()+oldsize,rhs);
133 return c && !strcmp(c,
c_str());
137 return !strcmp(
c_str(),s.c_str());
148 size_t new_used = size +
_lead;
152 throw std::runtime_error(
"SAXParser: cannot allocate memory");
183 for (
const char *c=
c_str(); *c && strchr(
" \n\r\t",*c); c++) {
190 return !strncmp(
c_str(),txt,strlen(txt));
193 size_t len = strlen(txt);
227 template<
typename Target >
inline Target
textToValue(
const char *txt);
231 return (
float)
ATOF( txt ) ;
256 return (
unsigned int) strtoul( txt, NULL, 10 );
261 return strtoul( txt, NULL, 10 );
264 #if defined(BOOST_HAS_LONG_LONG)
266 template<>
inline long long textToValue(
const char *txt)
268 #if defined(BOOST_HAS_MS_INT64)
275 template<>
inline unsigned long long textToValue(
const char *txt)
277 #if defined(BOOST_HAS_MS_INT64)
278 return _strtoui64(txt,NULL,10);
280 return strtoull( txt, NULL, 10 );
284 #endif // has long long
288 return strcmp(t,
"0") && strcmp(t,
"false");
296 template<>
inline boost::logic::tribool
textToValue(
const char *txt)
298 using namespace boost::logic;
300 return tribool(indeterminate);
310 return std::string( txt );
357 Attributes(
const char * _source_text,
size_t _source_text_len,
bool _autoUnescape) :
360 size=_source_text_len;
409 for (
size_t n=
attrs.size();n--;)
421 for (
size_t n=
attrs.size();n--;)
459 while (*c && !strchr(
" \n\r\t/",*c)) c++;
461 while (*c && strchr(
" \n\r\t",*c)) c++;
462 textbuff[indexNameEnd] = 0;
475 return !strcmp(test,
name);
498 template<
typename T >
506 return (
size_t)strtoul(
getValuePtr(Unescape),NULL,10);
514 void set(
const char *_name,
char *_value,
bool _needsUnescape)
527 attribute_list::const_iterator
begin()
const
530 return attrs.begin();
532 attribute_list::const_iterator
end()
const
537 attribute_list::const_iterator
find(
const std::string &name)
const
539 attribute_list::const_iterator it;
540 for (it =
begin(); it !=
end() ; it++ )
542 if (it->matchName(name.c_str()))
565 for (attribute_list::const_iterator it=
attrs.begin();it!=
attrs.end();it++)
567 if (it->matchName(name))
586 const std::string& data,
604 template <
typename T>
609 T defaultValue = T())
const
613 result = attr->
valueAs<T>(Unescape);
615 result = defaultValue;
622 const char * defaultValue = NULL)
const
632 template <
typename T>
647 std::string& result)
const
658 template <
typename T>
660 const std::string &name,
662 T defaultValue = T())
const
668 result = defaultValue;
728 #endif // _SAXPARSER_HPP_
T valueAs(XMLUnescapeBehavior_t Unescape) const
bool matchName(const char *test) const
virtual Status endElement(const std::string &name, stream_offset position)
PWIZ_API_DECL std::string xml_root_element_from_file(const std::string &filepath)
Returns the root element from an XML file; throws runtime_error if no element is found.
int version
contextual version available to control handler logic which support multiple versions of a schema; th...
Attributes(saxstring &str, bool _autoUnescape)
const char * getAttribute(const Attributes &attributes, const char *name, XMLUnescapeBehavior_t Unescape, const char *defaultValue=NULL) const
saxstring & operator+=(const SAXParser::saxstring &rhs)
Target textToValue(const char *txt)
virtual Status processingInstruction(const std::string &name, const std::string &data, stream_offset position)
attribute_list::const_iterator begin() const
void test_invariant() const
const char * findValueByName(const char *name, XMLUnescapeBehavior_t Unescape=XMLUnescapeDefault) const
SAX event handler interface.
char & operator[](size_t n)
attribute_list::const_iterator end() const
PWIZ_API_DECL void parseAttributes(std::string::size_type &index) const
Attributes & operator=(const Attributes &rhs)
std::string & getAttribute(const Attributes &attributes, const char *name, std::string &result) const
T & getAttribute(const Attributes &attributes, const std::string &name, T &result, T defaultValue=T()) const
Status(Flag _flag=Ok, Handler *_delegate=0)
virtual Status startElement(const std::string &name, const Attributes &attributes, stream_offset position)
PWIZ_API_DECL size_t count_trail_ws(const char *data, size_t len)
saxstring & operator=(const SAXParser::saxstring &rhs)
T & getAttribute(const Attributes &attributes, const char *name, T &result) const
Attributes(const char *_source_text, size_t _source_text_len, bool _autoUnescape)
bool ends_with(const char *txt) const
const char * c_str() const
boost::iostreams::stream_offset stream_offset
PWIZ_API_DECL std::string xml_root_element(const std::string &fileheader)
Returns the root element from an XML buffer; throws runtime_error if no element is found...
bool starts_with(const char *txt) const
virtual Status characters(const SAXParser::saxstring &text, stream_offset position)
const char * getTextBuffer() const
T & getAttribute(const Attributes &attributes, const char *name, T &result, XMLUnescapeBehavior_t Unescape, T defaultValue=T()) const
Handler returns the Status struct as a means of changing the parser's behavior.
bool operator==(const char *c) const
size_t valueAs(XMLUnescapeBehavior_t Unescape) const
const char * getValuePtr(XMLUnescapeBehavior_t Unescape=XMLUnescapeDefault) const
saxstring(const std::string &rhs)
bool parseCharacters
When false, no calls to characters() will be made.
const char * getName() const
PWIZ_API_DECL void unescapeXML(char *str)
bool autoUnescapeAttributes
Setting these to false will disable the auto-unescaping feature of the parser; this is useful for han...
const char * getTagName() const
attribute_list::const_iterator find(const std::string &name) const
const attribute * findAttributeByName(const char *name) const
PWIZ_API_DECL Position position(CVID cvid=CVID_Unknown)
returns a Position corresponding to one of the following CVIDs: CVID_Unknown: Position::Anywhere MS_m...
Attributes(const Attributes &rhs)
std::vector< attribute > attribute_list
PWIZ_API_DECL void parse(std::istream &is, Handler &handler)
Extract a single XML element from the istream, sending SAX events to the handler. ...
char * resize(size_t size)
void set(const char *_name, char *_value, bool _needsUnescape)
PWIZ_API_DECL std::string value(const std::string &id, const std::string &name)
convenience function to extract a named value from an id string
PWIZ_API_DECL std::string & decode_xml_id(std::string &str)
Decodes any characters encoded with their hexadecimal value, e.g.
bool autoUnescapeCharacters
PWIZ_API_DECL std::string decode_xml_id_copy(const std::string &str)
Decodes any characters encoded with their hexadecimal value, e.g.
std::string getValue(XMLUnescapeBehavior_t Unescape=XMLUnescapeDefault) const
saxstring(const SAXParser::saxstring &rhs)
std::ostream & operator<<(std::ostream &os, const saxstring &s)
bool istrue(const char *t)