helly           Thu Aug 14 17:14:04 2003 EDT

  Modified files:              
    /spl/examples       sub_dir.inc tree.php 
  Log:
  Add 'graphical' tree mode
  
Index: spl/examples/sub_dir.inc
diff -u spl/examples/sub_dir.inc:1.2 spl/examples/sub_dir.inc:1.3
--- spl/examples/sub_dir.inc:1.2        Wed Jul 16 17:51:09 2003
+++ spl/examples/sub_dir.inc    Thu Aug 14 17:14:04 2003
@@ -12,8 +12,8 @@
 {
        protected $adir = array();
        protected $cnt  = 0;
-       protected $path = "";
-       protected $curr = "";
+       protected $path = '';
+       protected $curr = '';
        protected $nodots = true;
 
        /**
@@ -22,9 +22,11 @@
         * @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 @@
                $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 @@
                                $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 @@
         */
        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");
        }
Index: spl/examples/tree.php
diff -u spl/examples/tree.php:1.2 spl/examples/tree.php:1.3
--- spl/examples/tree.php:1.2   Wed Jul 16 16:17:34 2003
+++ spl/examples/tree.php       Thu Aug 14 17:14:04 2003
@@ -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";
 }
 



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to