TYPO3  7.6
NgCharacterStream.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
17 {
23  private $_charReader;
24 
31 
37  private $_charset;
38 
44  private $_datas = '';
45 
51  private $_datasSize = 0;
52 
58  private $_map;
59 
65  private $_mapType = 0;
66 
72  private $_charCount = 0;
73 
79  private $_currentPos = 0;
80 
87  public function __construct(Swift_CharacterReaderFactory $factory, $charset)
88  {
89  $this->setCharacterReaderFactory($factory);
90  $this->setCharacterSet($charset);
91  }
92 
93  /* -- Changing parameters of the stream -- */
94 
100  public function setCharacterSet($charset)
101  {
102  $this->_charset = $charset;
103  $this->_charReader = null;
104  $this->_mapType = 0;
105  }
106 
113  {
114  $this->_charReaderFactory = $factory;
115  }
116 
120  public function flushContents()
121  {
122  $this->_datas = null;
123  $this->_map = null;
124  $this->_charCount = 0;
125  $this->_currentPos = 0;
126  $this->_datasSize = 0;
127  }
128 
135  {
136  $this->flushContents();
137  $blocks = 512;
138  $os->setReadPointer(0);
139  while (false !== ($read = $os->read($blocks))) {
140  $this->write($read);
141  }
142  }
143 
149  public function importString($string)
150  {
151  $this->flushContents();
152  $this->write($string);
153  }
154 
162  public function read($length)
163  {
164  if ($this->_currentPos >= $this->_charCount) {
165  return false;
166  }
167  $ret = false;
168  $length = ($this->_currentPos + $length > $this->_charCount)
169  ? $this->_charCount - $this->_currentPos
170  : $length;
171  switch ($this->_mapType) {
173  $len = $length * $this->_map;
174  $ret = substr($this->_datas,
175  $this->_currentPos * $this->_map,
176  $len);
177  $this->_currentPos += $length;
178  break;
179 
181  $end = $this->_currentPos + $length;
182  $end = $end > $this->_charCount
183  ? $this->_charCount
184  : $end;
185  $ret = '';
186  for (; $this->_currentPos < $length; ++$this->_currentPos) {
187  if (isset($this->_map[$this->_currentPos])) {
188  $ret .= '?';
189  } else {
190  $ret .= $this->_datas[$this->_currentPos];
191  }
192  }
193  break;
194 
196  $end = $this->_currentPos + $length;
197  $end = $end > $this->_charCount
198  ? $this->_charCount
199  : $end;
200  $ret = '';
201  $start = 0;
202  if ($this->_currentPos > 0) {
203  $start = $this->_map['p'][$this->_currentPos - 1];
204  }
205  $to = $start;
206  for (; $this->_currentPos < $end; ++$this->_currentPos) {
207  if (isset($this->_map['i'][$this->_currentPos])) {
208  $ret .= substr($this->_datas, $start, $to - $start).'?';
209  $start = $this->_map['p'][$this->_currentPos];
210  } else {
211  $to = $this->_map['p'][$this->_currentPos];
212  }
213  }
214  $ret .= substr($this->_datas, $start, $to - $start);
215  break;
216  }
217 
218  return $ret;
219  }
220 
228  public function readBytes($length)
229  {
230  $read = $this->read($length);
231  if ($read !== false) {
232  $ret = array_map('ord', str_split($read, 1));
233 
234  return $ret;
235  }
236 
237  return false;
238  }
239 
245  public function setPointer($charOffset)
246  {
247  if ($this->_charCount < $charOffset) {
248  $charOffset = $this->_charCount;
249  }
250  $this->_currentPos = $charOffset;
251  }
252 
258  public function write($chars)
259  {
260  if (!isset($this->_charReader)) {
261  $this->_charReader = $this->_charReaderFactory->getReaderFor(
262  $this->_charset);
263  $this->_map = array();
264  $this->_mapType = $this->_charReader->getMapType();
265  }
266  $ignored = '';
267  $this->_datas .= $chars;
268  $this->_charCount += $this->_charReader->getCharPositions(substr($this->_datas, $this->_datasSize), $this->_datasSize, $this->_map, $ignored);
269  if ($ignored !== false) {
270  $this->_datasSize = strlen($this->_datas) - strlen($ignored);
271  } else {
272  $this->_datasSize = strlen($this->_datas);
273  }
274  }
275 }