-
+ 0F433F5F598E0DC9C76B53816238FA812ADE6AD002D161FC8876AD5777A86E9376F3C51D97F0DD98D3E20A2EAFE38030A261695A72ACCB6E21629140F8FF7BDD
mp-wp/wp-includes/streams.php
(0 . 0)(1 . 191)
151376 <?php
151377 /**
151378 * PHP-Gettext External Library: StreamReader classes
151379 *
151380 * @package External
151381 * @subpackage PHP-gettext
151382 *
151383 * @internal
151384 Copyright (c) 2003, 2005 Danilo Segan <danilo@kvota.net>.
151385
151386 This file is part of PHP-gettext.
151387
151388 PHP-gettext is free software; you can redistribute it and/or modify
151389 it under the terms of the GNU General Public License as published by
151390 the Free Software Foundation; either version 2 of the License, or
151391 (at your option) any later version.
151392
151393 PHP-gettext is distributed in the hope that it will be useful,
151394 but WITHOUT ANY WARRANTY; without even the implied warranty of
151395 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
151396 GNU General Public License for more details.
151397
151398 You should have received a copy of the GNU General Public License
151399 along with PHP-gettext; if not, write to the Free Software
151400 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
151401
151402 */
151403
151404
151405 // Simple class to wrap file streams, string streams, etc.
151406 // seek is essential, and it should be byte stream
151407 class StreamReader {
151408 // should return a string [FIXME: perhaps return array of bytes?]
151409 function read($bytes) {
151410 return false;
151411 }
151412
151413 // should return new position
151414 function seekto($position) {
151415 return false;
151416 }
151417
151418 // returns current position
151419 function currentpos() {
151420 return false;
151421 }
151422
151423 // returns length of entire stream (limit for seekto()s)
151424 function length() {
151425 return false;
151426 }
151427 }
151428
151429 class StringReader {
151430 var $_pos;
151431 var $_str;
151432
151433 function StringReader($str='') {
151434 $this->_str = $str;
151435 $this->_pos = 0;
151436 // If string functions are overloaded, we need to use the mb versions
151437 $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
151438 }
151439
151440 function _substr($string, $start, $length) {
151441 if ($this->is_overloaded) {
151442 return mb_substr($string,$start,$length,'ascii');
151443 } else {
151444 return substr($string,$start,$length);
151445 }
151446 }
151447
151448 function _strlen($string) {
151449 if ($this->is_overloaded) {
151450 return mb_strlen($string,'ascii');
151451 } else {
151452 return strlen($string);
151453 }
151454 }
151455
151456 function read($bytes) {
151457 $data = $this->_substr($this->_str, $this->_pos, $bytes);
151458 $this->_pos += $bytes;
151459 if ($this->_strlen($this->_str)<$this->_pos)
151460 $this->_pos = $this->_strlen($this->_str);
151461
151462 return $data;
151463 }
151464
151465 function seekto($pos) {
151466 $this->_pos = $pos;
151467 if ($this->_strlen($this->_str)<$this->_pos)
151468 $this->_pos = $this->_strlen($this->_str);
151469 return $this->_pos;
151470 }
151471
151472 function currentpos() {
151473 return $this->_pos;
151474 }
151475
151476 function length() {
151477 return $this->_strlen($this->_str);
151478 }
151479 }
151480
151481
151482 class FileReader {
151483 var $_pos;
151484 var $_fd;
151485 var $_length;
151486
151487 function FileReader($filename) {
151488 if (file_exists($filename)) {
151489
151490 $this->_length=filesize($filename);
151491 $this->_pos = 0;
151492 $this->_fd = fopen($filename,'rb');
151493 if (!$this->_fd) {
151494 $this->error = 3; // Cannot read file, probably permissions
151495 return false;
151496 }
151497 } else {
151498 $this->error = 2; // File doesn't exist
151499 return false;
151500 }
151501 }
151502
151503 function read($bytes) {
151504 if ($bytes) {
151505 fseek($this->_fd, $this->_pos);
151506
151507 // PHP 5.1.1 does not read more than 8192 bytes in one fread()
151508 // the discussions at PHP Bugs suggest it's the intended behaviour
151509 while ($bytes > 0) {
151510 $chunk = fread($this->_fd, $bytes);
151511 $data .= $chunk;
151512 $bytes -= strlen($chunk);
151513 }
151514 $this->_pos = ftell($this->_fd);
151515
151516 return $data;
151517 } else return '';
151518 }
151519
151520 function seekto($pos) {
151521 fseek($this->_fd, $pos);
151522 $this->_pos = ftell($this->_fd);
151523 return $this->_pos;
151524 }
151525
151526 function currentpos() {
151527 return $this->_pos;
151528 }
151529
151530 function length() {
151531 return $this->_length;
151532 }
151533
151534 function close() {
151535 fclose($this->_fd);
151536 }
151537
151538 }
151539
151540 // Preloads entire file in memory first, then creates a StringReader
151541 // over it (it assumes knowledge of StringReader internals)
151542 class CachedFileReader extends StringReader {
151543 function CachedFileReader($filename) {
151544 parent::StringReader();
151545
151546 if (file_exists($filename)) {
151547
151548 $length=filesize($filename);
151549 $fd = fopen($filename,'rb');
151550
151551 if (!$fd) {
151552 $this->error = 3; // Cannot read file, probably permissions
151553 return false;
151554 }
151555 $this->_str = fread($fd, $length);
151556 fclose($fd);
151557
151558 } else {
151559 $this->error = 2; // File doesn't exist
151560 return false;
151561 }
151562 }
151563 }
151564
151565
151566 ?>