php-src/ext/simplexml/sxe.c

187 lines
5.6 KiB
C
Raw Normal View History

2004-01-18 23:33:38 +08:00
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
2011-01-01 10:17:06 +08:00
| Copyright (c) 1997-2011 The PHP Group |
2004-01-18 23:33:38 +08:00
+----------------------------------------------------------------------+
2006-01-01 20:51:34 +08:00
| This source file is subject to version 3.01 of the PHP license, |
2004-01-18 23:33:38 +08:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2006-01-01 20:51:34 +08:00
| http://www.php.net/license/3_01.txt |
2004-01-18 23:33:38 +08:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Marcus Boerger <helly@php.net> |
+----------------------------------------------------------------------+
*/
2004-01-21 04:59:45 +08:00
/* $Id$ */
2004-01-18 23:33:38 +08:00
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "zend_interfaces.h"
#include "ext/spl/php_spl.h"
#include "ext/spl/spl_iterators.h"
#include "sxe.h"
2004-01-18 23:33:38 +08:00
zend_class_entry *ce_SimpleXMLIterator = NULL;
zend_class_entry *ce_SimpleXMLElement;
2004-01-18 23:33:38 +08:00
#include "php_simplexml_exports.h"
2004-01-18 23:33:38 +08:00
2006-03-06 17:50:44 +08:00
/* {{{ proto void SimpleXMLIterator::rewind()
Rewind to first element */
PHP_METHOD(ce_SimpleXMLIterator, rewind)
2004-01-18 23:33:38 +08:00
{
php_sxe_iterator iter;
2004-01-18 23:33:38 +08:00
iter.sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
ce_SimpleXMLElement->iterator_funcs.funcs->rewind((zend_object_iterator*)&iter TSRMLS_CC);
2004-01-18 23:33:38 +08:00
}
/* }}} */
2006-03-06 17:50:44 +08:00
/* {{{ proto bool SimpleXMLIterator::valid()
Check whether iteration is valid */
PHP_METHOD(ce_SimpleXMLIterator, valid)
2004-01-18 23:33:38 +08:00
{
php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
RETURN_BOOL(sxe->iter.data);
}
/* }}} */
2006-03-06 17:50:44 +08:00
/* {{{ proto SimpleXMLIterator SimpleXMLIterator::current()
Get current element */
PHP_METHOD(ce_SimpleXMLIterator, current)
2004-01-18 23:33:38 +08:00
{
php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
2004-01-19 00:25:26 +08:00
if (!sxe->iter.data) {
return; /* return NULL */
}
2004-01-18 23:33:38 +08:00
RETURN_ZVAL(sxe->iter.data, 1, 0);
}
/* }}} */
2006-03-06 17:50:44 +08:00
/* {{{ proto string SimpleXMLIterator::key()
Get name of current child element */
PHP_METHOD(ce_SimpleXMLIterator, key)
2004-01-18 23:33:38 +08:00
{
xmlNodePtr curnode;
2004-01-19 00:25:26 +08:00
php_sxe_object *intern;
2004-01-18 23:33:38 +08:00
php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
2004-01-19 00:25:26 +08:00
if (!sxe->iter.data) {
RETURN_FALSE;
}
intern = (php_sxe_object *)zend_object_store_get_object(sxe->iter.data TSRMLS_CC);
if (intern != NULL && intern->node != NULL) {
curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->node)->node;
2004-01-18 23:33:38 +08:00
RETURN_STRINGL((char*)curnode->name, xmlStrlen(curnode->name), 1);
}
RETURN_FALSE;
2004-01-18 23:33:38 +08:00
}
/* }}} */
2006-03-06 17:50:44 +08:00
/* {{{ proto void SimpleXMLIterator::next()
Move to next element */
PHP_METHOD(ce_SimpleXMLIterator, next)
2004-01-18 23:33:38 +08:00
{
php_sxe_iterator iter;
2004-01-18 23:33:38 +08:00
iter.sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
ce_SimpleXMLElement->iterator_funcs.funcs->move_forward((zend_object_iterator*)&iter TSRMLS_CC);
2004-01-18 23:33:38 +08:00
}
/* }}} */
2006-03-06 17:50:44 +08:00
/* {{{ proto bool SimpleXMLIterator::hasChildren()
Check whether element has children (elements) */
PHP_METHOD(ce_SimpleXMLIterator, hasChildren)
2004-01-18 23:33:38 +08:00
{
php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
2004-01-19 00:25:26 +08:00
php_sxe_object *child;
2004-01-18 23:33:38 +08:00
xmlNodePtr node;
if (!sxe->iter.data || sxe->iter.type == SXE_ITER_ATTRLIST) {
2004-01-19 00:25:26 +08:00
RETURN_FALSE;
}
child = php_sxe_fetch_object(sxe->iter.data TSRMLS_CC);
2004-01-18 23:33:38 +08:00
GET_NODE(child, node);
if (node) {
node = node->children;
}
while (node && node->type != XML_ELEMENT_NODE) {
node = node->next;
}
RETURN_BOOL(node ? 1 : 0);
}
/* }}} */
2006-03-06 17:50:44 +08:00
/* {{{ proto SimpleXMLIterator SimpleXMLIterator::getChildren()
Get child element iterator */
PHP_METHOD(ce_SimpleXMLIterator, getChildren)
2004-01-18 23:33:38 +08:00
{
php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
if (!sxe->iter.data || sxe->iter.type == SXE_ITER_ATTRLIST) {
2004-01-19 00:25:26 +08:00
return; /* return NULL */
}
RETURN_ZVAL(sxe->iter.data, 1, 0);
2004-01-18 23:33:38 +08:00
}
static const zend_function_entry funcs_SimpleXMLIterator[] = {
PHP_ME(ce_SimpleXMLIterator, rewind, NULL, ZEND_ACC_PUBLIC)
PHP_ME(ce_SimpleXMLIterator, valid, NULL, ZEND_ACC_PUBLIC)
PHP_ME(ce_SimpleXMLIterator, current, NULL, ZEND_ACC_PUBLIC)
PHP_ME(ce_SimpleXMLIterator, key, NULL, ZEND_ACC_PUBLIC)
PHP_ME(ce_SimpleXMLIterator, next, NULL, ZEND_ACC_PUBLIC)
PHP_ME(ce_SimpleXMLIterator, hasChildren, NULL, ZEND_ACC_PUBLIC)
PHP_ME(ce_SimpleXMLIterator, getChildren, NULL, ZEND_ACC_PUBLIC)
2004-01-18 23:33:38 +08:00
{NULL, NULL, NULL}
};
/* }}} */
PHP_MINIT_FUNCTION(sxe) /* {{{ */
2004-01-18 23:33:38 +08:00
{
zend_class_entry **pce;
zend_class_entry sxi;
if (zend_hash_find(CG(class_table), "simplexmlelement", sizeof("SimpleXMLElement"), (void **) &pce) == FAILURE) {
ce_SimpleXMLElement = NULL;
ce_SimpleXMLIterator = NULL;
2004-01-18 23:33:38 +08:00
return SUCCESS; /* SimpleXML must be initialized before */
}
ce_SimpleXMLElement = *pce;
2004-01-18 23:33:38 +08:00
INIT_CLASS_ENTRY_EX(sxi, "SimpleXMLIterator", strlen("SimpleXMLIterator"), funcs_SimpleXMLIterator);
ce_SimpleXMLIterator = zend_register_internal_class_ex(&sxi, ce_SimpleXMLElement, NULL TSRMLS_CC);
ce_SimpleXMLIterator->create_object = ce_SimpleXMLElement->create_object;
2004-01-18 23:33:38 +08:00
zend_class_implements(ce_SimpleXMLIterator TSRMLS_CC, 1, spl_ce_RecursiveIterator);
zend_class_implements(ce_SimpleXMLIterator TSRMLS_CC, 1, spl_ce_Countable);
2004-01-18 23:33:38 +08:00
return SUCCESS;
}
/* }}} */
2004-01-18 23:33:38 +08:00
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/