From 5e1606b882c9e6d33b08ebbda0b8f48900800bda Mon Sep 17 00:00:00 2001 From: Marcus Boerger Date: Wed, 16 Jul 2003 18:47:26 +0000 Subject: [PATCH] Add tree example --- ext/spl/examples/tree.php | 92 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 ext/spl/examples/tree.php diff --git a/ext/spl/examples/tree.php b/ext/spl/examples/tree.php new file mode 100755 index 0000000000..88056e3993 --- /dev/null +++ b/ext/spl/examples/tree.php @@ -0,0 +1,92 @@ + + * + * Simply specify the path to tree with parameter . + * + * (c) Marcus Boerger + */ + +class sub_dir implements spl_sequence +{ + protected $adir = array(); + protected $cnt = 0; + protected $path = ""; + protected $curr = ""; + protected $nodots = true; + + function __construct($path, $nodots = true) { + $this->cnt = 0; + $this->path = $path; + } + + function rewind() { + while($this->cnt) { + unset($this->adir[$this->cnt--]); + } + $dir = new spl_dir($this->path); + $dir->path = ""; + $this->adir[1] = $dir; + $this->cnt = 1; + if ($this->nodots) { + while ($this->has_more()) { + $ent = $this->current(); + if ($ent != '.' && $ent != '..') { + break; + } + $this->next(); + } + } + } + + function next() { + if ($this->cnt) { + $dir = $this->adir[$this->cnt]; + $ent = $dir->current(); + $path = $dir->get_path().'/'.$ent; + if ($ent != '.' && $ent != '..' && is_dir($path)) { + $new = new spl_dir($path); + $new->path = $dir->path.$ent.'/'; + $new->cnt = $this->cnt++; + $this->adir[$this->cnt] = $new; + if ($this->nodots) { + while ($new->has_more()) { + $ent = $new->current(); + if ($ent != '.' && $ent != '..') { + break; + } + $new->next(); + } + } + } + $dir->next(); + } + } + + function has_more() { + while ($this->cnt) { + $dir = $this->adir[$this->cnt]; + if ($dir->has_more()) { + return true; + } + unset($this->adir[$this->cnt--]); + } + return false; + } + + function current() { + if ($this->cnt) { + $dir = $this->adir[$this->cnt]; + return $dir->path . $dir->current(); + } + throw new exception("No more elements available"); + } +} + +foreach(new sub_dir($argv[1]) as $f) { + echo "$f\n"; +} + +?> \ No newline at end of file -- 2.50.1