From 573f488c26944e8adee278bf5d465da56ff37402 Mon Sep 17 00:00:00 2001 From: svollbehr Date: Fri, 20 Nov 2009 17:32:20 +0000 Subject: [PATCH] Add support for float/double git-svn-id: http://php-reader.googlecode.com/svn/branches/zend@157 51a70ab9-7547-0410-9469-37e369ee0574 --- src/Zend/Io/Reader.php | 144 +++++++++++++++++++++++++++-------------- 1 file changed, 97 insertions(+), 47 deletions(-) diff --git a/src/Zend/Io/Reader.php b/src/Zend/Io/Reader.php index bd7622d..8170052 100644 --- a/src/Zend/Io/Reader.php +++ b/src/Zend/Io/Reader.php @@ -27,6 +27,7 @@ * @package Zend_Io * @author Sven Vollbehr * @author Ryan Butterfield + * @author Marc Bennewitz * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @version $Id$ @@ -208,8 +209,12 @@ class Zend_Io_Reader */ public final function readInt8() { - list(, $int) = unpack('c*', $this->read(1)); - return $int; + $ord = ord($this->read(1)); + if ($ord > 127) { + return -$ord - 2 * (128 - $ord); + } else { + return $ord; + } } /** @@ -221,8 +226,7 @@ class Zend_Io_Reader */ public final function readUInt8() { - list(, $int) = unpack('C*', $this->read(1)); - return $int; + return ord($this->read(1)); } /** @@ -423,49 +427,95 @@ class Zend_Io_Reader ($lohi * (0xffff+1) + $lolo); } -// TODO: How to determine float size? -// -// /** -// * Returns machine endian ordered binary data as a floating point number. -// * -// * @param string $value The binary data string. -// * @return float -// */ -// private function _fromFloat($value) -// { -// list(, $float) = unpack('f*', $value); -// return $float; -// } -// -// /** -// * Reads machine dependent size of bytes from the stream and returns -// * little-endian ordered binary data as a floating point number. -// * -// * @return float -// * @throws Zend_Io_Exception if an I/O error occurs -// */ -// public final function readFloatLE() -// { -// if ($this->_isBigEndian()) -// return $this->_fromFloat(strrev($this->read(?))); -// else -// return $this->_fromFloat($this->read(?)); -// } -// -// /** -// * Reads machine dependent size of bytes from the stream and returns -// * big-endian ordered binary data as a float point number. -// * -// * @return float -// * @throws Zend_Io_Exception if an I/O error occurs -// */ -// public final function readFloatBE() -// { -// if ($this->_isLittleEndian()) -// return $this->_fromFloat(strrev($this->read(?))); -// else -// return $this->_fromFloat($this->read(?)); -// } + /** + * Returns machine endian ordered binary data as a 32-bit floating point + * number as defined by IEEE 754. + * + * @param string $value The binary data string. + * @return float + */ + private function _fromFloat($value) + { + list(, $float) = unpack('f', $value); + return $float; + } + + /** + * Reads 4 bytes from the stream and returns little-endian ordered binary + * data as a 32-bit float point number as defined by IEEE 754. + * + * @return float + * @throws Zend_Io_Exception if an I/O error occurs + */ + public final function readFloatLE() + { + if ($this->_isBigEndian()) { + return $this->_fromFloat(strrev($this->read(4))); + } else { + return $this->_fromFloat($this->read(4)); + } + } + + /** + * Reads 4 bytes from the stream and returns big-endian ordered binary data + * as a 32-bit float point number as defined by IEEE 754. + * + * @return float + * @throws Zend_Io_Exception if an I/O error occurs + */ + public final function readFloatBE() + { + if ($this->_isLittleEndian()) { + return $this->_fromFloat(strrev($this->read(4))); + } else { + return $this->_fromFloat($this->read(4)); + } + } + + /** + * Returns machine endian ordered binary data as a 64-bit floating point + * number as defined by IEEE754. + * + * @param string $value The binary data string. + * @return float + */ + private function _fromDouble($value) + { + list(, $double) = unpack('d', $value); + return $double; + } + + /** + * Reads 8 bytes from the stream and returns little-endian ordered binary + * data as a 64-bit floating point number as defined by IEEE 754. + * + * @return float + * @throws Zend_Io_Exception if an I/O error occurs + */ + public final function readDoubleLE() + { + if ($this->_isBigEndian()) { + return $this->_fromDouble(strrev($this->read(8))); + } else { + return $this->_fromDouble($this->read(8)); + } + } + + /** + * Reads 8 bytes from the stream and returns big-endian ordered binary data + * as a 64-bit float point number as defined by IEEE 754. + * + * @return float + * @throws Zend_Io_Exception if an I/O error occurs + */ + public final function readDoubleBE() + { + if ($this->_isLittleEndian()) { + return $this->_fromDouble(strrev($this->read(8))); + } else { + return $this->_fromDouble($this->read(8)); + } + } /** * Reads length amount of bytes from the stream and returns