From 179fa413b047bede6e32109e2ce82437c5fb8d34 Mon Sep 17 00:00:00 2001 From: MenTaLguY Date: Mon, 16 Jan 2006 02:36:01 +0000 Subject: moving trunk for module inkscape (bzr r1) --- src/xml/quote.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/xml/quote.cpp (limited to 'src/xml/quote.cpp') diff --git a/src/xml/quote.cpp b/src/xml/quote.cpp new file mode 100644 index 000000000..b5505c5e1 --- /dev/null +++ b/src/xml/quote.cpp @@ -0,0 +1,86 @@ +/** \file + * XML quoting routines. + */ + +/* Based on Lauris' repr_quote_write in repr-io.cpp. + * + * Copyright (C) 1999-2002 Lauris Kaplinski + * Copyright (C) 2004 Monash University + * + * May be modified and/or redistributed under the terms of version 2 + * of the GNU General Public License: see the file `COPYING'. + */ + +#include +#include + + +/** \return strlen(xml_quote_strdup(\a val)) (without doing the malloc). + * \pre val != NULL + */ +static size_t +xml_quoted_strlen(char const *val) +{ + size_t ret = 0; + for (; *val != '\0'; val++) { + switch (*val) { + case '"': ret += sizeof(""") - 1; break; + case '&': ret += sizeof("&") - 1; break; + case '<': ret += sizeof("<") - 1; break; + case '>': ret += sizeof(">") - 1; break; + default: ++ret; break; + } + } + return ret; +} + +/** Writes \a src (including the NUL byte) to \a dest, doing XML quoting as necessary. + * + * \pre \a src != NULL. + * \pre \a dest must have enough space for (xml_quoted_strlen(src) + 1) bytes. + */ +static void +xml_quote(char *dest, char const *src) +{ +#define COPY_LIT(_lit) do { \ + size_t cpylen = sizeof(_lit "") - 1; \ + memcpy(dest, _lit, cpylen); \ + dest += cpylen; \ + } while(0) + + for (; *src != '\0'; ++src) { + switch (*src) { + case '"': COPY_LIT("""); break; + case '&': COPY_LIT("&"); break; + case '<': COPY_LIT("<"); break; + case '>': COPY_LIT(">"); break; + default: *dest++ = *src; break; + } + } + *dest = '\0'; + +#undef COPY_LIT +} + +/** \return A g_malloc'd buffer containing an XML-quoted version of \a src. + * \pre src != NULL. + */ +char * +xml_quote_strdup(char const *src) +{ + size_t const quoted_size = xml_quoted_strlen(src) + 1; + char *ret = (char *) g_malloc(quoted_size); + xml_quote(ret, src); + return ret; +} + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : -- cgit v1.2.3