forked from grobmeier/simplepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringstream.php
109 lines (93 loc) · 2.27 KB
/
stringstream.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* String Stream Wrapper
*
* This file allows you to use a PHP string like
* you would normally use a regular stream wrapper
*
* PHP5
*
* Created on Aug 7, 2008
*
* @package stringstream
* @author Sam Moffatt <sam.moffatt@toowoombarc.qld.gov.au>
* @author Toowoomba Regional Council Information Management Branch
* @license GNU/GPL http://www.gnu.org/licenses/gpl.html
* @copyright 2008 Toowoomba Regional Council/Sam Moffatt
* @version SVN: $Id:$
*/
class StringStreamController {
function &_getArray() {
static $strings = array();
return $strings;
}
function createRef($reference, &$string) {
$ref =& StringStreamController::_getArray();
$ref[$reference] =& $string;
}
function &getRef($reference) {
$ref =& StringStreamController::_getArray();
if(isset($ref[$reference])) {
return $ref[$reference];
} else {
$false = false;
return $false;
}
}
}
class StringStream {
var $_currentstring;
var $_path;
var $_mode;
var $_options;
var $_opened_path;
var $_pos;
var $_len;
function stream_open($path, $mode, $options, &$opened_path) {
$this->_currentstring = StringStreamController::getRef(str_replace('string://','',$path));
if($this->_currentstring) {
$this->_len = strlen($this->_currentstring);
$this->_pos = 0;
return true;
} else {
return false;
}
}
function stream_stat() {
return false;
}
function stream_read($count) {
$result = substr($this->_currentstring, $this->_pos, $count);
$this->_pos += $count;
return $result;
}
function stream_write($data) {
return strlen($data);
}
function stream_tell() {
return $this->_pos;
}
function stream_eof() {
if($this->_pos > $this->_len) return true;
return false;
}
function stream_seek($offset, $whence) {
//$whence: SEEK_SET, SEEK_CUR, SEEK_END
if($offset > $this->_len) return false; // we can't seek beyond our len
switch($whence) {
case SEEK_SET:
$this->_pos = $offset;
break;
case SEEK_CUR:
if(($this->_pos + $offset) < $this->_len) {
$this->_pos += $offset;
} else return false;
break;
case SEEK_END:
$this->_pos = $this->_len - $offset;
break;
}
return true;
}
}
stream_wrapper_register('string', 'StringStream') or die('Failed to register string stream');