~ubuntu-branches/ubuntu/raring/lurker/raring

« back to all changes in this revision

Viewing changes to common/XmlEscape.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Meurer
  • Date: 2004-09-26 16:27:51 UTC
  • Revision ID: james.westby@ubuntu.com-20040926162751-z1ohcjltv7ojtg6z
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  $Id: XmlEscape.cpp,v 1.9 2004/08/28 11:09:38 terpstra Exp $
 
2
 *  
 
3
 *  XmlEscape.cpp - A stream manipulator-like thing for escaping XML
 
4
 *  
 
5
 *  Copyright (C) 2002 - Wesley W. Terpstra
 
6
 *  
 
7
 *  License: GPL
 
8
 *  
 
9
 *  Authors: 'Wesley W. Terpstra' <wesley@terpstra.ca>
 
10
 *  
 
11
 *    This program is free software; you can redistribute it and/or modify
 
12
 *    it under the terms of the GNU General Public License as published by
 
13
 *    the Free Software Foundation; version 2.
 
14
 *    
 
15
 *    This program is distributed in the hope that it will be useful,
 
16
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 *    GNU General Public License for more details.
 
19
 *    
 
20
 *    You should have received a copy of the GNU General Public License
 
21
 *    along with this program; if not, write to the Free Software
 
22
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
 */
 
24
 
 
25
#define _XOPEN_SOURCE 500
 
26
#define _FILE_OFFSET_BITS 64
 
27
 
 
28
#include "XmlEscape.h"
 
29
 
 
30
#include <cstring>
 
31
 
 
32
XmlEscape xmlEscape;
 
33
 
 
34
ostream& XmlOstream::operator << (char c)
 
35
{
 
36
        if (c >= 0 && c <= 0x1f)
 
37
        {
 
38
                switch (c)
 
39
                {
 
40
                case '\n':      return o << "<br/>\n";
 
41
                case '\t':      return o << "        ";
 
42
                case '\r':      return o;
 
43
                default:        return o << "?"; // drop the char
 
44
                }
 
45
        }
 
46
        else
 
47
        {
 
48
                switch (c)
 
49
                {
 
50
                case '\'':      return o << "&apos;";
 
51
                case '<':       return o << "&lt;";
 
52
                case '>':       return o << "&gt;";
 
53
                case '"':       return o << "&quot;";
 
54
                case '&':       return o << "&amp;";
 
55
                default:        return o << c; // leave it alone
 
56
                                // this case includes high-ascii utf-8
 
57
                }
 
58
        }
 
59
}
 
60
 
 
61
string::size_type find_first_offensive_byte(const char* s, const char* e)
 
62
{
 
63
        const char* b = s;
 
64
        while (s != e)
 
65
        {
 
66
                char x = *s;
 
67
                if (x >= 0 && x <= 0x1f)
 
68
                {       // control char in utf-8 eh?
 
69
                        return s - b;
 
70
                }
 
71
                else
 
72
                {
 
73
                        switch (x)
 
74
                        {
 
75
                        case '\'':
 
76
                        case '<':
 
77
                        case '>':
 
78
                        case '"':
 
79
                        case '&':
 
80
                                // xml doesn't like these dudes
 
81
                                return s - b;
 
82
                        }
 
83
                }
 
84
                
 
85
                ++s;
 
86
        }
 
87
        
 
88
        return s - b; // the eos
 
89
}
 
90
 
 
91
ostream& XmlOstream::operator << (const string& s)
 
92
{
 
93
        string::size_type b = 0, e = s.length();
 
94
        while (1)
 
95
        {
 
96
                string::size_type x = find_first_offensive_byte(
 
97
                        s.c_str() + b, s.c_str() + e) + b;
 
98
                
 
99
                o.write(s.c_str() + b, x - b);
 
100
                if (x == e) return o;
 
101
                
 
102
                *this << s[x];
 
103
                b = x+1;
 
104
        }
 
105
}
 
106
 
 
107
ostream& XmlOstream::operator << (const char* s)
 
108
{
 
109
        while (1)
 
110
        {
 
111
                string::size_type x = find_first_offensive_byte(s, 0);
 
112
                
 
113
                o.write(s, x);
 
114
                s += x;
 
115
                if (!*s) return o;
 
116
                
 
117
                *this << *s;
 
118
                ++s;
 
119
        }
 
120
}