source: NonGTP/Boost/boost/archive/iterators/xml_unescape.hpp @ 857

Revision 857, 3.5 KB checked in by igarcia, 18 years ago (diff)
Line 
1#ifndef BOOST_ARCHIVE_ITERATORS_XML_UNESCAPE_HPP
2#define BOOST_ARCHIVE_ITERATORS_XML_UNESCAPE_HPP
3
4// MS compatible compilers support #pragma once
5#if defined(_MSC_VER) && (_MSC_VER >= 1020)
6# pragma once
7#endif
8
9/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10// xml_unescape.hpp
11
12// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
13// Use, modification and distribution is subject to the Boost Software
14// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15// http://www.boost.org/LICENSE_1_0.txt)
16
17//  See http://www.boost.org for updates, documentation, and revision history.
18
19#include <cassert>
20
21#include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
22#include <boost/throw_exception.hpp>
23#include <boost/pfto.hpp>
24
25#include <boost/archive/iterators/unescape.hpp>
26#include <boost/archive/iterators/dataflow_exception.hpp>
27
28namespace boost {
29namespace archive {
30namespace iterators {
31
32/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
33// replace &??? xml escape sequences with the corresponding characters
34template<class Base>
35class xml_unescape
36    : public unescape<xml_unescape<Base>, Base>
37{
38    friend class boost::iterator_core_access;
39    typedef xml_unescape<Base> this_t;
40    typedef unescape<this_t, Base> super_t;
41    typedef BOOST_DEDUCED_TYPENAME boost::iterator_reference<this_t> reference_type;
42
43    reference_type dereference() const {
44        return unescape<xml_unescape<Base>, Base>::dereference();
45    }
46public:
47    void drain_residue(const char *literal);
48    int drain();
49
50    template<class T>
51    xml_unescape(BOOST_PFTO_WRAPPER(T) start) :
52        super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast<T>(start))))
53    {}
54    // intel 7.1 doesn't like default copy constructor
55    xml_unescape(const xml_unescape & rhs) :
56        super_t(rhs.base_reference())
57    {}
58};
59
60template<class Base>
61void xml_unescape<Base>::drain_residue(const char * literal){
62    do{
63        if(* literal != * ++(this->base_reference()))
64            boost::throw_exception(
65                dataflow_exception(
66                    dataflow_exception::invalid_xml_escape_sequence
67                )
68            );
69    }
70    while('\0' != * ++literal);
71}
72
73// note key constraint on this function is that can't "look ahead" any
74// more than necessary into base iterator.  Doing so would alter the base
75// iterator refenence which would make subsequent iterator comparisons
76// incorrect and thereby break the composiblity of iterators.
77template<class Base>
78int xml_unescape<Base>::drain(){
79    int retval = * this->base_reference();
80    if('&' != retval){
81        return retval;
82    }
83    retval = * ++(this->base_reference());
84    switch(retval){
85    case 'l': // &lt;
86        drain_residue("t;");
87        retval = '<';
88        break;
89    case 'g': // &gt;
90        drain_residue("t;");
91        retval = '>';
92        break;
93    case 'a':
94        retval = * ++(this->base_reference());
95        switch(retval){
96        case 'p': // &apos;
97            drain_residue("os;");
98            retval = '\'';
99            break;
100        case 'm': // &amp;
101            drain_residue("p;");
102            retval = '&';
103            break;
104        }
105        break;
106    case 'q':
107        drain_residue("uot;");
108        retval = '"';
109        break;
110    }
111    return retval;
112}
113
114} // namespace iterators
115} // namespace archive
116} // namespace boost
117
118#endif // BOOST_ARCHIVE_ITERATORS_XML_UNESCAPE_HPP
Note: See TracBrowser for help on using the repository browser.