Location: PHPKode > projects > MpiBot > system/MPIConfig.class.php
<?php
	/* (C) Copyright 2004 Armond Carroll.  All rights reserved.
	   Distributed under the BSD License.  Please see license.txt for license,
	   or http://www.opensource.org/licenses/bsd-license.php.

	   http://mpibot.sourceforge.net

	   Purpose of this file:
	   	To parse the configuration file format.

	   File Version: 1.0
	   Last Modified: 4/30/2004 1:24AM
	*/

	class MPIConfig {
		public $cfg = array();
		function __construct($file) {
			$f = @file($file);
			if(!is_array($f)) error("$file: Could not open for parsing. File does not exist.");
			$lnum = 0;
			$in_multiline = false;
			foreach($f as $line) {
				$lnum++;
				$line = trim($line);
				if(empty($line) || $line[0] == '#' && $in_multiline == false) continue;
				elseif($line[0] == '@' && $in_multiline == false) {
					@list($name, $value) = explode('=', substr($line, 1), 2);
					$name = trim($name); $value = trim($value);
					if(empty($name)) error("$file: Line $lnum: Variable name is empty.");
					$h = explode(':', $name);
					if(count($h) < 2) error("$file: Line $lnum: Invalid syntax for varible name.");
					if(count($h) > 10) error("$file: Line $lnum: Directive 10 or more levels deep.  You should consider a better method.", OL_WARNING);
					$stack = '';
					$last = '';
					foreach($h as $hh) {
						$hh = trim($hh);
						if(empty($hh)) error("$file: Line $lnum: Empty directive after '$last'.");
						$last = $hh;
						$stack .= "'$hh'][";
					}
					$stack = substr($stack, 0, strlen($stack) - 2);
					eval('$this->cfg[' . $stack . '] = $value;');
				} elseif($line[0] == '%' && $in_multiline == false) {
					$started_multi_on = $lnum;
					$in_multiline = true;
					if(strpos($line, '[') === false) {
						error("$file: Line $lnum: Unexpected data.  Needs '['.");
					}
					$name = trim(substr($line, 1, strpos($line, '[') - 1));
					if(empty($name)) error("$file: Line $lnum: Variable name is empty.");
					$h = explode(':', $name);
					if(count($h) < 2) error("$file: Line $lnum: Invalid syntax for varible name.");
					if(count($h) > 10) error("$file: Line $lnum: Directive 10 or more levels deep.  You should consider a better method.", OL_WARNING);
					$stack = "";
					$last = '';
					foreach($h as $hh) {
						$hh = trim($hh);
						if(empty($hh)) error("$file: Line $lnum: Empty directive after '$last'.");
						$last = $hh;
						$stack .= "'$hh'][";
					}
					$stack = substr($stack, 0, strlen($stack) - 2);
					eval('$this->cfg[' . $stack . '] = array();');
					$stack .= '][';
				} elseif($line[0] == ']' && $in_multiline == true) {
					$in_multiline = false;
				} elseif($in_multiline == true) {
					eval('$this->cfg[' . $stack . '] = "' . $line . '";');
				} else {
					error("$file: Line $lnum: Unexpected '$line[0]'.");
				}
			}

			if($in_multiline == true) error("$file: Line $lnum: Unexpected End-of-File. Last multiline directive started on line $started_multi_on.");
		}
	}
?>
Return current item: MpiBot