<?php

/* Skriven av Björn Wikström, 2002
 * Fri att använda så länge dessa rader
 * står kvar.
 *
 * Exempel på användande:
 * <?php
 * $myShmop = new Shmop();
 * $myShmop->createShmop();
 * $myShmop->writeShmop( "SPiN!" );
 * echo $myShmop->readShmop();
 * $myShmop->closeShmop();
 * ?>
 */

class Shmop {
	var $curID;
	var $curSize;
	
	function createShmop() {
		if( ( $id = shmop_open( 0x0fff, "w", 0, 0 ) ) == true ) {
			$this->curID = $id;
			$this->curSize = shmop_size( $this->curID );
			
			return true;
		}
		
		$id = shmop_open( 0x0fff, "c", 0644, 128 )
			or return false;
		
		$this->curID = $id;
		$this->curSize = shmop_size( $this->curID );
		
		return true;
	}
	
	function writeShmop( $data ) {
		$bytes = shmop_write( $this->curID, $data, 0 );
		
		if( $bytes != strlen( $data ) )
			return false;
		
		return true;
	}
	
	function readShmop() {
		$data = shmop_read( $this->curID, 0, $this->curSize );
		
		if( !$data )
			return false;
		
		return $data;
	}
	
	function closeShmop() {
		shmop_delete( $this->curID );
		shmop_close( $this->curID );
	}
}

?>