diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-03-10 20:44:25 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-03-10 20:44:25 +0000 |
commit | fa127ac5a7b380d41d98eb789e0d51d061c6dea3 (patch) | |
tree | b6358f596dd3d063aef323e30412ea979b992bde | |
parent | b010c4b037f8a8f3760cf20c0f8b907fe11edd99 (diff) | |
download | libgats-fa127ac5a7b380d41d98eb789e0d51d061c6dea3.tar.gz libgats-fa127ac5a7b380d41d98eb789e0d51d061c6dea3.tar.bz2 libgats-fa127ac5a7b380d41d98eb789e0d51d061c6dea3.tar.xz libgats-fa127ac5a7b380d41d98eb789e0d51d061c6dea3.zip |
PHP Gats now understands the new float format.
-rw-r--r-- | php/phpgats.php | 95 |
1 files 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 | |||
213 | 213 | ||
214 | function encode() | 214 | function encode() |
215 | { | 215 | { |
216 | throw "Encoding Floats in phpgats doesn't yet work..."; | 216 | if( $this->elem == 0.0 ) |
217 | { | ||
218 | return 'Fz'; | ||
219 | } | ||
220 | else if( is_nan( $this->elem ) ) | ||
221 | { | ||
222 | return 'Fn'; | ||
223 | } | ||
224 | else if( is_infinite( $this->elem ) ) | ||
225 | { | ||
226 | if( $this->elem < 0.0 ) | ||
227 | return 'FI'; | ||
228 | else | ||
229 | return 'Fi'; | ||
230 | } | ||
231 | else | ||
232 | { | ||
233 | $e = $this->elem; | ||
234 | $neg = false; | ||
235 | if( $e < 0.0 ) | ||
236 | { | ||
237 | $e = -$e; | ||
238 | $neg = true; | ||
239 | } | ||
240 | $iScale = (int)(floor(log($e)/log(256.0))); | ||
241 | $e = $e/pow(256.0, $iScale); | ||
242 | $s = chr((int)($e)); | ||
243 | $e = $e - (int)($e); | ||
244 | for( $j = 0; $j < 150 && $e > 0.0; $j++ ) | ||
245 | { | ||
246 | $e = $e * 256.0; | ||
247 | $s .= chr((int)($e)); | ||
248 | $e -= (int)($e); | ||
249 | } | ||
250 | $ilen = strlen($s); | ||
251 | if( $neg ) $ilen = -$ilen; | ||
252 | return "f" . phpgats_writeInt($ilen) . $s . | ||
253 | phpgats_writeInt($iScale); | ||
254 | } | ||
217 | } | 255 | } |
218 | 256 | ||
219 | function getStr() | 257 | function getStr() |
@@ -342,7 +380,31 @@ function phpgats_pI( $str_data, &$offset, $dbg ) | |||
342 | 380 | ||
343 | function phpgats_pF( $str_data, &$offset, $dbg ) | 381 | function phpgats_pF( $str_data, &$offset, $dbg ) |
344 | { | 382 | { |
345 | throw "The phpgats Float parsing code doesn't work yet..."; | 383 | $str_tmp = ""; |
384 | $iSize = gmp_intval(phpgats_readInt( $str_data, $offset )); | ||
385 | $neg = false; | ||
386 | if( $iSize < 0.0 ) | ||
387 | { | ||
388 | $iSize = -$iSize; | ||
389 | $neg = true; | ||
390 | } | ||
391 | $i=0; | ||
392 | $str_tmp = ""; | ||
393 | while( $i<$iSize ) | ||
394 | { | ||
395 | phpgats_pC( $str_data, $offset+1 ); | ||
396 | $str_tmp .= $str_data[$offset++]; | ||
397 | ++$i; | ||
398 | } | ||
399 | $iScale = gmp_intval(phpgats_readInt( $str_data, $offset )); | ||
400 | $e = 0.0; | ||
401 | for( $j = $iSize-1; $j > 0; $j-- ) | ||
402 | { | ||
403 | $e = ($e+ord($str_tmp[$j]))*.00390625; | ||
404 | } | ||
405 | $e = ($e+ord($str_tmp[0])) * pow( 256.0, $iScale ); | ||
406 | if( $neg ) $e = -$e; | ||
407 | return new phpgats_Float( $e ); | ||
346 | } | 408 | } |
347 | 409 | ||
348 | function phpgats_pL( $str_data, &$offset, $dbg ) | 410 | function phpgats_pL( $str_data, &$offset, $dbg ) |
@@ -407,6 +469,18 @@ function phpgats_pM( $str_data, &$offset, $dbg=0 ) | |||
407 | $obj = phpgats_pF( $str_data, $offset, $dbg ); | 469 | $obj = phpgats_pF( $str_data, $offset, $dbg ); |
408 | return $obj; | 470 | return $obj; |
409 | break; | 471 | break; |
472 | case 'F': | ||
473 | phpgats_pC( $str_data, $offset ); | ||
474 | switch( $str_data[$offset++] ) | ||
475 | { | ||
476 | case 'Z': return new phpgats_Float( -0.0 ); | ||
477 | case 'z': return new phpgats_Float( 0.0 ); | ||
478 | case 'N': return new phpgats_Float( -NAN ); | ||
479 | case 'n': return new phpgats_Float( NAN ); | ||
480 | case 'I': return new phpgats_Float( -INF ); | ||
481 | case 'i': return new phpgats_Float( INF ); | ||
482 | } | ||
483 | break; | ||
410 | case '1': | 484 | case '1': |
411 | //echo "true\n"; | 485 | //echo "true\n"; |
412 | return new phpgats_Boolean( true ); | 486 | return new phpgats_Boolean( true ); |
@@ -458,8 +532,16 @@ function phpgats_writeGats( $elem ) | |||
458 | $str_out = "\x01" . pack( "N", strlen($str_out)+5 ) . $str_out; | 532 | $str_out = "\x01" . pack( "N", strlen($str_out)+5 ) . $str_out; |
459 | return $str_out; | 533 | return $str_out; |
460 | } | 534 | } |
461 | 535 | /* | |
462 | /*$l = new phpgats_List(); | 536 | $l = new phpgats_List(); |
537 | $l->append( new phpgats_Float( 123000000000.0 ) ); | ||
538 | $l->append( new phpgats_Float( -0.000087687 ) ); | ||
539 | $l->append( new phpgats_Float( -INF ) ); | ||
540 | $l->append( new phpgats_Float( INF ) ); | ||
541 | $l->append( new phpgats_Float( -NAN ) ); | ||
542 | $l->append( new phpgats_Float( NAN ) ); | ||
543 | $l->append( new phpgats_Float( -0.0 ) ); | ||
544 | $l->append( new phpgats_Float( 0.0 ) ); | ||
463 | $l->append( new phpgats_Integer("27") ); | 545 | $l->append( new phpgats_Integer("27") ); |
464 | $l->append( new phpgats_Boolean(true) ); | 546 | $l->append( new phpgats_Boolean(true) ); |
465 | $l->append( new phpgats_String("testing your monkey") ); | 547 | $l->append( new phpgats_String("testing your monkey") ); |
@@ -473,7 +555,10 @@ $theStr = phpgats_writeGats($d); | |||
473 | fwrite( $f, $theStr ); | 555 | fwrite( $f, $theStr ); |
474 | fclose( $f ); | 556 | fclose( $f ); |
475 | 557 | ||
476 | $obj = phpgats_parseGats( $theStr );*/ | 558 | $obj = phpgats_parseGats( $theStr ); |
559 | |||
560 | print_r( $obj ); | ||
561 | */ | ||
477 | 562 | ||
478 | ?> | 563 | ?> |
479 | 564 | ||