php-src/ext/spl/spl_sxe.c

209 lines
6.0 KiB
C
Raw Normal View History

2004-01-18 15:33:38 +00:00
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
2007-12-31 07:12:20 +00:00
| Copyright (c) 1997-2008 The PHP Group |
2004-01-18 15:33:38 +00:00
+----------------------------------------------------------------------+
2006-01-01 13:10:10 +00:00
| This source file is subject to version 3.01 of the PHP license, |
2004-01-18 15:33:38 +00: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 13:10:10 +00:00
| http://www.php.net/license/3_01.txt |
2004-01-18 15:33:38 +00: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-20 20:59:45 +00:00
/* $Id$ */
2004-01-18 15:33:38 +00: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 "php_spl.h"
#include "spl_functions.h"
#include "spl_engine.h"
#include "spl_iterators.h"
#include "spl_sxe.h"
#include "spl_array.h"
2004-01-18 15:33:38 +00:00
zend_class_entry *spl_ce_SimpleXMLIterator = NULL;
zend_class_entry *spl_ce_SimpleXMLElement;
2004-01-18 15:33:38 +00:00
#if HAVE_LIBXML && HAVE_SIMPLEXML
#include "ext/simplexml/php_simplexml_exports.h"
2004-01-18 15:33:38 +00:00
2006-12-21 22:56:58 +00:00
/* {{{ proto void SimpleXMLIterator::rewind() U
2006-03-06 09:47:03 +00:00
Rewind to first element */
SPL_METHOD(SimpleXMLIterator, rewind)
2004-01-18 15:33:38 +00:00
{
php_sxe_iterator iter;
2004-01-18 15:33:38 +00:00
iter.sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
spl_ce_SimpleXMLElement->iterator_funcs.funcs->rewind((zend_object_iterator*)&iter TSRMLS_CC);
2004-01-18 15:33:38 +00:00
}
/* }}} */
2006-12-21 22:56:58 +00:00
/* {{{ proto bool SimpleXMLIterator::valid() U
2006-03-06 09:47:03 +00:00
Check whether iteration is valid */
SPL_METHOD(SimpleXMLIterator, valid)
2004-01-18 15:33:38 +00:00
{
php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
RETURN_BOOL(sxe->iter.data);
}
/* }}} */
2006-12-21 22:56:58 +00:00
/* {{{ proto SimpleXMLIterator SimpleXMLIterator::current() U
2006-03-06 09:47:03 +00:00
Get current element */
SPL_METHOD(SimpleXMLIterator, current)
2004-01-18 15:33:38 +00:00
{
php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
2004-01-18 16:25:26 +00:00
if (!sxe->iter.data) {
return; /* return NULL */
}
2004-01-18 15:33:38 +00:00
RETURN_ZVAL(sxe->iter.data, 1, 0);
}
/* }}} */
2006-12-21 22:56:58 +00:00
/* {{{ proto string SimpleXMLIterator::key() U
2006-03-06 09:47:03 +00:00
Get name of current child element */
SPL_METHOD(SimpleXMLIterator, key)
2004-01-18 15:33:38 +00:00
{
xmlNodePtr curnode;
2004-01-18 16:25:26 +00:00
php_sxe_object *intern;
2004-01-18 15:33:38 +00:00
php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
2004-01-18 16:25:26 +00: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;
2005-08-23 09:33:46 +00:00
RETURN_U_STRINGL(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), (char*)curnode->name, xmlStrlen(curnode->name), 1);
2004-01-18 15:33:38 +00:00
}
2005-08-23 09:33:46 +00:00
RETURN_FALSE;
2004-01-18 15:33:38 +00:00
}
/* }}} */
2006-12-21 22:56:58 +00:00
/* {{{ proto void SimpleXMLIterator::next() U
2006-03-06 09:47:03 +00:00
Move to next element */
SPL_METHOD(SimpleXMLIterator, next)
2004-01-18 15:33:38 +00:00
{
php_sxe_iterator iter;
2004-01-18 15:33:38 +00:00
iter.sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
spl_ce_SimpleXMLElement->iterator_funcs.funcs->move_forward((zend_object_iterator*)&iter TSRMLS_CC);
2004-01-18 15:33:38 +00:00
}
/* }}} */
2006-12-21 22:56:58 +00:00
/* {{{ proto bool SimpleXMLIterator::hasChildren() U
2006-03-06 09:47:03 +00:00
Check whether element has children (elements) */
2004-01-18 15:33:38 +00:00
SPL_METHOD(SimpleXMLIterator, hasChildren)
{
php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
2004-01-18 16:25:26 +00:00
php_sxe_object *child;
2004-01-18 15:33:38 +00:00
xmlNodePtr node;
2005-10-29 20:37:59 +00:00
if (!sxe->iter.data || sxe->iter.type == SXE_ITER_ATTRLIST) {
2004-01-18 16:25:26 +00:00
RETURN_FALSE;
}
child = php_sxe_fetch_object(sxe->iter.data TSRMLS_CC);
2004-01-18 15:33:38 +00: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-12-21 22:56:58 +00:00
/* {{{ proto SimpleXMLIterator SimpleXMLIterator::getChildren() U
2006-03-06 09:47:03 +00:00
Get child element iterator */
2004-01-18 15:33:38 +00:00
SPL_METHOD(SimpleXMLIterator, getChildren)
{
php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
2005-10-29 20:37:59 +00:00
if (!sxe->iter.data || sxe->iter.type == SXE_ITER_ATTRLIST) {
2004-01-18 16:25:26 +00:00
return; /* return NULL */
}
RETURN_ZVAL(sxe->iter.data, 1, 0);
2004-01-18 15:33:38 +00:00
}
2006-12-21 22:56:58 +00:00
/* {{{ proto int SimpleXMLIterator::count() U
2006-03-06 09:47:03 +00:00
Get number of child elements */
SPL_METHOD(SimpleXMLIterator, count)
{
long count = 0;
Z_OBJ_HANDLER_P(getThis(), count_elements)(getThis(), &count TSRMLS_CC);
RETURN_LONG(count);
}
static const zend_function_entry spl_funcs_SimpleXMLIterator[] = {
2004-01-18 15:33:38 +00:00
SPL_ME(SimpleXMLIterator, rewind, NULL, ZEND_ACC_PUBLIC)
SPL_ME(SimpleXMLIterator, valid, NULL, ZEND_ACC_PUBLIC)
2004-01-18 15:33:38 +00:00
SPL_ME(SimpleXMLIterator, current, NULL, ZEND_ACC_PUBLIC)
SPL_ME(SimpleXMLIterator, key, NULL, ZEND_ACC_PUBLIC)
SPL_ME(SimpleXMLIterator, next, NULL, ZEND_ACC_PUBLIC)
SPL_ME(SimpleXMLIterator, hasChildren, NULL, ZEND_ACC_PUBLIC)
SPL_ME(SimpleXMLIterator, getChildren, NULL, ZEND_ACC_PUBLIC)
SPL_ME(SimpleXMLIterator, count, NULL, ZEND_ACC_PUBLIC)
2004-01-18 15:33:38 +00:00
{NULL, NULL, NULL}
};
/* }}} */
SPL_API PHP_MINIT_FUNCTION(spl_sxe) /* {{{ */
2004-01-18 15:33:38 +00:00
{
zend_class_entry **pce;
2004-01-18 15:33:38 +00:00
if (zend_hash_find(CG(class_table), "simplexmlelement", sizeof("SimpleXMLElement"), (void **) &pce) == FAILURE) {
spl_ce_SimpleXMLElement = NULL;
spl_ce_SimpleXMLIterator = NULL;
2004-01-18 15:33:38 +00:00
return SUCCESS; /* SimpleXML must be initialized before */
}
spl_ce_SimpleXMLElement = *pce;
REGISTER_SPL_SUB_CLASS_EX(SimpleXMLIterator, SimpleXMLElement, spl_ce_SimpleXMLElement->create_object, spl_funcs_SimpleXMLIterator);
2004-01-18 15:33:38 +00:00
REGISTER_SPL_IMPLEMENTS(SimpleXMLIterator, RecursiveIterator);
REGISTER_SPL_IMPLEMENTS(SimpleXMLIterator, Countable);
2004-01-18 15:33:38 +00:00
return SUCCESS;
}
/* }}} */
#else /* HAVE_LIBXML && HAVE_SIMPLEXML */
SPL_API PHP_MINIT_FUNCTION(spl_sxe) /* {{{ */
2004-01-18 15:33:38 +00:00
{
return SUCCESS;
}
#endif /* HAVE_LIBXML && HAVE_SIMPLEXML */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/