lot of various code simplifications, including removing useless settings,
[platal.git] / classes / VarStream.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
50a40a33 3 * Copyright (C) 2003-2006 Polytechnique.org *
0337d704 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
0337d704 22class VarStream
23{
24 // {{{ properties
63528107 25
0337d704 26 // Stream handler to read from global variables
27 var $varname;
28 var $position;
29
30 // }}}
31 // {{{ stream_open
32
33 function stream_open($path, $mode, $options, &$opened_path)
34 {
35 $url = parse_url($path);
36 $this->varname = $url['host'];
37 $this->position = 0;
38 if (!isset($GLOBALS[$this->varname]))
39 {
40 trigger_error('Global variable '.$this->varname.' does not exist', E_USER_WARNING);
41 return false;
42 }
43 return true;
44 }
45
46 // }}}
47 // {{{ stream_close
48
49 function stream_close()
50 {
51 }
63528107 52
0337d704 53 // }}}
54 // {{{ stream_read
55
56 function stream_read($count)
57 {
58 $ret = substr($GLOBALS[$this->varname], $this->position, $count);
59 $this->position += strlen($ret);
60 return $ret;
61 }
62
63 // }}}
64 // {{{ stream_write
65
66 function stream_write($data)
67 {
68 $len = strlen($data);
69 if ($len > $this->position + strlen($GLOBALS[$this->varname])) {
70 str_pad($GLOBALS[$this->varname], $len);
71 }
72
73 $GLOBALS[$this->varname] = substr_replace($GLOBALS[$this->varname], $data, $this->position, $len);
74 $this->position += $len;
75 }
63528107 76
0337d704 77 // }}}
78 // {{{ stream_eof
79
80 function stream_eof()
81 {
82 return $this->position >= strlen($GLOBALS[$this->varname]);
83 }
84
85 // }}}
86 // {{{ stream_tell
87
88 function stream_tell()
89 {
90 return $this->position;
91 }
92
93 // }}}
94 // {{{ stream_seek
95
96 function stream_seek($offs, $whence)
97 {
98 switch ($whence) {
99 case SEEK_SET:
100 $final = $offs;
101 break;
102
103 case SEEK_CUR:
104 $final += $offs;
105 break;
106
107 case SEEK_END:
108 $final = strlen($GLOBALS[$this->varname]) + $offs;
109 break;
110 }
111
112 if ($final < 0) {
113 return -1;
114 }
115 $this->position = $final;
116 return 0;
117 }
118
119 // }}}
120 // {{{ stream_flush
121
122 function stream_flush()
123 {
124 }
63528107 125
0337d704 126 // }}}
127}
128
0337d704 129stream_wrapper_register('var','VarStream');
130
0337d704 131?>