Location: PHPKode > scripts > BowML > BowStream.inc.php
<?php

/**
 * Class : BowStream
 * Developed by Jonas Eriksson
 * @copyright 2003 BICOM KB
 **/
  
 class BowStream
 {
	/*Instance variables*/
	var $buffer;
	var $position;
	var $from;
	var $to;
	
	/*Setter and getter for $buffer*/
	function buffer() { return $this->buffer; }
	function bufferSet( $aBuffer ) { $this->buffer = $aBuffer; }

	/*Setter and getter for $position*/
	function position() { return $this->position; }
	function positionSet( $aPosition ) { $this->position = $aPosition; }
	
	/*Setter and getter for $from*/
	function from() { return $this->from; }
	function fromSet( $aFromPosition ) { $this->from = $aFromPosition; }

	/*Setter and getter for $to*/
	function to() { return $this->to; }
	function toSet( $aToPosition ) { $this->to = $aToPosition; }
	
	/*Constructor*/
	function BowStream()
	{
		$this->bufferSet(array());
		$this->positionSet(0);
	}
	
	/*Return the number of characters occupied in the buffer*/
	function size()
	{
		$myBuffer = $this->buffer();
		$size = sizeof($myBuffer);
		return $size;
	}
	

	/*Returns the current character from the buffer. Return EOF if the stream is over.*/
	function currentCharacter()
	{
		$myBuffer =& $this->buffer();
		if($this->size() == $this->position())
			return "EOF";
		else
			return $myBuffer[$this->position()];
	}
	
	/*Returns the current character from the buffer and then sets the following character as the current one. Return EOF if the stream is over.*/
	function currentCharacterAndContinue()
	{
		$myBuffer =& $this->buffer();
		if($this->size() == $this->position())
			return "EOF";
		else
		{
			$this->positionSet($this->position+1);
			return $myBuffer[$this->position()-1];
		}
	}

	/*Returns the character following the current character from the 
	* buffer. If the current character is the last one; return "EOF"*/
	function nextCharacter()
	{
		$myBuffer =& $this->buffer();
		if($this->size() != $this->position())
		{
			$newPosition = $this->positionSet($this->position() + 1);
			return $myBuffer[$this->position()];
		}
		else
			return "EOF";
	}
	
	/*Pushes a string to the string buffer*/
	function bow_print($aString)
	{
		$myBuffer = $this->buffer();
		$newStringSize = strlen($aString);
		$limit = ($this->position() + $newStringSize);
		$j = 0;
		
		for($i=$this->position() ; $i < $limit ; $i++)
		{
			$myBuffer[$i] = $aString[$j];
			if ($j <= $newStringSize)
				$j++;
		}
		$this->buffer = $myBuffer;
		$this->positionSet($limit);
	}
	
	/*Flushes the string buffer from position 1 to the last position onto the receiver*/
	function bow_flush()
	{ 
		$tReturnString = "";
		$this->fromFirst();
		$tMyBuffer = $this->buffer;
		for($i = 0 ; $i <= $this->size() ; ++$i)
		{
			$tReturnString = "$tReturnString{$tMyBuffer[$i]}";
		}
		return $tReturnString;
	}
	
	/*Flushes the string buffer from current position to the given end position onto the receiver*/
	function bow_flushTo($endPosition)
	{ 
		$returnString = "";
		$myBuffer = $this->buffer;
		for($i = $this->position(); $i <= $endPosition; ++$i)
			$returnString = "$returnString{$myBuffer[$i]}";

		return $returnString;
	}	

	/*Flushes the string buffer from my property $from to $to*/
	function bow_flushFromTo()
	{
		$returnString = "";
		$myBuffer = $this->buffer;
		for($i = $this->from(); $i <= $this->to(); ++$i)
			$returnString = "$returnString{$myBuffer[$i]}";

		return $returnString;
	}
	
	/*Sets the position pointer to zero*/
	function fromFirst()
	{
		$this->positionSet(0);
	}
	
	/*Test method*/
	function test()
	{
		return "Test string from BowStream!!";
	}

 }
?>
Return current item: BowML