1
/* $Id: XmlEscape.cpp,v 1.9 2004/08/28 11:09:38 terpstra Exp $
3
* XmlEscape.cpp - A stream manipulator-like thing for escaping XML
5
* Copyright (C) 2002 - Wesley W. Terpstra
9
* Authors: 'Wesley W. Terpstra' <wesley@terpstra.ca>
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.
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.
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
25
#define _XOPEN_SOURCE 500
26
#define _FILE_OFFSET_BITS 64
28
#include "XmlEscape.h"
34
ostream& XmlOstream::operator << (char c)
36
if (c >= 0 && c <= 0x1f)
40
case '\n': return o << "<br/>\n";
41
case '\t': return o << " ";
43
default: return o << "?"; // drop the char
50
case '\'': return o << "'";
51
case '<': return o << "<";
52
case '>': return o << ">";
53
case '"': return o << """;
54
case '&': return o << "&";
55
default: return o << c; // leave it alone
56
// this case includes high-ascii utf-8
61
string::size_type find_first_offensive_byte(const char* s, const char* e)
67
if (x >= 0 && x <= 0x1f)
68
{ // control char in utf-8 eh?
80
// xml doesn't like these dudes
88
return s - b; // the eos
91
ostream& XmlOstream::operator << (const string& s)
93
string::size_type b = 0, e = s.length();
96
string::size_type x = find_first_offensive_byte(
97
s.c_str() + b, s.c_str() + e) + b;
99
o.write(s.c_str() + b, x - b);
100
if (x == e) return o;
107
ostream& XmlOstream::operator << (const char* s)
111
string::size_type x = find_first_offensive_byte(s, 0);