-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTailGrep.php
More file actions
354 lines (300 loc) · 8.01 KB
/
TailGrep.php
File metadata and controls
354 lines (300 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php // vim:ts=3:sts=3:sw=3:et:
/**
* TailGrep Utility
*
* PHP Version 5
*
* @category PHP
* @package MindFrame2
* @author Bryan C. Geraghty <bryan@ravensight.org>
* @copyright 2005-2011 Bryan C. Geraghty
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL
* @link https://github.com/archwisp/MindFrame2
*/
/**
* TailGrep Utility
* This class grabs the specified number of lines from the target
* file and caches them. Multiple searches can be performced
* against the cached lines
*
* @category PHP
* @package MindFrame2
* @author Bryan C. Geraghty <bryan@ravensight.org>
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL
* @link https://github.com/archwisp/MindFrame2
*/
class MindFrame2_TailGrep
{
/**
* @var resource
*/
private $_file_pointer;
/**
* @var string
*/
private $_file_name;
/**
* @var int
*/
private $_line_count;
/**
* @var string
*/
private $_buffer;
/**
* Construct
*
* @param string $file_name The target file to be searched
* @param int $line_count Number of lines from the end of the file to
* be searched
*/
public function __construct($file_name, $line_count)
{
if (!is_int($line_count))
{
throw new InvalidArgumentException('Expected integer value for argument #2 (line_count)');
}
$this->_file_name = $file_name;
$this->_line_count = $line_count;
}
/**
* Help out the garbage collector
*/
public function __destruct()
{
$this->closeFileIfOpen();
unset($this->_file_pointer, $this->_file_name, $this->_lines);
}
/**
* Performs the pattern matching
*
* @param string $expression The string to look for
*
* @return string
*/
public function grep($expression)
{
$this->fetchLinesOnce();
$array = explode("\n", $this->_buffer);
$matches = preg_grep('/' . $expression . '/i', $array);
$results = join("\n", $matches);
return $results;
}
/**
* Explodes the entire buffer into an array
*
* @return array
*/
public function fetchAll()
{
$this->fetchLinesOnce();
return explode("\n", $this->_buffer);
}
/**
* Fetches the specified number of lines from the specified file into the
* internal buffer
*
* @return bool
*/
public function fetchLines()
{
$this->openFileOnce();
$read_length = 8;
$buffer = NULL;
$line_count = 0;
fseek($this->_file_pointer, 0, SEEK_END);
while (($line_count <= ($this->_line_count)) && ($read_length > 0))
{
if ($this->readChunk($read_length, $buffer) === FALSE)
{
throw new RuntimeException('Failed to read chunk');
}
$line_count = $this->countLines($buffer);
$read_length = $this->adjustReadLength($line_count, $read_length, strlen($buffer));
}
// end while // ($line_count <= ($this->line_count))... //
$this->trimBuffer($buffer);
$this->_buffer = $buffer;
return TRUE;
}
/**
* Returns the size of the target file in bytes
*
* @return int
*/
protected function fetchFileSize()
{
return filesize($this->_file_name);
}
/**
* Checks to see if the internal buffer has been populated before fetching the
* contents from the file
*
* @return bool
*/
protected function fetchLinesOnce()
{
if ($this->_buffer === NULL)
{
$this->fetchLines();
return TRUE;
}
return FALSE;
}
/**
* Seek to the point where we want to start reading, read, then seek back
* to the point where we started reading
*
* @param int $read_length How many bytes to read
* @param string &$buffer The buffer to place the read contents into
*
* @return bool
*/
protected function readChunk($read_length, &$buffer)
{
if (!is_int($read_length))
{
throw new InvalidArgumentException('Expected integer value for argument #1 (read_length)');
}
fseek($this->_file_pointer, -$read_length, SEEK_CUR);
$read = fread($this->_file_pointer, $read_length);
fseek($this->_file_pointer, -$read_length, SEEK_CUR);
if ($read === FALSE)
{
return FALSE;
}
$buffer = $read . $buffer;
return TRUE;
}
/**
* If we haven't reached the half-way point, double the read length to
* speed things up. If the adjustment ends up being being begger than the file,
* re-adjust
*
* @param int $line_count The current number of lines in the buffer
* @param int $read_length The current number of bytes to read in each chunk
* @param int $buffer_size The current size of the buffer in bytes
*
* @return int
*/
protected function adjustReadLength($line_count, $read_length, $buffer_size)
{
if (!is_int($line_count))
{
throw new InvalidArgumentException('Expected integer value for argument #1 (line_count)');
}
if (!is_int($read_length))
{
throw new InvalidArgumentException('Expected integer value for argument #2 (read_length)');
}
if (!is_int($buffer_size))
{
throw new InvalidArgumentException('Expected integer value for argument #3 (buffer_size)');
}
$new_read_length = $read_length;
$file_size = $this->fetchFileSize();
if ($line_count <= ($this->_line_count / 2))
{
$new_read_length = ($read_length * 2);
}
if (($buffer_size + $new_read_length) > $file_size)
{
$new_read_length = ($file_size - $buffer_size);
}
return $new_read_length;
}
/**
* Removes any excess lines from the buffer because reading from the end of a
* file with inconsistent line lengths is not an exact process.
*
* @param string &$buffer The buffer containing the file contents
*
* @return bool
*/
protected function trimBuffer(&$buffer)
{
$diff = ($this->countLines($buffer) - $this->_line_count);
$offset = 0;
if ($diff > 0)
{
for ($x = 0; $x < $diff; $x++)
{
$offset = strpos($buffer, "\n", $offset) + 1;
}
// end for // ($x = 0; $x < $diff; $x++) //
$buffer = substr($buffer, $offset);
return TRUE;
}
// end if // ($diff > 0) //
return FALSE;
}
/**
* Counts the number of newline characters in the specified string. This function
* could be improved to provide cross-platform functionality.
*
* @param string $buffer The buffer containing the file contents
*
* @return int
*/
protected function countLines($buffer)
{
$count = 0;
$offset = 0;
while (($offset = strpos($buffer, "\n", $offset + 1)) !== FALSE)
{
$count++;
}
return $count;
}
/**
* Checks to see if the file pointer has already been opened before making the
* call to open the file.
*
* @return bool
*/
protected function openFileOnce()
{
if ($this->_file_pointer === NULL)
{
$this->openFile();
return TRUE;
}
return FALSE;
}
/**
* Opens the internal file pointer
*
* @return bool
*/
protected function openFile()
{
if (!file_exists($this->_file_name))
{
throw new RunTimeException('File could not be found');
}
if (!is_readable($this->_file_name))
{
throw new RunTimeException('Permission denied when opening file for reading');
}
$file_pointer = fopen($this->_file_name, 'r');
if ($file_pointer === FALSE)
{
throw new RunTimeException('Could not open file');
}
$this->_file_pointer = $file_pointer;
return TRUE;
}
/**
* Closes the internal file pointer
*
* @return bool
*/
protected function closeFileIfOpen()
{
if ($this->_file_pointer !== NULL)
{
return fclose($this->_file_pointer);
}
return FALSE;
}
}