colder          Sat Jul 19 11:20:05 2008 UTC

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

  Modified files:              
    /php-src/ext/spl    spl_directory.c spl_directory.h 
  Log:
  Implement DirectoryIterator::seek
  
http://cvs.php.net/viewvc.cgi/php-src/ext/spl/spl_directory.c?r1=1.159&r2=1.160&diff_format=u
Index: php-src/ext/spl/spl_directory.c
diff -u php-src/ext/spl/spl_directory.c:1.159 
php-src/ext/spl/spl_directory.c:1.160
--- php-src/ext/spl/spl_directory.c:1.159       Tue Jul  8 22:49:35 2008
+++ php-src/ext/spl/spl_directory.c     Sat Jul 19 11:20:04 2008
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: spl_directory.c,v 1.159 2008/07/08 22:49:35 colder Exp $ */
+/* $Id: spl_directory.c,v 1.160 2008/07/19 11:20:04 colder Exp $ */
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
@@ -776,6 +776,44 @@
 }
 /* }}} */
 
+/* {{{ proto void DirectoryIterator::seek(int position)
+   Seek to the given position */
+SPL_METHOD(DirectoryIterator, seek)
+{
+       spl_filesystem_object *intern    = 
(spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+       zval                  *retval    = NULL;
+       long                   pos;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &pos) == 
FAILURE) {
+               return;
+       }
+
+       if (intern->u.dir.index > pos) {
+               /* we first rewind */
+               zend_call_method_with_0_params(&this_ptr, Z_OBJCE_P(getThis()), 
&intern->u.dir.func_rewind, "rewind", &retval);
+               if (retval) {
+                       zval_ptr_dtor(&retval);
+               }
+       }
+
+       while (intern->u.dir.index < pos) {
+               int valid = 0;
+               zend_call_method_with_0_params(&this_ptr, Z_OBJCE_P(getThis()), 
&intern->u.dir.func_valid, "valid", &retval);
+               if (retval) {
+                       valid = zend_is_true(retval);
+                       zval_ptr_dtor(&retval);
+               }
+               if (!valid) {
+                       break;
+               }
+               zend_call_method_with_0_params(&this_ptr, Z_OBJCE_P(getThis()), 
&intern->u.dir.func_next, "next", &retval);
+               if (retval) {
+                       zval_ptr_dtor(&retval);
+               }
+       }
+} /* }}} */
+
+
 /* {{{ proto string DirectoryIterator::valid() U
    Check whether dir contains more entries */
 SPL_METHOD(DirectoryIterator, valid)
@@ -1768,6 +1806,12 @@
        ZEND_ARG_INFO(0, path)
 ZEND_END_ARG_INFO()
 
+static
+ZEND_BEGIN_ARG_INFO(arginfo_dir_it_seek, 0) 
+       ZEND_ARG_INFO(0, position)
+ZEND_END_ARG_INFO();
+
+
 /* the method table */
 /* each method can have its own parameters and visibility */
 static const zend_function_entry spl_DirectoryIterator_functions[] = {
@@ -1780,6 +1824,7 @@
        SPL_ME(DirectoryIterator, key,           NULL, ZEND_ACC_PUBLIC)
        SPL_ME(DirectoryIterator, current,       NULL, ZEND_ACC_PUBLIC)
        SPL_ME(DirectoryIterator, next,          NULL, ZEND_ACC_PUBLIC)
+       SPL_ME(DirectoryIterator, seek,          arginfo_dir_it_seek, 
ZEND_ACC_PUBLIC)
        SPL_MA(DirectoryIterator, __toString, DirectoryIterator, getFilename, 
NULL, ZEND_ACC_PUBLIC)
        {NULL, NULL, NULL}
 };
@@ -2704,12 +2749,13 @@
 
        REGISTER_SPL_STD_CLASS_EX(SplFileInfo, spl_filesystem_object_new, 
spl_SplFileInfo_functions);
        memcpy(&spl_filesystem_object_handlers, zend_get_std_object_handlers(), 
sizeof(zend_object_handlers));
-       spl_filesystem_object_handlers.clone_obj = spl_filesystem_object_clone;
-       spl_filesystem_object_handlers.cast_object = spl_filesystem_object_cast;
+       spl_filesystem_object_handlers.clone_obj      = 
spl_filesystem_object_clone;
+       spl_filesystem_object_handlers.cast_object    = 
spl_filesystem_object_cast;
        spl_filesystem_object_handlers.get_debug_info = 
spl_filesystem_object_get_debug_info;
 
        REGISTER_SPL_SUB_CLASS_EX(DirectoryIterator, SplFileInfo, 
spl_filesystem_object_new, spl_DirectoryIterator_functions);
        zend_class_implements(spl_ce_DirectoryIterator TSRMLS_CC, 1, 
zend_ce_iterator);
+       REGISTER_SPL_IMPLEMENTS(DirectoryIterator, SeekableIterator);
 
        spl_ce_DirectoryIterator->get_iterator = 
spl_filesystem_dir_get_iterator;
 
http://cvs.php.net/viewvc.cgi/php-src/ext/spl/spl_directory.h?r1=1.44&r2=1.45&diff_format=u
Index: php-src/ext/spl/spl_directory.h
diff -u php-src/ext/spl/spl_directory.h:1.44 
php-src/ext/spl/spl_directory.h:1.45
--- php-src/ext/spl/spl_directory.h:1.44        Tue May 20 21:46:13 2008
+++ php-src/ext/spl/spl_directory.h     Sat Jul 19 11:20:04 2008
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: spl_directory.h,v 1.44 2008/05/20 21:46:13 colder Exp $ */
+/* $Id: spl_directory.h,v 1.45 2008/07/19 11:20:04 colder Exp $ */
 
 #ifndef SPL_DIRECTORY_H
 #define SPL_DIRECTORY_H
@@ -82,6 +82,9 @@
                        int                sub_path_len;
                        int                index;
                        int                is_recursive;
+                       zend_function      *func_rewind;
+                       zend_function      *func_next;
+                       zend_function      *func_valid;
                } dir;
                struct {
                        php_stream         *stream;

http://cvs.php.net/viewvc.cgi/php-src/ext/spl/tests/dit_006.phpt?view=markup&rev=1.1
Index: php-src/ext/spl/tests/dit_006.phpt
+++ php-src/ext/spl/tests/dit_006.phpt
--TEST--
SPL: DirectoryIterator and seek
--FILE--
<?php
$di = new DirectoryIterator(__DIR__);
$di->seek(2);

$n = 0;
while ($di->valid()) {
    $n++;
    $di->next();
}

echo "With seek(2) we get $n\n";
$di->seek(0);

$m = 0;
while ($di->valid()) {
    $m++;
    $di->next();
}
echo "With seek(0) we get $m\n";

$o = 0;
$di->rewind();
while ($di->valid()) {
    $o++;
    $di->next();
}

echo "Without seek we get $o\n";

$p = 0;
$di->seek($o+1);
while ($di->valid()) {
    $p++;
    $di->next();
}

var_dump($n !== $m, $m === $o, $p === 0);
?>
===DONE===
--EXPECTF--
With seek(2) we get %d
With seek(0) we get %d
Without seek we get %d
bool(true)
bool(true)
bool(true)
===DONE===



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

Reply via email to