cvs: php4 /ext/domxml php_domxml.c

From: Date: Fri, 10 Nov 2000 10:27:58 +0000
Subject: cvs: php4 /ext/domxml php_domxml.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-2549@lists.php.net to get a copy of this message
steinm Fri Nov 10 02:27:58 2000 EDT Modified files: /php4/ext/domxml php_domxml.c Log: - Fixed many problems to make xpath working - Each function now expects an object and not a resource anymore I hope this makes it more straightforward for the PHP-Programmer

Index: php4/ext/domxml/php_domxml.c diff -u php4/ext/domxml/php_domxml.c:1.16 php4/ext/domxml/php_domxml.c:1.17 --- php4/ext/domxml/php_domxml.c:1.16 Thu Nov 9 22:09:40 2000 +++ php4/ext/domxml/php_domxml.c Fri Nov 10 02:27:57 2000 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_domxml.c,v 1.16 2000/11/10 06:09:40 steinm Exp $ */ +/* $Id: php_domxml.c,v 1.17 2000/11/10 10:27:57 steinm Exp $ */ #include "php.h" @@ -79,6 +79,7 @@ PHP_FALIAS(dumpmem, domxml_dumpmem, NULL) PHP_FALIAS(xpath_init, xpath_init, NULL) PHP_FALIAS(xpath_new_context, xpath_new_context, NULL) + PHP_FALIAS(xptr_new_context, xpath_new_context, NULL) {NULL, NULL, NULL} }; @@ -399,7 +400,7 @@ { /* don't know why that line was commented out in the previous version, so i left it (cmv) */ php_info_print_table_start(); - php_info_print_table_row(2, "DOM/XML Support", "enabled"); + php_info_print_table_row(2, "DOM/XML, XPath, XPointer Support", "enabled"); php_info_print_table_row(2, "libmxl Version", LIBXML_DOTTED_VERSION ); php_info_print_table_end(); } @@ -410,7 +411,7 @@ { zval *id, **tmp; int id_to_find; - xmlNode *nodep; + xmlNodePtr nodep; xmlAttr *attr; int type; int ret; @@ -422,23 +423,20 @@ php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } else { RETURN_FALSE; } } else if ((ZEND_NUM_ARGS() != 1) || getParameters(ht, 1, &id) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; + if (zend_hash_find(id->value.obj.properties, "node", sizeof("node"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } - nodep = (xmlNode *)zend_list_find(id_to_find, &type); - if (!nodep || type != le_domxmlnodep) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; - } - attr = nodep->properties; if (!attr) { RETURN_FALSE; @@ -510,21 +508,18 @@ php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } else { RETURN_FALSE; } } else if ((ZEND_NUM_ARGS() != 1) || getParameters(ht, 1, &id) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; - } - - nodep = (xmlNode *)zend_list_find(id_to_find, &type); - if (!nodep || type != le_domxmlnodep) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; + if (zend_hash_find(id->value.obj.properties, "node", sizeof("node"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } last = nodep->last; @@ -561,21 +556,18 @@ php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } else { RETURN_FALSE; } } else if ((ZEND_NUM_ARGS() != 1) || getParameters(ht, 1, &id) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; - } - - nodep = (xmlNode *)zend_list_find(id_to_find, &type); - if (!nodep || type != le_domxmlnodep) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; + if (zend_hash_find(id->value.obj.properties, "node", sizeof("node"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } last = nodep->parent; @@ -608,28 +600,28 @@ if (ZEND_NUM_ARGS() == 0) { id = getThis(); if (id) { - if ((zend_hash_find(id->value.obj.properties, "node", sizeof("node"), (void **)&tmp) == FAILURE) && - (zend_hash_find(id->value.obj.properties, "doc", sizeof("doc"), (void **)&tmp) == FAILURE)) { - php_error(E_WARNING, "unable to find my handle property"); - RETURN_FALSE; - } - id_to_find = (*tmp)->value.lval; + if (zend_hash_find(id->value.obj.properties, "node", sizeof("node"), (void **)&tmp) == FAILURE) { + if (zend_hash_find(id->value.obj.properties, "doc", sizeof("doc"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } else + ZEND_FETCH_RESOURCE(nodep,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) + } else + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } else { RETURN_FALSE; } } else if ((ZEND_NUM_ARGS() != 1) || getParameters(ht, 1, &id) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; + if ((zend_hash_find(id->value.obj.properties, "node", sizeof("node"), (void **)&tmp) == FAILURE) && + (zend_hash_find(id->value.obj.properties, "doc", sizeof("doc"), (void **)&tmp) == FAILURE)) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } - nodep = (xmlNode *)zend_list_find(id_to_find, &type); - if (!nodep || (type != le_domxmlnodep && type != le_domxmldocp)) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; - } - /* Even if the nodep is a XML_DOCUMENT_NODE the type is at the same position. */ @@ -681,24 +673,21 @@ php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } else { RETURN_FALSE; } - convert_to_string(arg1); } else if ((ZEND_NUM_ARGS() == 2) && getParameters(ht, 2, &id, &arg1) == SUCCESS) { - convert_to_long(id); - id_to_find = id->value.lval; - convert_to_string(arg1); + if (zend_hash_find(id->value.obj.properties, "node", sizeof("node"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } else { WRONG_PARAM_COUNT; } - nodep = (xmlNode *)zend_list_find(id_to_find, &type); - if (!nodep || type != le_domxmlnodep) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; - } + convert_to_string(arg1); value = xmlGetProp(nodep, arg1->value.str.val); if (!value) { @@ -718,7 +707,7 @@ xmlNode *nodep; xmlAttr *attr; int type; - + if ((ZEND_NUM_ARGS() == 2) && getParameters(ht, 2, &arg1, &arg2) == SUCCESS) { id = getThis(); if (id) { @@ -726,26 +715,22 @@ php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } else { RETURN_FALSE; } - convert_to_string(arg1); - convert_to_string(arg2); } else if ((ZEND_NUM_ARGS() == 3) && getParameters(ht, 3, &id, &arg1, &arg2) == SUCCESS) { - convert_to_long(id); - id_to_find = id->value.lval; - convert_to_string(arg1); - convert_to_string(arg2); + if (zend_hash_find(id->value.obj.properties, "node", sizeof("node"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } else { WRONG_PARAM_COUNT; } - nodep = (xmlNode *)zend_list_find(id_to_find, &type); - if (!nodep || type != le_domxmlnodep) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; - } + convert_to_string(arg1); + convert_to_string(arg2); attr = xmlSetProp(nodep, arg1->value.str.val, arg2->value.str.val); if (!attr) { @@ -770,24 +755,21 @@ id = getThis(); if (id) { if (zend_hash_find(id->value.obj.properties, "node", sizeof("node"), (void **)&tmp) == FAILURE) { - php_error(E_WARNING, "unable to find my node (%d)", id); + php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } else { RETURN_FALSE; } } else if ((ZEND_NUM_ARGS() != 1) || getParameters(ht, 1, &id) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; - } - - nodep = (xmlNode *)zend_list_find(id_to_find, &type); - if (!nodep || type != le_domxmlnodep) { - php_error(E_WARNING, "unable to find node identifier (%d)", id_to_find); - RETURN_FALSE; + if (zend_hash_find(id->value.obj.properties, "node", sizeof("node"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } if(0 > node_attributes(&return_value, nodep)) @@ -818,12 +800,12 @@ /* }}} */ /* {{{ proto string domxml_rootnew([int doc]) - Returns list of children nodes */ + Returns root nodes */ PHP_FUNCTION(domxml_rootnew) { zval *id, **tmp; int id_to_find; - xmlDoc *nodep; + xmlDoc *docp; xmlNode *last; int type; int ret; @@ -835,24 +817,21 @@ php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) } else { RETURN_FALSE; } } else if ((ZEND_NUM_ARGS() != 1) || getParameters(ht, 1, &id) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; - } - - nodep = (xmlDoc *)zend_list_find(id_to_find, &type); - if (!nodep || type != le_domxmldocp) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; + if (zend_hash_find(id->value.obj.properties, "doc", sizeof("doc"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) } - last = nodep->children; + last = docp->children; if (!last) { RETURN_FALSE; } @@ -898,21 +877,18 @@ php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) } else { RETURN_FALSE; } } else if ((ZEND_NUM_ARGS() != 1) || getParameters(ht, 1, &id) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; - } - - docp = (xmlDoc *)zend_list_find(id_to_find, &type); - if (!docp || type != le_domxmldocp) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; + if (zend_hash_find(id->value.obj.properties, "doc", sizeof("doc"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) } node = docp->children; @@ -957,21 +933,18 @@ php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) } else { RETURN_FALSE; } } else if ((ZEND_NUM_ARGS() != 1) || getParameters(ht, 1, &id) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; - } - - docp = (xmlDoc *)zend_list_find(id_to_find, &type); - if (!docp || type != le_domxmldocp) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; + if (zend_hash_find(id->value.obj.properties, "doc", sizeof("doc"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) } dtd = docp->intSubset; @@ -1010,21 +983,18 @@ php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) } else { RETURN_FALSE; } } else if ((ZEND_NUM_ARGS() != 1) || getParameters(ht, 1, &id) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; - } - - docp = (xmlDoc *)zend_list_find(id_to_find, &type); - if (!docp || type != le_domxmldocp) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; + if (zend_hash_find(id->value.obj.properties, "doc", sizeof("doc"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) } xmlDocDumpMemory(docp, &mem, &size); @@ -1111,7 +1081,7 @@ xmlNode *child, *nodep; int type; int ret; - + if (ZEND_NUM_ARGS() == 2) { id = getThis(); if (id) { @@ -1119,7 +1089,7 @@ php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) if(getParameters(ht, 2, &name, &content) == FAILURE) WRONG_PARAM_COUNT; } else { @@ -1128,18 +1098,15 @@ } else if ((ZEND_NUM_ARGS() != 3) || getParameters(ht, 3, &id, &name, &content) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; + if (zend_hash_find(id->value.obj.properties, "node", sizeof("node"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(nodep,xmlNodePtr,tmp,-1, "DomNode", le_domxmlnodep) } convert_to_string(name); convert_to_string(content); - nodep = (xmlNode *)zend_list_find(id_to_find, &type); - if (!nodep || type != le_domxmlnodep) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; - } - if(content->value.str.len) child = xmlNewChild(nodep, NULL, name->value.str.val, content->value.str.val); else @@ -1178,7 +1145,7 @@ php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) if (getParameters(ht, 1, &name) == FAILURE) WRONG_PARAM_COUNT; } else { @@ -1187,17 +1154,14 @@ } else if ((ZEND_NUM_ARGS() != 2) || getParameters(ht, 2, &id, &name) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; + if (zend_hash_find(id->value.obj.properties, "doc", sizeof("doc"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) } convert_to_string(name); - docp = (xmlDoc *)zend_list_find(id_to_find, &type); - if (!docp || type != le_domxmldocp) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; - } - node = xmlNewDocNode(docp, NULL, name->value.str.val, NULL); if (!node) { RETURN_FALSE; @@ -1345,10 +1309,11 @@ while(last) { zval *child; + int ret; /* Each child is a node object */ MAKE_STD_ZVAL(child); -/* ret = zend_list_insert(last, le_domxmlnodep); */ + ret = zend_list_insert(last, le_domxmlnodep); /* construct a node object for each child */ object_init_ex(child, domxmlnode_class_entry_ptr); @@ -1361,7 +1326,7 @@ add_property_long(child, "type", last->type); if(last->content) add_property_stringl(child, "content", (char *) last->content, strlen(last->content), 1); -/* add_property_resource(child, "node", ret); */ + add_property_resource(child, "node", ret); /* Get the namespace of the current node and add it as a property */ /* if(!node_namespace(&namespace, last)) @@ -1485,23 +1450,20 @@ php_error(E_WARNING, "unable to find my handle property"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) } else { RETURN_FALSE; } } else if ((ZEND_NUM_ARGS() != 1) || getParameters(ht, 1, &id) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; + if (zend_hash_find(id->value.obj.properties, "doc", sizeof("doc"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my handle property"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(docp,xmlDocPtr,tmp,-1, "DomDocument", le_domxmldocp) } - docp = (xmlDocPtr) zend_list_find(id_to_find, &type); - if (!docp || type != le_domxmldocp) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; - } - #if defined(LIBXML_XPTR_ENABLED) if(mode == PHP_XPTR) ctx = xmlXPtrNewContext(docp, NULL, NULL); @@ -1547,10 +1509,11 @@ id = getThis(); if (id) { if (zend_hash_find(id->value.obj.properties, "xpathctx", sizeof("xpathctx"), (void **)&tmp) == FAILURE) { - php_error(E_WARNING, "unable to find my handle property"); + php_error(E_WARNING, "unable to find my xpath context"); RETURN_FALSE; } - id_to_find = (*tmp)->value.lval; + ZEND_FETCH_RESOURCE(ctxp,xmlXPathContextPtr,tmp,-1, "XPathContext", le_xpathctxp) +// id_to_find = (*tmp)->value.lval; if (getParameters(ht, 1, &str) == FAILURE) WRONG_PARAM_COUNT; } else { @@ -1559,17 +1522,14 @@ } else if ((ZEND_NUM_ARGS() != 2) || getParameters(ht, 2, &id, &str) == FAILURE) { WRONG_PARAM_COUNT; } else { - convert_to_long(id); - id_to_find = id->value.lval; + if (zend_hash_find(id->value.obj.properties, "xpathctx", sizeof("xpathctx"), (void **)&tmp) == FAILURE) { + php_error(E_WARNING, "unable to find my own xpath context"); + RETURN_FALSE; + } + ZEND_FETCH_RESOURCE(ctxp,xmlXPathContextPtr,tmp,-1, "XPathContext", le_xpathctxp) } convert_to_string(str); - ctxp = (xmlXPathContextPtr) zend_list_find(id_to_find, &type); - if (!ctxp || type != le_xpathctxp) { - php_error(E_WARNING, "unable to find identifier (%d)", id_to_find); - RETURN_FALSE; - } - #if defined(LIBXML_XPTR_ENABLED) if(mode == PHP_XPTR) { xpathobjp = xmlXPtrEval(BAD_CAST str->value.str.val, ctxp); @@ -1602,6 +1562,7 @@ zval *arr; xmlNodeSetPtr nodesetp; + MAKE_STD_ZVAL(arr); if (array_init(arr) == FAILURE) { xmlXPathFreeObject(xpathobjp); RETURN_FALSE;
« previous php.cvs (#2549) next »