helly           Thu Oct  7 19:08:19 2004 EDT

  Added files:                 
    /php-src/ext/spl/tests      spl_001.phpt 

  Modified files:              
    /php-src/ext/spl    php_spl.c spl_iterators.c spl_iterators.h 
  Log:
  - Added iterator_to_array() and iterator_count()
  
  
http://cvs.php.net/diff.php/php-src/ext/spl/php_spl.c?r1=1.28&r2=1.29&ty=u
Index: php-src/ext/spl/php_spl.c
diff -u php-src/ext/spl/php_spl.c:1.28 php-src/ext/spl/php_spl.c:1.29
--- php-src/ext/spl/php_spl.c:1.28      Thu Apr 29 19:02:11 2004
+++ php-src/ext/spl/php_spl.c   Thu Oct  7 19:08:16 2004
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_spl.c,v 1.28 2004/04/29 23:02:11 helly Exp $ */
+/* $Id: php_spl.c,v 1.29 2004/10/07 23:08:16 helly Exp $ */
 
 #ifdef HAVE_CONFIG_H
        #include "config.h"
@@ -45,6 +45,10 @@
        PHP_FE(spl_classes,             NULL)
        PHP_FE(class_parents,           NULL)
        PHP_FE(class_implements,        NULL)
+#ifdef SPL_ITERATORS_H
+       PHP_FE(iterator_to_array,       NULL)
+       PHP_FE(iterator_count,          NULL)
+#endif /* SPL_ITERATORS_H */
        {NULL, NULL, NULL}
 };
 /* }}} */
http://cvs.php.net/diff.php/php-src/ext/spl/spl_iterators.c?r1=1.39&r2=1.40&ty=u
Index: php-src/ext/spl/spl_iterators.c
diff -u php-src/ext/spl/spl_iterators.c:1.39 php-src/ext/spl/spl_iterators.c:1.40
--- php-src/ext/spl/spl_iterators.c:1.39        Wed Jul 28 18:53:10 2004
+++ php-src/ext/spl/spl_iterators.c     Thu Oct  7 19:08:16 2004
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: spl_iterators.c,v 1.39 2004/07/28 22:53:10 helly Exp $ */
+/* $Id: spl_iterators.c,v 1.40 2004/10/07 23:08:16 helly Exp $ */
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
@@ -1322,6 +1322,70 @@
        {NULL, NULL, NULL}
 };
 
+/* {{{ array iterator_to_array(IteratorAggregate $it) 
+   Copy the iterator into an array */
+PHP_FUNCTION(iterator_to_array)
+{
+       zval                   *obj, **data;
+       zend_object_iterator   *iter;
+       char                   *str_key;
+       uint                    str_key_len;
+       ulong                   int_key;
+       int                     key_type;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, 
zend_ce_aggregate) == FAILURE) {
+               RETURN_FALSE;
+       }
+       
+       array_init(return_value);
+       
+       iter = Z_OBJCE_P(obj)->get_iterator(Z_OBJCE_P(obj), obj TSRMLS_CC);
+
+       iter->funcs->rewind(iter TSRMLS_CC);
+       while (iter->funcs->valid(iter TSRMLS_CC) == SUCCESS) {
+               key_type = iter->funcs->get_current_key(iter, &str_key, &str_key_len, 
&int_key TSRMLS_CC);
+               iter->funcs->get_current_data(iter, &data TSRMLS_CC);
+               (*data)->refcount++;
+               switch(key_type) {
+                       case HASH_KEY_IS_STRING:
+                               add_assoc_zval_ex(return_value, str_key, str_key_len, 
*data);
+                               efree(str_key);
+                               break;
+                       case HASH_KEY_IS_LONG:
+                               add_index_zval(return_value, int_key, *data);
+                               break;
+               }
+               iter->funcs->move_forward(iter TSRMLS_CC);
+       }
+       iter->funcs->dtor(iter TSRMLS_CC);
+}
+/* }}} */
+
+/* {{{ int iterator_count(IteratorAggregate $it) 
+   Count the elements in an iterator */
+PHP_FUNCTION(iterator_count)
+{
+       zval                   *obj;
+       zend_object_iterator   *iter;
+       long                    count = 0;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, 
zend_ce_aggregate) == FAILURE) {
+               RETURN_FALSE;
+       }
+       
+       iter = Z_OBJCE_P(obj)->get_iterator(Z_OBJCE_P(obj), obj TSRMLS_CC);
+
+       iter->funcs->rewind(iter TSRMLS_CC);
+       while (iter->funcs->valid(iter TSRMLS_CC) == SUCCESS) {
+               count++;
+               iter->funcs->move_forward(iter TSRMLS_CC);
+       }
+       iter->funcs->dtor(iter TSRMLS_CC);
+       
+       RETURN_LONG(count);
+}
+/* }}} */
+
 /* {{{ PHP_MINIT_FUNCTION(spl_iterators)
  */
 PHP_MINIT_FUNCTION(spl_iterators)
http://cvs.php.net/diff.php/php-src/ext/spl/spl_iterators.h?r1=1.9&r2=1.10&ty=u
Index: php-src/ext/spl/spl_iterators.h
diff -u php-src/ext/spl/spl_iterators.h:1.9 php-src/ext/spl/spl_iterators.h:1.10
--- php-src/ext/spl/spl_iterators.h:1.9 Mon Mar  8 13:05:41 2004
+++ php-src/ext/spl/spl_iterators.h     Thu Oct  7 19:08:16 2004
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: spl_iterators.h,v 1.9 2004/03/08 18:05:41 helly Exp $ */
+/* $Id: spl_iterators.h,v 1.10 2004/10/07 23:08:16 helly Exp $ */
 
 #ifndef SPL_ITERATORS_H
 #define SPL_ITERATORS_H
@@ -35,6 +35,9 @@
 
 PHP_MINIT_FUNCTION(spl_iterators);
 
+PHP_FUNCTION(iterator_to_array);
+PHP_FUNCTION(iterator_count);
+
 typedef enum {
        DIT_Default = 0,
        DIT_LimitIterator,

http://cvs.php.net/co.php/php-src/ext/spl/tests/spl_001.phpt?r=1.1&p=1
Index: php-src/ext/spl/tests/spl_001.phpt
+++ php-src/ext/spl/tests/spl_001.phpt
--TEST--
SPL: iterator_to_array() and iterator_count()
--FILE--
<?php

$it = new ArrayObject(array("x"=>1, 1=>2, 3=>3, 4, "1"=>5));

$ar = iterator_to_array($it);

var_dump(iterator_count($it));

print_r($ar);

foreach($ar as $v)
{
        var_dump($v);
}

?>
===DONE===
--EXPECT--
int(4)
Array
(
    [x] => 1
    [1] => 5
    [3] => 3
    [4] => 4
)
int(1)
int(5)
int(3)
int(4)
===DONE===

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to