* @package Mammut\IO */ class Textfile extends File implements iLineReader { const _VERSION_ = '0.5.0.2'; /** * create a new file object * * @param string $path * the path of the object */ public function __construct($path, $ro = false) { parent::__construct($path, $ro); } /** * Skips the BOM on the beginning of a text file. * * Does nothing if current position is not 0 */ protected function skipBOM() { if(ftell($this->fp) != 0) return; $test = fread($this->fp, 4); $target = 0; // UTF8 BOM if(substr($test, 0, 3) == "\xEF\xBB\xBF") $target = 3; // UTF16 BOM elseif(substr($test, 0, 2) == "\xFE\xFF") $target = 2; elseif(substr($test, 0, 2) == "\xFF\xFE") $target = 2; fseek($this->fp, $target); } /** * (non-PHPdoc) * @see \Mammut\IO\iLineReader::readLine() */ public function readLine($trim = TRUE) { if(!$this->isOpen()) throw new IllegalStateException("file not opened"); if($this->isEOF()) return false; $this->skipBOM(); $data = fgets($this->fp); if ($trim) $data = rtrim($data,"\r\n"); return $data; } /** * Writes a line in the file, followed by a newline character * * @param string $data * the string to write * @throws IllegalStateException if the file is not open for writing */ public function writeLine($data) { if(!$this->isOpen()) throw new IllegalStateException("file not opened"); fwrite($this->fp, $data . PHP_EOL); } }