Back to index

php5  5.3.10
splfileobject.inc
Go to the documentation of this file.
00001 <?php
00002 
00018 class SplFileObject extends SplFileInfo implements RecursiveIterator, SeekableIterator
00019 {
00021        const DROP_NEW_LINE   = 0x00000001;
00022 
00023        private $fp;
00024        private $fname;
00025        private $line     = NULL;
00026        private $lnum     = 0;
00027        private $max_len  = 0;
00028        private $flags    = 0;
00029        private $delimiter= ',';
00030        private $enclosure= '"';
00031        
00042        function __construct($file_name, $open_mode = 'r', $use_include_path = false, $context = NULL)
00043        {
00044               $this->fp = fopen($file_name, $open_mode, $use_include_path, $context);
00045               if (!$this->fp)
00046               {
00047                      throw new RuntimeException("Cannot open file $file_name");
00048               }
00049               $this->fname = $file_name;
00050        }
00051        
00055        function eof()
00056        {
00057               return eof($this->fp);
00058        }
00059 
00063        function fgets()
00064        {
00065               $this->freeLine();
00066               $this->lnum++;
00067               $buf = fgets($this->fp, $this->max_len);
00068               
00069               return $buf;
00070        }
00071 
00077        function fgetcsv($delimiter = NULL, $enclosure = NULL)
00078        {
00079               $this->freeLine();
00080               $this->lnum++;
00081               switch(fun_num_args())
00082               {
00083                      case 0:
00084                             $delimiter = $this->delimiter;
00085                      case 1:
00086                             $enclosure = $this->enclosure;
00087                      default:
00088                      case 2:
00089                             break;
00090               }
00091               return fgetcsv($this->fp, $this->max_len, $delimiter, $enclosure); 
00092        }
00093 
00100        function setCsvControl($delimiter = ';', $enclosure = '"')
00101        {
00102               $this->delimiter = $delimiter;
00103               $this->enclosure = $enclosure;
00104        }
00105 
00109        function getCsvControl($delimiter = ',', $enclosure = '"')
00110        {
00111               return array($this->delimiter, $this->enclosure);
00112        }
00113 
00118        function flock($operation, &$wouldblock)
00119        {
00120               return flock($this->fp, $operation, $wouldblock);
00121        }
00122 
00127        function fflush()
00128        {
00129               return fflush($this->fp);
00130        }
00131 
00135        function ftell()
00136        {
00137               return ftell($this->fp);
00138        }
00139 
00146        function fseek($pos, $whence = SEEK_SET)
00147        {
00148               return fseek($this->fp, $pos, $whence);
00149        }
00150 
00155        function fgetc()
00156        {
00157               $this->freeLine();
00158               $c = fgetc($this->fp);
00159               if ($c == '\n') {
00160                      $this->lnum++;
00161               }
00162        }
00163 
00167        function fpassthru()
00168        {
00169               return fpassthru($this->fp);
00170        }
00171 
00175        function fgetss($allowable_tags = NULL)
00176        {
00177               return fgetss($this->fp, $allowable_tags);
00178        }
00179 
00183        function fscanf($format /* , ... */)
00184        {
00185               $this->freeLine();
00186               $this->lnum++;
00187               return fscanf($this->fp, $format /* , ... */);
00188        }
00189 
00194        function fwrite($str, $length = NULL)
00195        {
00196               return fwrite($this->fp, $length);
00197        }
00198 
00202        function fstat()
00203        {
00204               return fstat($this->fp);
00205        }
00206 
00210        function ftruncate($size)
00211        {
00212               return ftruncate($this->fp, $size);
00213        }
00214 
00218        function setFlags($flags)
00219        {
00220               $this->flags = $flags;
00221        }
00222 
00226        function getFlags()
00227        {
00228               return $this->flags;
00229        }
00230 
00234        function setMaxLineLen($max_len)
00235        {
00236               $this->max_len = $max_len;
00237        }
00238 
00242        function getMaxLineLen()
00243        {
00244               return $this->max_len;
00245        }
00246 
00250        function hasChildren()
00251        {
00252               return false;
00253        }
00254 
00258        function getChildren()
00259        {
00260               return NULL;
00261        }
00262 
00266        function rewind()
00267        {
00268               $this->freeLine();
00269               $this->lnum = 0;
00270        }
00271 
00275        function valid()
00276        {
00277               return !$this->eof();
00278        }
00279        
00284        function current()
00285        {
00286               if (is_null($this->line))
00287               {
00288                      $this->line = getCurrentLine();
00289               }
00290               return $this->line;
00291        }
00292 
00301        function key()
00302        {
00303               return $this->lnum;
00304        }
00305 
00308        function next()
00309        {
00310               $this->freeLine();
00311        }
00312 
00316        private function readLine()
00317        {
00318               if ($this->eof())
00319               {
00320                      $this->freeLine();
00321                      throw new RuntimeException("Cannot read from file " . $this->fname);
00322               }
00323               if ($this->line) {
00324                      $this->lnum++;
00325               }
00326               $this->freeLine();
00327               $this->line = fgets($this->fp, $this->max_len);
00328               return $this->line;
00329        }
00330 
00334        private function freeLine()
00335        {
00336               if ($this->line) {
00337                      $this->line = NULL;
00338               }
00339        }
00340 
00341        /*
00342         * @note If you DO overload this function key() and current() will increment
00343         *       $this->lnum automatically. If not then function reaLine() will do
00344         *       that for you.
00345         */ 
00346        function getCurrentLine()
00347        {
00348               $this->freeLine();
00349               if ($this->eof())
00350               {
00351                      throw new RuntimeException("Cannot read from file " . $this->fname);
00352               }
00353               $this->readLine();
00354        }
00355 
00359        function __toString()
00360        {
00361               return current();
00362        }
00363 
00367        function seek($line_pos)
00368        {
00369               $this->rewind();
00370               while($this->lnum < $line_pos && !$this->eof())
00371               {
00372                      $this->getCurrentLine();
00373               }
00374        }
00375 }
00376 
00377 ?>