From: Marcus Boerger Date: Thu, 14 Aug 2003 21:14:04 +0000 (+0000) Subject: Add 'graphical' tree mode X-Git-Tag: RELEASE_0_7~650 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=175c25c2a8a63129f8eda39b67b1c97f80fb1030;p=php Add 'graphical' tree mode --- diff --git a/ext/spl/examples/sub_dir.inc b/ext/spl/examples/sub_dir.inc index b18ddbfe41..aa6daba5f9 100755 --- a/ext/spl/examples/sub_dir.inc +++ b/ext/spl/examples/sub_dir.inc @@ -12,8 +12,8 @@ class sub_dir implements spl_sequence { protected $adir = array(); protected $cnt = 0; - protected $path = ""; - protected $curr = ""; + protected $path = ''; + protected $curr = ''; protected $nodots = true; /** @@ -22,9 +22,11 @@ class sub_dir implements spl_sequence * @param path The path to iterate. * @param nodots Whether or not to display the entries '.' and '..'. */ - function __construct($path, $nodots = true) { + function __construct($path, $nodots = true, $graph = false) { $this->cnt = 0; $this->path = $path; + $this->nodots = $nodots; + $this->graph = $graph; } /** @@ -41,12 +43,12 @@ class sub_dir implements spl_sequence $this->adir[1] = $dir; $this->cnt = 1; if ($this->nodots) { - while ($this->has_more()) { - $ent = $this->current(); + while ($dir->has_more()) { + $ent = $dir->current(); if ($ent != '.' && $ent != '..') { break; } - $this->next(); + $dir->next(); } } } @@ -67,13 +69,17 @@ class sub_dir implements spl_sequence $new->cnt = $this->cnt++; $this->adir[$this->cnt] = $new; if ($this->nodots) { + $dir->has_more = false; while ($new->has_more()) { $ent = $new->current(); if ($ent != '.' && $ent != '..') { + $dir->has_more = true; break; } $new->next(); } + } else { + $dir->has_more = $dir->has_more(); } } $dir->next(); @@ -99,8 +105,20 @@ class sub_dir implements spl_sequence */ function current() { if ($this->cnt) { - $dir = $this->adir[$this->cnt]; - return $dir->path . $dir->current(); + if ($this->graph) { + $prefix = ''; + for ($i = 1; $i < $this->cnt; $i++) { + $dir = $this->adir[$i]; + $prefix .= $dir->has_more() ? '| ' : ' '; + } + $dir = $this->adir[$this->cnt]; + $ent = $dir->current(); + $prefix .= $dir->has_more() ? '+-' : '\-'; + return $prefix . $ent; + } else { + $dir = $this->adir[$this->cnt]; + return $dir->path . $dir->current(); + } } throw new exception("No more elements available"); } diff --git a/ext/spl/examples/tree.php b/ext/spl/examples/tree.php index d5f54c3354..a0149bc18d 100755 --- a/ext/spl/examples/tree.php +++ b/ext/spl/examples/tree.php @@ -11,7 +11,7 @@ require_once("sub_dir.inc"); -foreach(new sub_dir($argv[1]) as $f) { +foreach(new sub_dir($argv[1], true, isset($argv[1]) ? $argv[1] : false) as $f) { echo "$f\n"; }