SPL-StandardPHPLibrary
splfileobject.inc
Go to the documentation of this file.
1 <?php
2 
19 {
21  const DROP_NEW_LINE = 0x00000001;
22 
23  private $fp;
24  private $fname;
25  private $line = NULL;
26  private $lnum = 0;
27  private $max_len = 0;
28  private $flags = 0;
29  private $delimiter= ',';
30  private $enclosure= '"';
31 
42  function __construct($file_name, $open_mode = 'r', $use_include_path = false, $context = NULL)
43  {
44  $this->fp = fopen($file_name, $open_mode, $use_include_path, $context);
45  if (!$this->fp)
46  {
47  throw new RuntimeException("Cannot open file $file_name");
48  }
49  $this->fname = $file_name;
50  }
51 
55  function eof()
56  {
57  return eof($this->fp);
58  }
59 
63  function fgets()
64  {
65  $this->freeLine();
66  $this->lnum++;
67  $buf = fgets($this->fp, $this->max_len);
68 
69  return $buf;
70  }
71 
77  function fgetcsv($delimiter = NULL, $enclosure = NULL)
78  {
79  $this->freeLine();
80  $this->lnum++;
81  switch(fun_num_args())
82  {
83  case 0:
85  case 1:
87  default:
88  case 2:
89  break;
90  }
91  return fgetcsv($this->fp, $this->max_len, $delimiter, $enclosure);
92  }
93 
100  function setCsvControl($delimiter = ';', $enclosure = '"')
101  {
102  $this->delimiter = $delimiter;
103  $this->enclosure = $enclosure;
104  }
105 
109  function getCsvControl($delimiter = ',', $enclosure = '"')
110  {
111  return array($this->delimiter, $this->enclosure);
112  }
113 
118  function flock($operation, &$wouldblock)
119  {
120  return flock($this->fp, $operation, $wouldblock);
121  }
122 
127  function fflush()
128  {
129  return fflush($this->fp);
130  }
131 
135  function ftell()
136  {
137  return ftell($this->fp);
138  }
139 
146  function fseek($pos, $whence = SEEK_SET)
147  {
148  return fseek($this->fp, $pos, $whence);
149  }
150 
155  function fgetc()
156  {
157  $this->freeLine();
158  $c = fgetc($this->fp);
159  if ($c == '\n') {
160  $this->lnum++;
161  }
162  }
163 
167  function fpassthru()
168  {
169  return fpassthru($this->fp);
170  }
171 
175  function fgetss($allowable_tags = NULL)
176  {
177  return fgetss($this->fp, $allowable_tags);
178  }
179 
183  function fscanf($format /* , ... */)
184  {
185  $this->freeLine();
186  $this->lnum++;
187  return fscanf($this->fp, $format /* , ... */);
188  }
189 
194  function fwrite($str, $length = NULL)
195  {
196  return fwrite($this->fp, $length);
197  }
198 
202  function fstat()
203  {
204  return fstat($this->fp);
205  }
206 
210  function ftruncate($size)
211  {
212  return ftruncate($this->fp, $size);
213  }
214 
218  function setFlags($flags)
219  {
220  $this->flags = $flags;
221  }
222 
226  function getFlags()
227  {
228  return $this->flags;
229  }
230 
235  {
236  $this->max_len = $max_len;
237  }
238 
242  function getMaxLineLen()
243  {
244  return $this->max_len;
245  }
246 
250  function hasChildren()
251  {
252  return false;
253  }
254 
258  function getChildren()
259  {
260  return NULL;
261  }
262 
266  function rewind()
267  {
268  $this->freeLine();
269  $this->lnum = 0;
270  }
271 
275  function valid()
276  {
277  return !$this->eof();
278  }
279 
284  function current()
285  {
286  if (is_null($this->line))
287  {
288  $this->line = getCurrentLine();
289  }
290  return $this->line;
291  }
292 
301  function key()
302  {
303  return $this->lnum;
304  }
305 
308  function next()
309  {
310  $this->freeLine();
311  }
312 
316  private function readLine()
317  {
318  if ($this->eof())
319  {
320  $this->freeLine();
321  throw new RuntimeException("Cannot read from file " . $this->fname);
322  }
323  if ($this->line) {
324  $this->lnum++;
325  }
326  $this->freeLine();
327  $this->line = fgets($this->fp, $this->max_len);
328  return $this->line;
329  }
330 
334  private function freeLine()
335  {
336  if ($this->line) {
337  $this->line = NULL;
338  }
339  }
340 
341  /*
342  * @note If you DO overload this function key() and current() will increment
343  * $this->lnum automatically. If not then function reaLine() will do
344  * that for you.
345  */
346  function getCurrentLine()
347  {
348  $this->freeLine();
349  if ($this->eof())
350  {
351  throw new RuntimeException("Cannot read from file " . $this->fname);
352  }
353  $this->readLine();
354  }
355 
359  function __toString()
360  {
361  return current();
362  }
363 
367  function seek($line_pos)
368  {
369  $this->rewind();
370  while($this->lnum < $line_pos && !$this->eof())
371  {
372  $this->getCurrentLine();
373  }
374  }
375 }
376 
377 ?>
fflush()
Flush current data.
freeLine()
Free the current line buffer and increment the line counter.
__construct($file_name, $open_mode= 'r', $use_include_path=false, $context=NULL)
Constructs a new file object.
fscanf($format)
Scan the next line.
fwrite($str, $length=NULL)
getCsvControl($delimiter= ',', $enclosure= '"')
seekable iterator
rewind()
Invalidate current line buffer and set line number to 0.
setCsvControl($delimiter= ';', $enclosure= '"')
Set the delimiter and enclosure character used in fgetcsv.
flock($operation, &$wouldblock)
fseek($pos, $whence=SEEK_SET)
Interface for recursive iteration with RecursiveIteratorIterator.
fgetcsv($delimiter=NULL, $enclosure=NULL)
fgetss($allowable_tags=NULL)
Get a line from the file and strip HTML tags.
$length
seek($line_pos)
setMaxLineLen($max_len)
Object representation for any stream.
fgets()
increase current line number
fpassthru()
Read and return remaining part of stream.
Exception thrown for errors that are only detectable at runtime.
Definition: spl.php:423
next()
Invalidate current line buffer.
File info class.
Definition: spl.php:856
const DROP_NEW_LINE
Flag: wheter to suppress new lines.