Arbit - project tracking

PHPillow - PHP CouchDB connector

Browse source code

File: / src/ classes/ tool/ string_stream.php

Type
text/plain text/plain
Last Author
kore
Version
133
Line Rev. Author Source
1 115 kore <?php
2 kore /**
3 kore * phpillow string stream
4 kore *
5 kore * This file is part of phpillow.
6 kore *
7 kore * phpillow is free software; you can redistribute it and/or modify it under
8 kore * the terms of the GNU Lesser General Public License as published by the Free
9 kore * Software Foundation; version 3 of the License.
10 kore *
11 kore * phpillow is distributed in the hope that it will be useful, but WITHOUT ANY
12 kore * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 kore * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 kore * more details.
15 kore *
16 kore * You should have received a copy of the GNU Lesser General Public License
17 kore * along with phpillow; if not, write to the Free Software Foundation, Inc., 51
18 kore * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 kore *
20 kore * @package Core
21 kore * @version $Revision: 114 $
22 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
23 kore */
24 kore
25 kore /**
26 kore * Stream wrapper for plain strings
27 kore *
28 kore * @package Core
29 kore * @version $Revision: 114 $
30 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
31 kore */
32 kore class phpillowToolStringStream
33 kore {
34 kore /**
35 kore * Current position inside the string
36 kore *
37 kore * @var int
38 kore */
39 kore protected $position = 0;
40 kore
41 kore /**
42 kore * String, wrapped by the stream
43 kore *
44 kore * @var string
45 kore */
46 133 kore protected $string;
47 115 kore
48 kore /**
49 133 kore * Cached length of the string
50 kore *
51 kore * @var int
52 kore */
53 kore protected $length;
54 kore
55 kore /**
56 115 kore * Open stream
57 kore *
58 kore * @param string $path
59 kore * @param string $mode
60 kore * @param mixed $options
61 kore * @param mixed $opened_path
62 kore * @return bool
63 kore */
64 kore public function stream_open( $path, $mode, $options, &$opened_path )
65 kore {
66 kore $this->string = substr( $path, strpos( $path, '//' ) + 2 );
67 kore $this->position = 0;
68 133 kore $this->length = strlen( $this->string );
69 115 kore
70 kore return true;
71 kore }
72 kore
73 kore /**
74 kore * Read from stream
75 kore *
76 kore * @param int $count
77 kore * @return string
78 kore */
79 kore public function stream_read( $count )
80 kore {
81 kore $ret = substr( $this->string, $this->position, $count );
82 kore $this->position += strlen( $ret );
83 kore return $ret;
84 kore }
85 kore
86 kore /**
87 kore * Write to stream
88 kore *
89 kore * @param string $data
90 kore * @return int
91 kore */
92 kore public function stream_write( $data )
93 kore {
94 kore $left = substr( $this->string, 0, $this->position );
95 kore $right = substr( $this->string, $this->position + strlen( $data ) );
96 kore $this->string = $left . $data . $right;
97 133 kore $this->position += $written = strlen( $data );
98 kore $this->length = strlen( $this->string );
99 kore return $written;
100 115 kore }
101 kore
102 kore /**
103 kore * Tell current stream position
104 kore *
105 kore * @return int
106 kore */
107 kore public function stream_tell()
108 kore {
109 kore return $this->position;
110 kore }
111 kore
112 kore /**
113 kore * Has the stream reached its end?
114 kore *
115 kore * @return bool
116 kore */
117 kore public function stream_eof()
118 kore {
119 133 kore return $this->position >= $this->length;
120 115 kore }
121 kore
122 kore /**
123 kore * Seek to a defined position in the string
124 kore *
125 kore * @param int $offset
126 kore * @param int $whence
127 kore * @return bool
128 kore */
129 kore public function stream_seek($offset, $whence)
130 kore {
131 kore switch ( $whence ) {
132 kore case SEEK_SET:
133 133 kore if ( ( $offset < $this->length ) &&
134 115 kore ( $offset >= 0 ) )
135 kore {
136 kore $this->position = $offset;
137 kore return true;
138 kore }
139 kore else
140 kore {
141 kore return false;
142 kore }
143 kore break;
144 kore
145 kore case SEEK_CUR:
146 kore if ( $offset >= 0 )
147 kore {
148 kore $this->position += $offset;
149 kore return true;
150 kore }
151 kore else
152 kore {
153 kore return false;
154 kore }
155 kore break;
156 kore
157 kore case SEEK_END:
158 133 kore if ( ( $this->length + $offset ) >= 0 )
159 115 kore {
160 133 kore $this->position = $this->length + $offset;
161 115 kore return true;
162 kore }
163 kore else
164 kore {
165 kore return false;
166 kore }
167 kore break;
168 kore
169 kore default:
170 kore return false;
171 kore }
172 kore }
173 kore
174 kore /**
175 kore * Returns information about the stream
176 kore *
177 kore * @return void
178 kore */
179 kore public function stream_stat()
180 kore {
181 kore return array();
182 kore }
183 kore }
184 kore