<?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
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()
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();
}
function __toString()
{
- return $this->strvalue;
+ if (!$this->getStrVal) {
+ throw new exception('CachingIterator does not fetch string value (see CachingIterator::__construct)');
+ }
+ return $this->strValue;
}
}
$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";
}
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();
}
function next() {
$this->it->next();
- $this->index++;
+ $this->pos++;
+ }
+
+ function getPosition() {
+ return $this->pos;
}
}
<?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;
$this->next();
$position++;
}
- return $position;
}*/
}