buffer = ""; $this->linebreak = $linebreak; } /** * Prunes empty lines out of the buffer. * @return void */ protected function pruneEmptyLines() { //no need to do this when there are no linebreaks. if($this->linebreak === "") return; if(strpos($this->buffer, $this->linebreak) === FALSE) return; $hasLinebreakAtEnd = (substr($this->buffer, (-1) * strlen($this->linebreak)) === $this->linebreak) ? TRUE : FALSE; $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); if($hasLinebreakAtEnd === TRUE) $this->buffer .= $this->linebreak; } /** * 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 * @throws Exception_GeneralException */ public function getNextLine() { if($this->linebreak === "") throw new Exception_GeneralException("Cannot return a line - no linebreak is set!", 1290964174); 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; } /** * 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 */ public function getNextChars($length) { 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; } /** * Checks whether the buffer contains more lines to process. * @return boolean * @throws Exception_GeneralException */ public function hasLines() { if($this->linebreak === "") throw new Exception_GeneralException("Cannot tell whether the buffer has lines - no linebreak set!", 1290964243); return (trim(strstr($this->buffer, $this->linebreak, TRUE)) !== "") ? TRUE : FALSE; } /** * 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; } } ?>