c2ephp

agents/PRAYFile.php

Go to the documentation of this file.
00001 <?php
00002 
00003 require_once(dirname(__FILE__).'/../support/IReader.php');
00004 require_once(dirname(__FILE__).'/../support/StringReader.php');
00005 require_once(dirname(__FILE__).'/PRAY/PrayBlock.php');
00007 
00010 class PRAYFile {
00011 
00013 
00014     private $reader;
00015     private $blocks=array();
00016     private $parsed = false;
00017 
00019 
00020 
00022 
00026     function PRAYFile($reader=null) {
00027         $this->blocks = array();
00028         if($reader instanceof IReader) {
00029             $this->reader = $reader;
00030             $this->Parse();
00031         } else {
00032             $this->parsed = true; //make sure no one tries to parse it...
00033         }
00034     }
00035 
00037 
00039     private function Parse() {
00040         if(!$this->parsed) {
00041             if($this->ParseHeader()) {
00042                 while($this->ParseBlockHeader()) {
00043                 }
00044                 $this->parsed = true;
00045                 //print_r($this->blocks);
00046                 return $this->blocks;
00047             } else {
00048                 echo "Failed at block header: NOT A PRAY FILE";
00049                 return FALSE;
00050             }
00051         }
00052 
00053         return $this->blocks;
00054     }
00055 
00057 
00059 
00062     public function Compile() {
00063         $compiled = 'PRAY';
00064         foreach($this->blocks as $block) {
00065             $compiled .= $block->Compile();
00066         }
00067         return $compiled;
00068     }
00069 
00071 
00074     public function AddBlock(PrayBlock $block) {
00075         foreach($this->blocks as $checkBlock) {
00076             if($checkBlock->GetName() == $block->GetName()) {
00077                 throw new Exception("PRAY Files cannot contain multiple blocks with the same name");
00078             }
00079         }
00080         $this->blocks[] = $block;
00081     }
00082 
00084 
00091     public function GetBlocks($type=FALSE) { //gets all blocks or one type of block
00092         if(!$type) {
00093             return $this->blocks;
00094         } else {
00095             if(is_string($type)) {
00096                 $type = array($type);
00097             }
00098             $retblocks = array();
00099             foreach($this->blocks as $block) {
00100                 if(in_array($block->GetType(),$type)) {
00101                     $retblocks[] = $block;
00102                 }
00103             }
00104             return $retblocks;
00105         }
00106     }
00107 
00109 
00113     public function GetBlockByName($name) {
00114         foreach($this->blocks as $blockid => $block) {
00115 
00116             if($block->GetName() == $name) {
00117                 return $block;
00118             }
00119         }
00120         return null;
00121     }
00122 
00124 
00129     private function ParseHeader() {
00130         if($this->reader->Read(4) == "PRAY") {
00131             return true;
00132         } else {
00133             return false;
00134         }
00135     }
00136 
00138 
00144     private function ParseBlockHeader() {
00145         $blocktype = $this->reader->Read(4);
00146         if($blocktype=="") {
00147             return false;
00148         }
00149         $name = trim($this->reader->Read(128));
00150         if($name=="") {
00151             return false;
00152         }
00153         $length = $this->reader->ReadInt(4);
00154         if($length===false) {
00155             return false;
00156         }
00157         $fulllength = $this->reader->ReadInt(4); //full means uncompressed
00158         if($fulllength===false) {
00159             return false;
00160         }
00161         $flags = $this->reader->ReadInt(4);
00162         if($flags===false) {
00163             return false;
00164         }
00165 
00166         $content = $this->reader->Read($length);
00167         $this->blocks[] = PrayBlock::MakePrayBlock($blocktype,$this,$name,$content,$flags);
00168 
00169         return true;
00170     }
00171 
00173 }
00174 
00175 ?>
 All Classes Files Functions Variables Enumerations