cvs: php4 /pear/File Find.php

From: Date: Fri, 21 Jul 2000 06:44:20 +0000
Subject: cvs: php4 /pear/File Find.php
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-830@lists.php.net to get a copy of this message
sterling Thu Jul 20 23:44:20 2000 EDT Modified files: /php4/pear/File Find.php Log: Beautify, simplify, and make use of PEAR_ERROR. Index: php4/pear/File/Find.php diff -u php4/pear/File/Find.php:1.3 php4/pear/File/Find.php:1.4 --- php4/pear/File/Find.php:1.3 Sun Mar 5 19:13:34 2000 +++ php4/pear/File/Find.php Thu Jul 20 23:44:20 2000 @@ -13,175 +13,173 @@ // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ -// | Authors: Sterling Hughes <sterling@designmultimedia.com> | -// | | +// | Authors: Sterling Hughes <sterling@php.net> | // +----------------------------------------------------------------------+ // -// $Id: +// $Id: Find.php,v 1.4 2000/07/21 06:44:20 sterling Exp $ // // Commonly needed functions searching directory trees // +include_once 'PEAR_ERROR.php'; -define("N_PATH", "Invalid Path"); -define("N_OPEN", "Cannot Open File"); -define("N_WRITE", "Cannot Write to file"); -define("N_CLOSE", "Cannot Close file or directory"); - -class File_Find { - -var $errmess; // Error message is stored here -var $err; // if there was an error this is set to 1, otherwise it is set to 0 - -/* - * array glob ( string pattern, string directory_path[, string pattern_type]) -- will search the current directory to find matches - * for the specified pattern. It only searches the current directory and directory names are included in the search. If you are looking to - * search a directory tree then see the search method. - */ - -function glob ( $pattern, $dirpath, $pattern_type="PHP" ) { - $dh = @opendir($dirpath) or File_Find::_RaiseException(N_PATH); - if ( strtolower($pattern_type) == "perl" ) { - while ($entry = readdir($dh)) { - if ( preg_match($pattern,$entry) && $entry != '.' && $entry != '..' ) { - $matches[] = $entry; - } - } - } else { - while ($entry = readdir($dh)) { - if ( strtolower(substr($pattern, -2)) == "/i") { - if (eregi(substr($pattern,0,-2), $entry)) { - $matches[] = $entry; - } - } else { - if (ereg($pattern, $entry)) { - $matches[] = $entry; - } - } - } - } - @closedir($dh); - return $matches; -} - -var $_dirs; -var $files = array(); -var $directories = array(); - -/* - * array maptree ( string directory_path ) -- Will return two arrays the first a listing of all the sub directories under the - * specified directory and the second listing all the files under the specified directory. - */ -function maptree ( $directory ) { - $this->directories = array(); - $this->_dirs = array($directory); - - while (count($this->_dirs)) { - $t = array_pop($this->_dirs); - File_Find::_build( $t ); - array_push($this->directories, $t); - } - return array($this->directories, $this->files); -} - - -/* internal function to build singular directory trees */ - -function _build ( $directory ) { - $dh = @opendir($directory) or File_Find::_RaiseException(N_PATH); - while ( $entry = readdir($dh) ) { - if ($entry != '.' && $entry != '..') { - $entry = $directory . $entry; - if ( is_dir($entry) ) { - $ent_name = $entry . '/'; - array_push($this->_dirs, $ent_name); - } else { - array_push($this->files, $entry); - } - } - } - @closedir($dh); -} - -/* - * array search ( string pattern, string directory [, string regex_type] ) -- Will search an entire directory tree for a pattern specified - * by the 'pattern' argument, in a directory specified by the 'directory' argument, of a pattern type specified by 'regex_type'. If - * regex_type is left out than PHP type regular expressions will be used. If you want to specify the use of eregi instead of ereg attach a - * '/i' onto the very end of your regular expression. - */ - -function search ( $pattern, $directory, $type="PHP" ) { - File_Find::maptree($directory); - if ( strtolower($type) == "perl" ) { - while (list(,$entry) = each($this->files)) { - if ( preg_match($pattern,$entry) && $entry != '.' && $entry != '..' ) { - $matches[] = $entry; - } - } - } else { - while (list(,$entry) = each($this->files)) { - if ( strtolower(substr($pattern, -2)) == "/i") { - if (eregi(substr($pattern,0,-2), $entry)) { - $matches[] = $entry; - } - } else { - if (ereg($pattern, $entry)) { - $matches[] = $entry; - } - } - } - } - return $matches; - -} -function _RaiseException ( $error_message ) { - $this->errmess = $error_message; - $this->err = 1; -} +class File_Find +{ + var $_dirs = array (); + var $files = array (); + var $directories = array (); + + /* + * array glob ( string pattern, string directory_path[, string pattern_type]) + * will search the current directory to find matches for the specified pattern. + * It only searches the current directory and directory names are included in the + * search. If you are looking to search a directory tree then see the search + * method. + */ + function &glob ($pattern, $dirpath, $pattern_type='php') + { + $dh = @opendir ($dirpath) + + if (!$dh) { + $pe = new ExceptionFileFind ("Cannot open directory"); + return ($pe); + } + + $match_function = File_Find::_determineRegex ($pattern_type); + + while ($entry = @readdir ($dh)) + { + if ($match_function ($pattern, $entry) && + $entry != '.' && + $entry != '..') { + $matches[] = &$entry; + } + } + + @closedir ($dh); + return ($matches); + } + + + /* + * array maptree ( string directory_path ) + * Will return two arrays the first a listing of all the sub directories under + * the specified directory and the second listing all the files under the + * specified directory. + */ + function maptree ($directory) + { + $this->_dirs = array ($directory); + + while (count ($this->_dirs)) + { + $dir = array_pop ($this->_dirs); + File_Find::_build ($dir); + array_push ($this->directories, $dir); + } + + return array (&$this->directories, &$this->files); + } + + /* + * array search ( string pattern, string directory [, string regex_type] ) + * Will search an entire directory tree for a pattern specified by the 'pattern' + * argument, in a directory specified by the 'directory' argument, of a pattern + * type specified by 'regex_type'. If regex_type is left out than PHP type regular + * expressions will be used. If you want to specify the use of eregi instead of + * ereg attach a '/i' onto the very end of your regular expression. + */ + function &search ($pattern, $directory, $type='php') { + list (,$files) = File_Find::maptree ($directory); + $match_function = File_Find::_determineRegex ($type); + + reset ($files); + while (list (,$entry) = each ($files)) + { + if ($match_function ($pattern, $entry)) + $matches[] = $entry; + } + + return ($matches); + } + + /* + * bool isError ($var) + * Determine whether or not a variable is a PEAR exception + */ + function isError ($var) + { + return (is_object ($var) && + get_class ($var) == 'ExceptionFileFind'); + } + + /* + * double File_Find_version(void) + * Returns the current File_Find version. + */ + function File_Find_version() + { + return (1.1); + } + + /* + * internal function to build singular directory trees, used by + * File_Find::maptree() + */ + function _build ($directory) + { + $dh = @opendir ($directory); + + if (!$dh) { + $pe = new ExceptionFileFind ("Cannot open directory"); + return ($pe); + } + + while ($entry = @readdir ($dh)) + { + if ($entry != '.' && + $entry != '..') { + $entry = $directory . $entry; + + if (is_dir ($entry)) + array_push ($this->_dirs, "${entry}/"); + else + array_push ($this->files, $entry); + } + } + + @closedir ($dh); + } + + /* + * internal function to determine the type of regular expression to + * use, implemented by File_Find::glob() and File_Find::search() + */ + function _determineRegex ($type) + { + if (strtolower ($pattern_type) == 'perl') { + $match_function = 'preg_match'; + } else if (strtolower (substr ($pattern, -2)) == '/i') { + $match_function = 'eregi'; + } else { + $match_function = 'ereg'; + } + + return ($match_function); + } -/* - * double File_Find_version(void) -- Returns the current version of File_Find - */ -function File_Find_version() { - return 1.0; -} - //End Class } -?> - -<?php -/* EXAMPLES FOR THIS CLASS - -Mapping an entire directory structure: - -require('File/Find.php'); -$searcher = new File_Find; -list ($directories,$files) = $searcher->maptree('/pear'); -print implode("\n<br>\n", $directories); - -(Yes That's it) - -Searching an entire directory structure: -require('File/Find.php'); -$fs = new File_Find; -$files = $fs->search('/\.c$/','/cvs/php4', 'perl'); -print implode("\n<br>\n", $files); - -Searching a singular directory: - -require('File/Find.php'); -$fs = new File_Find; -$files = $fs->glob('/.*/', '/cvs/', 'perl'); -print implode("\n<br>\n", $files); - -Usages: -Filecrawler (I built a complete crawler in 20 lines) -Removing a directory tree. -... - -END EXAMPLES +class ExceptionFileFind extends PEAR_ERROR +{ + var $CLASSNAME = 'ExceptionFileFind'; + var $ERROR_MESSAGE_PREPEND = 'Error in ExceptionFileFind'; + + function ExceptionFileFind ($message, $file = __FILE__, $line = __LINE__) + { + parent::PEAR_ERROR ($message, $file, $line); + } +} -*/ ?>

« previous php.cvs (#830) next »