From 6b110bebb8930b171145597cfa37ff1c1ac753f7 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Mon, 6 Dec 2010 23:15:39 +0000 Subject: Simplify DOMImplementation API by replacing dom_strings with const char * svn path=/trunk/libdom/; revision=11024 --- Makefile | 2 +- bindings/hubbub/parser.c | 34 +++++----- bindings/xml/xmlparser.c | 45 ++----------- include/dom/core/implementation.h | 11 ++-- src/core/implementation.c | 134 ++++++++++++++++++++++++++++++-------- src/core/node.c | 6 +- test/testutils/domtsasserts.c | 25 ++----- test/testutils/domtsasserts.h | 2 +- 8 files changed, 147 insertions(+), 112 deletions(-) diff --git a/Makefile b/Makefile index 84d78fe..8da3d46 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ TESTRUNNER := $(PERL) build/testtools/testrunner.pl WARNFLAGS := -Wall -W -Wundef -Wpointer-arith -Wcast-align \ -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes \ -Wmissing-declarations -Wnested-externs -Werror -pedantic -CFLAGS := -std=c99 -D_BSD_SOURCE -I$(CURDIR)/include/ \ +CFLAGS := -std=c99 -D_BSD_SOURCE -D_GNU_SOURCE -I$(CURDIR)/include/ \ -I$(CURDIR)/src -I$(CURDIR)/binding $(WARNFLAGS) $(CFLAGS) # Parserutils & wapcaplet diff --git a/bindings/hubbub/parser.c b/bindings/hubbub/parser.c index 5566662..97d3e24 100644 --- a/bindings/hubbub/parser.c +++ b/bindings/hubbub/parser.c @@ -316,42 +316,38 @@ static hubbub_error create_doctype(void *parser, const hubbub_doctype *doctype, { dom_hubbub_parser *dom_parser = (dom_hubbub_parser *) parser; dom_exception err; - struct dom_string *qname, *public_id = NULL, *system_id = NULL; + char *qname, *public_id = NULL, *system_id = NULL; struct dom_document_type *dtype; *result = NULL; - err = dom_string_create(dom_parser->alloc, dom_parser->pw, - doctype->name.ptr, doctype->name.len, &qname); - if (err != DOM_NO_ERR) { + qname = strndup((const char *) doctype->name.ptr, + (size_t) doctype->name.len); + if (qname == NULL) { dom_parser->msg(DOM_MSG_CRITICAL, dom_parser->mctx, "Can't create doctype name"); goto fail; } if (doctype->public_missing == false) { - err = dom_string_create(dom_parser->alloc, dom_parser->pw, - doctype->public_id.ptr, - doctype->public_id.len, &public_id); + public_id = strndup((const char *) doctype->public_id.ptr, + (size_t) doctype->public_id.len); } else { - err = dom_string_create(dom_parser->alloc, dom_parser->pw, - NULL, 0, &public_id); + public_id = strdup(""); } - if (err != DOM_NO_ERR) { + if (public_id == NULL) { dom_parser->msg(DOM_MSG_CRITICAL, dom_parser->mctx, "Can't create doctype public id"); goto clean1; } if (doctype->system_missing == false) { - err = dom_string_create(dom_parser->alloc, dom_parser->pw, - doctype->system_id.ptr, - doctype->system_id.len, &system_id); + system_id = strndup((const char *) doctype->system_id.ptr, + (size_t) doctype->system_id.len); } else { - err = dom_string_create(dom_parser->alloc, dom_parser->pw, - NULL, 0, &system_id); + system_id = strdup(""); } - if (err != DOM_NO_ERR) { + if (system_id == NULL) { dom_parser->msg(DOM_MSG_CRITICAL, dom_parser->mctx, "Can't create doctype system id"); goto clean2; @@ -369,13 +365,13 @@ static hubbub_error create_doctype(void *parser, const hubbub_doctype *doctype, *result = dtype; clean3: - dom_string_unref(system_id); + free(system_id); clean2: - dom_string_unref(public_id); + free(public_id); clean1: - dom_string_unref(qname); + free(qname); fail: if (*result == NULL) diff --git a/bindings/xml/xmlparser.c b/bindings/xml/xmlparser.c index f8b3cb9..ff1f9d6 100644 --- a/bindings/xml/xmlparser.c +++ b/bindings/xml/xmlparser.c @@ -1163,63 +1163,30 @@ void xml_parser_add_document_type(dom_xml_parser *parser, { xmlDtdPtr dtd = (xmlDtdPtr) child; struct dom_document_type *doctype, *ins_doctype = NULL; - struct dom_string *qname, *public_id, *system_id; + const char *qname, *public_id, *system_id; dom_exception err; /* Create qname for doctype */ - err = _dom_document_create_string(parser->doc, dtd->name, - strlen((const char *) dtd->name), &qname); - if (err != DOM_NO_ERR) { - parser->msg(DOM_MSG_CRITICAL, parser->mctx, - "No memory for doctype name"); - return; - } + qname = (const char *) dtd->name; /* Create public ID for doctype */ - err = _dom_document_create_string(parser->doc, - dtd->ExternalID, - (dtd->ExternalID == NULL) ? 0 - : strlen((const char *) dtd->ExternalID), - &public_id); - if (err != DOM_NO_ERR) { - dom_string_unref(qname); - parser->msg(DOM_MSG_CRITICAL, parser->mctx, - "No memory for doctype public id"); - return; - } + public_id = dtd->ExternalID != NULL ? + (const char *) dtd->ExternalID : ""; /* Create system ID for doctype */ - err = _dom_document_create_string(parser->doc, - dtd->SystemID, - (dtd->SystemID == NULL) ? 0 - : strlen((const char *) dtd->SystemID), - &system_id); - if (err != DOM_NO_ERR) { - dom_string_unref(public_id); - dom_string_unref(qname); - parser->msg(DOM_MSG_CRITICAL, parser->mctx, - "No memory for doctype system id"); - return; - } + system_id = dtd->SystemID != NULL ? + (const char *) dtd->SystemID : ""; /* Create doctype */ err = dom_implementation_create_document_type( qname, public_id, system_id, parser->alloc, parser->pw, &doctype); if (err != DOM_NO_ERR) { - dom_string_unref(system_id); - dom_string_unref(public_id); - dom_string_unref(qname); parser->msg(DOM_MSG_CRITICAL, parser->mctx, "Failed to create document type"); return; } - /* No longer need qname, public_id, system_id */ - dom_string_unref(system_id); - dom_string_unref(public_id); - dom_string_unref(qname); - /* Add doctype to document */ err = dom_node_append_child(parent, (struct dom_node *) doctype, (struct dom_node **) (void *) &ins_doctype); diff --git a/include/dom/core/implementation.h b/include/dom/core/implementation.h index 6fb381c..6b3f143 100644 --- a/include/dom/core/implementation.h +++ b/include/dom/core/implementation.h @@ -13,7 +13,6 @@ #include #include #include -#include struct dom_document; struct dom_document_type; @@ -21,24 +20,24 @@ struct dom_document_type; typedef const char *dom_implementation; dom_exception dom_implementation_has_feature( - struct dom_string *feature, struct dom_string *version, + const char *feature, const char *version, bool *result); dom_exception dom_implementation_create_document_type( - struct dom_string *qname, - struct dom_string *public_id, struct dom_string *system_id, + const char *qname, + const char *public_id, const char *system_id, dom_alloc alloc, void *pw, struct dom_document_type **doctype); dom_exception dom_implementation_create_document( - struct dom_string *namespace, struct dom_string *qname, + const char *namespace, const char *qname, struct dom_document_type *doctype, dom_alloc alloc, void *pw, dom_events_default_action_fetcher daf, struct dom_document **doc); dom_exception dom_implementation_get_feature( - struct dom_string *feature, struct dom_string *version, + const char *feature, const char *version, void **object); #endif diff --git a/src/core/implementation.c b/src/core/implementation.c index 4dc8cf3..58adaf3 100644 --- a/src/core/implementation.c +++ b/src/core/implementation.c @@ -5,6 +5,8 @@ * Copyright 2007 John-Mark Bell */ +#include + #include #include "core/document.h" @@ -24,7 +26,7 @@ * \return DOM_NO_ERR. */ dom_exception dom_implementation_has_feature( - struct dom_string *feature, struct dom_string *version, + const char *feature, const char *version, bool *result) { UNUSED(feature); @@ -57,39 +59,80 @@ dom_exception dom_implementation_has_feature( * finished with it. */ dom_exception dom_implementation_create_document_type( - struct dom_string *qname, struct dom_string *public_id, - struct dom_string *system_id, + const char *qname, const char *public_id, + const char *system_id, dom_alloc alloc, void *pw, struct dom_document_type **doctype) { struct dom_document_type *d; - struct dom_string *prefix = NULL, *lname = NULL; + dom_string *qname_s = NULL, *prefix = NULL, *lname = NULL; + dom_string *public_id_s = NULL, *system_id_s = NULL; dom_exception err; - if (qname != NULL && _dom_validate_name(qname) == false) + if (qname != NULL) { + err = dom_string_create(alloc, pw, (const uint8_t *) qname, + strlen(qname), &qname_s); + if (err != DOM_NO_ERR) + return err; + } + + if (qname_s != NULL && _dom_validate_name(qname_s) == false) { + dom_string_unref(qname_s); return DOM_INVALID_CHARACTER_ERR; + } - err = _dom_namespace_split_qname(qname, &prefix, &lname); - if (err != DOM_NO_ERR) + err = _dom_namespace_split_qname(qname_s, &prefix, &lname); + if (err != DOM_NO_ERR) { + dom_string_unref(qname_s); return err; + } if ((prefix != NULL && _dom_validate_ncname(prefix) == false) || - (lname != NULL && _dom_validate_ncname(lname) == false)) + (lname != NULL && + _dom_validate_ncname(lname) == false)) { + dom_string_unref(lname); + dom_string_unref(prefix); + dom_string_unref(qname_s); return DOM_NAMESPACE_ERR; + } + + if (public_id != NULL) { + err = dom_string_create(alloc, pw, (const uint8_t *) public_id, + strlen(public_id), &public_id_s); + if (err != DOM_NO_ERR) { + dom_string_unref(lname); + dom_string_unref(prefix); + dom_string_unref(qname_s); + return err; + } + } + + if (system_id != NULL) { + err = dom_string_create(alloc, pw, (const uint8_t *) system_id, + strlen(system_id), &system_id_s); + if (err != DOM_NO_ERR) { + dom_string_unref(public_id_s); + dom_string_unref(lname); + dom_string_unref(prefix); + dom_string_unref(qname_s); + return err; + } + } /* Create the doctype */ - err = _dom_document_type_create(qname, public_id, system_id, + err = _dom_document_type_create(qname_s, public_id_s, system_id_s, alloc, pw, &d); - if (err != DOM_NO_ERR) - return err; - *doctype = d; - if (prefix != NULL) - dom_string_unref(prefix); - if (lname != NULL) - dom_string_unref(lname); + if (err == DOM_NO_ERR) + *doctype = d; - return DOM_NO_ERR; + dom_string_unref(system_id_s); + dom_string_unref(public_id_s); + dom_string_unref(prefix); + dom_string_unref(lname); + dom_string_unref(qname_s); + + return err; } /** @@ -125,29 +168,58 @@ dom_exception dom_implementation_create_document_type( * finished with it. */ dom_exception dom_implementation_create_document( - struct dom_string *namespace, struct dom_string *qname, + const char *namespace, const char *qname, struct dom_document_type *doctype, dom_alloc alloc, void *pw, dom_events_default_action_fetcher daf, struct dom_document **doc) { struct dom_document *d; + dom_string *namespace_s = NULL, *qname_s = NULL; dom_exception err; - if (qname != NULL && _dom_validate_name(qname) == false) - return DOM_INVALID_CHARACTER_ERR; + if (namespace != NULL) { + err = dom_string_create(alloc, pw, (const uint8_t *) namespace, + strlen(namespace), &namespace_s); + if (err != DOM_NO_ERR) + return err; + } + + if (qname != NULL) { + err = dom_string_create(alloc, pw, (const uint8_t *) qname, + strlen(qname), &qname_s); + if (err != DOM_NO_ERR) { + dom_string_unref(namespace_s); + return err; + } + } + + if (qname_s != NULL && _dom_validate_name(qname_s) == false) { + dom_string_unref(qname_s); + dom_string_unref(namespace_s); + return DOM_INVALID_CHARACTER_ERR; + } - err = _dom_namespace_validate_qname(qname, namespace); - if (err != DOM_NO_ERR) + err = _dom_namespace_validate_qname(qname_s, namespace_s); + if (err != DOM_NO_ERR) { + dom_string_unref(qname_s); + dom_string_unref(namespace_s); return DOM_NAMESPACE_ERR; + } - if (doctype != NULL && dom_node_get_parent(doctype) != NULL) + if (doctype != NULL && dom_node_get_parent(doctype) != NULL) { + dom_string_unref(qname_s); + dom_string_unref(namespace_s); return DOM_WRONG_DOCUMENT_ERR; + } /* Create document object */ err = _dom_document_create(alloc, pw, daf, &d); - if (err != DOM_NO_ERR) + if (err != DOM_NO_ERR) { + dom_string_unref(qname_s); + dom_string_unref(namespace_s); return err; + } /* Set its doctype, if necessary */ if (doctype != NULL) { @@ -157,6 +229,8 @@ dom_exception dom_implementation_create_document( (struct dom_node *) doctype, &ins_doctype); if (err != DOM_NO_ERR) { dom_node_unref((struct dom_node *) d); + dom_string_unref(qname_s); + dom_string_unref(namespace_s); return err; } @@ -166,13 +240,15 @@ dom_exception dom_implementation_create_document( } /* Create root element and attach it to document */ - if (qname != NULL) { + if (qname_s != NULL) { struct dom_element *e; struct dom_node *inserted; err = dom_document_create_element_ns(d, namespace, qname, &e); if (err != DOM_NO_ERR) { dom_node_unref((struct dom_node *) d); + dom_string_unref(qname_s); + dom_string_unref(namespace_s); return err; } @@ -181,6 +257,8 @@ dom_exception dom_implementation_create_document( if (err != DOM_NO_ERR) { dom_node_unref((struct dom_node *) e); dom_node_unref((struct dom_node *) d); + dom_string_unref(qname_s); + dom_string_unref(namespace_s); return err; } @@ -191,6 +269,10 @@ dom_exception dom_implementation_create_document( dom_node_unref((struct dom_node *) e); } + /* Clean up strings we created */ + dom_string_unref(qname_s); + dom_string_unref(namespace_s); + *doc = d; return DOM_NO_ERR; @@ -209,7 +291,7 @@ dom_exception dom_implementation_create_document( * the provided memory (de)allocation function. */ dom_exception dom_implementation_get_feature( - struct dom_string *feature, struct dom_string *version, + const char *feature, const char *version, void **object) { UNUSED(feature); diff --git a/src/core/node.c b/src/core/node.c index c10f740..71977b4 100644 --- a/src/core/node.c +++ b/src/core/node.c @@ -1299,7 +1299,8 @@ dom_exception _dom_node_is_supported(dom_node_internal *node, UNUSED(node); - dom_implementation_has_feature(feature, version, &has); + dom_implementation_has_feature(_dom_string_data(feature), + _dom_string_data(version), &has); *result = has; @@ -1792,7 +1793,8 @@ dom_exception _dom_node_get_feature(dom_node_internal *node, UNUSED(node); - dom_implementation_has_feature(feature, version, &has); + dom_implementation_has_feature(_dom_string_data(feature), + _dom_string_data(version), &has); if (has) { *result = node; diff --git a/test/testutils/domtsasserts.c b/test/testutils/domtsasserts.c index 488f294..03d0907 100644 --- a/test/testutils/domtsasserts.c +++ b/test/testutils/domtsasserts.c @@ -217,35 +217,24 @@ bool is_contenttype(const char *type) return false; } -bool has_feature(char *feature, char *version) +bool has_feature(const char *feature, const char *version) { dom_exception err; bool ret; - dom_string *df, *dv; - err = dom_string_create(myrealloc, NULL, (const uint8_t *)feature, - feature == NULL ? 0 : strlen(feature), &df); - if (err != DOM_NO_ERR) - return false; - - err = dom_string_create(myrealloc, NULL, (const uint8_t *)version, - version == NULL ? 0 : strlen(version), &dv); - if (err != DOM_NO_ERR) { - dom_string_unref(df); - return false; - } + if (feature == NULL) + feature = ""; + + if (version == NULL) + version = ""; - err = dom_implementation_has_feature(df, dv, &ret); + err = dom_implementation_has_feature(feature, version, &ret); /* Here, when we come with exception, we should return false, * TODO: this need to be improved, but I can't figure out how */ if (err != DOM_NO_ERR) { - dom_string_unref(df); - dom_string_unref(dv); return false; } - dom_string_unref(df); - dom_string_unref(dv); return ret; } diff --git a/test/testutils/domtsasserts.h b/test/testutils/domtsasserts.h index bb39fe5..c301d88 100644 --- a/test/testutils/domtsasserts.h +++ b/test/testutils/domtsasserts.h @@ -55,7 +55,7 @@ bool is_uri_equals(char *scheme, char *path, char *host, bool is_contenttype(const char *type); -bool has_feature(char *feature, char *version); +bool has_feature(const char *feature, const char *version); bool implementation_attribute(char *name, bool value); -- cgit v1.2.3