file search

neon 14.11.03 15:50

Pieni luokka tiedostosta etsimistä varten.

 Tekstiversio  Arvo: 0 (4 ääntä)  Äänestä: +  -
<?PHP
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Author:         neon <neon@neon-line.net>                            |
// +----------------------------------------------------------------------+
/**
* @class File
* @desc Tool for file searching. By default uses eregi() change if you wish.
* @author neon <neon@neon-line.net>
*/

class File
{
    /**
    * @var resource resource
    * @desc file resource
    * @access private
    */

    var $resource;
    /**
    * @var string buffer
    * @desc
    * @access private
    */

    var $buffer;
    /**
    * @var int len
    * @desc buffer len before newline, default 4096
    * @access private
    */

    var $len;
    /**
    * @var array position
    * @desc position of matches
    * @access private
    */

    var $position;
    /**
    * @var int row
    * @desc current row
    * @access public
    */

    var $row;
    /**
    * @var array limits
    * @desc array(1=>firstrow in file, 2 => lastrow in file)
    * @access private
    */

    var $limits;
    /**
    * @var array results
    * @desc
    * @access public
    */

    var $results;

    /**
    * void File( resource resource )
    * @param resource filepointer
    * @access public
    */

    function File(&$resource)
    {
        if(!is_resource($resource))
            trigger_error("Wrong data type.",512);
        $this->resource = &$resource;
        /** set defaults **/
        $this->len = 4096;
        $this->limits = array(0,false);
        @rewind($this->resource); // begining of the file
    }
    /**
    * void setBufferLen( int bufferLen )
    * @param int bufferLen
    * @access public
    */

    function setBufferLen($bufferLen)
    {
        $this->len = $bufferLen;
    }
    /**
    * void setLimits( int start, int count )
    * @param int start start row
    * @param int count max results
    * @access public
    */

    function setLimits($start,$count)
    {
        $last = $start+$count;
        $this->limits= array($start,$last);
    }
    /**
    * array search( string pattern )
    * @param string pattern a regexp pattern
    * @return array OR false if no matches
    * @access public
    */

    function search($pattern)
    {
        $this->_cleanUp();
        while(($this->buffer = fgets($this->resource,$this->len)) != '') {
            $this->row++;
            $this->position = $this->position+strlen($this->buffer);
            if($this->row < $this->limits[0])
                continue; // go forward
            if($this->row > $this->limits[1] && $this->limits[1] != false)
                break 1; // terminate loop
            if(eregi($pattern,$this->buffer) == true) {
                $this->_register();
            }
        }
        return $this->_getResults();
    }
    /**
    * void _register( void )
    * @access private
    */

    function _register()
    {
        $start = $this->position - strlen($this->buffer)-1;
        $data = substr($this->buffer,0,strlen($this->buffer)-1);
        $item = array(
                "DATA" => $data,
                "ROW"  => $this->row,
                "START" => $start,
                "END"  => $this->position);
        array_push($this->results,$item);
    }
    /*
    * array _getResults( void )
    * @access private
    */

    function _getResults()
    {
        if(count($this->results) > 0)
            return $this->results;
        else
            return false;
    }
    /**
    * void _cleanUp( void )
    * @access private
    */

    function _cleanUp()
    {
        $this->buffer = null;
        $this->results = array();
        $this->row = 0;
        $this->position = 0;
    }
}
?>