X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FReversedFileReader.php;h=fbd32d51fd4886f25cc165dc65c6ee1d539c55d1;hb=739ca603bc388c97a11a8d6a4843396bf5d52088;hp=eeedc1a5ccba435fe6ecac8f6055677924eb9101;hpb=5b9aeeeca95165203ed2753ba44ce6d8fbdea321;p=friendica.git diff --git a/src/Util/ReversedFileReader.php b/src/Util/ReversedFileReader.php index eeedc1a5cc..fbd32d51fd 100644 --- a/src/Util/ReversedFileReader.php +++ b/src/Util/ReversedFileReader.php @@ -1,6 +1,6 @@ _fh = fopen($filename, 'r'); - if (!$this->_fh) { + /** + * Open $filename for read and reset iterator + * + * @param string $filename File to open + * @return $this + */ + public function open(string $filename): ReversedFileReader + { + $this->fh = fopen($filename, 'r'); + if (!$this->fh) { // this should use a custom exception. - throw \Exception("Unable to open $filename"); + throw new \Exception("Unable to open $filename"); + } + $this->filesize = filesize($filename); + $this->pos = -1; + $this->buffer = null; + $this->key = -1; + $this->value = null; + return $this; + } + + /** + * Read $size bytes behind last position + * + * @param int $size + * @return string + */ + private function _read(int $size): string + { + $this->pos -= $size; + fseek($this->fh, $this->pos); + return fread($this->fh, $size); + } + + /** + * Read next line from end of file + * Return null if no lines are left to read + * + * @return string|null Depending on data being buffered + */ + private function _readline() + { + $buffer = & $this->buffer; + while (true) { + if ($this->pos == 0) { + return array_pop($buffer); + } + if (is_null($buffer)) { + return null; + } + if (count($buffer) > 1) { + return array_pop($buffer); + } + $buffer = explode(self::SEPARATOR, $this->_read(self::BUFFER_SIZE) . $buffer[0]); } - $this->filesize = filesize($filename); - $this->pos = -1; - $this->buffer = null; - $this->key = -1; - $this->value = null; - } - - public function _read($size) - { - $this->pos -= $size; - fseek($this->_fh, $this->pos); - return fread($this->_fh, $size); - } - - public function _readline() - { - $buffer =& $this->buffer; - while (true) { - if ($this->pos == 0) { - return array_pop($buffer); - } - if (count($buffer) > 1) { - return array_pop($buffer); - } - $buffer = explode(self::SEPARATOR, $this->_read(self::BUFFER_SIZE) . $buffer[0]); - } - } - - public function next() - { - ++$this->key; - $this->value = $this->_readline(); - } - - public function rewind() - { - if ($this->filesize > 0) { - $this->pos = $this->filesize; - $this->value = null; - $this->key = -1; - $this->buffer = explode(self::SEPARATOR, $this->_read($this->filesize % self::BUFFER_SIZE ?: self::BUFFER_SIZE)); - $this->next(); - } - } - - public function key() + } + + /** + * Fetch next line from end and set it as current iterator value. + * + * @see Iterator::next() + * @return void + */ + public function next() + { + ++$this->key; + $this->value = $this->_readline(); + } + + /** + * Rewind iterator to the first line at the end of file + * + * @see Iterator::rewind() + * @return void + */ + public function rewind() + { + if ($this->filesize > 0) { + $this->pos = $this->filesize; + $this->value = null; + $this->key = -1; + $this->buffer = explode(self::SEPARATOR, $this->_read($this->filesize % self::BUFFER_SIZE ?: self::BUFFER_SIZE)); + $this->next(); + } + } + + /** + * Return current line number, starting from zero at the end of file + * + * @see Iterator::key() + * @return int + */ + public function key(): int { return $this->key; } - public function current() + /** + * Return current line + * + * @see Iterator::current() + * @return string + */ + public function current(): string { return $this->value; } - public function valid() + /** + * Checks if current iterator value is valid, that is, we readed all lines in files + * + * @see Iterator::valid() + * @return bool + */ + public function valid(): bool { return ! is_null($this->value); }