From 2f533e99b8668e47416f6cd31b275e464188ed47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Fri, 3 Apr 2009 14:02:22 +0000 Subject: [PATCH] UnsingedInteger --- src/xml-cxx/xml.hxx | 32 ++++++++++------------ src/xml.cxx | 66 +++++++++++++++++++++++---------------------- 2 files changed, 48 insertions(+), 50 deletions(-) diff --git a/src/xml-cxx/xml.hxx b/src/xml-cxx/xml.hxx index c1fdb44..8554ae1 100644 --- a/src/xml-cxx/xml.hxx +++ b/src/xml-cxx/xml.hxx @@ -5,8 +5,8 @@ // 1 2 3 4 5 6 7 8 // 45678901234567890123456789012345678901234567890123456789012345678901234567890 -#ifndef XML_HXX -#define XML_HXX +#ifndef LIB_XML_CXX_HXX +#define LIB_XML_CXX_HXX #include #include @@ -85,9 +85,10 @@ namespace xml { //---------------------------------------------------------------------------- class type_mismatch: public exception { public: - type_mismatch(const Node& t, const std::string& txt) throw(): - exception("wrong type, text contains mismatching character\ntext: " - +txt, t) {} + type_mismatch(const Node& t, const std::string& txt, + const std::string& comment) throw(): + exception("wrong type, text contains mismatching character\n"+comment + +"\ntext: "+txt, t) {} }; //---------------------------------------------------------------------------- class access_error: public exception { @@ -266,25 +267,20 @@ namespace xml { throw(); virtual String& append(const Node& o) throw(cannot_have_children); Node& operator=(const std::string& contents) throw(); - private: + protected: std::string _text; }; //---------------------------------------------------------------------------- - class UnsignedInteger: public Node { + class UnsignedInteger: public String { public: - String(std::string name, const std::string& text = std::string()) throw(); + UnsignedInteger(std::string name, unsigned long i = 0) throw(); virtual std::auto_ptr clone() const throw(); - virtual ~String() throw() {} - virtual std::string text() const throw(); - virtual String& text(const std::string& txt) throw(tag_expected, - type_mismatch); - virtual std::ostream& out(std::ostream& o, unsigned int level=0) const - throw(); - virtual String& append(const Node& o) throw(cannot_have_children); - Node& operator=(const std::string& contents) throw(); - private: - std::string _text; + virtual ~UnsignedInteger() throw() {} + virtual UnsignedInteger& text(const std::string& txt) + throw(tag_expected, type_mismatch); + unsigned long number() const throw(); + static unsigned long number(const Node& node) throw(); }; //---------------------------------------------------------------------------- diff --git a/src/xml.cxx b/src/xml.cxx index cb1741f..1ac02b2 100644 --- a/src/xml.cxx +++ b/src/xml.cxx @@ -340,41 +340,43 @@ namespace xml { } //---------------------------------------------------------------------------- - UnsingedInteger::UnsingedInteger(std::string name, const std::string& text) throw(): - Node(name), _text(text) { - } - std::auto_ptr UnsingedInteger::clone() const throw() { - return std::auto_ptr(new UnsingedInteger(*this)); - } - std::string UnsingedInteger::text() const throw() { - return _text; - } - UnsingedInteger& UnsingedInteger::text(const std::string& txt) throw(tag_expected, - type_mismatch) { - _text = txt; - return *this; - } - std::ostream& UnsingedInteger::out(std::ostream& o, unsigned int level) const throw() { - if (_text.size()) { - o<first<<"=\""<second<<'"'; - o<<'>'<<_text<<"'; - } else { - o<first<<"=\""<second<<'"'; - o<<"/>"; + UnsignedInteger::UnsignedInteger(std::string name, unsigned long i) throw(): + String(name, + dynamic_cast(std::stringstream()< UnsignedInteger::clone() const throw() { + return std::auto_ptr(new UnsignedInteger(*this)); + } + UnsignedInteger& UnsignedInteger::text(const std::string& txt) + throw(tag_expected, type_mismatch) { + std::string::size_type + start(txt.find_first_not_of(" \t\n\r")), + last(txt.find_last_of(" \t\n\r")); + if (start==std::string::npos) { // empty - set to 0 + _text = "0"; + return *this; } - return o; + if (txt.find_first_not_of + ("0123456789", start, + last!=std::string::npos?last-start+1:txt.size()-start) + !=std::string::npos) + throw type_mismatch(*this, txt, + "unsigned integer may only contain numbers"); + unsigned long i(0); + std::stringstream(txt)>>i; + std::stringstream ss; + ss<>i; + return i; } //----------------------------------------------------------------------------