<?php

/*
 * Code to parse a pfile from the MUD-Con base. Written by Myself and Davion.
 */

class pfile {

	protected $db;
	protected $path = "/home/alhaen/OtherRealm/account";
	
	var $name;
	var $passwd;
	var $level;
	var $mud;
	var $url;
	var $mmade;
	
	function __construct($db) {
		$this->db = $db;
	}
	
	/*
	 * Load a pfile.
	 */
	public function loadPfile($player) {
		$file = fopen($path . "/" . $player, 'r') or die("pfileParser failed.");
		while( false !== ($word = readWord($file) ) ) {
			if(!strcasecmp($word, 'Name') ) {
				$this->name = $this->sRead($file);
				continue;
			}
			if(!strcasecmp($word, 'Pswd') ) {
				$this->passwd = $this->sRead($file);
				continue;
			}
			if(!strcasecmp($word, 'Lvl' ) ) {
				$this->level = $this->nRead($file);
				continue;
			}
			if(!strcasecmp($word, 'CmnF' ) || !strcasecmp($word, 'ChnF') ) {
				$this->nRead($file);
				continue;
			}
			if(!strcasecmp($word, 'Read' ) ) {
				$this->sRead($file);
				continue;
			}
			if(!strcasecmp($word, 'Mud') ) {
				$this->mud = $this->sRead($file);
				continue;
			}
			if(!strcasecmp($word, 'Url') ) {
				$this->url = $this->sRead($file);
				continue;
			}
			if(!strcasecmp($word, 'MMade') ) {
				$this->mmade = $this->nRead($file);
				continue;
			}
			if(!strcasecmp($word, 'CPst') || !strcasecmp($word, 'CRpl') || !strcasecmp($word, 'CTpc')) {
				$this->sRead($file);
				continue;
			}
			if(!strcasecmp($word, "$" ) )
				break;
		}
		fclose($file);
	}
	
	/*
	 * Read a word until ' '
	 */
	public function readWord($file) {
		$string = '';
		while ( false !== ($c = fgetc($file)) ) {
			if ($c == ' ')
				return $string;
			$string .= $c;
		}
		return false;
	}
	
	/* 
	 * Read a string to ~
	 */
	function sRead($file) {
		$string = '';
		while( false !== ($c = fgetc($file) ) ) {
			if($c == '~' ) {
				fgetc($file);
				return $string;
			}
			$string .= $c;
		}
		return false;
	}
	
	/*
	 * Read a #
	 */
	function nRead($file) {
		$number = '';
		while(false !== ($c = fgetc($file) ) ) {
			if($c == "\n" ) 
				return $number;
			$number .= $c;
		}
		return false;
	}
	
}

?>