<commit_before><?php
/*
 * Copyright 2015 <NAME> <http://jacksleight.com/>
 * This source file is subject to the MIT license that is bundled with this package in the file LICENCE. 
 */

namespace Coast;

class File extends \Coast\File\Path
{
    protected $_chars = [',', '"', '\\'];
    protected $_handle;

    public static function createTemp()
    {
        $path = str_replace(DIRECTORY_SEPARATOR, '/', tempnam(sys_get_temp_dir(), 'php'));
        if (!$path) {
            throw new \Exception('Could not create tempoary file');
        }
        return new \Coast\File($path);
    }

    public function chars($delimiter = ',', $enclosure = '"', $escape = '\\')
    {
        $this->_chars = [$delimiter, $enclosure, $escape];
        return $this;
    }
    
    public function open($mode = 'r')
    {
        $this->_handle = fopen($this->_name, $mode);
        return $this;
    }

    public function close()
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        fclose($this->_handle);
        $this->_handle = null;
        return $this;
    }

    public function read($length = null)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        $size = $this->size();
        return isset($length)
            ? fread($this->_handle, $length)
            : fread($this->_handle, $size ? $size : 1);
    }

    public function write($string, $length = null)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        isset($length)
            ? fwrite($this->_handle, $string, $length)
            : fwrite($this->_handle, $string);
        return $this;
    }

    public function get($length = null)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        return isset($length)
            ? fgets($this->_handle, $length)
            : fgets($this->_handle);
    }

    public function put($string, $length = null)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        $this->write($string, $length) . $this->write("\n");
        return $this;
    }

    public function getCsv($length = 0)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        return fgetcsv($this->_handle, $length, $this->_chars[0], $this->_chars[1], $this->_chars[2]);
    }

    public function putCsv($array)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        fputcsv($this->_handle, $array, $this->_chars[0], $this->_chars[1]);
        return $this;
    }

    public function seek($offset, $whence = SEEK_SET)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        fseek($this->_handle, $offset, $whence);
        return $this;
    }

    public function truncate($length = 0)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        ftruncate($this->_handle, $length);
        return $this;
    }

    public function moveUpload(\Coast\Dir $dir, $baseName = null)
    {
        $name = "{$dir}/" . (isset($baseName)
            ? $this->_parseBaseName($baseName)
            : $this->baseName());<commit_msg>In memory file support.<commit_after><?php
/*
 * Copyright 2015 <NAME> <http://jacksleight.com/>
 * This source file is subject to the MIT license that is bundled with this package in the file LICENCE. 
 */

namespace Coast;

class File extends \Coast\File\Path
{
    protected $_chars = [',', '"', '\\'];
    protected $_handle;

    public static function createTemp()
    {
        $path = str_replace(DIRECTORY_SEPARATOR, '/', tempnam(sys_get_temp_dir(), 'php'));
        if (!$path) {
            throw new \Exception('Could not create tempoary file');
        }
        return new \Coast\File($path);
    }

    public static function createMemory($max = null)
    {
        return new \Coast\File("php://temp" . (isset($max) ? "/maxmemory:{$max}" : null));
    }

    public function chars($delimiter = ',', $enclosure = '"', $escape = '\\')
    {
        $this->_chars = [$delimiter, $enclosure, $escape];
        return $this;
    }
    
    public function open($mode = 'r')
    {
        $this->_handle = fopen($this->_name, $mode);
        return $this;
    }

    public function close()
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        fclose($this->_handle);
        $this->_handle = null;
        return $this;
    }

    public function read($length = null)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        if (isset($length)) {
            return fread($this->_handle, $length);
        } else {
            if (strpos($this->_name, 'php://') === 0) {
                return stream_get_contents($this->_handle);
            } else {
                $size = $this->size();
                return fread($this->_handle, $size ? $size : 1);
            }
        }
    }

    public function write($string, $length = null)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        isset($length)
            ? fwrite($this->_handle, $string, $length)
            : fwrite($this->_handle, $string);
        return $this;
    }

    public function get($length = null)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        return isset($length)
            ? fgets($this->_handle, $length)
            : fgets($this->_handle);
    }

    public function put($string, $length = null)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        $this->write($string, $length) . $this->write("\n");
        return $this;
    }

    public function getCsv($length = 0)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        return fgetcsv($this->_handle, $length, $this->_chars[0], $this->_chars[1], $this->_chars[2]);
    }

    public function putCsv($array)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        fputcsv($this->_handle, $array, $this->_chars[0], $this->_chars[1]);
        return $this;
    }

    public function seek($offset, $whence = SEEK_SET)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        fseek($this->_handle, $offset, $whence);
        return $this;
    }

    public function rewind()
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        rewind($this->_handle);
        return $this;
    }

    public function truncate($length = 0)
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        ftruncate($this->_handle, $length);
        return $this;
    }

    public function tell()
    {
        if (!isset($this->_handle)) {
            throw new \Exception("File '{$this}' is not open");
        }
        return ftell($this->_handle);
    }

    public function moveUpload(\Coast\Dir $dir, $baseName = null)
    {
        $name = "{$dir}/" . (isset($baseName)
            ? $this->_parseBaseName($baseName)
            : $this->baseName());
{'fraction_non_alphanumeric': 0.16951530612244897, 'fraction_numerical': 0.0031887755102040817, 'mean_word_length': 1.989325200152497, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 81, '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': '1589348', 'n_tokens_mistral': 2342, 'n_tokens_neox': 2293, 'n_words': 566}