~ubuntu-branches/ubuntu/hardy/videolink/hardy

« back to all changes in this revision

Viewing changes to xml_utils.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ben Hutchings
  • Date: 2006-10-15 02:05:46 UTC
  • Revision ID: james.westby@ubuntu.com-20061015020546-0t2uoscwlmwyrp5q
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2005-6 Ben Hutchings <ben@decadent.org.uk>.
 
2
// See the file "COPYING" for licence details.
 
3
 
 
4
#include <cassert>
 
5
#include <cstddef>
 
6
 
 
7
#include "xml_utils.hpp"
 
8
 
 
9
std::string xml_escape(const std::string & str)
 
10
{
 
11
    std::string result;
 
12
    std::size_t begin = 0;
 
13
 
 
14
    for (;;)
 
15
    {
 
16
        std::size_t end = str.find_first_of("\"&'<>", begin);
 
17
        result.append(str, begin, end - begin);
 
18
        if (end == std::string::npos)
 
19
            return result;
 
20
 
 
21
        const char * entity = NULL;
 
22
        switch (str[end])
 
23
        {
 
24
        case '"':  entity = "&quot;"; break;
 
25
        case '&':  entity = "&amp;";  break;
 
26
        case '\'': entity = "&apos;"; break;
 
27
        case '<':  entity = "&lt;";   break;
 
28
        case '>':  entity = "&gt;";   break;
 
29
        }
 
30
        assert(entity);
 
31
        result.append(entity);
 
32
 
 
33
        begin = end + 1;
 
34
    }
 
35
}