2010-11-21 23:48:11 +01:00
< ? php
/**
* Buffer class for a string .
* Will fix issues with sockets that don ' t care about linebreaks .
* Can also be used for all kinds of purpose .
* @ author jpt
* @ package Misc
*/
class Misc_Buffer {
/**
* Bufferstring - contains all the data .
* @ var string
*/
protected $buffer ;
/**
* Linebreak to use for pulling lines out of the buffer .
* @ var string
*/
protected $linebreak ;
/**
* Default constructor . Sets a default linebreak and initializes the buffer .
* @ param $linebreak
* @ return void
*/
2010-12-18 15:42:56 +01:00
public function __construct ( $linebreak = " " ) {
2010-11-21 23:48:11 +01:00
$this -> buffer = " " ;
$this -> linebreak = $linebreak ;
}
/**
* Prunes empty lines out of the buffer .
* @ return void
*/
protected function pruneEmptyLines () {
2010-11-28 19:35:08 +01:00
//no need to do this when there are no linebreaks.
if ( $this -> linebreak === " " ) return ;
2010-11-21 23:48:11 +01:00
if ( strpos ( $this -> buffer , $this -> linebreak ) === FALSE ) return ;
2011-06-26 01:16:28 +02:00
$hasLinebreakAtEnd = ( substr ( $this -> buffer , ( - 1 ) * strlen ( $this -> linebreak )) === $this -> linebreak ) ? TRUE : FALSE ;
2010-11-21 23:48:11 +01:00
$lines = explode ( $this -> linebreak , $this -> buffer );
foreach ( $lines AS $key => $line ) {
$line = str_replace ( $this -> linebreak , " " , $line );
$line = trim ( $line );
if ( $line === " " ) unset ( $lines [ $key ]);
}
$this -> buffer = implode ( $this -> linebreak , $lines );
2011-06-26 01:16:28 +02:00
if ( $hasLinebreakAtEnd === TRUE ) $this -> buffer .= $this -> linebreak ;
2010-11-21 23:48:11 +01:00
}
/**
* Appends data to the buffer .
* @ param string $data
* @ return void
*/
public function addData ( $data ) {
$this -> buffer .= $data ;
$this -> pruneEmptyLines ();
}
/**
* Returns the next line in the buffer and removes it from the buffer .
* @ return string
2010-11-28 19:35:08 +01:00
* @ throws Exception_GeneralException
2010-11-21 23:48:11 +01:00
*/
public function getNextLine () {
2010-11-28 19:35:08 +01:00
if ( $this -> linebreak === " " ) throw new Exception_GeneralException ( " Cannot return a line - no linebreak is set! " , 1290964174 );
2010-11-21 23:48:11 +01:00
if ( ! $this -> hasLines ()) return " " ;
list ( $line ) = explode ( $this -> linebreak , $this -> buffer );
$this -> buffer = str_replace ( $line . $this -> linebreak , " " , $this -> buffer );
$line = trim ( $line );
$result = ( $line !== " " ) ? $line . $this -> linebreak : " " ;
return $result ;
}
2010-11-28 19:35:08 +01:00
/**
2010-12-19 18:56:58 +01:00
* Returns the length of the buffer .
* @ return int
*/
public function getBufferLength () {
return strlen ( $this -> buffer );
}
/**
* Returns the next $length chars from the buffer or throws an exception if this cannot be done .
* @ throws Exception_GeneralException
* @ return string
2010-11-28 19:35:08 +01:00
*/
public function getNextChars ( $length ) {
2010-12-19 18:56:58 +01:00
if ( strlen ( $this -> buffer ) < $length ) throw new Exception_GeneralException ( " Cannot return " . $length . " chars, there are only " . strlen ( $this -> buffer ) . " chars left! " , 1292780386 );
$data = substr ( $this -> buffer , 0 , $length );
$this -> buffer = substr ( $this -> buffer , $length );
return $data ;
2010-11-28 19:35:08 +01:00
}
2010-11-21 23:48:11 +01:00
/**
* Checks whether the buffer contains more lines to process .
* @ return boolean
2010-11-28 19:35:08 +01:00
* @ throws Exception_GeneralException
2010-11-21 23:48:11 +01:00
*/
public function hasLines () {
2010-11-28 19:35:08 +01:00
if ( $this -> linebreak === " " ) throw new Exception_GeneralException ( " Cannot tell whether the buffer has lines - no linebreak set! " , 1290964243 );
2010-11-21 23:48:11 +01:00
return ( trim ( strstr ( $this -> buffer , $this -> linebreak , TRUE )) !== " " ) ? TRUE : FALSE ;
}
2010-12-01 00:25:15 +01:00
/**
* Returns TRUE when there is data in the buffer .
* @ return boolean
*/
public function hasData () {
return ( strlen ( $this -> buffer ) > 0 ) ? TRUE : FALSE ;
}
/**
* Returns the full buffer contents .
* Also truncates the buffer .
* @ return string
*/
public function getAllBufferContents () {
$return = $this -> buffer ;
$this -> buffer = " " ;
return $return ;
}
2010-11-21 23:48:11 +01:00
}
?>