#native_company# #native_desc#
#native_cta#

M4ApplicationsVariable

By Fredrik Larsson
on March 27, 2001

Version: 0.9

Type: Class

Category: Other

License: GNU General Public License

Description: Application type variable,
like M$ ASP application variable

<?
/*

2001-03-01 - Fredrik ([email protected])
Create ASP application type variables 
(compile with --enable-shmop )

 M4ApplicationsVariable version 0.9
 Lots of bugs, isnt very good...
 do not fixes its own mess...

 TODO:
 	Enable dynamic size of keysTable, and keys

 
debug true: output lots of debugging data
usage
//set .php
include "class.M4App.php";

$app = new M4ApplicationsVariable(false);

$app->set("foo", "hey was up?");

//
//get.php
include "class.M4App.php";

$app = new M4ApplicationsVariable();

echo $app->get("foo");

//


class M4ApplicationsVariable
{
	var $keysTable = array();	//table containing all keys
	var $keysID    = array();	//table containing key systemid
	var $keysSize  = 100;		//size of index table
	var $keySize   = 100;		//size of each key
	var $keysSysID = 4095;		//system id of key (0xfff)
	var $keyHandle = false;	    //system handle for key
	var $debug     = false;
	
	function M4ApplicationsVariable($debug_ = false)
	{
		$this->debug = $debug_;
		$this->readKeys();
	}

	function readKeys()
	{
		$this->keyHandle = @shmop_open($this->keysSysID, "a", 0644, $this->keysSize);

			if (!$this->keyHandle)
			{
				return false;
				echo "<pre>error[M4ApplicationsVariable]: unable to open allocated memory</pre>";
				exit;
			}

		//$tmp = shmop_size($this->keys);
		$ptr = shmop_read($this->keyHandle, 0, $this->keysSize);
		$ptr = substr($ptr, 0, strpos($ptr, "|") );

		if ($this->debug == true)
			echo "data(readkeys) (in): " . $ptr . "<br>";

		$ptr = trim($ptr);
		
		$t = split(",", $ptr);
		$tc = count($t);

		$this->keysTable = array();
		
		for ($i = 0; $i < $tc;$i++)
		{
			$tkey = split("=", $t[$i] );
			$this->keysTable[ $tkey[0] ] = $tkey[1];
		}
		
		$this->close($this->keyHandle);
		return $ptr;
	}
	function writeKeys($instr)
	{
		if ($this->debug == true)
		{
			echo "*writeKeys*";
			echo "handle:" . $this->keyHandle . "<br>";
			echo "sys:" . $this->keysSysID . "<br>";
		}
		$this->keyHandle = shmop_open($this->keysSysID, "c", 0644, $this->keysSize);
		if (!$this->keyHandle)
		{
			echo "<pre>error[M4ApplicationsVariable]: unable to allocate memory</pre>";
			exit;
		}

		$instr = str_replace ("|", "", $instr) . "|";

		$result = shmop_write($this->keyHandle, $instr, 0);

		if ($result == 0 && $instr == "")
		{
			if ($this->debug == true)
				echo "killing self";
				
			$this->destroySelf();
		}
		
		if ($this->debug == true)
		{	
			echo "Bytes written:" . $result . "<br>";
			echo "Data (out):" . $instr . "<br><br>";
		}
		if($result != strlen($instr))
		{
			echo "<pre>error[M4ApplicationsVariable.writeKeys]: unable to write to allocated memory</pre>";
			exit;
		}
		$this->close($this->keyHandle);
	}

	function addKey($key)
	{
		if ($this->debug == true)
			echo "*addkey*";
		$ptr = $this->readKeys();

		if ($this->debug == true)
		{
			echo "Ant: " . count($this->keysTable) . "<br>";
			echo "Current data: " . $ptr . "<br>";
		}
		
		$addr = (int) ( (int) $this->keysSysID + (int) count($this->keysTable) +1 );
		$ptr .= ((count($this->keysTable) == 0) ? "" : ",") . $key . "=" . $addr;
		$this->keysTable[ $key ] = $addr;

		if ($this->debug == true)
		{
			echo "New data: " . $ptr . "<br>";
		}
		$this->writeKeys($ptr);
	}
	
	function showKeys()
	{
       while ( list ($key,$value) = each ($this->keysTable) )
       {
//               if (!(empty($key)))
               {
					printf("key: %s [%x]<br>", $key, $value);
               }
       }
	}

	function set($key, $value)
	{
		if ($this->debug == true)
			echo "*set*";
			
		if (! isset($this->keysTable[ $key ] )) //key dont exisist
		{
			$this->addKey($key);
		}
	
		if ($this->debug == true)
			echo "segment (set): " . $this->keysTable[$key] . "<br>";
		
		$id = shmop_open($this->keysTable[$key], "c", 0644, $this->keysSize);
		if (!$id)
		{
			echo "<pre>error[M4ApplicationsVariable.set]: unable to allocate memory</pre>";
			exit;
		}
		$value .= "|";
		
		$result = shmop_write($id, $value, 0);
	
		if ($this->debug == true)
		{
			echo "Bytes written (set):" . $result . "<br>";
			echo "Data (out)(set): " .    $value . "<br>";
		}	
		if($result != strlen($value))
		{
			echo "<pre>error[M4ApplicationsVariable.set: unable to write to allocated memory</pre>";
			exit;
		}
		$this->close($id);
	}
	function get($key)
	{
		if (! isset($this->keysTable[ $key ] )) //key dont exisist
		{
			return false;
			echo "<pre>error[M4ApplicationsVariable.get]: nu such key</pre>";
			exit;			
		}
	
		if ($this->debug == true)
			echo "segment: " . $this->keysTable[$key] . "<br>";
		
		$id = @shmop_open($this->keysTable[$key], "a", 0644, $this->keysSize);
		if (!$id)
		{
			echo "<pre>error[M4ApplicationsVariable.set]: unable to open allocated memory</pre>";
			exit;
		}
		$ptr = shmop_read($id, 0, $this->keysSize);
		$ptr = substr($ptr, 0, strpos($ptr, "|") );

		if ($this->debug == true)
			echo "data (in): " . $ptr . "<br>";
	
		$ptr = trim($ptr);	
		$this->close($id);
		
		return $ptr;
	}
	function kill($key)
	{
	
		if ($this->debug == true)
			echo "*kill ($key)*";
			
		if (! isset($this->keysTable[ $key ] )) //key dont exisist
		{
			echo "<pre>error[M4ApplicationsVariable.get]: nu such key</pre>";
			exit;			
		}
		$this->destroy($this->keysTable[ $key ] );
		
		$ptr = $this->readKeys();

		if ($this->debug == true)
			echo "data (in): " . $ptr . "<br>";

		$ptr = trim($ptr);
		
		$t = split(",", $ptr);
		$tc = count($t);

		$nptr = "";
		for ($i = 0; $i < $tc;$i++)
		{
		
			$tkey = split("=", $t[$i] );
			echo "!" . $tkey[0] . "!";
			if (strstr($key, $tkey[0]) == false)
			{	
				$this->keysTable[ $tkey[0] ] = $tkey[1];
				$nptr .= $tkey[0] . "=" . $tkey[1] . ",";
			}
		}

		if ($this->debug == true)
			echo "data (out) " . $nptr . "<br>";

		$this->writeKeys($nptr);
	}
	function destroy($add)
	{
	
		$id = shmop_open($add, "c", 0644, $this->keysSize);
		
		if(!shmop_delete($id))
		{
			echo "<pre>error[M4ApplicationsVariable]: unable to deallocate from memory</pre>";
			exit;
		}
		
		$this->close($id);
	}

	function destroySelf()
	{
		$this->keyHandle = shmop_open($this->keysSysID, "c", 0644, $this->keysSize);
		
		if(!shmop_delete($this->keyHandle))
		{
			echo "<pre>error[M4ApplicationsVariable]: unable to deallocate from memory</pre>";
			exit;
		}
		
		$this->close($this->keyHandle);
		$this->keyHandle = false;
	}
	function close($id)
	{
		shmop_close($id);
	}


}
?>