]> git.mxchange.org Git - friendica.git/blob - src/Util/ReversedFileReader.php
bdc31f0cba158f3b5f86796cd54ed8fb537f8d41
[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  * An iterator which returns lines from file in reversed order
26  *
27  * original code https://stackoverflow.com/a/10494801
28  */
29 class ReversedFileReader implements \Iterator
30 {
31         const BUFFER_SIZE = 4096;
32         const SEPARATOR   = "\n";
33
34         /** @var resource */
35         private $fh = null;
36
37         /** @var int */
38         private $filesize = -1;
39
40         /** @var int */
41         private $pos = -1;
42
43         /** @var array */
44         private $buffer = null;
45
46         /** @var int */
47         private $key = -1;
48
49         /** @var string */
50         private $value = null;
51
52         /**
53          * Open $filename for read and reset iterator
54          *
55          * @param string $filename      File to open
56          * @return $this
57          */
58         public function open(string $filename)
59         {
60                 $this->fh = fopen($filename, 'r');
61                 if (!$this->fh) {
62                         // this should use a custom exception.
63                         throw \Exception("Unable to open $filename");
64                 }
65                 $this->filesize = filesize($filename);
66                 $this->pos      = -1;
67                 $this->buffer   = null;
68                 $this->key      = -1;
69                 $this->value    = null;
70                 return $this;
71         }
72
73         /**
74          * Read $size bytes behind last position
75          *
76          * @return string
77          */
78         private function _read($size)
79         {
80                 $this->pos -= $size;
81                 fseek($this->fh, $this->pos);
82                 return fread($this->fh, $size);
83         }
84
85         /**
86          * Read next line from end of file
87          * Return null if no lines are left to read
88          *
89          * @return ?string
90          */
91         private function _readline()
92         {
93                 $buffer = & $this->buffer;
94                 while (true) {
95                         if ($this->pos == 0) {
96                                 return array_pop($buffer);
97                         }
98                         if (count($buffer) > 1) {
99                                 return array_pop($buffer);
100                         }
101                         $buffer = explode(self::SEPARATOR, $this->_read(self::BUFFER_SIZE) . $buffer[0]);
102                 }
103         }
104
105         /**
106          * Fetch next line from end and set it as current iterator value.
107          *
108          * @see Iterator::next()
109          * @return void
110          */
111         public function next()
112         {
113                 ++$this->key;
114                 $this->value = $this->_readline();
115         }
116
117         /**
118          * Rewind iterator to the first line at the end of file
119          *
120          * @see Iterator::rewind()
121          * @return void
122          */
123         public function rewind()
124         {
125                 if ($this->filesize > 0) {
126                         $this->pos    = $this->filesize;
127                         $this->value  = null;
128                         $this->key    = -1;
129                         $this->buffer = explode(self::SEPARATOR, $this->_read($this->filesize % self::BUFFER_SIZE ?: self::BUFFER_SIZE));
130                         $this->next();
131                 }
132         }
133
134         /**
135          * Return current line number, starting from zero at the end of file
136          *
137          * @see Iterator::key()
138          * @return int
139          */
140         public function key()
141         {
142                 return $this->key;
143         }
144
145         /**
146          * Return current line
147          *
148          * @see Iterator::current()
149          * @return string
150          */
151         public function current()
152         {
153                 return $this->value;
154         }
155
156         /**
157          * Checks if current iterator value is valid, that is, we readed all lines in files
158          *
159          * @see Iterator::valid()
160          * @return bool
161          */
162         public function valid()
163         {
164                 return ! is_null($this->value);
165         }
166 }