php-src/ext/simplexml/sxe.c

228 lines
6.3 KiB
C
Raw Normal View History

2004-01-18 15:33:38 +00:00
/*
+----------------------------------------------------------------------+
2014-09-19 16:33:14 +00:00
| PHP Version 7 |
2004-01-18 15:33:38 +00:00
+----------------------------------------------------------------------+
2017-01-02 15:30:12 +00:00
| Copyright (c) 1997-2017 The PHP Group |
2004-01-18 15:33:38 +00:00
+----------------------------------------------------------------------+
2006-01-01 12:51:34 +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 12:51:34 +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_simplexml.h"
#include "ext/spl/php_spl.h"
#include "ext/spl/spl_iterators.h"
#include "sxe.h"
2004-01-18 15:33:38 +00:00
PHP_SXE_API zend_class_entry *ce_SimpleXMLIterator = NULL;
PHP_SXE_API zend_class_entry *ce_SimpleXMLElement;
2004-01-18 15:33:38 +00:00
#include "php_simplexml_exports.h"
2004-01-18 15:33:38 +00:00
2006-03-06 09:50:44 +00:00
/* {{{ proto void SimpleXMLIterator::rewind()
Rewind to first element */
PHP_METHOD(ce_SimpleXMLIterator, rewind)
2004-01-18 15:33:38 +00:00
{
php_sxe_iterator iter;
2015-01-03 09:22:58 +00:00
if (zend_parse_parameters_none() == FAILURE) {
return;
}
2004-01-18 15:33:38 +00:00
iter.sxe = Z_SXEOBJ_P(getThis());
2014-12-13 22:06:14 +00:00
ce_SimpleXMLElement->iterator_funcs.funcs->rewind((zend_object_iterator*)&iter);
2004-01-18 15:33:38 +00:00
}
/* }}} */
2006-03-06 09:50:44 +00:00
/* {{{ proto bool SimpleXMLIterator::valid()
Check whether iteration is valid */
PHP_METHOD(ce_SimpleXMLIterator, valid)
2004-01-18 15:33:38 +00:00
{
php_sxe_object *sxe = Z_SXEOBJ_P(getThis());
2015-01-03 09:22:58 +00:00
if (zend_parse_parameters_none() == FAILURE) {
return;
}
2004-01-18 15:33:38 +00:00
RETURN_BOOL(!Z_ISUNDEF(sxe->iter.data));
2004-01-18 15:33:38 +00:00
}
/* }}} */
2006-03-06 09:50:44 +00:00
/* {{{ proto SimpleXMLIterator SimpleXMLIterator::current()
Get current element */
PHP_METHOD(ce_SimpleXMLIterator, current)
2004-01-18 15:33:38 +00:00
{
php_sxe_object *sxe = Z_SXEOBJ_P(getThis());
2015-06-12 10:33:14 +00:00
zval *data;
2015-01-03 09:22:58 +00:00
if (zend_parse_parameters_none() == FAILURE) {
return;
}
2004-01-18 15:33:38 +00:00
if (Z_ISUNDEF(sxe->iter.data)) {
2004-01-18 16:25:26 +00:00
return; /* return NULL */
}
2015-06-12 10:33:14 +00:00
data = &sxe->iter.data;
ZVAL_DEREF(data);
ZVAL_COPY(return_value, data);
2004-01-18 15:33:38 +00:00
}
/* }}} */
2006-03-06 09:50:44 +00:00
/* {{{ proto string SimpleXMLIterator::key()
Get name of current child element */
PHP_METHOD(ce_SimpleXMLIterator, key)
2004-01-18 15:33:38 +00:00
{
xmlNodePtr curnode;
2004-01-18 16:25:26 +00:00
php_sxe_object *intern;
php_sxe_object *sxe = Z_SXEOBJ_P(getThis());
2015-01-03 09:22:58 +00:00
if (zend_parse_parameters_none() == FAILURE) {
return;
}
2004-01-18 15:33:38 +00:00
if (Z_ISUNDEF(sxe->iter.data)) {
2004-01-18 16:25:26 +00:00
RETURN_FALSE;
}
intern = Z_SXEOBJ_P(&sxe->iter.data);
2004-01-18 16:25:26 +00:00
if (intern != NULL && intern->node != NULL) {
curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->node)->node;
RETURN_STRINGL((char*)curnode->name, xmlStrlen(curnode->name));
2004-01-18 15:33:38 +00:00
}
2015-01-03 09:22:58 +00:00
RETURN_FALSE;
2004-01-18 15:33:38 +00:00
}
/* }}} */
2006-03-06 09:50:44 +00:00
/* {{{ proto void SimpleXMLIterator::next()
Move to next element */
PHP_METHOD(ce_SimpleXMLIterator, next)
2004-01-18 15:33:38 +00:00
{
php_sxe_iterator iter;
2015-01-03 09:22:58 +00:00
if (zend_parse_parameters_none() == FAILURE) {
return;
}
2004-01-18 15:33:38 +00:00
iter.sxe = Z_SXEOBJ_P(getThis());
2014-12-13 22:06:14 +00:00
ce_SimpleXMLElement->iterator_funcs.funcs->move_forward((zend_object_iterator*)&iter);
2004-01-18 15:33:38 +00:00
}
/* }}} */
2006-03-06 09:50:44 +00:00
/* {{{ proto bool SimpleXMLIterator::hasChildren()
2015-01-03 09:22:58 +00:00
Check whether element has children (elements) */
PHP_METHOD(ce_SimpleXMLIterator, hasChildren)
2004-01-18 15:33:38 +00:00
{
php_sxe_object *sxe = Z_SXEOBJ_P(getThis());
2004-01-18 16:25:26 +00:00
php_sxe_object *child;
2004-01-18 15:33:38 +00:00
xmlNodePtr node;
2015-01-03 09:22:58 +00:00
if (zend_parse_parameters_none() == FAILURE) {
return;
}
2004-01-18 15:33:38 +00:00
if (Z_ISUNDEF(sxe->iter.data) || sxe->iter.type == SXE_ITER_ATTRLIST) {
2004-01-18 16:25:26 +00:00
RETURN_FALSE;
}
child = Z_SXEOBJ_P(&sxe->iter.data);
2004-01-18 16:25:26 +00:00
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-03-06 09:50:44 +00:00
/* {{{ proto SimpleXMLIterator SimpleXMLIterator::getChildren()
2015-01-03 09:22:58 +00:00
Get child element iterator */
PHP_METHOD(ce_SimpleXMLIterator, getChildren)
2004-01-18 15:33:38 +00:00
{
php_sxe_object *sxe = Z_SXEOBJ_P(getThis());
2015-06-12 10:33:14 +00:00
zval *data;
2015-01-03 09:22:58 +00:00
if (zend_parse_parameters_none() == FAILURE) {
return;
}
2004-01-18 15:33:38 +00:00
if (Z_ISUNDEF(sxe->iter.data) || sxe->iter.type == SXE_ITER_ATTRLIST) {
2004-01-18 16:25:26 +00:00
return; /* return NULL */
}
2015-06-12 10:33:14 +00:00
data = &sxe->iter.data;
ZVAL_DEREF(data);
ZVAL_COPY(return_value, data);
2004-01-18 15:33:38 +00:00
}
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO(arginfo_simplexmliterator__void, 0)
ZEND_END_ARG_INFO()
/* }}} */
static const zend_function_entry funcs_SimpleXMLIterator[] = {
PHP_ME(ce_SimpleXMLIterator, rewind, arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
PHP_ME(ce_SimpleXMLIterator, valid, arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
PHP_ME(ce_SimpleXMLIterator, current, arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
PHP_ME(ce_SimpleXMLIterator, key, arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
PHP_ME(ce_SimpleXMLIterator, next, arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
PHP_ME(ce_SimpleXMLIterator, hasChildren, arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
PHP_ME(ce_SimpleXMLIterator, getChildren, arginfo_simplexmliterator__void, ZEND_ACC_PUBLIC)
2016-06-21 21:40:50 +00:00
PHP_FE_END
2004-01-18 15:33:38 +00:00
};
/* }}} */
PHP_MINIT_FUNCTION(sxe) /* {{{ */
2004-01-18 15:33:38 +00:00
{
zend_class_entry *pce;
zend_class_entry sxi;
if ((pce = zend_hash_str_find_ptr(CG(class_table), "simplexmlelement", sizeof("SimpleXMLElement") - 1)) == NULL) {
ce_SimpleXMLElement = NULL;
ce_SimpleXMLIterator = NULL;
2004-01-18 15:33:38 +00:00
return SUCCESS; /* SimpleXML must be initialized before */
}
ce_SimpleXMLElement = pce;
2004-01-18 15:33:38 +00:00
INIT_CLASS_ENTRY_EX(sxi, "SimpleXMLIterator", sizeof("SimpleXMLIterator") - 1, funcs_SimpleXMLIterator);
2014-12-13 22:06:14 +00:00
ce_SimpleXMLIterator = zend_register_internal_class_ex(&sxi, ce_SimpleXMLElement);
ce_SimpleXMLIterator->create_object = ce_SimpleXMLElement->create_object;
2004-01-18 15:33:38 +00:00
2014-12-13 22:06:14 +00:00
zend_class_implements(ce_SimpleXMLIterator, 1, spl_ce_RecursiveIterator);
zend_class_implements(ce_SimpleXMLIterator, 1, zend_ce_countable);
2004-01-18 15:33:38 +00:00
return SUCCESS;
}
/* }}} */
2004-01-18 15:33:38 +00:00
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/