--- /dev/null
+<?php
+
+class CachingIterator
+{
+ protected $it;
+ protected $current;
+ protected $key;
+ protected $more;
+ protected $strvalue;
+
+ function __construct(Iterator $it) {
+ $this->it = $it;
+ }
+
+ function rewind() {
+ $this->it->rewind();
+ $this->next();
+ }
+
+ function next() {
+ if ($this->more = $this->it->hasMore()) {
+ $this->current = $this->it->current();
+ $this->key = $this->it->key();
+ $this->strvalue = (string)$this->current;
+ } else {
+ $this->current = NULL;
+ $this->key = NULL;
+ $this->strvalue = '';
+ }
+ $this->it->next();
+ }
+
+ function hasMore() {
+ return $this->more;
+ }
+
+ function hasNext() {
+ return $this->it->hasMore();
+ }
+
+ function current() {
+ return $this->current;
+ }
+
+ function key() {
+ return $this->key;
+ }
+
+ function __call($func, $params) {
+ return call_user_func_array(array($this->it, $func), $params);
+ }
+
+ function __toString() {
+ return $this->strvalue;
+ }
+}
+
+?>
\ No newline at end of file
--- /dev/null
+<?php
+
+class CachingRecursiveIterator extends CachingIterator implements RecursiveIterator
+{
+ protected $hasChildren;
+ protected $getChildren;
+
+ function __construct(RecursiveIterator $it) {
+ parent::__construct($it);
+ }
+
+ function next() {
+ if ($this->hasChildren = $this->it->hasChildren()) {
+ $this->getChildren = new CachingRecursiveIterator($this->it->getChildren());
+ } else {
+ $this->getChildren = NULL;
+ }
+ parent::next();
+ }
+
+ function hasChildren() {
+ return $this->hasChildren;
+ }
+
+ function getChildren() {
+ return $this->getChildren;
+ }
+}
+
+?>
\ No newline at end of file
$length = $argc > 3 ? $argv[3] : NULL;
-foreach(new RecursiveIteratorIterator(new DirectoryTreeIterator($argv[1])) as $pathname => $file) {
- echo "$pathname\n";
+foreach(new LimitIterator(new DirectoryTreeIterator($argv[1]), @$argv[2], $length) as $pathname => $file) {
+ echo "$file\n";
}
?>
\ No newline at end of file
--- /dev/null
+<?php
+
+class DirectoryTreeIterator extends RecursiveIteratorIterator
+{
+ function __construct($path)
+ {
+ parent::__construct(new CachingRecursiveIterator(new RecursiveDirectoryIterator($path)), 1);
+ }
+
+ function current()
+ {
+ $tree = '';
+ for ($l=0; $l < $this->getLevel(); $l++) {
+ $tree .= $this->getSubIterator($l)->hasMore() ? '| ' : ' ';
+ }
+ return $tree . ($this->getSubIterator($l)->hasNext() ? '|-' : '\-')
+ . $this->getSubIterator($l);
+ }
+}
+
+?>
\ No newline at end of file
{
$this->it->rewind();
$this->index = 0;
- if (is_a($this->it, 'SeekableIterator')) {
+ if ($this->it instanceof SeekableIterator) {
$this->index = $this->it->seek($this->offset);
} else {
while($this->index < $this->offset && $this->it->hasMore()) {
--- /dev/null
+<?php
+
+interface RecursiveIterator implements Iterator
+{
+ function hasChildren();
+ function getChildren();
+}
+
+?>
\ No newline at end of file
{
private $done = false;
+ function rewind() {
+ parent::rewind();
+ $this->done = false;
+ }
+
function hasMore() {
return !$this->done && parent::hasMore();
}