c2ephp

support/FileReader.php

Go to the documentation of this file.
00001 <?php
00002 require_once(dirname(__FILE__).'/IReader.php');
00004 
00009 class FileReader implements IReader {
00010 
00012 
00013     private $fp;
00014 
00016     
00019 
00022     public function FileReader($filename)
00023     {
00024         if(!file_exists($filename))
00025             throw new Exception("File does not exist: ".$filename);
00026         if(!is_file($filename))
00027             throw new Exception("Target is not a file.");
00028         if(!is_readable($filename))
00029             throw new Exception("File exists, but is not readable.");
00030 
00031         $this->fp = fopen($filename, 'rb');
00032     }
00033 
00034     public function Read($count)
00035     {
00036         if($count > 0) {
00037             return fread($this->fp, $count);
00038         }
00039         return '';
00040     }
00041 
00042     public function ReadInt($count)
00043     {
00044         $int = 0;
00045         for($x = 0; $x < $count; $x++)
00046         {
00047             $buffer = (ord(fgetc($this->fp)) << ($x * 8));
00048             if($buffer === false)
00049                 throw new Exception("Read failure");
00050             $int += $buffer;
00051         }
00052         return $int;
00053     }
00054 
00055     public function GetPosition()
00056     {
00057         return ftell($this->fp);
00058     }
00059 
00060     public function GetSubString($start,$length = FALSE)
00061     {
00062         $oldpos = ftell($this->fp);
00063         fseek($this->fp,$start);
00064         $data = '';
00065         if($length === false) {
00066             while($newdata = $this->Read(4096)) {
00067                 if(strlen($newdata) == 0) {
00068                     break;
00069                 }
00070                 $data .= $newdata;
00071             }
00072         } else {
00073             $data = fread($this->fp,$length);
00074         }
00075         fseek($this->fp,$oldpos);
00076         return $data;
00077     }
00078     public function ReadCString() {
00079         $string = '';
00080         while(($char = $this->Read(1)) !== false) {
00081             if(ord($char) == 0) {
00082                 break;
00083             }
00084             $string.=$char;
00085         }
00086         return $string;
00087     }
00088     public function Seek($position)
00089     {
00090         fseek($this->fp, $position);
00091     }
00092 
00093     public function Skip($count)
00094     {
00095         fseek($this->fp, $count, SEEK_CUR);
00096     }
00097 }
00098 ?>
 All Classes Files Functions Variables Enumerations