XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
NgCharacterStream.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  CharacterStream implementation using an array in Swift Mailer.
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 3 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, see <http://www.gnu.org/licenses/>.
18 
19  */
20 
21 //@require 'Swift/CharacterStream.php';
22 //@require 'Swift/OutputByteStream.php';
23 
24 
32 Class Swift_CharacterStream_NgCharacterStream
33  implements Swift_CharacterStream
34 {
35 
41  private $_charReader;
42 
49 
55  private $_charset;
56 
62  private $_datas = "";
63 
69  private $_datasSize = 0;
70 
76  private $_map;
77 
83  private $_mapType = 0;
84 
90  private $_charCount = 0;
91 
97  private $_currentPos = 0;
98 
105  public function __construct(Swift_CharacterReaderFactory $factory,
106  $charset)
107  {
108  $this->setCharacterReaderFactory($factory);
109  $this->setCharacterSet($charset);
110  }
111 
112  /* -- Changing parameters of the stream -- */
113 
118  public function setCharacterSet($charset)
119  {
120  $this->_charset = $charset;
121  $this->_charReader = null;
122  $this->_mapType = 0;
123  }
124 
129  public function setCharacterReaderFactory(
131  {
132  $this->_charReaderFactory = $factory;
133  }
134 
139  public function flushContents()
140  {
141  $this->_datas = null;
142  $this->_map = null;
143  $this->_charCount = 0;
144  $this->_currentPos = 0;
145  $this->_datasSize = 0;
146  }
147 
154  {
155  $this->flushContents();
156  $blocks=512;
157  $os->setReadPointer(0);
158  while(false!==($read = $os->read($blocks)))
159  $this->write($read);
160  }
161 
167  public function importString($string)
168  {
169  $this->flushContents();
170  $this->write($string);
171  }
172 
179  public function read($length)
180  {
181  if ($this->_currentPos>=$this->_charCount)
182  {
183  return false;
184  }
185  $ret=false;
186  $length = ($this->_currentPos+$length > $this->_charCount)
187  ? $this->_charCount - $this->_currentPos
188  : $length;
189  switch ($this->_mapType)
190  {
192  $len = $length*$this->_map;
193  $ret = substr($this->_datas,
194  $this->_currentPos * $this->_map,
195  $len);
196  $this->_currentPos += $length;
197  break;
198 
200  $end = $this->_currentPos + $length;
201  $end = $end > $this->_charCount
202  ?$this->_charCount
203  :$end;
204  $ret = '';
205  for (; $this->_currentPos < $length; ++$this->_currentPos)
206  {
207  if (isset ($this->_map[$this->_currentPos]))
208  {
209  $ret .= '?';
210  }
211  else
212  {
213  $ret .= $this->_datas[$this->_currentPos];
214  }
215  }
216  break;
217 
219  $end = $this->_currentPos + $length;
220  $end = $end > $this->_charCount
221  ?$this->_charCount
222  :$end;
223  $ret = '';
224  $start = 0;
225  if ($this->_currentPos>0)
226  {
227  $start = $this->_map['p'][$this->_currentPos-1];
228  }
229  $to = $start;
230  for (; $this->_currentPos < $end; ++$this->_currentPos)
231  {
232  if (isset($this->_map['i'][$this->_currentPos])) {
233  $ret .= substr($this->_datas, $start, $to - $start).'?';
234  $start = $this->_map['p'][$this->_currentPos];
235  } else {
236  $to = $this->_map['p'][$this->_currentPos];
237  }
238  }
239  $ret .= substr($this->_datas, $start, $to - $start);
240  break;
241  }
242  return $ret;
243  }
244 
251  public function readBytes($length)
252  {
253  $read=$this->read($length);
254  if ($read!==false)
255  {
256  $ret = array_map('ord', str_split($read, 1));
257  return $ret;
258  }
259  return false;
260  }
261 
267  public function setPointer($charOffset)
268  {
269  if ($this->_charCount<$charOffset){
270  $charOffset=$this->_charCount;
271  }
272  $this->_currentPos = $charOffset;
273  }
274 
280  public function write($chars)
281  {
282  if (!isset($this->_charReader))
283  {
284  $this->_charReader = $this->_charReaderFactory->getReaderFor(
285  $this->_charset);
286  $this->_map = array();
287  $this->_mapType = $this->_charReader->getMapType();
288  }
289  $ignored='';
290  $this->_datas .= $chars;
291  $this->_charCount += $this->_charReader->getCharPositions(substr($this->_datas, $this->_datasSize), $this->_datasSize, $this->_map, $ignored);
292  if ($ignored!==false) {
293  $this->_datasSize=strlen($this->_datas)-strlen($ignored);
294  }
295  else
296  {
297  $this->_datasSize=strlen($this->_datas);
298  }
299  }
300 }