c2ephp
|
00001 <?php 00002 require_once(dirname(__FILE__).'/IReader.php'); 00004 00009 class StringReader implements IReader { 00010 00012 00013 private $position; 00014 private $string; 00016 00018 00021 public function StringReader($string) { 00022 $this->string = $string; 00023 $this->position = 0; 00024 } 00025 public function Read($characters) { 00026 if($characters > 0) { 00027 if($this->position+$characters > strlen($this->string)) { 00028 return false; 00029 } 00030 $str = substr($this->string,$this->position,$characters); 00031 00032 $this->position += $characters; 00033 return $str; 00034 } 00035 return ""; 00036 } 00037 public function ReadCString() { 00038 $string = ''; 00039 while(($char = $this->Read(1)) !== false) { 00040 $string.=$char; 00041 if($char == "\0") { 00042 break; 00043 } 00044 } 00045 return substr($string,0,-1); 00046 } 00047 public function Seek($position) { 00048 $this->position = $position; 00049 } 00050 public function Skip($count) { 00051 $this->position += $count; 00052 } 00053 public function ReadInt($characters) { 00054 return BytesToIntLilEnd($this->Read($characters)); 00055 } 00056 public function GetPosition() { 00057 return $this->position; 00058 } 00059 public function GetSubString($start,$length = FALSE) { 00060 if($length == FALSE) { 00061 $length = strlen($this->string)-$start; 00062 } 00063 $str = substr($this->string,$start,$length); 00064 return $str; 00065 } 00066 } 00067 00068 function BytesToIntLilEnd($string) { //little endian 00069 if($string == "") { 00070 return false; 00071 } 00072 $length = strlen($string); 00073 $int = 0; 00074 for($i=0;$i<$length;$i++) { 00075 $int += ord($string{$i})<<($i*8); 00076 } 00077 return $int; 00078 } 00079 ?>