Add support for float/double
git-svn-id: http://php-reader.googlecode.com/svn/branches/zend@157 51a70ab9-7547-0410-9469-37e369ee0574
This commit is contained in:
@@ -27,6 +27,7 @@
|
|||||||
* @package Zend_Io
|
* @package Zend_Io
|
||||||
* @author Sven Vollbehr <sven@vollbehr.eu>
|
* @author Sven Vollbehr <sven@vollbehr.eu>
|
||||||
* @author Ryan Butterfield <buttza@gmail.com>
|
* @author Ryan Butterfield <buttza@gmail.com>
|
||||||
|
* @author Marc Bennewitz <marc-bennewitz@arcor.de>
|
||||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
@@ -208,8 +209,12 @@ class Zend_Io_Reader
|
|||||||
*/
|
*/
|
||||||
public final function readInt8()
|
public final function readInt8()
|
||||||
{
|
{
|
||||||
list(, $int) = unpack('c*', $this->read(1));
|
$ord = ord($this->read(1));
|
||||||
return $int;
|
if ($ord > 127) {
|
||||||
|
return -$ord - 2 * (128 - $ord);
|
||||||
|
} else {
|
||||||
|
return $ord;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -221,8 +226,7 @@ class Zend_Io_Reader
|
|||||||
*/
|
*/
|
||||||
public final function readUInt8()
|
public final function readUInt8()
|
||||||
{
|
{
|
||||||
list(, $int) = unpack('C*', $this->read(1));
|
return ord($this->read(1));
|
||||||
return $int;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -423,49 +427,95 @@ class Zend_Io_Reader
|
|||||||
($lohi * (0xffff+1) + $lolo);
|
($lohi * (0xffff+1) + $lolo);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: How to determine float size?
|
/**
|
||||||
//
|
* Returns machine endian ordered binary data as a 32-bit floating point
|
||||||
// /**
|
* number as defined by IEEE 754.
|
||||||
// * Returns machine endian ordered binary data as a floating point number.
|
*
|
||||||
// *
|
* @param string $value The binary data string.
|
||||||
// * @param string $value The binary data string.
|
* @return float
|
||||||
// * @return float
|
*/
|
||||||
// */
|
private function _fromFloat($value)
|
||||||
// private function _fromFloat($value)
|
{
|
||||||
// {
|
list(, $float) = unpack('f', $value);
|
||||||
// list(, $float) = unpack('f*', $value);
|
return $float;
|
||||||
// return $float;
|
}
|
||||||
// }
|
|
||||||
//
|
/**
|
||||||
// /**
|
* Reads 4 bytes from the stream and returns little-endian ordered binary
|
||||||
// * Reads machine dependent size of bytes from the stream and returns
|
* data as a 32-bit float point number as defined by IEEE 754.
|
||||||
// * little-endian ordered binary data as a floating point number.
|
*
|
||||||
// *
|
* @return float
|
||||||
// * @return float
|
* @throws Zend_Io_Exception if an I/O error occurs
|
||||||
// * @throws Zend_Io_Exception if an I/O error occurs
|
*/
|
||||||
// */
|
public final function readFloatLE()
|
||||||
// public final function readFloatLE()
|
{
|
||||||
// {
|
if ($this->_isBigEndian()) {
|
||||||
// if ($this->_isBigEndian())
|
return $this->_fromFloat(strrev($this->read(4)));
|
||||||
// return $this->_fromFloat(strrev($this->read(?)));
|
} else {
|
||||||
// else
|
return $this->_fromFloat($this->read(4));
|
||||||
// return $this->_fromFloat($this->read(?));
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// /**
|
/**
|
||||||
// * Reads machine dependent size of bytes from the stream and returns
|
* Reads 4 bytes from the stream and returns big-endian ordered binary data
|
||||||
// * big-endian ordered binary data as a float point number.
|
* as a 32-bit float point number as defined by IEEE 754.
|
||||||
// *
|
*
|
||||||
// * @return float
|
* @return float
|
||||||
// * @throws Zend_Io_Exception if an I/O error occurs
|
* @throws Zend_Io_Exception if an I/O error occurs
|
||||||
// */
|
*/
|
||||||
// public final function readFloatBE()
|
public final function readFloatBE()
|
||||||
// {
|
{
|
||||||
// if ($this->_isLittleEndian())
|
if ($this->_isLittleEndian()) {
|
||||||
// return $this->_fromFloat(strrev($this->read(?)));
|
return $this->_fromFloat(strrev($this->read(4)));
|
||||||
// else
|
} else {
|
||||||
// return $this->_fromFloat($this->read(?));
|
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 <var>length</var> amount of bytes from the stream and returns
|
* Reads <var>length</var> amount of bytes from the stream and returns
|
||||||
|
|||||||
Reference in New Issue
Block a user