2003-05-02 07:28:28 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2003-06-13 03:30:54 +08:00
|
|
|
| PHP Version 5 |
|
2003-05-02 07:28:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2004-01-09 02:17:39 +08:00
|
|
|
| Copyright (c) 1997-2004 The PHP Group |
|
2003-05-02 07:28:28 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2003-06-13 03:30:54 +08:00
|
|
|
| This source file is subject to version 3.0 of the PHP license, |
|
2003-05-02 07:28:28 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-13 03:30:54 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.php.net/license/3_0.txt. |
|
2003-05-02 07:28:28 +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$ */
|
|
|
|
|
2003-05-02 07:28:28 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
#include "php_ini.h"
|
|
|
|
#include "ext/standard/info.h"
|
2003-11-09 22:05:36 +08:00
|
|
|
#include "zend_interfaces.h"
|
2003-05-02 07:28:28 +08:00
|
|
|
|
|
|
|
#include "php_spl.h"
|
|
|
|
#include "spl_functions.h"
|
|
|
|
#include "spl_engine.h"
|
2004-04-27 23:42:45 +08:00
|
|
|
#include "spl_iterators.h"
|
2003-05-02 07:28:28 +08:00
|
|
|
#include "spl_array.h"
|
|
|
|
|
2003-11-09 22:05:36 +08:00
|
|
|
SPL_METHOD(Array, __construct);
|
|
|
|
SPL_METHOD(Array, getIterator);
|
|
|
|
SPL_METHOD(Array, rewind);
|
|
|
|
SPL_METHOD(Array, current);
|
|
|
|
SPL_METHOD(Array, key);
|
|
|
|
SPL_METHOD(Array, next);
|
2004-03-09 01:33:31 +08:00
|
|
|
SPL_METHOD(Array, valid);
|
2004-04-25 19:14:11 +08:00
|
|
|
SPL_METHOD(Array, offsetExists);
|
|
|
|
SPL_METHOD(Array, offsetGet);
|
|
|
|
SPL_METHOD(Array, offsetSet);
|
|
|
|
SPL_METHOD(Array, offsetUnset);
|
2004-04-25 21:04:36 +08:00
|
|
|
SPL_METHOD(Array, append);
|
2004-04-25 19:14:11 +08:00
|
|
|
SPL_METHOD(Array, getArrayCopy);
|
2004-04-27 23:42:45 +08:00
|
|
|
SPL_METHOD(Array, seek);
|
2004-04-29 05:45:41 +08:00
|
|
|
SPL_METHOD(Array, count);
|
2003-07-20 04:54:22 +08:00
|
|
|
|
2003-08-05 07:00:57 +08:00
|
|
|
static
|
|
|
|
ZEND_BEGIN_ARG_INFO(arginfo_array___construct, 0)
|
|
|
|
ZEND_ARG_INFO(0, array)
|
|
|
|
ZEND_END_ARG_INFO();
|
|
|
|
|
2004-04-25 19:14:11 +08:00
|
|
|
static
|
|
|
|
ZEND_BEGIN_ARG_INFO(arginfo_array_offsetGet, 0)
|
|
|
|
ZEND_ARG_INFO(0, index)
|
|
|
|
ZEND_END_ARG_INFO();
|
|
|
|
|
|
|
|
static
|
|
|
|
ZEND_BEGIN_ARG_INFO(arginfo_array_offsetSet, 0)
|
|
|
|
ZEND_ARG_INFO(0, index)
|
|
|
|
ZEND_ARG_INFO(0, newval)
|
|
|
|
ZEND_END_ARG_INFO();
|
|
|
|
|
2004-04-25 21:04:36 +08:00
|
|
|
static
|
|
|
|
ZEND_BEGIN_ARG_INFO(arginfo_array_append, 0)
|
|
|
|
ZEND_ARG_INFO(0, value)
|
|
|
|
ZEND_END_ARG_INFO();
|
|
|
|
|
2004-04-27 23:42:45 +08:00
|
|
|
static
|
|
|
|
ZEND_BEGIN_ARG_INFO(arginfo_array_seek, 0)
|
|
|
|
ZEND_ARG_INFO(0, position)
|
|
|
|
ZEND_END_ARG_INFO();
|
|
|
|
|
2003-11-17 13:32:43 +08:00
|
|
|
static zend_function_entry spl_funcs_ArrayObject[] = {
|
2003-11-09 22:05:36 +08:00
|
|
|
SPL_ME(Array, __construct, arginfo_array___construct, ZEND_ACC_PUBLIC)
|
2004-04-25 19:14:11 +08:00
|
|
|
SPL_ME(Array, offsetExists, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
|
|
|
|
SPL_ME(Array, offsetGet, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
|
|
|
|
SPL_ME(Array, offsetSet, arginfo_array_offsetSet, ZEND_ACC_PUBLIC)
|
|
|
|
SPL_ME(Array, offsetUnset, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
|
2004-04-25 21:04:36 +08:00
|
|
|
SPL_ME(Array, append, arginfo_array_append, ZEND_ACC_PUBLIC)
|
2004-04-25 19:14:11 +08:00
|
|
|
SPL_ME(Array, getArrayCopy, NULL, ZEND_ACC_PUBLIC)
|
2004-04-29 05:45:41 +08:00
|
|
|
SPL_ME(Array, count, NULL, ZEND_ACC_PUBLIC)
|
2004-04-30 06:52:49 +08:00
|
|
|
/* ArrayObject specific */
|
|
|
|
SPL_ME(Array, getIterator, NULL, ZEND_ACC_PUBLIC)
|
2003-07-20 09:22:03 +08:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2003-11-09 22:05:36 +08:00
|
|
|
static zend_function_entry spl_funcs_ArrayIterator[] = {
|
2004-04-25 19:14:11 +08:00
|
|
|
SPL_ME(Array, __construct, arginfo_array___construct, ZEND_ACC_PUBLIC)
|
|
|
|
SPL_ME(Array, offsetExists, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
|
|
|
|
SPL_ME(Array, offsetGet, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
|
|
|
|
SPL_ME(Array, offsetSet, arginfo_array_offsetSet, ZEND_ACC_PUBLIC)
|
|
|
|
SPL_ME(Array, offsetUnset, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
|
2004-04-25 21:04:36 +08:00
|
|
|
SPL_ME(Array, append, arginfo_array_append, ZEND_ACC_PUBLIC)
|
2004-04-25 19:14:11 +08:00
|
|
|
SPL_ME(Array, getArrayCopy, NULL, ZEND_ACC_PUBLIC)
|
2004-04-29 05:45:41 +08:00
|
|
|
SPL_ME(Array, count, NULL, ZEND_ACC_PUBLIC)
|
2004-04-30 06:52:49 +08:00
|
|
|
/* ArrayIterator specific */
|
|
|
|
SPL_ME(Array, rewind, NULL, ZEND_ACC_PUBLIC)
|
|
|
|
SPL_ME(Array, current, NULL, ZEND_ACC_PUBLIC)
|
|
|
|
SPL_ME(Array, key, NULL, ZEND_ACC_PUBLIC)
|
|
|
|
SPL_ME(Array, next, NULL, ZEND_ACC_PUBLIC)
|
|
|
|
SPL_ME(Array, valid, NULL, ZEND_ACC_PUBLIC)
|
|
|
|
SPL_ME(Array, seek, arginfo_array_seek, ZEND_ACC_PUBLIC)
|
2003-07-20 04:54:22 +08:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2004-11-01 18:45:54 +08:00
|
|
|
static zend_function_entry spl_funcs_Countable[] = {
|
|
|
|
SPL_ABSTRACT_ME(Countable, count, NULL)
|
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
2003-07-20 09:22:03 +08:00
|
|
|
|
2003-11-17 13:32:43 +08:00
|
|
|
zend_object_handlers spl_handler_ArrayObject;
|
|
|
|
zend_class_entry * spl_ce_ArrayObject;
|
2003-11-09 22:05:36 +08:00
|
|
|
|
|
|
|
zend_object_handlers spl_handler_ArrayIterator;
|
|
|
|
zend_class_entry * spl_ce_ArrayIterator;
|
2003-07-20 04:54:22 +08:00
|
|
|
|
2004-11-01 18:45:54 +08:00
|
|
|
zend_class_entry *spl_ce_Countable;
|
|
|
|
|
2003-07-20 04:54:22 +08:00
|
|
|
typedef struct _spl_array_object {
|
|
|
|
zend_object std;
|
|
|
|
zval *array;
|
|
|
|
HashPosition pos;
|
|
|
|
} spl_array_object;
|
|
|
|
|
2004-02-04 20:45:47 +08:00
|
|
|
/* {{{ spl_array_object_free_storage */
|
|
|
|
static void spl_array_object_free_storage(void *object TSRMLS_DC)
|
2003-07-20 04:54:22 +08:00
|
|
|
{
|
|
|
|
spl_array_object *intern = (spl_array_object *)object;
|
|
|
|
|
|
|
|
zend_hash_destroy(intern->std.properties);
|
|
|
|
FREE_HASHTABLE(intern->std.properties);
|
|
|
|
|
2003-11-17 13:25:47 +08:00
|
|
|
zval_ptr_dtor(&intern->array);
|
2003-07-20 04:54:22 +08:00
|
|
|
|
|
|
|
efree(object);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ spl_array_object_new */
|
|
|
|
static zend_object_value spl_array_object_new_ex(zend_class_entry *class_type, spl_array_object **obj, spl_array_object *orig TSRMLS_DC)
|
|
|
|
{
|
|
|
|
zend_object_value retval;
|
|
|
|
spl_array_object *intern;
|
|
|
|
zval *tmp;
|
|
|
|
|
|
|
|
intern = emalloc(sizeof(spl_array_object));
|
|
|
|
memset(intern, 0, sizeof(spl_array_object));
|
|
|
|
intern->std.ce = class_type;
|
|
|
|
*obj = intern;
|
|
|
|
|
|
|
|
ALLOC_HASHTABLE(intern->std.properties);
|
|
|
|
zend_hash_init(intern->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
|
|
|
|
zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
|
|
|
|
|
|
|
|
if (orig) {
|
|
|
|
intern->array = orig->array;
|
|
|
|
ZVAL_ADDREF(intern->array);
|
|
|
|
} else {
|
|
|
|
MAKE_STD_ZVAL(intern->array);
|
|
|
|
array_init(intern->array);
|
|
|
|
}
|
|
|
|
zend_hash_internal_pointer_reset_ex(HASH_OF(intern->array), &intern->pos);
|
|
|
|
|
2004-02-04 20:45:47 +08:00
|
|
|
retval.handle = zend_objects_store_put(intern, NULL, (zend_objects_free_object_storage_t) spl_array_object_free_storage, NULL TSRMLS_CC);
|
2003-11-09 22:05:36 +08:00
|
|
|
if (class_type == spl_ce_ArrayIterator) {
|
|
|
|
retval.handlers = &spl_handler_ArrayIterator;
|
2003-07-20 09:22:03 +08:00
|
|
|
} else {
|
2003-11-17 13:32:43 +08:00
|
|
|
retval.handlers = &spl_handler_ArrayObject;
|
2003-07-20 09:22:03 +08:00
|
|
|
}
|
2003-07-20 04:54:22 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ spl_array_object_new */
|
|
|
|
static zend_object_value spl_array_object_new(zend_class_entry *class_type TSRMLS_DC)
|
|
|
|
{
|
|
|
|
spl_array_object *tmp;
|
|
|
|
return spl_array_object_new_ex(class_type, &tmp, NULL TSRMLS_CC);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ spl_array_object_clone */
|
|
|
|
static zend_object_value spl_array_object_clone(zval *zobject TSRMLS_DC)
|
|
|
|
{
|
|
|
|
zend_object_value new_obj_val;
|
|
|
|
zend_object *old_object;
|
|
|
|
zend_object *new_object;
|
|
|
|
zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
|
|
|
|
spl_array_object *intern;
|
|
|
|
|
|
|
|
old_object = zend_objects_get_address(zobject TSRMLS_CC);
|
|
|
|
new_obj_val = spl_array_object_new_ex(old_object->ce, &intern, (spl_array_object*)old_object TSRMLS_CC);
|
|
|
|
new_object = &intern->std;
|
|
|
|
|
|
|
|
zend_objects_clone_members(new_object, new_obj_val, old_object, handle TSRMLS_CC);
|
|
|
|
|
|
|
|
return new_obj_val;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ spl_array_read_dimension */
|
2004-02-13 04:52:48 +08:00
|
|
|
static zval *spl_array_read_dimension(zval *object, zval *offset, int type TSRMLS_DC)
|
2003-07-20 04:54:22 +08:00
|
|
|
{
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
zval **retval;
|
|
|
|
long index;
|
|
|
|
|
|
|
|
switch(Z_TYPE_P(offset)) {
|
|
|
|
case IS_STRING:
|
2003-07-25 04:28:15 +08:00
|
|
|
if (zend_symtable_find(HASH_OF(intern->array), Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1, (void **) &retval) == FAILURE) {
|
2004-02-13 04:52:48 +08:00
|
|
|
zend_error(E_NOTICE, "Undefined index: %s", Z_STRVAL_P(offset));
|
2003-07-25 04:28:15 +08:00
|
|
|
return EG(uninitialized_zval_ptr);
|
|
|
|
} else {
|
|
|
|
return *retval;
|
2003-07-20 04:54:22 +08:00
|
|
|
}
|
|
|
|
case IS_DOUBLE:
|
|
|
|
case IS_RESOURCE:
|
|
|
|
case IS_BOOL:
|
|
|
|
case IS_LONG:
|
|
|
|
if (offset->type == IS_DOUBLE) {
|
|
|
|
index = (long)Z_DVAL_P(offset);
|
2003-07-25 04:28:15 +08:00
|
|
|
} else {
|
2003-07-20 04:54:22 +08:00
|
|
|
index = Z_LVAL_P(offset);
|
|
|
|
}
|
|
|
|
if (zend_hash_index_find(HASH_OF(intern->array), index, (void **) &retval) == FAILURE) {
|
2004-02-13 04:52:48 +08:00
|
|
|
zend_error(E_NOTICE, "Undefined offset: %ld", Z_LVAL_P(offset));
|
2003-07-20 04:54:22 +08:00
|
|
|
return EG(uninitialized_zval_ptr);
|
|
|
|
} else {
|
|
|
|
return *retval;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
zend_error(E_WARNING, "Illegal offset type");
|
|
|
|
return EG(uninitialized_zval_ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ spl_array_write_dimension */
|
2003-08-05 04:17:53 +08:00
|
|
|
static void spl_array_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC)
|
2003-07-20 04:54:22 +08:00
|
|
|
{
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
long index;
|
|
|
|
|
2004-03-14 04:01:00 +08:00
|
|
|
if (!offset) {
|
|
|
|
value->refcount++;
|
2004-04-14 00:21:24 +08:00
|
|
|
zend_hash_next_index_insert(HASH_OF(intern->array), (void**)&value, sizeof(void*), NULL);
|
2004-03-14 04:01:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2003-07-20 04:54:22 +08:00
|
|
|
switch(Z_TYPE_P(offset)) {
|
|
|
|
case IS_STRING:
|
2004-02-13 05:43:10 +08:00
|
|
|
value->refcount++;
|
2003-07-25 04:28:15 +08:00
|
|
|
zend_symtable_update(HASH_OF(intern->array), Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1, (void**)&value, sizeof(void*), NULL);
|
|
|
|
return;
|
2003-07-20 04:54:22 +08:00
|
|
|
case IS_DOUBLE:
|
|
|
|
case IS_RESOURCE:
|
|
|
|
case IS_BOOL:
|
|
|
|
case IS_LONG:
|
|
|
|
if (offset->type == IS_DOUBLE) {
|
|
|
|
index = (long)Z_DVAL_P(offset);
|
2003-07-25 04:28:15 +08:00
|
|
|
} else {
|
2003-07-20 04:54:22 +08:00
|
|
|
index = Z_LVAL_P(offset);
|
|
|
|
}
|
2004-02-13 05:43:10 +08:00
|
|
|
value->refcount++;
|
2004-04-14 00:21:24 +08:00
|
|
|
zend_hash_index_update(HASH_OF(intern->array), index, (void**)&value, sizeof(void*), NULL);
|
2003-07-20 04:54:22 +08:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
zend_error(E_WARNING, "Illegal offset type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-08-05 04:17:53 +08:00
|
|
|
/* {{{ spl_array_unset_dimension */
|
|
|
|
static void spl_array_unset_dimension(zval *object, zval *offset TSRMLS_DC)
|
|
|
|
{
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
long index;
|
|
|
|
|
|
|
|
switch(Z_TYPE_P(offset)) {
|
|
|
|
case IS_STRING:
|
2004-10-05 03:54:35 +08:00
|
|
|
if (HASH_OF(intern->array) == &EG(symbol_table)) {
|
2004-10-05 04:17:06 +08:00
|
|
|
if (zend_delete_global_variable(Z_STRVAL_P(offset), Z_STRLEN_P(offset) TSRMLS_CC)) {
|
2004-10-05 03:54:35 +08:00
|
|
|
zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (zend_symtable_del(HASH_OF(intern->array), Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1) == FAILURE) {
|
|
|
|
zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
|
|
|
|
}
|
2003-08-05 04:17:53 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
case IS_DOUBLE:
|
|
|
|
case IS_RESOURCE:
|
|
|
|
case IS_BOOL:
|
|
|
|
case IS_LONG:
|
|
|
|
if (offset->type == IS_DOUBLE) {
|
|
|
|
index = (long)Z_DVAL_P(offset);
|
|
|
|
} else {
|
|
|
|
index = Z_LVAL_P(offset);
|
|
|
|
}
|
|
|
|
if (zend_hash_index_del(HASH_OF(intern->array), index) == FAILURE) {
|
2003-09-04 22:44:55 +08:00
|
|
|
zend_error(E_NOTICE,"Undefined offset: %ld", Z_LVAL_P(offset));
|
2003-08-05 04:17:53 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
zend_error(E_WARNING, "Illegal offset type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-11-17 13:25:47 +08:00
|
|
|
/* {{{ spl_array_has_dimension */
|
|
|
|
static int spl_array_has_dimension(zval *object, zval *offset, int check_empty TSRMLS_DC)
|
|
|
|
{
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
long index;
|
|
|
|
|
|
|
|
switch(Z_TYPE_P(offset)) {
|
|
|
|
case IS_STRING:
|
|
|
|
return zend_symtable_exists(HASH_OF(intern->array), Z_STRVAL_P(offset), Z_STRLEN_P(offset)+1);
|
|
|
|
case IS_DOUBLE:
|
|
|
|
case IS_RESOURCE:
|
|
|
|
case IS_BOOL:
|
|
|
|
case IS_LONG:
|
|
|
|
if (offset->type == IS_DOUBLE) {
|
|
|
|
index = (long)Z_DVAL_P(offset);
|
|
|
|
} else {
|
|
|
|
index = Z_LVAL_P(offset);
|
|
|
|
}
|
|
|
|
return zend_hash_index_exists(HASH_OF(intern->array), index);
|
|
|
|
default:
|
|
|
|
zend_error(E_WARNING, "Illegal offset type");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2004-04-25 19:14:11 +08:00
|
|
|
/* {{{ proto bool ArrayObject::offsetExists(mixed $index)
|
2004-04-30 06:52:49 +08:00
|
|
|
proto bool ArrayIterator::offsetExists(mixed $index)
|
2004-04-25 19:14:11 +08:00
|
|
|
Returns whether the requested $index exists. */
|
|
|
|
SPL_METHOD(Array, offsetExists)
|
|
|
|
{
|
|
|
|
zval *index;
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &index) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
2004-06-22 03:15:27 +08:00
|
|
|
RETURN_BOOL(spl_array_has_dimension(getThis(), index, 1 TSRMLS_CC));
|
2004-04-25 19:14:11 +08:00
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto bool ArrayObject::offsetGet(mixed $index)
|
2004-04-30 06:52:49 +08:00
|
|
|
proto bool ArrayIterator::offsetGet(mixed $index)
|
2004-04-25 19:14:11 +08:00
|
|
|
Returns the value at the specified $index. */
|
|
|
|
SPL_METHOD(Array, offsetGet)
|
|
|
|
{
|
|
|
|
zval *index, *value;
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &index) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
value = spl_array_read_dimension(getThis(), index, BP_VAR_R TSRMLS_CC);
|
2004-06-21 00:46:27 +08:00
|
|
|
RETURN_ZVAL(value, 1, 0);
|
2004-04-25 19:14:11 +08:00
|
|
|
} /* }}} */
|
|
|
|
|
2004-04-30 06:52:49 +08:00
|
|
|
/* {{{ proto void ArrayObject::offsetSet(mixed $index, mixed $newval)
|
|
|
|
proto void ArrayIterator::offsetSet(mixed $index, mixed $newval)
|
2004-04-25 19:14:11 +08:00
|
|
|
Sets the value at the specified $index to $newval. */
|
|
|
|
SPL_METHOD(Array, offsetSet)
|
|
|
|
{
|
2004-04-25 21:04:36 +08:00
|
|
|
zval *index, *value = NULL;
|
2004-04-25 19:14:11 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &index, &value) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
spl_array_write_dimension(getThis(), index, value TSRMLS_CC);
|
|
|
|
} /* }}} */
|
|
|
|
|
2004-11-01 08:26:59 +08:00
|
|
|
|
|
|
|
void spl_array_iterator_append(zval *object, zval *append_value TSRMLS_DC) /* {{{ */
|
2004-04-25 21:04:36 +08:00
|
|
|
{
|
2004-04-30 06:25:45 +08:00
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
HashTable *aht = HASH_OF(intern->array);
|
|
|
|
|
|
|
|
if (!aht) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Z_TYPE_P(intern->array) == IS_OBJECT) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Cannot append properties to objects, use %s::offsetSet() instead", Z_OBJCE_P(object)->name);
|
|
|
|
}
|
|
|
|
|
2004-11-01 08:26:59 +08:00
|
|
|
spl_array_write_dimension(object, NULL, append_value TSRMLS_CC);
|
2004-04-30 06:25:45 +08:00
|
|
|
if (!intern->pos) {
|
|
|
|
intern->pos = aht->pListTail;
|
|
|
|
}
|
2004-04-25 21:04:36 +08:00
|
|
|
} /* }}} */
|
|
|
|
|
2004-11-01 08:26:59 +08:00
|
|
|
/* {{{ proto void ArrayObject::append(mixed $newval)
|
|
|
|
proto void ArrayIterator::append(mixed $newval)
|
|
|
|
Appends the value (cannot be called for objects). */
|
|
|
|
SPL_METHOD(Array, append)
|
|
|
|
{
|
|
|
|
zval *value;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
spl_array_iterator_append(getThis(), value TSRMLS_CC);
|
|
|
|
} /* }}} */
|
|
|
|
|
2004-04-30 06:52:49 +08:00
|
|
|
/* {{{ proto void ArrayObject::offsetUnset(mixed $index)
|
|
|
|
proto void ArrayIterator::offsetUnset(mixed $index)
|
2004-04-25 19:14:11 +08:00
|
|
|
Unsets the value at the specified $index. */
|
|
|
|
SPL_METHOD(Array, offsetUnset)
|
|
|
|
{
|
|
|
|
zval *index;
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &index) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
spl_array_unset_dimension(getThis(), index TSRMLS_CC);
|
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
/* {{ proto array ArrayObject::getArrayCopy()
|
2004-04-30 06:52:49 +08:00
|
|
|
proto array ArrayIterator::getArrayCopy()
|
2004-04-25 19:14:11 +08:00
|
|
|
Return a copy of the contained array */
|
|
|
|
SPL_METHOD(Array, getArrayCopy)
|
|
|
|
{
|
|
|
|
zval *object = getThis(), *tmp;
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
|
|
|
|
array_init(return_value);
|
|
|
|
zend_hash_copy(HASH_OF(return_value), HASH_OF(intern->array), (copy_ctor_func_t) zval_add_ref, &tmp, sizeof(zval*));
|
|
|
|
} /* }}} */
|
|
|
|
|
2003-08-05 04:17:53 +08:00
|
|
|
/* {{{ spl_array_get_properties */
|
|
|
|
static HashTable *spl_array_get_properties(zval *object TSRMLS_DC)
|
2003-07-20 04:54:22 +08:00
|
|
|
{
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
|
|
|
|
return HASH_OF(intern->array);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2004-04-25 19:14:11 +08:00
|
|
|
SPL_API int spl_hash_verify_pos(spl_array_object * intern TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
HashTable *ht = HASH_OF(intern->array);
|
|
|
|
Bucket *p;
|
|
|
|
|
|
|
|
/* IS_CONSISTENT(ht);*/
|
|
|
|
|
|
|
|
/* HASH_PROTECT_RECURSION(ht);*/
|
|
|
|
p = ht->pListHead;
|
|
|
|
while (p != NULL) {
|
|
|
|
if (p == intern->pos) {
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
p = p->pListNext;
|
|
|
|
}
|
|
|
|
/* HASH_UNPROTECT_RECURSION(ht); */
|
|
|
|
zend_hash_internal_pointer_reset_ex(HASH_OF(intern->array), &intern->pos);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2004-04-27 23:42:45 +08:00
|
|
|
static int spl_array_skip_protected(spl_array_object *intern TSRMLS_DC) /* {{{ */
|
2004-04-25 19:14:11 +08:00
|
|
|
{
|
|
|
|
char *string_key;
|
|
|
|
uint string_length;
|
|
|
|
ulong num_key;
|
|
|
|
HashTable *aht = HASH_OF(intern->array);
|
|
|
|
|
|
|
|
if (Z_TYPE_P(intern->array) == IS_OBJECT) {
|
|
|
|
do {
|
|
|
|
if (zend_hash_get_current_key_ex(aht, &string_key, &string_length, &num_key, 0, &intern->pos) == HASH_KEY_IS_STRING) {
|
|
|
|
if (!string_length || string_key[0]) {
|
2004-04-27 23:42:45 +08:00
|
|
|
return SUCCESS;
|
2004-04-25 19:14:11 +08:00
|
|
|
}
|
|
|
|
} else {
|
2004-04-27 23:42:45 +08:00
|
|
|
return SUCCESS;
|
2004-04-25 19:14:11 +08:00
|
|
|
}
|
|
|
|
if (zend_hash_has_more_elements_ex(aht, &intern->pos) != SUCCESS) {
|
2004-04-27 23:42:45 +08:00
|
|
|
return FAILURE;
|
2004-04-25 19:14:11 +08:00
|
|
|
}
|
|
|
|
zend_hash_move_forward_ex(aht, &intern->pos);
|
|
|
|
} while (1);
|
|
|
|
}
|
2004-04-27 23:42:45 +08:00
|
|
|
return FAILURE;
|
2004-04-25 19:14:11 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2004-04-27 23:42:45 +08:00
|
|
|
static int spl_array_next(spl_array_object *intern TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
HashTable *aht = HASH_OF(intern->array);
|
|
|
|
|
|
|
|
zend_hash_move_forward_ex(aht, &intern->pos);
|
|
|
|
if (Z_TYPE_P(intern->array) == IS_OBJECT) {
|
|
|
|
return spl_array_skip_protected(intern TSRMLS_CC);
|
|
|
|
} else {
|
|
|
|
return zend_hash_has_more_elements_ex(aht, &intern->pos);
|
|
|
|
}
|
|
|
|
} /* }}} */
|
|
|
|
|
2004-04-25 19:14:11 +08:00
|
|
|
/* define an overloaded iterator structure */
|
|
|
|
typedef struct {
|
|
|
|
zend_object_iterator intern;
|
|
|
|
spl_array_object *object;
|
|
|
|
} spl_array_it;
|
|
|
|
|
|
|
|
static void spl_array_it_dtor(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
spl_array_it *iterator = (spl_array_it *)iter;
|
|
|
|
|
|
|
|
zval_ptr_dtor((zval**)&iterator->intern.data);
|
|
|
|
|
|
|
|
efree(iterator);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static int spl_array_it_valid(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
spl_array_it *iterator = (spl_array_it *)iter;
|
|
|
|
spl_array_object *object = iterator->object;
|
|
|
|
HashTable *aht = HASH_OF(object->array);
|
|
|
|
|
|
|
|
if (!aht) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::valid(): Array was modified outside object and is no longer an array");
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object->pos && object->array->is_ref && spl_hash_verify_pos(object TSRMLS_CC) == FAILURE) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::valid(): Array was modified outside object and internal position is no longer valid");
|
|
|
|
return FAILURE;
|
|
|
|
} else {
|
|
|
|
return zend_hash_has_more_elements_ex(aht, &object->pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static void spl_array_it_get_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
spl_array_it *iterator = (spl_array_it *)iter;
|
|
|
|
spl_array_object *object = iterator->object;
|
|
|
|
HashTable *aht = HASH_OF(object->array);
|
|
|
|
|
|
|
|
if (zend_hash_get_current_data_ex(aht, (void**)data, &object->pos) == FAILURE) {
|
|
|
|
*data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static int spl_array_it_get_current_key(zend_object_iterator *iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
spl_array_it *iterator = (spl_array_it *)iter;
|
|
|
|
spl_array_object *object = iterator->object;
|
|
|
|
HashTable *aht = HASH_OF(object->array);
|
|
|
|
|
|
|
|
if (!aht) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::current(): Array was modified outside object and is no longer an array");
|
|
|
|
return HASH_KEY_NON_EXISTANT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object->array->is_ref && spl_hash_verify_pos(object TSRMLS_CC) == FAILURE) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::current(): Array was modified outside object and internal position is no longer valid");
|
|
|
|
return HASH_KEY_NON_EXISTANT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return zend_hash_get_current_key_ex(aht, str_key, str_key_len, int_key, 1, &object->pos);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static void spl_array_it_move_forward(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
spl_array_it *iterator = (spl_array_it *)iter;
|
|
|
|
spl_array_object *object = iterator->object;
|
2004-04-27 23:42:45 +08:00
|
|
|
|
2004-04-25 19:14:11 +08:00
|
|
|
HashTable *aht = HASH_OF(object->array);
|
|
|
|
|
|
|
|
if (!aht) {
|
2004-04-27 23:42:45 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::current(): Array was modified outside object and is no longer an array");
|
2004-04-25 19:14:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object->array->is_ref && spl_hash_verify_pos(object TSRMLS_CC) == FAILURE) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::next(): Array was modified outside object and internal position is no longer valid");
|
|
|
|
} else {
|
2004-04-27 23:42:45 +08:00
|
|
|
spl_array_next(object TSRMLS_CC);
|
2004-04-25 19:14:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static void spl_array_it_rewind(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
spl_array_it *iterator = (spl_array_it *)iter;
|
|
|
|
spl_array_object *object = iterator->object;
|
|
|
|
HashTable *aht = HASH_OF(object->array);
|
|
|
|
|
|
|
|
if (!aht) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "ArrayIterator::rewind(): Array was modified outside object and is no longer an array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_hash_internal_pointer_reset_ex(aht, &object->pos);
|
|
|
|
spl_array_skip_protected(object TSRMLS_CC);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* iterator handler table */
|
|
|
|
zend_object_iterator_funcs spl_array_it_funcs = {
|
|
|
|
spl_array_it_dtor,
|
|
|
|
spl_array_it_valid,
|
|
|
|
spl_array_it_get_current_data,
|
|
|
|
spl_array_it_get_current_key,
|
|
|
|
spl_array_it_move_forward,
|
|
|
|
spl_array_it_rewind
|
|
|
|
};
|
|
|
|
|
|
|
|
zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
spl_array_it *iterator = emalloc(sizeof(spl_array_it));
|
|
|
|
spl_array_object *array_object = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
|
|
|
|
object->refcount++;
|
|
|
|
iterator->intern.data = (void*)object;
|
|
|
|
iterator->intern.funcs = &spl_array_it_funcs;
|
|
|
|
iterator->object = array_object;
|
|
|
|
|
|
|
|
return (zend_object_iterator*)iterator;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-11-27 07:28:35 +08:00
|
|
|
/* {{{ proto void ArrayObject::__construct(array|object ar = array())
|
|
|
|
proto void ArrayIterator::__construct(array|object ar = array())
|
2003-07-20 04:54:22 +08:00
|
|
|
Cronstructs a new array iterator from a path. */
|
2003-11-09 22:05:36 +08:00
|
|
|
SPL_METHOD(Array, __construct)
|
2003-07-20 04:54:22 +08:00
|
|
|
{
|
|
|
|
zval *object = getThis();
|
|
|
|
spl_array_object *intern;
|
|
|
|
zval **array;
|
|
|
|
|
|
|
|
if (ZEND_NUM_ARGS() == 0) {
|
|
|
|
return; /* nothing to do */
|
|
|
|
}
|
|
|
|
/* exceptions do not work yet
|
|
|
|
php_set_error_handling(EH_THROW, zend_exception_get_default() TSRMLS_CC);*/
|
|
|
|
|
2004-04-14 03:06:16 +08:00
|
|
|
intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
|
2003-07-20 04:54:22 +08:00
|
|
|
if (ZEND_NUM_ARGS() > 1 || zend_get_parameters_ex(1, &array) == FAILURE) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
2004-04-14 03:06:16 +08:00
|
|
|
if (Z_TYPE_PP(array) == IS_OBJECT && (Z_OBJ_HT_PP(array) == &spl_handler_ArrayObject || Z_OBJ_HT_PP(array) == &spl_handler_ArrayIterator)) {
|
|
|
|
spl_array_object *other = (spl_array_object*)zend_object_store_get_object(*array TSRMLS_CC);
|
|
|
|
zval_dtor(intern->array);
|
|
|
|
FREE_ZVAL(intern->array);
|
|
|
|
intern->array = other->array;
|
|
|
|
} else {
|
|
|
|
if (!HASH_OF(*array)) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Passed variable is not an array or object, using empty array instead");
|
|
|
|
php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
zval_dtor(intern->array);
|
|
|
|
FREE_ZVAL(intern->array);
|
|
|
|
intern->array = *array;
|
2003-07-20 04:54:22 +08:00
|
|
|
}
|
|
|
|
ZVAL_ADDREF(intern->array);
|
|
|
|
|
|
|
|
zend_hash_internal_pointer_reset_ex(HASH_OF(intern->array), &intern->pos);
|
|
|
|
|
|
|
|
php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-11-27 07:28:35 +08:00
|
|
|
/* {{{ proto ArrayIterator ArrayObject::getIterator()
|
2003-11-17 13:32:43 +08:00
|
|
|
Create a new iterator from a ArrayObject instance */
|
2003-11-09 22:05:36 +08:00
|
|
|
SPL_METHOD(Array, getIterator)
|
2003-07-20 09:22:03 +08:00
|
|
|
{
|
|
|
|
zval *object = getThis();
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
spl_array_object *iterator;
|
|
|
|
HashTable *aht = HASH_OF(intern->array);
|
|
|
|
|
|
|
|
if (!aht) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return_value->type = IS_OBJECT;
|
2003-11-09 22:05:36 +08:00
|
|
|
return_value->value.obj = spl_array_object_new_ex(spl_ce_ArrayIterator, &iterator, intern TSRMLS_CC);
|
2003-07-20 09:22:03 +08:00
|
|
|
return_value->refcount = 1;
|
|
|
|
return_value->is_ref = 1;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-11-27 07:28:35 +08:00
|
|
|
/* {{{ proto void ArrayIterator::rewind()
|
2003-07-20 04:54:22 +08:00
|
|
|
Rewind array back to the start */
|
2003-11-09 22:05:36 +08:00
|
|
|
SPL_METHOD(Array, rewind)
|
2003-07-20 04:54:22 +08:00
|
|
|
{
|
|
|
|
zval *object = getThis();
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
2003-07-20 09:22:03 +08:00
|
|
|
HashTable *aht = HASH_OF(intern->array);
|
2003-07-20 04:54:22 +08:00
|
|
|
|
2003-07-20 09:22:03 +08:00
|
|
|
if (!aht) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_hash_internal_pointer_reset_ex(aht, &intern->pos);
|
2004-04-25 19:14:11 +08:00
|
|
|
spl_array_skip_protected(intern TSRMLS_CC);
|
2003-07-20 04:54:22 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2004-04-30 06:52:49 +08:00
|
|
|
/* {{{ proto void ArrayIterator::seek(int $position)
|
2004-04-27 23:42:45 +08:00
|
|
|
Seek to position. */
|
|
|
|
SPL_METHOD(Array, seek)
|
|
|
|
{
|
|
|
|
long position;
|
|
|
|
zval *object = getThis();
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
HashTable *aht = HASH_OF(intern->array);
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &position) == FAILURE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aht) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_hash_internal_pointer_reset_ex(aht, &intern->pos);
|
|
|
|
|
2004-09-01 04:54:00 +08:00
|
|
|
while (position-- > 0 && spl_array_next(intern TSRMLS_CC) == SUCCESS);
|
2004-04-27 23:42:45 +08:00
|
|
|
} /* }}} */
|
|
|
|
|
2004-05-06 16:57:20 +08:00
|
|
|
int spl_array_object_count_elements(zval *object, long *count TSRMLS_DC) /* {{{ */
|
2004-04-29 05:45:41 +08:00
|
|
|
{
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
HashTable *aht = HASH_OF(intern->array);
|
|
|
|
HashPosition pos;
|
|
|
|
|
|
|
|
if (!aht) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
|
2004-05-06 16:57:20 +08:00
|
|
|
*count = 0;
|
|
|
|
return FAILURE;
|
2004-04-29 05:45:41 +08:00
|
|
|
}
|
|
|
|
|
2004-04-29 15:22:02 +08:00
|
|
|
if (Z_TYPE_P(intern->array) == IS_OBJECT) {
|
2004-05-06 16:57:20 +08:00
|
|
|
/* We need to store the 'pos' since we'll modify it in the functions
|
|
|
|
* we're going to call and which do not support 'pos' as parameter. */
|
2004-04-29 15:22:02 +08:00
|
|
|
pos = intern->pos;
|
2004-05-06 16:57:20 +08:00
|
|
|
*count = 0;
|
2004-04-29 15:22:02 +08:00
|
|
|
zend_hash_internal_pointer_reset_ex(aht, &intern->pos);
|
2004-09-01 04:54:00 +08:00
|
|
|
while(intern->pos && spl_array_next(intern TSRMLS_CC) == SUCCESS) {
|
2004-05-06 16:57:20 +08:00
|
|
|
(*count)++;
|
2004-04-29 15:22:02 +08:00
|
|
|
}
|
|
|
|
intern->pos = pos;
|
2004-05-06 16:57:20 +08:00
|
|
|
return SUCCESS;
|
2004-04-29 15:22:02 +08:00
|
|
|
} else {
|
2004-05-06 16:57:20 +08:00
|
|
|
*count = zend_hash_num_elements(aht);
|
|
|
|
return SUCCESS;
|
2004-04-29 15:22:02 +08:00
|
|
|
}
|
2004-05-06 16:57:20 +08:00
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto int ArrayObject::count()
|
|
|
|
proto int ArrayIterator::count()
|
|
|
|
Return the number of elements in the Iterator. */
|
|
|
|
SPL_METHOD(Array, count)
|
|
|
|
{
|
|
|
|
long count;
|
|
|
|
|
|
|
|
spl_array_object_count_elements(getThis(), &count TSRMLS_CC);
|
|
|
|
RETURN_LONG(count);
|
2004-04-29 05:45:41 +08:00
|
|
|
} /* }}} */
|
|
|
|
|
2004-03-21 00:09:42 +08:00
|
|
|
/* {{{ proto mixed|NULL ArrayIterator::current()
|
2003-07-20 04:54:22 +08:00
|
|
|
Return current array entry */
|
2003-11-09 22:05:36 +08:00
|
|
|
SPL_METHOD(Array, current)
|
2003-07-20 04:54:22 +08:00
|
|
|
{
|
|
|
|
zval *object = getThis();
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
zval **entry;
|
2003-07-20 09:22:03 +08:00
|
|
|
HashTable *aht = HASH_OF(intern->array);
|
2003-07-20 04:54:22 +08:00
|
|
|
|
2003-07-20 09:22:03 +08:00
|
|
|
if (!aht) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-04-25 19:14:11 +08:00
|
|
|
if (intern->array->is_ref && spl_hash_verify_pos(intern TSRMLS_CC) == FAILURE) {
|
2003-07-20 09:22:03 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and internal position is no longer valid");
|
2004-03-21 00:09:42 +08:00
|
|
|
return;
|
2003-07-20 09:22:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (zend_hash_get_current_data_ex(aht, (void **) &entry, &intern->pos) == FAILURE) {
|
2004-03-21 00:09:42 +08:00
|
|
|
return;
|
2003-07-20 04:54:22 +08:00
|
|
|
}
|
|
|
|
*return_value = **entry;
|
|
|
|
zval_copy_ctor(return_value);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2004-03-21 00:09:42 +08:00
|
|
|
/* {{{ proto mixed|NULL ArrayIterator::key()
|
2003-07-20 04:54:22 +08:00
|
|
|
Return current array key */
|
2003-11-09 22:05:36 +08:00
|
|
|
SPL_METHOD(Array, key)
|
2003-07-20 04:54:22 +08:00
|
|
|
{
|
|
|
|
zval *object = getThis();
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
|
|
|
char *string_key;
|
|
|
|
uint string_length;
|
|
|
|
ulong num_key;
|
2003-07-20 09:22:03 +08:00
|
|
|
HashTable *aht = HASH_OF(intern->array);
|
|
|
|
|
|
|
|
if (!aht) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
|
|
|
|
return;
|
|
|
|
}
|
2003-07-20 04:54:22 +08:00
|
|
|
|
2004-04-25 19:14:11 +08:00
|
|
|
if (intern->array->is_ref && spl_hash_verify_pos(intern TSRMLS_CC) == FAILURE) {
|
2003-07-20 09:22:03 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and internal position is no longer valid");
|
2004-03-21 00:09:42 +08:00
|
|
|
return;
|
2003-07-20 09:22:03 +08:00
|
|
|
}
|
|
|
|
|
2004-04-25 19:14:11 +08:00
|
|
|
switch (zend_hash_get_current_key_ex(aht, &string_key, &string_length, &num_key, 1, &intern->pos)) {
|
2003-07-20 04:54:22 +08:00
|
|
|
case HASH_KEY_IS_STRING:
|
2004-04-25 19:14:11 +08:00
|
|
|
RETVAL_STRINGL(string_key, string_length - 1, 0);
|
2003-07-20 04:54:22 +08:00
|
|
|
break;
|
|
|
|
case HASH_KEY_IS_LONG:
|
|
|
|
RETVAL_LONG(num_key);
|
|
|
|
break;
|
|
|
|
case HASH_KEY_NON_EXISTANT:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-11-27 07:28:35 +08:00
|
|
|
/* {{{ proto void ArrayIterator::next()
|
2003-07-20 04:54:22 +08:00
|
|
|
Move to next entry */
|
2003-11-09 22:05:36 +08:00
|
|
|
SPL_METHOD(Array, next)
|
2003-07-20 04:54:22 +08:00
|
|
|
{
|
|
|
|
zval *object = getThis();
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
2003-07-20 09:22:03 +08:00
|
|
|
HashTable *aht = HASH_OF(intern->array);
|
2003-07-20 04:54:22 +08:00
|
|
|
|
2003-07-20 09:22:03 +08:00
|
|
|
if (!aht) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-04-25 19:14:11 +08:00
|
|
|
if (intern->array->is_ref && spl_hash_verify_pos(intern TSRMLS_CC) == FAILURE) {
|
2003-07-20 09:22:03 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and internal position is no longer valid");
|
|
|
|
} else {
|
2004-04-27 23:42:45 +08:00
|
|
|
spl_array_next(intern TSRMLS_CC);
|
2003-07-20 09:22:03 +08:00
|
|
|
}
|
2003-07-20 04:54:22 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2004-03-09 01:33:31 +08:00
|
|
|
/* {{{ proto bool ArrayIterator::valid()
|
2003-07-20 04:54:22 +08:00
|
|
|
Check whether array contains more entries */
|
2004-03-09 01:33:31 +08:00
|
|
|
SPL_METHOD(Array, valid)
|
2003-07-20 04:54:22 +08:00
|
|
|
{
|
|
|
|
zval *object = getThis();
|
|
|
|
spl_array_object *intern = (spl_array_object*)zend_object_store_get_object(object TSRMLS_CC);
|
2003-07-20 09:22:03 +08:00
|
|
|
HashTable *aht = HASH_OF(intern->array);
|
|
|
|
|
|
|
|
if (!aht) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and is no longer an array");
|
|
|
|
return;
|
|
|
|
}
|
2003-07-20 04:54:22 +08:00
|
|
|
|
2004-04-25 19:14:11 +08:00
|
|
|
if (intern->pos && intern->array->is_ref && spl_hash_verify_pos(intern TSRMLS_CC) == FAILURE) {
|
2003-07-20 09:22:03 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Array was modified outside object and internal position is no longer valid");
|
|
|
|
RETURN_FALSE;
|
|
|
|
} else {
|
|
|
|
RETURN_BOOL(zend_hash_has_more_elements_ex(aht, &intern->pos) == SUCCESS);
|
|
|
|
}
|
2003-07-20 04:54:22 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2004-05-06 16:57:20 +08:00
|
|
|
/* {{{ PHP_MINIT_FUNCTION(spl_array) */
|
|
|
|
PHP_MINIT_FUNCTION(spl_array)
|
|
|
|
{
|
|
|
|
REGISTER_SPL_STD_CLASS_EX(ArrayObject, spl_array_object_new, spl_funcs_ArrayObject);
|
|
|
|
zend_class_implements(spl_ce_ArrayObject TSRMLS_CC, 1, zend_ce_aggregate);
|
|
|
|
zend_class_implements(spl_ce_ArrayObject TSRMLS_CC, 1, zend_ce_arrayaccess);
|
|
|
|
memcpy(&spl_handler_ArrayObject, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
|
|
|
|
spl_handler_ArrayObject.clone_obj = spl_array_object_clone;
|
|
|
|
spl_handler_ArrayObject.read_dimension = spl_array_read_dimension;
|
|
|
|
spl_handler_ArrayObject.write_dimension = spl_array_write_dimension;
|
|
|
|
spl_handler_ArrayObject.unset_dimension = spl_array_unset_dimension;
|
|
|
|
spl_handler_ArrayObject.has_dimension = spl_array_has_dimension;
|
|
|
|
spl_handler_ArrayObject.get_properties = spl_array_get_properties;
|
|
|
|
spl_handler_ArrayObject.count_elements = spl_array_object_count_elements;
|
|
|
|
|
|
|
|
REGISTER_SPL_STD_CLASS_EX(ArrayIterator, spl_array_object_new, spl_funcs_ArrayIterator);
|
|
|
|
zend_class_implements(spl_ce_ArrayIterator TSRMLS_CC, 1, zend_ce_iterator);
|
|
|
|
zend_class_implements(spl_ce_ArrayIterator TSRMLS_CC, 1, zend_ce_arrayaccess);
|
|
|
|
REGISTER_SPL_IMPLEMENTS(ArrayIterator, SeekableIterator);
|
|
|
|
memcpy(&spl_handler_ArrayIterator, &spl_handler_ArrayObject, sizeof(zend_object_handlers));
|
|
|
|
spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator;
|
|
|
|
|
2004-11-01 18:45:54 +08:00
|
|
|
REGISTER_SPL_INTERFACE(Countable);
|
|
|
|
|
|
|
|
REGISTER_SPL_IMPLEMENTS(ArrayObject, Countable);
|
|
|
|
REGISTER_SPL_IMPLEMENTS(ArrayIterator, Countable);
|
|
|
|
|
2004-05-06 16:57:20 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-05-02 07:28:28 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: fdm=marker
|
|
|
|
* vim: noet sw=4 ts=4
|
|
|
|
*/
|