diff options
Diffstat (limited to 'src/xml')
| -rw-r--r-- | src/xml/quote.cpp | 118 | ||||
| -rw-r--r-- | src/xml/rebase-hrefs.cpp | 6 | ||||
| -rw-r--r-- | src/xml/repr-io.cpp | 18 | ||||
| -rw-r--r-- | src/xml/repr-util.cpp | 1 |
4 files changed, 68 insertions, 75 deletions
diff --git a/src/xml/quote.cpp b/src/xml/quote.cpp index 02c12dfb0..b889b890d 100644 --- a/src/xml/quote.cpp +++ b/src/xml/quote.cpp @@ -1,81 +1,77 @@ /** \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'. + * @brief XML quoting routines + *//* + * Authors: + * Krzysztof KosiĆski <tweenk.pl@gmail.com> + * + * This file is in the public domain. */ +#include "xml/quote.h" #include <cstring> #include <glib.h> -#include "quote.h" - -/** \return strlen(xml_quote_strdup(\a val)) (without doing the malloc). - * \pre val != NULL - */ -size_t -xml_quoted_strlen(char const *val) +/// Returns the length of the string after quoting the characters <code>"&<></code>. +size_t xml_quoted_strlen(char const *val) { - size_t ret = 0; - if (val != NULL) { - 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; - } + if (!val) return 0; + size_t len = 0; + + for (char const *valp = val; *valp; ++valp) { + switch (*valp) { + case '"': + len += 6; // " + break; + case '&': + len += 5; // & + break; + case '<': + case '>': + len += 4; // < or > + break; + default: + ++len; + break; } } - return ret; + return len; } -/** 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) +char *xml_quote_strdup(char const *src) { -#define COPY_LIT(_lit) do { \ - size_t cpylen = sizeof(_lit "") - 1; \ - memcpy(dest, _lit, cpylen); \ - dest += cpylen; \ - } while(0) + size_t len = xml_quoted_strlen(src); + char *result = static_cast<char*>(g_malloc(len + 1)); + char *resp = result; - 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; + for (char const *srcp = src; *srcp; ++srcp) { + switch(*srcp) { + case '"': + strcpy(resp, """); + resp += 6; + break; + case '&': + strcpy(resp, "&"); + resp += 5; + break; + case '<': + strcpy(resp, "<"); + resp += 4; + break; + case '>': + strcpy(resp, ">"); + resp += 4; + break; + default: + *resp++ = *srcp; + break; } } - *dest = '\0'; - -#undef COPY_LIT + *resp = 0; + return result; } -/** \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; -} +// quote: ", &, <, > + /* Local Variables: diff --git a/src/xml/rebase-hrefs.cpp b/src/xml/rebase-hrefs.cpp index 9d4f4f9fc..2bcae5d81 100644 --- a/src/xml/rebase-hrefs.cpp +++ b/src/xml/rebase-hrefs.cpp @@ -220,9 +220,9 @@ void Inkscape::XML::rebase_hrefs(SPDocument *const doc, gchar const *const new_b * * Note also that Inkscape only supports fragment hrefs (href="#pattern257") for many of these * cases. */ - GSList const *images = doc->getResourceList("image"); - for (GSList const *l = images; l != NULL; l = l->next) { - Inkscape::XML::Node *ir = static_cast<SPObject *>(l->data)->getRepr(); + std::set<SPObject *> images = doc->getResourceList("image"); + for (std::set<SPObject *>::const_iterator it = images.begin(); it != images.end(); ++it) { + Inkscape::XML::Node *ir = (*it)->getRepr(); std::string uri; { diff --git a/src/xml/repr-io.cpp b/src/xml/repr-io.cpp index a4146f215..4a6f59b43 100644 --- a/src/xml/repr-io.cpp +++ b/src/xml/repr-io.cpp @@ -38,6 +38,7 @@ #include "preferences.h" #include <glibmm/miscutils.h> +#include <map> using Inkscape::IO::Writer; using Inkscape::Util::List; @@ -50,8 +51,8 @@ using Inkscape::XML::calc_abs_doc_base; using Inkscape::XML::rebase_href_attrs; Document *sp_repr_do_read (xmlDocPtr doc, const gchar *default_ns); -static Node *sp_repr_svg_read_node (Document *xml_doc, xmlNodePtr node, const gchar *default_ns, GHashTable *prefix_map); -static gint sp_repr_qualified_name (gchar *p, gint len, xmlNsPtr ns, const xmlChar *name, const gchar *default_ns, GHashTable *prefix_map); +static Node *sp_repr_svg_read_node (Document *xml_doc, xmlNodePtr node, const gchar *default_ns, std::map<std::string, std::string> &prefix_map); +static gint sp_repr_qualified_name (gchar *p, gint len, xmlNsPtr ns, const xmlChar *name, const gchar *default_ns, std::map<std::string, std::string> &prefix_map); static void sp_repr_write_stream_root_element(Node *repr, Writer &out, bool add_whitespace, gchar const *default_ns, int inlineattrs, int indent, @@ -486,8 +487,7 @@ Document *sp_repr_do_read (xmlDocPtr doc, const gchar *default_ns) return NULL; } - GHashTable * prefix_map; - prefix_map = g_hash_table_new (g_str_hash, g_str_equal); + std::map<std::string, std::string> prefix_map; Document *rdoc = new Inkscape::XML::SimpleDocument(); @@ -536,21 +536,17 @@ Document *sp_repr_do_read (xmlDocPtr doc, const gchar *default_ns) } } - g_hash_table_destroy (prefix_map); - return rdoc; } -gint sp_repr_qualified_name (gchar *p, gint len, xmlNsPtr ns, const xmlChar *name, const gchar */*default_ns*/, GHashTable *prefix_map) +gint sp_repr_qualified_name (gchar *p, gint len, xmlNsPtr ns, const xmlChar *name, const gchar */*default_ns*/, std::map<std::string, std::string> &prefix_map) { const xmlChar *prefix; if (ns){ if (ns->href ) { prefix = reinterpret_cast<const xmlChar*>( sp_xml_ns_uri_prefix(reinterpret_cast<const gchar*>(ns->href), reinterpret_cast<const char*>(ns->prefix)) ); - void* p0 = reinterpret_cast<gpointer>(const_cast<xmlChar *>(prefix)); - void* p1 = reinterpret_cast<gpointer>(const_cast<xmlChar *>(ns->href)); - g_hash_table_insert( prefix_map, p0, p1 ); + prefix_map[reinterpret_cast<const char*>(prefix)] = reinterpret_cast<const char*>(ns->href); } else { prefix = NULL; @@ -567,7 +563,7 @@ gint sp_repr_qualified_name (gchar *p, gint len, xmlNsPtr ns, const xmlChar *nam } } -static Node *sp_repr_svg_read_node (Document *xml_doc, xmlNodePtr node, const gchar *default_ns, GHashTable *prefix_map) +static Node *sp_repr_svg_read_node (Document *xml_doc, xmlNodePtr node, const gchar *default_ns, std::map<std::string, std::string> &prefix_map) { xmlAttrPtr prop; xmlNodePtr child; diff --git a/src/xml/repr-util.cpp b/src/xml/repr-util.cpp index f7a437163..ce93bccab 100644 --- a/src/xml/repr-util.cpp +++ b/src/xml/repr-util.cpp @@ -528,6 +528,7 @@ unsigned int sp_repr_set_svg_double(Inkscape::XML::Node *repr, gchar const *key, { g_return_val_if_fail(repr != NULL, FALSE); g_return_val_if_fail(key != NULL, FALSE); + g_return_val_if_fail(val==val, FALSE);//tests for nan Inkscape::SVGOStringStream os; os << val; |
