helly           Sat Dec  6 14:03:18 2003 EDT

  Modified files:              
    /spl/examples       autoload.inc cachingiterator.inc directorytree.php 
                        limititerator.inc seekableiterator.inc 
  Log:
  Update examples
  
Index: spl/examples/autoload.inc
diff -u spl/examples/autoload.inc:1.1 spl/examples/autoload.inc:1.2
--- spl/examples/autoload.inc:1.1       Thu Dec  4 14:39:46 2003
+++ spl/examples/autoload.inc   Sat Dec  6 14:03:17 2003
@@ -1,7 +1,7 @@
 <?php
 
-function __autoload($file) {
-       
require_once(dirname($_SERVER['PATH_TRANSLATED']).'/'.strtolower($file).'.inc');
+function __autoload($classname) {
+       
require_once(dirname($_SERVER['PATH_TRANSLATED']).'/'.strtolower($classname).'.inc');
 }
 
 ?>
\ No newline at end of file
Index: spl/examples/cachingiterator.inc
diff -u spl/examples/cachingiterator.inc:1.3 spl/examples/cachingiterator.inc:1.4
--- spl/examples/cachingiterator.inc:1.3        Thu Dec  4 15:56:32 2003
+++ spl/examples/cachingiterator.inc    Sat Dec  6 14:03:17 2003
@@ -6,11 +6,13 @@
        protected $current;
        protected $key;
        protected $more;
-       protected $strvalue;
+       protected $strValue;
+       protected $getStrVal;
 
-       function __construct(Iterator $it)
+       function __construct(Iterator $it, $getStrVal = true)
        {
                $this->it = $it;
+               $this->getStrVal = (boolean)$getStrVal;
        }
 
        function rewind()
@@ -24,15 +26,17 @@
                if ($this->more = $this->it->hasMore()) {
                        $this->current = $this->it->current();
                        $this->key = $this->it->key();
-                       if (is_object($this->current)) {
-                               $this->strvalue = $this->current->__toString();
-                       } else {
-                               $this->strvalue = (string)$this->current;
+                       if ($this->getStrVal) {
+                               if (is_object($this->current)) {
+                                       $this->strValue = $this->current->__toString();
+                               } else {
+                                       $this->strValue = (string)$this->current;
+                               }
                        }
                } else {
                        $this->current = NULL;
                        $this->key = NULL;
-                       $this->strvalue = '';
+                       $this->strValue = '';
                }
                $this->it->next();
        }
@@ -64,7 +68,10 @@
        
        function __toString()
        {
-               return $this->strvalue;
+               if (!$this->getStrVal) {
+                       throw new exception('CachingIterator does not fetch string 
value (see CachingIterator::__construct)');
+               }
+               return $this->strValue;
        }
 }
 
Index: spl/examples/directorytree.php
diff -u spl/examples/directorytree.php:1.5 spl/examples/directorytree.php:1.6
--- spl/examples/directorytree.php:1.5  Thu Dec  4 14:39:46 2003
+++ spl/examples/directorytree.php      Sat Dec  6 14:03:17 2003
@@ -25,6 +25,7 @@
 $length = $argc > 3 ? $argv[3] : NULL;
 
 foreach(new LimitIterator(new DirectoryTreeIterator($argv[1]), @$argv[2], $length) as 
$file) {
+//foreach(new DirectoryTreeIterator($argv[1]) as $file) {
        echo $file ."\n";
 }
 
Index: spl/examples/limititerator.inc
diff -u spl/examples/limititerator.inc:1.3 spl/examples/limititerator.inc:1.4
--- spl/examples/limititerator.inc:1.3  Tue Nov 18 17:18:38 2003
+++ spl/examples/limititerator.inc      Sat Dec  6 14:03:17 2003
@@ -5,33 +5,49 @@
        protected $it;
        protected $offset;
        protected $count;
-       protected $index;
+       private $pos;
 
-       // negative offset is  respected
        // count === NULL means all
-       function __construct(Iterator $it, $offset = 0, $count = NULL)
+       function __construct(Iterator $it, $offset = 0, $count = -1)
        {
+               if ($offset < 0) {
+                       throw new exception('Parameter offset must be > 0');
+               }
+               if ($count < 0 && $count != -1) {
+                       throw new exception('Parameter count must either be -1 or a 
value greater than or equal to 0');
+               }
                $this->it     = $it;
                $this->offset = $offset;
                $this->count  = $count;
-               $this->index  = 0;
+               $this->pos    = 0;
        }
        
-       function rewind()
-       {
-               $this->it->rewind();
-               $this->index = 0;
+       function seek($position) {
+               if ($position < $this->offset) {
+                       throw new exception('Cannot seek to '.$position.' which is 
below offset '.$this->offset);
+               }
+               if ($position > $this->offset + $this->count && $this->count != -1) {
+                       throw new exception('Cannot seek to '.$position.' which is 
behind offset '.$this->offset.' plus count '.$this->count);
+               }
                if ($this->it instanceof SeekableIterator) {
-                       $this->index = $this->it->seek($this->offset);
+                       $this->it->seek($position);
+                       $this->pos = $position;
                } else {
-                       while($this->index < $this->offset && $this->it->hasMore()) {
+                       while($this->pos < $position && $this->it->hasMore()) {
                                $this->next();
                        }
                }
        }
+
+       function rewind()
+       {
+               $this->it->rewind();
+               $this->pos = 0;
+               $this->seek($this->offset);
+       }
        
        function hasMore() {
-               return (is_null($this->count) || $this->index < $this->offset + 
$this->count)
+               return ($this->count == -1 || $this->pos < $this->offset + 
$this->count)
                         && $this->it->hasMore();
        }
        
@@ -45,7 +61,11 @@
 
        function next() {
                $this->it->next();
-               $this->index++;
+               $this->pos++;
+       }
+
+       function getPosition() {
+               return $this->pos;
        }
 }
 
Index: spl/examples/seekableiterator.inc
diff -u spl/examples/seekableiterator.inc:1.3 spl/examples/seekableiterator.inc:1.4
--- spl/examples/seekableiterator.inc:1.3       Sun Nov 16 19:55:37 2003
+++ spl/examples/seekableiterator.inc   Sat Dec  6 14:03:17 2003
@@ -1,7 +1,21 @@
 <?php
 
+/** \brief seekable iterator
+ *
+ * Turns a normal iterator ino a seekable iterator. When there is a way
+ * to seek on an iterator LimitIterator can use this to efficiently rewind
+ * to offset.
+ */
 interface SeekableIterator implements Iterator
 {
+       /** Seek to an absolute position
+        *
+        * \param $index position to seek to
+        * \return void
+        *
+        * \note The method should throw an exception if it is not possible to
+        *       seek to the given position.
+        */
        function seek($index);
 /*             $this->rewind();
                $position = 0;
@@ -9,7 +23,6 @@
                        $this->next();
                        $position++;
                }
-               return $position;
        }*/
 }
 

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

Reply via email to