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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
<?php
/** @file splfileobject.inc
* @ingroup SPL
* @brief class FileObject
* @author Marcus Boerger
* @date 2003 - 2009
*
* SPL - Standard PHP Library
*/
/** @ingroup SPL
* @brief Object representation for any stream
* @author Marcus Boerger
* @version 1.1
* @since PHP 5.1
*/
class SplFileObject extends SplFileInfo implements RecursiveIterator, SeekableIterator
{
/** Flag: wheter to suppress new lines */
const DROP_NEW_LINE = 0x00000001;
private $fp;
private $fname;
private $line = NULL;
private $lnum = 0;
private $max_len = 0;
private $flags = 0;
private $delimiter= ',';
private $enclosure= '"';
/**
* Constructs a new file object
*
* @param $file_name The name of the stream to open
* @param $open_mode The file open mode
* @param $use_include_path Whether to search in include paths
* @param $context A stream context
* @throw RuntimeException If file cannot be opened (e.g. insufficient
* access rights).
*/
function __construct($file_name, $open_mode = 'r', $use_include_path = false, $context = NULL)
{
$this->fp = fopen($file_name, $open_mode, $use_include_path, $context);
if (!$this->fp)
{
throw new RuntimeException("Cannot open file $file_name");
}
$this->fname = $file_name;
}
/**
* @return whether the end of the stream is reached
*/
function eof()
{
return feof($this->fp);
}
/** increase current line number
* @return next line from stream
*/
function fgets()
{
$this->freeLine();
$this->lnum++;
$buf = fgets($this->fp, $this->max_len);
return $buf;
}
/**
* @param delimiter character used as field separator
* @param enclosure end of
* @return array containing read data
*/
function fgetcsv($delimiter = NULL, $enclosure = NULL)
{
$this->freeLine();
$this->lnum++;
switch(fun_num_args())
{
case 0:
$delimiter = $this->delimiter;
case 1:
$enclosure = $this->enclosure;
default:
case 2:
break;
}
return fgetcsv($this->fp, $this->max_len, $delimiter, $enclosure);
}
/**
* Set the delimiter and enclosure character used in fgetcsv
*
* @param delimiter new delimiter, defaults to ','
* @param enclosure new enclosure, defaults to '"'
*/
function setCsvControl($delimiter = ';', $enclosure = '"')
{
$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
}
/**
* @return array(delimiter, enclosure) as used in fgetcsv
*/
function getCsvControl($delimiter = ',', $enclosure = '"')
{
return array($this->delimiter, $this->enclosure);
}
/**
* @param operation lock operation (LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB)
* @retval $wouldblock whether the operation would block
*/
function flock($operation, &$wouldblock)
{
return flock($this->fp, $operation, $wouldblock);
}
/**
* Flush current data
* @return success or failure
*/
function fflush()
{
return fflush($this->fp);
}
/**
* @return current file position
*/
function ftell()
{
return ftell($this->fp);
}
/**
* @param pos new file position
* @param whence seek method (SEEK_SET, SEEK_CUR, SEEK_END)
* @return Upon success, returns 0; otherwise, returns -1. Note that
* seeking past EOF is not considered an error.
*/
function fseek($pos, $whence = SEEK_SET)
{
return fseek($this->fp, $pos, $whence);
}
/**
* @return next char from file
* @note a new line character does not increase $this->lnum
*/
function fgetc()
{
$this->freeLine();
$c = fgetc($this->fp);
if ($c == '\n') {
$this->lnum++;
}
}
/** Read and return remaining part of stream
* @return size of remaining part passed through
*/
function fpassthru()
{
return fpassthru($this->fp);
}
/** Get a line from the file and strip HTML tags
* @param $allowable_tags tags to keep in the string
*/
function fgetss($allowable_tags = NULL)
{
return fgetss($this->fp, $allowable_tags);
}
/** Scan the next line
* @param $format string specifying format to parse
*/
function fscanf($format /* , ... */)
{
$this->freeLine();
$this->lnum++;
return fscanf($this->fp, $format /* , ... */);
}
/**
* @param $str to write
* @param $length maximum line length to write
*/
function fwrite($str, $length = NULL)
{
return fwrite($this->fp, $length);
}
/**
* @return array of file stat information
*/
function fstat()
{
return fstat($this->fp);
}
/**
* @param $size new size to truncate file to
*/
function ftruncate($size)
{
return ftruncate($this->fp, $size);
}
/**
* @param $flags new flag set
*/
function setFlags($flags)
{
$this->flags = $flags;
}
/**
* @return current set of flags
*/
function getFlags()
{
return $this->flags;
}
/**
* @param $max_len set the maximum line length read
*/
function setMaxLineLen($max_len)
{
$this->max_len = $max_len;
}
/**
* @return current setting for max line
*/
function getMaxLineLen()
{
return $this->max_len;
}
/**
* @return false
*/
function hasChildren()
{
return false;
}
/**
* @return false
*/
function getChildren()
{
return NULL;
}
/**
* Invalidate current line buffer and set line number to 0.
*/
function rewind()
{
$this->freeLine();
$this->lnum = 0;
}
/**
* @return whether more data can be read
*/
function valid()
{
return !$this->eof();
}
/**
* @note Fill current line buffer if not done yet.
* @return line buffer
*/
function current()
{
if (is_null($this->line))
{
$this->line = getCurrentLine();
}
return $this->line;
}
/**
* @return line number
* @note fgetc() will increase the line number when reaing a new line char.
* This has the effect key() called on a read a new line will already
* return the increased line number.
* @note Line counting works as long as you only read the file and do not
* use fseek().
*/
function key()
{
return $this->lnum;
}
/** Invalidate current line buffer.
*/
function next()
{
$this->freeLine();
}
/**
* @return next line read from file and increase the line counter
*/
private function readLine()
{
if ($this->eof())
{
$this->freeLine();
throw new RuntimeException("Cannot read from file " . $this->fname);
}
if ($this->line) {
$this->lnum++;
}
$this->freeLine();
$this->line = fgets($this->fp, $this->max_len);
return $this->line;
}
/**
* Free the current line buffer and increment the line counter
*/
private function freeLine()
{
if ($this->line) {
$this->line = NULL;
}
}
/*
* @note If you DO overload this function key() and current() will increment
* $this->lnum automatically. If not then function reaLine() will do
* that for you.
*/
function getCurrentLine()
{
$this->freeLine();
if ($this->eof())
{
throw new RuntimeException("Cannot read from file " . $this->fname);
}
$this->readLine();
}
/**
* @return current line
*/
function __toString()
{
return current();
}
/**
* @param $line_pos Seek to this line
*/
function seek($line_pos)
{
$this->rewind();
while($this->lnum < $line_pos && !$this->eof())
{
$this->getCurrentLine();
}
}
}
?>
|