]> git.mxchange.org Git - friendica.git/blob - src/Util/ReversedFileReader.php
8a3083f7d8d14a31489d3cd2263d65af3a29cbce
[friendica.git] / src / Util / ReversedFileReader.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2021, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24
25 /**
26  * An iterator which returns lines from file in reversed order
27  *
28  * original code https://stackoverflow.com/a/10494801
29  */
30 class ReversedFileReader implements \Iterator
31 {
32     const BUFFER_SIZE = 4096;
33     const SEPARATOR = "\n";
34
35     public function __construct($filename)
36     {
37         $this->_fh = fopen($filename, 'r');
38                 if (!$this->_fh) {
39                         // this should use a custom exception.
40                         throw \Exception("Unable to open $filename");
41                 }
42         $this->_filesize = filesize($filename);
43         $this->_pos = -1;
44         $this->_buffer = null;
45         $this->_key = -1;
46         $this->_value = null;
47     }
48
49     public function _read($size)
50     {
51         $this->_pos -= $size;
52         fseek($this->_fh, $this->_pos);
53         return fread($this->_fh, $size);
54     }
55
56     public function _readline()
57     {
58         $buffer =& $this->_buffer;
59         while (true) {
60             if ($this->_pos == 0) {
61                 return array_pop($buffer);
62             }
63             if (count($buffer) > 1) {
64                 return array_pop($buffer);
65             }
66             $buffer = explode(self::SEPARATOR, $this->_read(self::BUFFER_SIZE) . $buffer[0]);
67         }
68     }
69
70     public function next()
71     {
72         ++$this->_key;
73         $this->_value = $this->_readline();
74     }
75
76     public function rewind()
77     {
78         if ($this->_filesize > 0) {
79             $this->_pos = $this->_filesize;
80             $this->_value = null;
81             $this->_key = -1;
82             $this->_buffer = explode(self::SEPARATOR, $this->_read($this->_filesize % self::BUFFER_SIZE ?: self::BUFFER_SIZE));
83             $this->next();
84         }
85     }
86
87     public function key()
88         {
89                 return $this->_key;
90         }
91
92     public function current()
93         {
94                 return $this->_value;
95         }
96
97     public function valid()
98         {
99                 return ! is_null($this->_value);
100         }
101 }