UnsingedInteger
This commit is contained in:
@@ -5,8 +5,8 @@
|
|||||||
// 1 2 3 4 5 6 7 8
|
// 1 2 3 4 5 6 7 8
|
||||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||||
|
|
||||||
#ifndef XML_HXX
|
#ifndef LIB_XML_CXX_HXX
|
||||||
#define XML_HXX
|
#define LIB_XML_CXX_HXX
|
||||||
|
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -85,9 +85,10 @@ namespace xml {
|
|||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
class type_mismatch: public exception {
|
class type_mismatch: public exception {
|
||||||
public:
|
public:
|
||||||
type_mismatch(const Node& t, const std::string& txt) throw():
|
type_mismatch(const Node& t, const std::string& txt,
|
||||||
exception("wrong type, text contains mismatching character\ntext: "
|
const std::string& comment) throw():
|
||||||
+txt, t) {}
|
exception("wrong type, text contains mismatching character\n"+comment
|
||||||
|
+"\ntext: "+txt, t) {}
|
||||||
};
|
};
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
class access_error: public exception {
|
class access_error: public exception {
|
||||||
@@ -266,25 +267,20 @@ namespace xml {
|
|||||||
throw();
|
throw();
|
||||||
virtual String& append(const Node& o) throw(cannot_have_children);
|
virtual String& append(const Node& o) throw(cannot_have_children);
|
||||||
Node& operator=(const std::string& contents) throw();
|
Node& operator=(const std::string& contents) throw();
|
||||||
private:
|
protected:
|
||||||
std::string _text;
|
std::string _text;
|
||||||
};
|
};
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
class UnsignedInteger: public Node {
|
class UnsignedInteger: public String {
|
||||||
public:
|
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<Node> clone() const throw();
|
virtual std::auto_ptr<Node> clone() const throw();
|
||||||
virtual ~String() throw() {}
|
virtual ~UnsignedInteger() throw() {}
|
||||||
virtual std::string text() const throw();
|
virtual UnsignedInteger& text(const std::string& txt)
|
||||||
virtual String& text(const std::string& txt) throw(tag_expected,
|
throw(tag_expected, type_mismatch);
|
||||||
type_mismatch);
|
unsigned long number() const throw();
|
||||||
virtual std::ostream& out(std::ostream& o, unsigned int level=0) const
|
static unsigned long number(const Node& node) throw();
|
||||||
throw();
|
|
||||||
virtual String& append(const Node& o) throw(cannot_have_children);
|
|
||||||
Node& operator=(const std::string& contents) throw();
|
|
||||||
private:
|
|
||||||
std::string _text;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
62
src/xml.cxx
62
src/xml.cxx
@@ -340,41 +340,43 @@ namespace xml {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
UnsingedInteger::UnsingedInteger(std::string name, const std::string& text) throw():
|
UnsignedInteger::UnsignedInteger(std::string name, unsigned long i) throw():
|
||||||
Node(name), _text(text) {
|
String(name,
|
||||||
|
dynamic_cast<std::stringstream&>(std::stringstream()<<i).str()) {
|
||||||
}
|
}
|
||||||
std::auto_ptr<Node> UnsingedInteger::clone() const throw() {
|
std::auto_ptr<Node> UnsignedInteger::clone() const throw() {
|
||||||
return std::auto_ptr<Node>(new UnsingedInteger(*this));
|
return std::auto_ptr<Node>(new UnsignedInteger(*this));
|
||||||
}
|
}
|
||||||
std::string UnsingedInteger::text() const throw() {
|
UnsignedInteger& UnsignedInteger::text(const std::string& txt)
|
||||||
return _text;
|
throw(tag_expected, type_mismatch) {
|
||||||
}
|
std::string::size_type
|
||||||
UnsingedInteger& UnsingedInteger::text(const std::string& txt) throw(tag_expected,
|
start(txt.find_first_not_of(" \t\n\r")),
|
||||||
type_mismatch) {
|
last(txt.find_last_of(" \t\n\r"));
|
||||||
_text = txt;
|
if (start==std::string::npos) { // empty - set to 0
|
||||||
|
_text = "0";
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
_text = ss.str();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
std::ostream& UnsingedInteger::out(std::ostream& o, unsigned int level) const throw() {
|
unsigned long UnsignedInteger::number() const throw() {
|
||||||
if (_text.size()) {
|
return number(*this);
|
||||||
o<<std::string(level, '\t')<<'<'<<name();
|
|
||||||
for (Attributes::const_iterator it(_attributes.begin());
|
|
||||||
it!=_attributes.end(); ++it)
|
|
||||||
o<<' '<<it->first<<"=\""<<it->second<<'"';
|
|
||||||
o<<'>'<<_text<<"</"<<name()<<'>';
|
|
||||||
} else {
|
|
||||||
o<<std::string(level, '\t')<<'<'<<name();
|
|
||||||
for (Attributes::const_iterator it(_attributes.begin());
|
|
||||||
it!=_attributes.end(); ++it)
|
|
||||||
o<<' '<<it->first<<"=\""<<it->second<<'"';
|
|
||||||
o<<"/>";
|
|
||||||
}
|
|
||||||
return o;
|
|
||||||
}
|
}
|
||||||
UnsingedInteger& UnsingedInteger::append(const Node& o) throw(cannot_have_children) {
|
unsigned long UnsignedInteger::number(const Node& node) throw() {
|
||||||
throw cannot_have_children(*this, o);
|
unsigned long i(0);
|
||||||
}
|
std::stringstream ss(node.text());
|
||||||
Node& UnsingedInteger::operator=(const std::string& contents) throw() {
|
ss>>i;
|
||||||
return text(contents);
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
Reference in New Issue
Block a user