diff options
author | Anas Nashif <anas.nashif@intel.com> | 2012-10-30 12:57:26 -0700 |
---|---|---|
committer | Anas Nashif <anas.nashif@intel.com> | 2012-10-30 12:57:26 -0700 |
commit | 1a78a62555be32868418fe52f8e330c9d0f95d5a (patch) | |
tree | d3765a80e7d3b9640ec2e930743630cd6b9fce2b /tools/regression/src/detail | |
download | boost-1a78a62555be32868418fe52f8e330c9d0f95d5a.tar.gz boost-1a78a62555be32868418fe52f8e330c9d0f95d5a.tar.bz2 boost-1a78a62555be32868418fe52f8e330c9d0f95d5a.zip |
Imported Upstream version 1.49.0upstream/1.49.0
Diffstat (limited to 'tools/regression/src/detail')
-rw-r--r-- | tools/regression/src/detail/tiny_xml.cpp | 167 | ||||
-rw-r--r-- | tools/regression/src/detail/tiny_xml.hpp | 70 | ||||
-rw-r--r-- | tools/regression/src/detail/tiny_xml_test.cpp | 17 | ||||
-rw-r--r-- | tools/regression/src/detail/tiny_xml_test.txt | 17 |
4 files changed, 271 insertions, 0 deletions
diff --git a/tools/regression/src/detail/tiny_xml.cpp b/tools/regression/src/detail/tiny_xml.cpp new file mode 100644 index 0000000000..682c04ff7b --- /dev/null +++ b/tools/regression/src/detail/tiny_xml.cpp @@ -0,0 +1,167 @@ +// tiny XML sub-set tools implementation -----------------------------------// + +// (C) Copyright Beman Dawes 2002. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include "tiny_xml.hpp" +#include <cassert> +#include <cstring> + +namespace +{ + + void eat_whitespace( char & c, std::istream & in ) + { + while ( c == ' ' || c == '\r' || c == '\n' || c == '\t' ) + in.get( c ); + } + + std::string get_name( char & c, std::istream & in ) + { + std::string result; + eat_whitespace( c, in ); + while ( std::strchr( + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.", c ) + != 0 ) + { + result += c; + if(!in.get( c )) + throw std::string("xml: unexpected eof"); + } + return result; + } + + void eat_delim( char & c, std::istream & in, + char delim, const std::string & msg ) + { + eat_whitespace( c, in ); + if ( c != delim ) + throw std::string("xml syntax error, expected ") + delim + + " (" + msg + ")"; + in.get( c ); + } + + std::string get_value( char & c, std::istream & in ) + { + std::string result; + while ( c != '\"' ) + { + result += c; + in.get( c ); + } + in.get( c ); + return result; + } + +} + +namespace boost +{ + namespace tiny_xml + { + + // parse -----------------------------------------------------------------// + + element_ptr parse( std::istream & in, const std::string & msg ) + { + char c = 0; // current character + element_ptr e( new element ); + + if(!in.get( c )) + throw std::string("xml: unexpected eof"); + if ( c == '<' ) + if(!in.get( c )) + throw std::string("xml: unexpected eof"); + + e->name = get_name( c, in ); + eat_whitespace( c, in ); + + // attributes + while ( c != '>' ) + { + attribute a; + a.name = get_name( c, in ); + + eat_delim( c, in, '=', msg ); + eat_delim( c, in, '\"', msg ); + + a.value = get_value( c, in ); + + e->attributes.push_back( a ); + eat_whitespace( c, in ); + } + if(!in.get( c )) // next after '>' + throw std::string("xml: unexpected eof"); + + eat_whitespace( c, in ); + + // sub-elements + while ( c == '<' ) + { + if ( in.peek() == '/' ) break; + e->elements.push_back( parse( in, msg ) ); + in.get( c ); // next after '>' + eat_whitespace( c, in ); + } + + // content + if ( c != '<' ) + { + e->content += '\n'; + while ( c != '<' ) + { + e->content += c; + if(!in.get( c )) + throw std::string("xml: unexpected eof"); + } + } + + assert( c == '<' ); + if(!in.get( c )) // next after '<' + throw std::string("xml: unexpected eof"); + + eat_delim( c, in, '/', msg ); + std::string end_name( get_name( c, in ) ); + if ( e->name != end_name ) + throw std::string("xml syntax error: beginning name ") + + e->name + " did not match end name " + end_name + + " (" + msg + ")"; + + eat_delim( c, in, '>', msg ); + return e; + } + + // write ---------------------------------------------------------------// + + void write( const element & e, std::ostream & out ) + { + out << "<" << e.name; + if ( !e.attributes.empty() ) + { + for( attribute_list::const_iterator itr = e.attributes.begin(); + itr != e.attributes.end(); ++itr ) + { + out << " " << itr->name << "=\"" << itr->value << "\""; + } + } + out << ">"; + if ( !e.elements.empty() ) + { + out << "\n"; + for( element_list::const_iterator itr = e.elements.begin(); + itr != e.elements.end(); ++itr ) + { + write( **itr, out ); + } + } + if ( !e.content.empty() ) + { + out << e.content; + } + out << "</" << e.name << ">\n"; + } + + } // namespace tiny_xml +} // namespace boost + diff --git a/tools/regression/src/detail/tiny_xml.hpp b/tools/regression/src/detail/tiny_xml.hpp new file mode 100644 index 0000000000..f9d91d2652 --- /dev/null +++ b/tools/regression/src/detail/tiny_xml.hpp @@ -0,0 +1,70 @@ +// tiny XML sub-set tools --------------------------------------------------// + +// (C) Copyright Beman Dawes 2002. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Provides self-contained tools for this XML sub-set: +// +// element ::= { "<" name { name "=" "\"" value "\"" } ">" +// {element} [contents] "</" name ">" } +// +// The point of "self-contained" is to minimize tool-chain dependencies. + +#ifndef BOOST_TINY_XML_H +#define BOOST_TINY_XML_H + +#include "boost/smart_ptr.hpp" // for shared_ptr +#include "boost/utility.hpp" // for noncopyable +#include <list> +#include <iostream> +#include <string> + +namespace boost +{ + namespace tiny_xml + { + class element; + struct attribute + { + std::string name; + std::string value; + + attribute(){} + attribute( const std::string & name, const std::string & value ) + : name(name), value(value) {} + }; + typedef boost::shared_ptr< element > element_ptr; + typedef std::list< element_ptr > element_list; + typedef std::list< attribute > attribute_list; + + class element + : private boost::noncopyable // because deep copy sematics would be required + { + public: + std::string name; + attribute_list attributes; + element_list elements; + std::string content; + + element() {} + explicit element( const std::string & name ) : name(name) {} + }; + + element_ptr parse( std::istream & in, const std::string & msg ); + // Precondition: stream positioned at either the initial "<" + // or the first character after the initial "<". + // Postcondition: stream positioned at the first character after final + // ">" (or eof). + // Returns: an element_ptr to an element representing the parsed stream. + // Throws: std::string on syntax error. msg appended to what() string. + + void write( const element & e, std::ostream & out ); + + } +} + +#endif // BOOST_TINY_XML_H + + + diff --git a/tools/regression/src/detail/tiny_xml_test.cpp b/tools/regression/src/detail/tiny_xml_test.cpp new file mode 100644 index 0000000000..b5c0542ba4 --- /dev/null +++ b/tools/regression/src/detail/tiny_xml_test.cpp @@ -0,0 +1,17 @@ +// tiny XML test program ---------------------------------------------------// + +// Copyright Beman Dawes 2002. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include "tiny_xml.hpp" + +#include <iostream> + +int main() +{ + boost::tiny_xml::element_ptr tree( boost::tiny_xml::parse( std::cin ) ); + boost::tiny_xml::write( *tree, std::cout ); + return 0; +} + diff --git a/tools/regression/src/detail/tiny_xml_test.txt b/tools/regression/src/detail/tiny_xml_test.txt new file mode 100644 index 0000000000..b248cbf062 --- /dev/null +++ b/tools/regression/src/detail/tiny_xml_test.txt @@ -0,0 +1,17 @@ +<root> +<frontmatter> +// (C) Copyright Beman Dawes 2002. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +</frontmatter> +<element-1 at-1="abcd" at-2 = "defg" > +<element-1a> +It's Howdy Doody time! +</element-1a> +<element-1b>It's not Howdy Doody time!</element-1b> +</element-1> +<element-2> +It's +Eastern Standard time! +</element-2> +</root> |