PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/dom - dom_iterators.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 0 153 0.0 %
Date: 2014-04-16 Functions: 0 10 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Christian Stocker <chregu@php.net>                          |
      16             :    |          Rob Richards <rrichards@php.net>                            |
      17             :    +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #ifdef HAVE_CONFIG_H
      23             : #include "config.h"
      24             : #endif
      25             : 
      26             : #include "php.h"
      27             : #if HAVE_LIBXML && HAVE_DOM
      28             : #include "php_dom.h"
      29             : #include "dom_ce.h"
      30             : 
      31             : typedef struct _nodeIterator nodeIterator;
      32             : struct _nodeIterator {
      33             :         int cur;
      34             :         int index;
      35             :         xmlNode *node;
      36             : };
      37             : 
      38             : typedef struct _notationIterator notationIterator;
      39             : struct _notationIterator {
      40             :         int cur;
      41             :         int index;
      42             :         xmlNotation *notation;
      43             : };
      44             : 
      45           0 : static void itemHashScanner (void *payload, void *data, xmlChar *name) /* {{{ */
      46             : {
      47           0 :         nodeIterator *priv = (nodeIterator *)data;
      48             : 
      49           0 :         if(priv->cur < priv->index) {
      50           0 :                 priv->cur++;
      51             :         } else {
      52           0 :                 if(priv->node == NULL) {
      53           0 :                         priv->node = (xmlNode *)payload;
      54             :                 }
      55             :         }
      56           0 : }
      57             : /* }}} */
      58             : 
      59           0 : xmlNodePtr create_notation(const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID) /* {{{ */
      60             : {
      61             :         xmlEntityPtr ret;
      62             : 
      63           0 :         ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
      64           0 :     memset(ret, 0, sizeof(xmlEntity));
      65           0 :     ret->type = XML_NOTATION_NODE;
      66           0 :     ret->name = xmlStrdup(name);
      67           0 :         ret->ExternalID = xmlStrdup(ExternalID);
      68           0 :         ret->SystemID = xmlStrdup(SystemID);
      69           0 :         ret->length = 0;
      70           0 :         ret->content = NULL;
      71           0 :         ret->URI = NULL;
      72           0 :         ret->orig = NULL;
      73           0 :         ret->children = NULL;
      74           0 :         ret->parent = NULL;
      75           0 :         ret->doc = NULL;
      76           0 :         ret->_private = NULL;
      77           0 :         ret->last = NULL;
      78           0 :         ret->prev = NULL;
      79           0 :         return((xmlNodePtr) ret);
      80             : }
      81             : /* }}} */
      82             : 
      83           0 : xmlNode *php_dom_libxml_hash_iter(xmlHashTable *ht, int index) /* {{{ */
      84             : {
      85           0 :         xmlNode *nodep = NULL;
      86             :         nodeIterator *iter;
      87             :         int htsize;
      88             : 
      89           0 :         if ((htsize = xmlHashSize(ht)) > 0 && index < htsize) {
      90           0 :                 iter = emalloc(sizeof(nodeIterator));
      91           0 :                 iter->cur = 0;
      92           0 :                 iter->index = index;
      93           0 :                 iter->node = NULL;
      94           0 :                 xmlHashScan(ht, itemHashScanner, iter);
      95           0 :                 nodep = iter->node;
      96           0 :                 efree(iter);
      97           0 :                 return nodep;
      98             :         } else {
      99           0 :                 return NULL;
     100             :         }
     101             : }
     102             : /* }}} */
     103             : 
     104           0 : xmlNode *php_dom_libxml_notation_iter(xmlHashTable *ht, int index) /* {{{ */
     105             : {
     106             :         notationIterator *iter;
     107           0 :         xmlNotation *notep = NULL;
     108             :         int htsize;
     109             : 
     110           0 :         if ((htsize = xmlHashSize(ht)) > 0 && index < htsize) {
     111           0 :                 iter = emalloc(sizeof(notationIterator));
     112           0 :                 iter->cur = 0;
     113           0 :                 iter->index = index;
     114           0 :                 iter->notation = NULL;
     115           0 :                 xmlHashScan(ht, itemHashScanner, iter);
     116           0 :                 notep = iter->notation;
     117           0 :                 efree(iter);
     118           0 :                 return create_notation(notep->name, notep->PublicID, notep->SystemID);
     119             :         } else {
     120           0 :                 return NULL;
     121             :         }
     122             : }
     123             : /* }}} */
     124             : 
     125           0 : static void php_dom_iterator_dtor(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
     126             : {
     127           0 :         php_dom_iterator *iterator = (php_dom_iterator *)iter;
     128             : 
     129           0 :         zval_ptr_dtor((zval**)&iterator->intern.data);
     130             : 
     131           0 :         if (iterator->curobj) {
     132           0 :                 zval_ptr_dtor((zval**)&iterator->curobj);
     133             :         }
     134             : 
     135           0 :         efree(iterator);
     136           0 : }
     137             : /* }}} */
     138             : 
     139           0 : static int php_dom_iterator_valid(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
     140             : {
     141             : 
     142           0 :         php_dom_iterator *iterator = (php_dom_iterator *)iter;
     143             : 
     144           0 :         if (iterator->curobj) {
     145           0 :                 return SUCCESS;
     146             :         } else {
     147           0 :                 return FAILURE;
     148             :         }
     149             : }
     150             : /* }}} */
     151             : 
     152           0 : static void php_dom_iterator_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC) /* {{{ */
     153             : {
     154           0 :         php_dom_iterator *iterator = (php_dom_iterator *)iter;
     155             : 
     156           0 :         *data = &iterator->curobj;
     157           0 : }
     158             : /* }}} */
     159             : 
     160           0 : static void php_dom_iterator_current_key(zend_object_iterator *iter, zval *key TSRMLS_DC) /* {{{ */
     161             : {
     162           0 :         php_dom_iterator *iterator = (php_dom_iterator *)iter;
     163           0 :         zval *object = (zval *)iterator->intern.data;
     164             : 
     165           0 :         if (instanceof_function(Z_OBJCE_P(object), dom_nodelist_class_entry TSRMLS_CC)) {
     166           0 :                 ZVAL_LONG(key, iter->index);
     167             :         } else {
     168           0 :                 dom_object *intern = (dom_object *)zend_object_store_get_object(iterator->curobj TSRMLS_CC);
     169             : 
     170           0 :                 if (intern != NULL && intern->ptr != NULL) {
     171           0 :                         xmlNodePtr curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->ptr)->node;
     172           0 :                         ZVAL_STRINGL(key, (char *) curnode->name, xmlStrlen(curnode->name), 1);
     173             :                 } else {
     174           0 :                         ZVAL_NULL(key);
     175             :                 }
     176             :         }
     177           0 : }
     178             : /* }}} */
     179             : 
     180           0 : static void php_dom_iterator_move_forward(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
     181             : {
     182           0 :         zval *curobj, *curattr = NULL;
     183             :         zval *object;
     184           0 :         xmlNodePtr curnode = NULL, basenode;
     185             :         dom_object *intern;
     186             :         dom_object *nnmap;
     187             :         dom_nnodemap_object *objmap;
     188           0 :         int ret, previndex=0;
     189             :         HashTable *nodeht;
     190             :         zval **entry;
     191             : 
     192           0 :         php_dom_iterator *iterator = (php_dom_iterator *)iter;
     193             : 
     194           0 :         object = (zval *)iterator->intern.data;
     195           0 :         nnmap = (dom_object *)zend_object_store_get_object(object TSRMLS_CC);
     196           0 :         objmap = (dom_nnodemap_object *)nnmap->ptr;
     197             : 
     198           0 :         curobj = iterator->curobj;
     199           0 :         intern = (dom_object *)zend_object_store_get_object(curobj TSRMLS_CC);
     200           0 :         if (intern != NULL && intern->ptr != NULL) {
     201           0 :                 if (objmap->nodetype != XML_ENTITY_NODE && 
     202           0 :                         objmap->nodetype != XML_NOTATION_NODE) {
     203           0 :                         if (objmap->nodetype == DOM_NODESET) {
     204           0 :                                 nodeht = HASH_OF(objmap->baseobjptr);
     205           0 :                                 zend_hash_move_forward(nodeht);
     206           0 :                                 if (zend_hash_get_current_data(nodeht, (void **) &entry)==SUCCESS) {
     207           0 :                                         curattr = *entry;
     208             :                                         Z_ADDREF_P(curattr);
     209             :                                 }
     210             :                         } else {
     211           0 :                                 curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->ptr)->node;
     212           0 :                                 if (objmap->nodetype == XML_ATTRIBUTE_NODE || 
     213           0 :                                         objmap->nodetype == XML_ELEMENT_NODE) {
     214           0 :                                         curnode = curnode->next;
     215             :                                 } else {
     216             :                                         /* Nav the tree evey time as this is LIVE */
     217           0 :                                         basenode = dom_object_get_node(objmap->baseobj);
     218           0 :                                         if (basenode && (basenode->type == XML_DOCUMENT_NODE || 
     219           0 :                                                 basenode->type == XML_HTML_DOCUMENT_NODE)) {
     220           0 :                                                 basenode = xmlDocGetRootElement((xmlDoc *) basenode);
     221           0 :                                         } else if (basenode) {
     222           0 :                                                 basenode = basenode->children;
     223             :                                         } else {
     224           0 :                                                 goto err;
     225             :                                         }
     226           0 :                                         curnode = dom_get_elements_by_tag_name_ns_raw(basenode, objmap->ns, objmap->local, &previndex, iter->index);
     227             :                                 }
     228             :                         }
     229             :                 } else {
     230           0 :                         if (objmap->nodetype == XML_ENTITY_NODE) {
     231           0 :                                 curnode = php_dom_libxml_hash_iter(objmap->ht, iter->index);
     232             :                         } else {
     233           0 :                                 curnode = php_dom_libxml_notation_iter(objmap->ht, iter->index);
     234             :                         }
     235             :                 }
     236             :         }
     237             : err:
     238           0 :         zval_ptr_dtor((zval**)&curobj);
     239           0 :         if (curnode) {
     240           0 :                 MAKE_STD_ZVAL(curattr);
     241           0 :                 curattr = php_dom_create_object(curnode, &ret, curattr, objmap->baseobj TSRMLS_CC);
     242             :         }
     243             : 
     244           0 :         iterator->curobj = curattr;
     245           0 : }
     246             : /* }}} */
     247             : 
     248             : zend_object_iterator_funcs php_dom_iterator_funcs = {
     249             :         php_dom_iterator_dtor,
     250             :         php_dom_iterator_valid,
     251             :         php_dom_iterator_current_data,
     252             :         php_dom_iterator_current_key,
     253             :         php_dom_iterator_move_forward,
     254             :         NULL
     255             : };
     256             : 
     257           0 : zend_object_iterator *php_dom_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC) /* {{{ */
     258             : {
     259             :         dom_object *intern;
     260             :         dom_nnodemap_object *objmap;
     261           0 :         xmlNodePtr nodep, curnode=NULL;
     262           0 :         zval *curattr = NULL;
     263           0 :         int ret, curindex = 0;
     264             :         HashTable *nodeht;
     265             :         zval **entry;
     266             :         php_dom_iterator *iterator;
     267             : 
     268           0 :         if (by_ref) {
     269           0 :                 zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
     270             :         }
     271           0 :         iterator = emalloc(sizeof(php_dom_iterator));
     272             : 
     273             :         Z_ADDREF_P(object);
     274           0 :         iterator->intern.data = (void*)object;
     275           0 :         iterator->intern.funcs = &php_dom_iterator_funcs;
     276             : 
     277           0 :         intern = (dom_object *)zend_object_store_get_object(object TSRMLS_CC);
     278           0 :         objmap = (dom_nnodemap_object *)intern->ptr;
     279           0 :         if (objmap != NULL) {
     280           0 :                 if (objmap->nodetype != XML_ENTITY_NODE && 
     281           0 :                         objmap->nodetype != XML_NOTATION_NODE) {
     282           0 :                         if (objmap->nodetype == DOM_NODESET) {
     283           0 :                                 nodeht = HASH_OF(objmap->baseobjptr);
     284           0 :                                 zend_hash_internal_pointer_reset(nodeht);
     285           0 :                                 if (zend_hash_get_current_data(nodeht, (void **) &entry)==SUCCESS) {
     286           0 :                                         curattr = *entry;
     287             :                                         Z_ADDREF_P(curattr);
     288             :                                 }
     289             :                         } else {
     290           0 :                                 nodep = (xmlNode *)dom_object_get_node(objmap->baseobj);
     291           0 :                                 if (!nodep) {
     292           0 :                                         goto err;
     293             :                                 }
     294           0 :                                 if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
     295           0 :                                         if (objmap->nodetype == XML_ATTRIBUTE_NODE) {
     296           0 :                                                 curnode = (xmlNodePtr) nodep->properties;
     297             :                                         } else {
     298           0 :                                                 curnode = (xmlNodePtr) nodep->children;
     299             :                                         }
     300             :                                 } else {
     301           0 :                                         if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
     302           0 :                                                 nodep = xmlDocGetRootElement((xmlDoc *) nodep);
     303             :                                         } else {
     304           0 :                                                 nodep = nodep->children;
     305             :                                         }
     306           0 :                                         curnode = dom_get_elements_by_tag_name_ns_raw(nodep, objmap->ns, objmap->local, &curindex, 0);
     307             :                                 }
     308             :                         }
     309             :                 } else {
     310           0 :                         if (objmap->nodetype == XML_ENTITY_NODE) {
     311           0 :                                 curnode = php_dom_libxml_hash_iter(objmap->ht, 0);
     312             :                         } else {
     313           0 :                                 curnode = php_dom_libxml_notation_iter(objmap->ht, 0);
     314             :                         }
     315             :                 }
     316             :         }
     317             : err:
     318           0 :         if (curnode) {
     319           0 :                 MAKE_STD_ZVAL(curattr);
     320           0 :                 curattr = php_dom_create_object(curnode, &ret, curattr, objmap->baseobj TSRMLS_CC);
     321             :         }
     322             : 
     323           0 :         iterator->curobj = curattr;
     324             : 
     325           0 :         return (zend_object_iterator*)iterator;
     326             : }
     327             : /* }}} */
     328             : 
     329             : #endif
     330             : 
     331             : /*
     332             :  * Local variables:
     333             :  * tab-width: 4
     334             :  * c-basic-offset: 4
     335             :  * End:
     336             :  * vim600: noet sw=4 ts=4 fdm=marker
     337             :  * vim<600: noet sw=4 ts=4
     338             :  */

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:47:46 +0000 (9 hours ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.