All pastes #330448 Raw Edit

Something

public text v1 · immutable
#330448 ·published 2007-01-27 22:26 UTC
rendered paste body
<?php

class MemoryStream {

	protected $position;
	protected $filename;
	static protected $files = array();

	function stream_open($path, $mode, $options, &$opened_path)
	{
		$url = parse_url($path);
		$this->filename = $url["host"];
		$this->position = 0;

		return true;
	}

	function stream_read($count)
	{
		$ret = substr(self::$files[$this->filename], $this->position, $count);
		$this->position += strlen($ret);
		return $ret;
	}

	function stream_write($data)
	{
		$left = substr(self::$files[$this->filename], 0, $this->position);
		$right = substr(self::$files[$this->filename], $this->position + strlen($data));
		self::$files[$this->filename] = $left . $data . $right;
		$this->position += strlen($data);
		return strlen($data);
	}

	function stream_tell()
	{
		return $this->position;
	}

	function stream_eof()
	{
		return $this->position >= strlen(self::$files[$this->filename]);
	}

	function stream_seek($offset, $whence)
	{
		switch ($whence)
		{
			case SEEK_SET:
				if ($offset < strlen(self::$files[$this->filename]) && $offset >= 0)
				{
					$this->position = $offset;
					return true;
				}
				else
				{
					return false;
				}
				break;

			case SEEK_CUR:
				if ($offset >= 0)
				{
					$this->position += $offset;
					return true;
				}
				else
				{
					return false;
				}
				break;

			case SEEK_END:
				if (strlen(self::$files[$this->filename]) + $offset >= 0)
				{
					$this->position = strlen(self::$files[$this->filename]) + $offset;
					return true;
				}
				else
				{
					return false;
				}
				break;

			default:
				return false;
		}
	}
	
	static public function flush(){
		self::$files = array();
	}
}

stream_wrapper_register("mem", "MemoryStream") or die("Failed to register protocol");

?>