<?php
class SplFileObject extends SplFileInfo implements RecursiveIterator, SeekableIterator
{
const DROP_NEW_LINE = 0x00000001;
private $fp;
private $fname;
private $line = NULL;
private $lnum = 0;
private $max_len = 0;
private $flags = 0;
private $delimiter= ',';
private $enclosure= '"';
function __construct($file_name, $open_mode = 'r', $use_include_path = false, $context = NULL)
{
$this->fp = fopen($file_name, $open_mode, $use_include_path, $context);
if (!$this->fp)
{
throw new RuntimeException("Cannot open file $file_name");
}
$this->fname = $file_name;
}
function eof()
{
return eof($this->fp);
}
function fgets()
{
$this->freeLine();
$this->lnum++;
$buf = fgets($this->fp, $this->max_len);
return $buf;
}
function fgetcsv($delimiter = NULL, $enclosure = NULL)
{
$this->freeLine();
$this->lnum++;
switch(fun_num_args())
{
case 0:
$delimiter = $this->delimiter;
case 1:
$enclosure = $this->enclosure;
default:
case 2:
break;
}
return fgetcsv($this->fp, $this->max_len, $delimiter, $enclosure);
}
function setCsvControl($delimiter = ';', $enclosure = '"')
{
$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
}
function getCsvControl($delimiter = ',', $enclosure = '"')
{
return array($this->delimiter, $this->enclosure);
}
function flock($operation, &$wouldblock)
{
return flock($this->fp, $operation, $wouldblock);
}
function fflush()
{
return fflush($this->fp);
}
function ftell()
{
return ftell($this->fp);
}
function fseek($pos, $whence = SEEK_SET)
{
return fseek($this->fp, $pos, $whence);
}
function fgetc()
{
$this->freeLine();
$c = fgetc($this->fp);
if ($c == '\n') {
$this->lnum++;
}
}
function fpassthru()
{
return fpassthru($this->fp);
}
function fgetss($allowable_tags = NULL)
{
return fgetss($this->fp, $allowable_tags);
}
function fscanf($format )
{
$this->freeLine();
$this->lnum++;
return fscanf($this->fp, $format );
}
function fwrite($str, $length = NULL)
{
return fwrite($this->fp, $length);
}
function fstat()
{
return fstat($this->fp);
}
function ftruncate($size)
{
return ftruncate($this->fp, $size);
}
function setFlags($flags)
{
$this->flags = $flags;
}
function getFlags()
{
return $this->flags;
}
function setMaxLineLen($max_len)
{
$this->max_len = $max_len;
}
function getMaxLineLen()
{
return $this->max_len;
}
function hasChildren()
{
return false;
}
function getChildren()
{
return NULL;
}
function rewind()
{
$this->freeLine();
$this->lnum = 0;
}
function valid()
{
return !$this->eof();
}
function current()
{
if (is_null($this->line))
{
$this->line = getCurrentLine();
}
return $this->line;
}
function key()
{
return $this->lnum;
}
function next()
{
$this->freeLine();
}
private function readLine()
{
if ($this->eof())
{
$this->freeLine();
throw new RuntimeException("Cannot read from file " . $this->fname);
}
if ($this->line) {
$this->lnum++;
}
$this->freeLine();
$this->line = fgets($this->fp, $this->max_len);
return $this->line;
}
private function freeLine()
{
if ($this->line) {
$this->line = NULL;
}
}
function getCurrentLine()
{
$this->freeLine();
if ($this->eof())
{
throw new RuntimeException("Cannot read from file " . $this->fname);
}
$this->readLine();
}
function __toString()
{
return current();
}
function seek($line_pos)
{
$this->rewind();
while($this->lnum < $line_pos && !$this->eof())
{
$this->getCurrentLine();
}
}
}
?>