a5d2c5120fdbfd0ead322cbe558ab42f81117b6e
[platform/upstream/boost.git] / boost / archive / iterators / xml_escape.hpp
1 #ifndef BOOST_ARCHIVE_ITERATORS_XML_ESCAPE_HPP
2 #define BOOST_ARCHIVE_ITERATORS_XML_ESCAPE_HPP
3
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER)
6 # pragma once
7 #endif
8
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // xml_escape.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 <boost/assert.hpp>
20 #include <boost/serialization/pfto.hpp>
21 #include <boost/archive/iterators/escape.hpp>
22
23 namespace boost { 
24 namespace archive {
25 namespace iterators {
26
27 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
28 // insert escapes into xml text
29
30 template<class Base>
31 class xml_escape 
32     : public escape<xml_escape<Base>, Base>
33 {
34     friend class boost::iterator_core_access;
35
36     typedef escape<xml_escape<Base>, Base> super_t;
37
38 public:
39     char fill(const char * & bstart, const char * & bend);
40     wchar_t fill(const wchar_t * & bstart, const wchar_t * & bend);
41
42     template<class T>
43     xml_escape(BOOST_PFTO_WRAPPER(T) start) :
44         super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast< T >(start))))
45     {}
46     // intel 7.1 doesn't like default copy constructor
47     xml_escape(const xml_escape & rhs) : 
48         super_t(rhs.base_reference())
49     {}
50 };
51
52 template<class Base>
53 char xml_escape<Base>::fill(
54     const char * & bstart, 
55     const char * & bend
56 ){
57     char current_value = * this->base_reference();
58     switch(current_value){
59     case '<':
60         bstart = "&lt;";
61         bend = bstart + 4;
62         break;
63     case '>':
64         bstart = "&gt;";
65         bend = bstart + 4;
66         break;
67     case '&':
68         bstart = "&amp;";
69         bend = bstart + 5;
70         break;
71     case '"':
72         bstart = "&quot;";
73         bend = bstart + 6;
74         break;
75     case '\'':
76         bstart = "&apos;";
77         bend = bstart + 6;
78         break;
79     default:
80         return current_value;
81     }
82     return *bstart;
83 }
84
85 template<class Base>
86 wchar_t xml_escape<Base>::fill(
87     const wchar_t * & bstart, 
88     const wchar_t * & bend
89 ){
90     wchar_t current_value = * this->base_reference();
91     switch(current_value){
92     case '<':
93         bstart = L"&lt;";
94         bend = bstart + 4;
95         break;
96     case '>':
97         bstart = L"&gt;";
98         bend = bstart + 4;
99         break;
100     case '&':
101         bstart = L"&amp;";
102         bend = bstart + 5;
103         break;
104     case '"':
105         bstart = L"&quot;";
106         bend = bstart + 6;
107         break;
108     case '\'':
109         bstart = L"&apos;";
110         bend = bstart + 6;
111         break;
112     default:
113         return current_value;
114     }
115     return *bstart;
116 }
117
118 } // namespace iterators
119 } // namespace archive
120 } // namespace boost
121
122 #endif // BOOST_ARCHIVE_ITERATORS_XML_ESCAPE_HPP