File: xml_utils.cpp

package info (click to toggle)
videolink 1.2.5-1
  • links: PTS
  • area: contrib
  • in suites: lenny
  • size: 392 kB
  • ctags: 526
  • sloc: cpp: 3,316; ansic: 884; makefile: 127
file content (35 lines) | stat: -rw-r--r-- 751 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright 2005-6 Ben Hutchings <ben@decadent.org.uk>.
// See the file "COPYING" for licence details.

#include <cassert>
#include <cstddef>

#include "xml_utils.hpp"

std::string xml_escape(const std::string & str)
{
    std::string result;
    std::size_t begin = 0;

    for (;;)
    {
	std::size_t end = str.find_first_of("\"&'<>", begin);
	result.append(str, begin, end - begin);
	if (end == std::string::npos)
	    return result;

	const char * entity = NULL;
	switch (str[end])
	{
	case '"':  entity = "&quot;"; break;
	case '&':  entity = "&amp;";  break;
	case '\'': entity = "&apos;"; break;
	case '<':  entity = "&lt;";   break;
	case '>':  entity = "&gt;";   break;
	}
	assert(entity);
	result.append(entity);

	begin = end + 1;
    }
}