<commit_before><?php
/*****************************************************************************

File/Stream I/O Abstraction

This started as a way to more easily add functionality to reading highly-
specified parts of files.  However, it quickly became easy to just wrap a
few of the normal file I/O functions, and provide a solid file handling object.

*****************************************************************************/

/*----------------------------------------------------------------------------
Default Namespace
----------------------------------------------------------------------------*/

namespace hzphp\IO;

/*----------------------------------------------------------------------------
Dependencies
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
Classes
----------------------------------------------------------------------------*/


/**
 * Abstracts file/stream I/O.
 */
class StreamIO {

    /*------------------------------------------------------------------------
    Public Properties
    ------------------------------------------------------------------------*/

    /*------------------------------------------------------------------------
    Protected Properties
    ------------------------------------------------------------------------*/

    //file stream handle
    protected $stream = null;


    /*------------------------------------------------------------------------
    Private Properties
    ------------------------------------------------------------------------*/

    /*------------------------------------------------------------------------
    Public Methods
    ------------------------------------------------------------------------*/


    /**
     * Constructor.
     *
     * @param file The open file stream from which we will extract data.
     */
    public function __construct( $stream ) {
        $this->stream = $stream;
    }


    /**
     * Extracts a part of the current file.
     *
     * @param offset The offset from the current position in the file
     * @param length The number of bytes to extract
     *               If null or not given, extract the remainder of the
     *               stream contents.
     * @param reset  Whether or not to reset the file position
     * @return       The extracted data as a string, or false on failure
     */
    public function extract( $offset = 0, $length = null, $reset = false ) {

        //capture the current position in the stream
        $position = $this->tell();

        //see if we need to change the file position
        if( $offset != 0 ) {
            $this->seek( $offset, SEEK_CUR );<commit_msg>Adding features to StreamIO class.<commit_after><?php
/*****************************************************************************

File/Stream I/O Abstraction

This started as a way to more easily add functionality to reading highly-
specified parts of files.  However, it quickly became easy to just wrap a
few of the normal file I/O functions, and provide a solid file handling object.

*****************************************************************************/

/*----------------------------------------------------------------------------
Default Namespace
----------------------------------------------------------------------------*/

namespace hzphp\IO;

/*----------------------------------------------------------------------------
Dependencies
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
Classes
----------------------------------------------------------------------------*/


/**
 * Abstracts file/stream I/O.
 */
class StreamIO {

    /*------------------------------------------------------------------------
    Public Properties
    ------------------------------------------------------------------------*/

    /*------------------------------------------------------------------------
    Protected Properties
    ------------------------------------------------------------------------*/

    //user file's name
    protected $filename = null;

    //file stream handle
    protected $stream = null;


    /*------------------------------------------------------------------------
    Private Properties
    ------------------------------------------------------------------------*/

    /*------------------------------------------------------------------------
    Public Methods
    ------------------------------------------------------------------------*/


    /**
     * StreamIO instance constructor.
     *
     * @param stream The open file stream from which we will extract data.
     *               This can also be a file name (string) that will be opened
     *               during construction, and closed when the object is
     *               garbage collected.
     * @param mode   If passing a file name, specify the mode in which the
     *               file will be opened (default: 'r')
     * @throws       RuntimeException if the file (specified by name) can not
     *               be opened
     */
    public function __construct( $stream, $mode = 'r' ) {

        //detect file name instead of resource
        if( is_string( $stream ) ) {
            $this->filename = $stream;
            $this->stream   = fopen( $stream, $mode );
            if( $this->stream === false ) {
                throw \RuntimeException( "Unable to open file $stream." );
            }
        }

        //detect normal resource
        else if( is_resource( $stream ) ) {
            $this->stream = $stream;
        }
    }

    /**
     * StreamIO instance desctructor.
     *
     */
    public function __destruct() {

        //see if we were given a file name instead of a stream resource
        if( $this->filename !== null ) {

            //close the file
            $this->close();
        }
    }



    /**
     * Closes the stream resource.
     *
     */
    public function close() {

        //avoid re-closing files that were already closed
        if( $this->stream !== null ) {

            //close the file, and clear the stream property
            fclose( $this->stream );
            $this->stream = null;
        }
    }


    /**
     * Extracts a part of the current file.
     *
     * @param offset The offset from the current position in the file
     * @param length The number of bytes to extract
     *               If null or not given, extract the remainder of the
     *               stream contents.
     * @param reset  Whether or not to reset the file position
     * @return       The extracted data as a string, or false on failure
     */
    public function extract( $offset = 0, $length = null, $reset = false ) {

        //capture the current position in the stream
        $position = $this->tell();

        //see if we need to change the file position
        if( $offset != 0 ) {
            $this->seek( $offset, SEEK_CUR );
{'fraction_non_alphanumeric': 0.3957716701902748, 'fraction_numerical': 0.0005637773079633545, 'mean_word_length': 0.8857294711666224, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2171991', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1272, 'n_words': 514}