From fa127ac5a7b380d41d98eb789e0d51d061c6dea3 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 10 Mar 2011 20:44:25 +0000 Subject: PHP Gats now understands the new float format. --- php/phpgats.php | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 90 insertions(+), 5 deletions(-) diff --git a/php/phpgats.php b/php/phpgats.php index c2b7857..5a387b7 100644 --- a/php/phpgats.php +++ b/php/phpgats.php @@ -213,7 +213,45 @@ class phpgats_Float extends phpgats_Element function encode() { - throw "Encoding Floats in phpgats doesn't yet work..."; + if( $this->elem == 0.0 ) + { + return 'Fz'; + } + else if( is_nan( $this->elem ) ) + { + return 'Fn'; + } + else if( is_infinite( $this->elem ) ) + { + if( $this->elem < 0.0 ) + return 'FI'; + else + return 'Fi'; + } + else + { + $e = $this->elem; + $neg = false; + if( $e < 0.0 ) + { + $e = -$e; + $neg = true; + } + $iScale = (int)(floor(log($e)/log(256.0))); + $e = $e/pow(256.0, $iScale); + $s = chr((int)($e)); + $e = $e - (int)($e); + for( $j = 0; $j < 150 && $e > 0.0; $j++ ) + { + $e = $e * 256.0; + $s .= chr((int)($e)); + $e -= (int)($e); + } + $ilen = strlen($s); + if( $neg ) $ilen = -$ilen; + return "f" . phpgats_writeInt($ilen) . $s . + phpgats_writeInt($iScale); + } } function getStr() @@ -342,7 +380,31 @@ function phpgats_pI( $str_data, &$offset, $dbg ) function phpgats_pF( $str_data, &$offset, $dbg ) { - throw "The phpgats Float parsing code doesn't work yet..."; + $str_tmp = ""; + $iSize = gmp_intval(phpgats_readInt( $str_data, $offset )); + $neg = false; + if( $iSize < 0.0 ) + { + $iSize = -$iSize; + $neg = true; + } + $i=0; + $str_tmp = ""; + while( $i<$iSize ) + { + phpgats_pC( $str_data, $offset+1 ); + $str_tmp .= $str_data[$offset++]; + ++$i; + } + $iScale = gmp_intval(phpgats_readInt( $str_data, $offset )); + $e = 0.0; + for( $j = $iSize-1; $j > 0; $j-- ) + { + $e = ($e+ord($str_tmp[$j]))*.00390625; + } + $e = ($e+ord($str_tmp[0])) * pow( 256.0, $iScale ); + if( $neg ) $e = -$e; + return new phpgats_Float( $e ); } function phpgats_pL( $str_data, &$offset, $dbg ) @@ -407,6 +469,18 @@ function phpgats_pM( $str_data, &$offset, $dbg=0 ) $obj = phpgats_pF( $str_data, $offset, $dbg ); return $obj; break; + case 'F': + phpgats_pC( $str_data, $offset ); + switch( $str_data[$offset++] ) + { + case 'Z': return new phpgats_Float( -0.0 ); + case 'z': return new phpgats_Float( 0.0 ); + case 'N': return new phpgats_Float( -NAN ); + case 'n': return new phpgats_Float( NAN ); + case 'I': return new phpgats_Float( -INF ); + case 'i': return new phpgats_Float( INF ); + } + break; case '1': //echo "true\n"; return new phpgats_Boolean( true ); @@ -458,8 +532,16 @@ function phpgats_writeGats( $elem ) $str_out = "\x01" . pack( "N", strlen($str_out)+5 ) . $str_out; return $str_out; } - -/*$l = new phpgats_List(); +/* +$l = new phpgats_List(); +$l->append( new phpgats_Float( 123000000000.0 ) ); +$l->append( new phpgats_Float( -0.000087687 ) ); +$l->append( new phpgats_Float( -INF ) ); +$l->append( new phpgats_Float( INF ) ); +$l->append( new phpgats_Float( -NAN ) ); +$l->append( new phpgats_Float( NAN ) ); +$l->append( new phpgats_Float( -0.0 ) ); +$l->append( new phpgats_Float( 0.0 ) ); $l->append( new phpgats_Integer("27") ); $l->append( new phpgats_Boolean(true) ); $l->append( new phpgats_String("testing your monkey") ); @@ -473,7 +555,10 @@ $theStr = phpgats_writeGats($d); fwrite( $f, $theStr ); fclose( $f ); -$obj = phpgats_parseGats( $theStr );*/ +$obj = phpgats_parseGats( $theStr ); + +print_r( $obj ); + */ ?> -- cgit v1.2.3