From: Marcus Boerger Date: Wed, 16 Jul 2003 20:18:33 +0000 (+0000) Subject: More examples X-Git-Tag: BEFORE_ARG_INFO~215 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=49277b2ac6ab8175a64174174daddbc3413bbee3;p=php More examples --- diff --git a/ext/spl/examples/filter.inc b/ext/spl/examples/filter.inc new file mode 100755 index 0000000000..23fe38cbae --- /dev/null +++ b/ext/spl/examples/filter.inc @@ -0,0 +1,92 @@ +rewind(); + } + $this->it = $it; + $this->regex = $regex; + $this->fetch(); + } + + /** + * Destruct the iterator. + */ + function __destruct() { + unset($this->it); + } + + /** + * Fetch next element and store it. + * + * @return void + */ + protected function fetch() { + $this->curr = false; + while ($this->it->has_more()) { + $curr = $this->it->current(); + if (ereg($this->regex, $curr)) { + $this->curr = $curr; + return; + } + $this->it->next(); + }; + } + + /** + * Move to next element + * + * @return void + */ + function next() { + $this->it->next(); + $this->fetch(); + } + + /** + * @return Whether more elements are available + */ + function has_more() { + return $this->curr !== false; + } + + /** + * @return The current value + */ + function current() { + return $this->curr; + } + + /** + * hidden __clone + */ + protected function __clone() { + // disallow clone + } +} + +?> \ No newline at end of file diff --git a/ext/spl/examples/filter_tree.php b/ext/spl/examples/filter_tree.php new file mode 100755 index 0000000000..8ee4cef556 --- /dev/null +++ b/ext/spl/examples/filter_tree.php @@ -0,0 +1,20 @@ + + * + * Simply specify the path to tree with parameter . + * The regular expression is used to filter the tree. + * + * (c) Marcus Boerger + */ + +require_once("sub_dir.inc"); +require_once("filter.inc"); + +foreach(new filter(new sub_dir($argv[1]), $argv[2]) as $f) { + echo "$f\n"; +} + +?> \ No newline at end of file diff --git a/ext/spl/examples/sub_dir.inc b/ext/spl/examples/sub_dir.inc new file mode 100755 index 0000000000..74477c82eb --- /dev/null +++ b/ext/spl/examples/sub_dir.inc @@ -0,0 +1,116 @@ +cnt = 0; + $this->path = $path; + } + + /** + * Rewind the directory. + * + * @return void + */ + 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(); + } + } + } + + /** + * Move to net dir or file entry. + * + * @return void + */ + 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(); + } + } + + /** + * @return Whether more dirs or files entries are available. + */ + function has_more() { + while ($this->cnt) { + $dir = $this->adir[$this->cnt]; + if ($dir->has_more()) { + return true; + } + unset($this->adir[$this->cnt--]); + } + return false; + } + + /** + * @return The current dir or file entry. + */ + function current() { + if ($this->cnt) { + $dir = $this->adir[$this->cnt]; + return $dir->path . $dir->current(); + } + throw new exception("No more elements available"); + } + + /** + * Hidden __clone + */ + protected function __clone() { + // disallow clone + } +} + +?> \ No newline at end of file