summaryrefslogtreecommitdiff
path: root/ext/spl/examples
diff options
context:
space:
mode:
authorMarcus Boerger <helly@php.net>2003-08-14 21:14:04 +0000
committerMarcus Boerger <helly@php.net>2003-08-14 21:14:04 +0000
commit175c25c2a8a63129f8eda39b67b1c97f80fb1030 (patch)
treeba1bd1b3fee3dd50e6f90400b9f0a85fd2672df6 /ext/spl/examples
parent29c434b4b7752cb252030dd993fc3b6474739bca (diff)
downloadphp-git-175c25c2a8a63129f8eda39b67b1c97f80fb1030.tar.gz
Add 'graphical' tree mode
Diffstat (limited to 'ext/spl/examples')
-rwxr-xr-xext/spl/examples/sub_dir.inc34
-rwxr-xr-xext/spl/examples/tree.php2
2 files changed, 27 insertions, 9 deletions
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";
}