1
1
Fork 0
friendica_2019-12_sharedHos.../src/Util/ReversedFileReader.php

126 lines
2.7 KiB
PHP
Raw Normal View History

<?php
/**
* @copyright Copyright (C) 2021, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Util;
/**
* An iterator which returns lines from file in reversed order
*
* original code https://stackoverflow.com/a/10494801
*/
class ReversedFileReader implements \Iterator
{
2021-05-24 22:05:02 +02:00
const BUFFER_SIZE = 4096;
const SEPARATOR = "\n";
/** @var resource */
private $fh = null;
2021-05-24 22:05:02 +02:00
/** @var int */
private $filesize = -1;
2021-05-24 21:47:10 +02:00
2021-05-24 22:05:02 +02:00
/** @var int */
private $pos = -1;
2021-05-24 21:47:10 +02:00
2021-05-24 22:05:02 +02:00
/** @var array */
private $buffer = null;
2021-05-24 21:47:10 +02:00
2021-05-24 22:05:02 +02:00
/** @var int */
private $key = -1;
2021-05-24 21:47:10 +02:00
2021-05-24 22:05:02 +02:00
/** @var string */
private $value = null;
2021-05-24 21:47:10 +02:00
/**
* Open $filename for read and reset iterator
2021-08-19 15:20:25 +02:00
*
* @param string $filename File to open
* @return $this
*/
public function open(string $filename)
2021-05-24 22:05:02 +02:00
{
$this->fh = fopen($filename, 'r');
if (!$this->fh) {
// this should use a custom exception.
throw \Exception("Unable to open $filename");
}
2021-05-24 22:05:02 +02:00
$this->filesize = filesize($filename);
$this->pos = -1;
$this->buffer = null;
$this->key = -1;
$this->value = null;
return $this;
2021-05-24 22:05:02 +02:00
}
private function _read($size)
2021-05-24 22:05:02 +02:00
{
$this->pos -= $size;
fseek($this->fh, $this->pos);
return fread($this->fh, $size);
2021-05-24 22:05:02 +02:00
}
private function _readline()
2021-05-24 22:05:02 +02:00
{
$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]);
}
}
2021-05-24 22:05:02 +02:00
public function next()
{
++$this->key;
$this->value = $this->_readline();
}
2021-05-24 22:05:02 +02:00
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();
}
}
2021-05-24 22:05:02 +02:00
public function key()
{
2021-05-24 21:47:10 +02:00
return $this->key;
}
2021-05-24 22:05:02 +02:00
public function current()
{
2021-05-24 21:47:10 +02:00
return $this->value;
}
2021-05-24 22:05:02 +02:00
public function valid()
{
2021-05-24 21:47:10 +02:00
return ! is_null($this->value);
}
}