]> granicus.if.org Git - php/commitdiff
Update examples
authorMarcus Boerger <helly@php.net>
Sat, 6 Dec 2003 19:03:17 +0000 (19:03 +0000)
committerMarcus Boerger <helly@php.net>
Sat, 6 Dec 2003 19:03:17 +0000 (19:03 +0000)
ext/spl/examples/autoload.inc
ext/spl/examples/cachingiterator.inc
ext/spl/examples/directorytree.php
ext/spl/examples/limititerator.inc
ext/spl/examples/seekableiterator.inc

index 632ab8e4e80d79da590b0368a97d003b5e61392d..34072f8e4a6b0de359d7ce5715f5fa9b4cc76ea9 100755 (executable)
@@ -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 c54f650e7ee26c1c820f6dba5d6ac643b1113f7e..a9cc075b704e923e359f36537ae93b78ab46a11d 100644 (file)
@@ -6,11 +6,13 @@ class CachingIterator
        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 @@ class CachingIterator
                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 @@ class CachingIterator
        
        function __toString()
        {
-               return $this->strvalue;
+               if (!$this->getStrVal) {
+                       throw new exception('CachingIterator does not fetch string value (see CachingIterator::__construct)');
+               }
+               return $this->strValue;
        }
 }
 
index 31af2495a74d777011dcd4911a47472dc4d0fc14..ff644f66021fd494032a7383f502983b654ebb8e 100755 (executable)
@@ -25,6 +25,7 @@ EOF;
 $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 1b0e927a2ea6be977584aed666853abc8ce49730..e471f5d0e469d8b9e7173b22a7c2d0d516417cb7 100755 (executable)
@@ -5,33 +5,49 @@ class LimitIterator implements Iterator
        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 @@ class LimitIterator implements Iterator
 
        function next() {
                $this->it->next();
-               $this->index++;
+               $this->pos++;
+       }
+
+       function getPosition() {
+               return $this->pos;
        }
 }
 
index d7381fc3f830eca4eae22cab0db9a72fc477c68c..7e47009260054053dbb5b86be51dcb08e9dc334a 100755 (executable)
@@ -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 @@ interface SeekableIterator implements Iterator
                        $this->next();
                        $position++;
                }
-               return $position;
        }*/
 }