aboutsummaryrefslogtreecommitdiff
path: root/php/phpgats.php
diff options
context:
space:
mode:
Diffstat (limited to 'php/phpgats.php')
-rw-r--r--php/phpgats.php231
1 files changed, 226 insertions, 5 deletions
diff --git a/php/phpgats.php b/php/phpgats.php
index 188dbc0..68ab82b 100644
--- a/php/phpgats.php
+++ b/php/phpgats.php
@@ -1,29 +1,67 @@
1<?php 1<?php
2 2
3/* 3/** @mainpage phpgats.php PHP Gats Library
4 * @function phpgats_parseGats( $str_data (string) ); 4 * @section Notes
5 * @returns gats element 5 *
6 * This library requires that php be built with the The GNU Multiple Precision Arithmetic Library (GMP)
7 *
8 * @section Usage
9 *
10 * function phpgats_parseGats( $str_data (string) );
11 * returns phpgats_Element
6 * 12 *
7 * @function phpgats_writeGats( $elem (phpgats_Element subclass) ); 13 * function phpgats_writeGats( $elem (phpgats_Element) );
8 * @returns binary encoded gats string 14 * returns binary encoded gats string
15 *
16 * @section Types
17 *
18 * phpgats_Element - Base Class for all phpgats types\n
19 * phpgats_Boolean\n
20 * phpgats_Dictionary\n
21 * phpgats_Float\n
22 * phpgats_Integer\n
23 * phpgats_List\n
24 * phpgats_String\n
9 */ 25 */
10 26
27/**
28 * The base class for all phpgats types
29 */
11class phpgats_Element 30class phpgats_Element
12{ 31{
32 /**
33 * Get the binary representation of the gats type
34 * @returns (string) binary gats data
35 */
13 function encode() 36 function encode()
14 { 37 {
15 return ""; 38 return "";
16 } 39 }
40
41 /**
42 * Get a string representation of this type
43 * @returns (string)
44 */
17 function getStr() 45 function getStr()
18 { 46 {
19 return ""; 47 return "";
20 } 48 }
49
50 /**
51 * Get the gats type of the current element
52 * @returns (string) one of: ("boolean"|"dictionary"|"float"|"integer"|"list"|"string")
53 */
21 function getGatsType() 54 function getGatsType()
22 { 55 {
23 return ""; 56 return "";
24 } 57 }
25} 58}
26 59
60/**
61 * Write a gats packed integer
62 * @param $iIn (either a string representing a number, a gmp_int, or an int) The integer to be converted
63 * @returns (string) packed int binary data
64 */
27function phpgats_writeInt( $iIn ) 65function phpgats_writeInt( $iIn )
28{ 66{
29 $ret = ""; 67 $ret = "";
@@ -58,6 +96,12 @@ function phpgats_writeInt( $iIn )
58 return $ret; 96 return $ret;
59} 97}
60 98
99/**
100 * Read a gats packed integer into a gmp_int
101 * @param $sIn (string) the input stream
102 * @param &$pos (integer) the current position in the input stream (will be modified)
103 * @returns (gmp_int) the integer
104 */
61function phpgats_readInt( $sIn, &$pos ) 105function phpgats_readInt( $sIn, &$pos )
62{ 106{
63 $neg = false; 107 $neg = false;
@@ -79,20 +123,35 @@ function phpgats_readInt( $sIn, &$pos )
79 return $iOut; 123 return $iOut;
80} 124}
81 125
126/**
127 * Boolean Gats type (true or false)
128 */
82class phpgats_Boolean extends phpgats_Element 129class phpgats_Boolean extends phpgats_Element
83{ 130{
84 public $elem = false; 131 public $elem = false;
85 132
133 /**
134 * Creates a phpgats_Boolean out of a php type
135 * @param $_elem (mixed) the php type to convert to a gats boolean
136 */
86 function __construct( $_elem ) 137 function __construct( $_elem )
87 { 138 {
88 $this->elem = ($_elem==true)?true:false; 139 $this->elem = ($_elem==true)?true:false;
89 } 140 }
90 141
142 /**
143 * Sets this phpgats_Boolean type based on php type $_elem
144 * @param $_elem (mixed) the php type to convert to a gats boolean
145 */
91 function set( $_elem ) 146 function set( $_elem )
92 { 147 {
93 $this->elem = ($_elem==true)?true:false; 148 $this->elem = ($_elem==true)?true:false;
94 } 149 }
95 150
151 /**
152 * Get the php type of this element
153 * @returns (bool)
154 */
96 function get() 155 function get()
97 { 156 {
98 return $this->elem; 157 return $this->elem;
@@ -108,26 +167,45 @@ class phpgats_Boolean extends phpgats_Element
108 return ($this->elem==true)?"true":"false"; 167 return ($this->elem==true)?"true":"false";
109 } 168 }
110 169
170 /**
171 * Get the gats type of the current element
172 * @returns "boolean"
173 */
111 function getGatsType() 174 function getGatsType()
112 { 175 {
113 return "boolean"; 176 return "boolean";
114 } 177 }
115} 178}
116 179
180/**
181 * String Gats type
182 */
117class phpgats_String extends phpgats_Element 183class phpgats_String extends phpgats_Element
118{ 184{
119 public $elem = ""; 185 public $elem = "";
120 186
187 /**
188 * Creates a phpgats_String out of a php type
189 * @param $_elem (mixed) the php type to convert to a gats string
190 */
121 function __construct( $_elem ) 191 function __construct( $_elem )
122 { 192 {
123 $this->elem = $_elem . ""; 193 $this->elem = $_elem . "";
124 } 194 }
125 195
196 /**
197 * Sets this phpgats_String type based on php type $_elem
198 * @param $_elem (mixed) the php type to convert to a gats string
199 */
126 function set( $_elem ) 200 function set( $_elem )
127 { 201 {
128 $this->elem = $_elem . ""; 202 $this->elem = $_elem . "";
129 } 203 }
130 204
205 /**
206 * Get the php type of this element
207 * @returns (string)
208 */
131 function get() 209 function get()
132 { 210 {
133 return $this->elem; 211 return $this->elem;
@@ -143,16 +221,27 @@ class phpgats_String extends phpgats_Element
143 return $this->get(); 221 return $this->get();
144 } 222 }
145 223
224 /**
225 * Get the gats type of the current element
226 * @returns "string"
227 */
146 function getGatsType() 228 function getGatsType()
147 { 229 {
148 return "string"; 230 return "string";
149 } 231 }
150} 232}
151 233
234/**
235 * Integer Gats type (gmp_int)
236 */
152class phpgats_Integer extends phpgats_Element 237class phpgats_Integer extends phpgats_Element
153{ 238{
154 public $elem = 0; 239 public $elem = 0;
155 240
241 /**
242 * Creates a phpgats_Integer out of a php type
243 * @param $_elem (mixed) the php type to convert to a gats integer
244 */
156 function __construct( $_elem ) 245 function __construct( $_elem )
157 { 246 {
158 if( $_elem === '' ) 247 if( $_elem === '' )
@@ -170,16 +259,28 @@ class phpgats_Integer extends phpgats_Element
170 getType($_elem) . "'."); 259 getType($_elem) . "'.");
171 } 260 }
172 261
262 /**
263 * Sets this phpgats_Integer type based on php type $_elem
264 * @param $_elem (mixed) the php type to convert to a gats integer
265 */
173 function set( $_elem ) 266 function set( $_elem )
174 { 267 {
175 $this->elem = gmp_init($_elem); 268 $this->elem = gmp_init($_elem);
176 } 269 }
177 270
271 /**
272 * Get the php type of this element
273 * @returns (int)
274 */
178 function get() 275 function get()
179 { 276 {
180 return gmp_intval($this->elem); 277 return gmp_intval($this->elem);
181 } 278 }
182 279
280 /**
281 * Get the php type of this element
282 * @returns (gmp_int)
283 */
183 function get64() 284 function get64()
184 { 285 {
185 return $this->elem; 286 return $this->elem;
@@ -195,26 +296,45 @@ class phpgats_Integer extends phpgats_Element
195 return gmp_strval($this->elem); 296 return gmp_strval($this->elem);
196 } 297 }
197 298
299 /**
300 * Get the gats type of the current element
301 * @returns "integer"
302 */
198 function getGatsType() 303 function getGatsType()
199 { 304 {
200 return "integer"; 305 return "integer";
201 } 306 }
202} 307}
203 308
309/**
310 * Float Gats type
311 */
204class phpgats_Float extends phpgats_Element 312class phpgats_Float extends phpgats_Element
205{ 313{
206 public $elem = 0; 314 public $elem = 0;
207 315
316 /**
317 * Creates a phpgats_Float out of a php type
318 * @param $_elem (mixed) the php type to convert to a gats float
319 */
208 function __construct( $_elem ) 320 function __construct( $_elem )
209 { 321 {
210 $this->elem = $_elem+0.0; 322 $this->elem = $_elem+0.0;
211 } 323 }
212 324
325 /**
326 * Sets this phpgats_Float type based on php type $_elem
327 * @param $_elem (mixed) the php type to convert to a gats float
328 */
213 function set( $_elem ) 329 function set( $_elem )
214 { 330 {
215 $this->elem = $_elem+0.0; 331 $this->elem = $_elem+0.0;
216 } 332 }
217 333
334 /**
335 * Get the php type of this element
336 * @returns (float)
337 */
218 function get() 338 function get()
219 { 339 {
220 return $this->elem; 340 return $this->elem;
@@ -268,16 +388,27 @@ class phpgats_Float extends phpgats_Element
268 return $this->elem+""; 388 return $this->elem+"";
269 } 389 }
270 390
391 /**
392 * Get the gats type of the current element
393 * @returns "float"
394 */
271 function getGatsType() 395 function getGatsType()
272 { 396 {
273 return "float"; 397 return "float";
274 } 398 }
275} 399}
276 400
401/**
402 * List Gats type
403 */
277class phpgats_List extends phpgats_Element 404class phpgats_List extends phpgats_Element
278{ 405{
279 public $elems = array(); 406 public $elems = array();
280 407
408 /**
409 * Append an element to this phpgats_List
410 * @param $_elem (phpgats_Element) the element to append
411 */
281 function append( $_elem ) 412 function append( $_elem )
282 { 413 {
283 if( !is_subclass_of( $_elem, "phpgats_Element" ) ) 414 if( !is_subclass_of( $_elem, "phpgats_Element" ) )
@@ -286,11 +417,19 @@ class phpgats_List extends phpgats_Element
286 array_push( $this->elems, $_elem ); 417 array_push( $this->elems, $_elem );
287 } 418 }
288 419
420 /**
421 * Returns the php array that is the core of this element
422 * @returns (array)
423 */
289 function get() 424 function get()
290 { 425 {
291 return $this->elems; 426 return $this->elems;
292 } 427 }
293 428
429 /**
430 * Returns the current size of this list
431 * @returns (int) the number of elements
432 */
294 function size() 433 function size()
295 { 434 {
296 return count($this->elems); 435 return count($this->elems);
@@ -307,16 +446,28 @@ class phpgats_List extends phpgats_Element
307 return $s_out; 446 return $s_out;
308 } 447 }
309 448
449 /**
450 * Get the gats type of the current element
451 * @returns "list"
452 */
310 function getGatsType() 453 function getGatsType()
311 { 454 {
312 return "list"; 455 return "list";
313 } 456 }
314} 457}
315 458
459/**
460 * Dictionary Gats type
461 */
316class phpgats_Dictionary extends phpgats_Element 462class phpgats_Dictionary extends phpgats_Element
317{ 463{
318 public $elems = array(); 464 public $elems = array();
319 465
466 /**
467 * Append an element to the phpgats_Dictionary
468 * @param $_name (string) the key under which to place this element
469 * @param $_val (phpgats_Element) the phpgats_Element to place in the dictionary
470 */
320 function append( $_name, $_val ) 471 function append( $_name, $_val )
321 { 472 {
322 if( !is_subclass_of( $_val, "phpgats_Element" ) || 473 if( !is_subclass_of( $_val, "phpgats_Element" ) ||
@@ -326,11 +477,19 @@ class phpgats_Dictionary extends phpgats_Element
326 $this->elems[$_name] = $_val; 477 $this->elems[$_name] = $_val;
327 } 478 }
328 479
480 /**
481 * Returns the php associative array that is the core of this element
482 * @returns (array)
483 */
329 function get() 484 function get()
330 { 485 {
331 return $this->elems; 486 return $this->elems;
332 } 487 }
333 488
489 /**
490 * Returns the current size of the dictionary
491 * @returns (int)
492 */
334 function size() 493 function size()
335 { 494 {
336 return count($this->elems); 495 return count($this->elems);
@@ -349,18 +508,34 @@ class phpgats_Dictionary extends phpgats_Element
349 return $s_out; 508 return $s_out;
350 } 509 }
351 510
511 /**
512 * Get the gats type of the current element
513 * @returns "dictionary"
514 */
352 function getGatsType() 515 function getGatsType()
353 { 516 {
354 return "dictionary"; 517 return "dictionary";
355 } 518 }
356} 519}
357 520
521/**
522 * Make sure we can read a character off the input stream
523 * @param $str_data (string) the input stream
524 * @param $offset (integer) the current position in the input stream
525 */
358function phpgats_pC( $str_data, $offset ) 526function phpgats_pC( $str_data, $offset )
359{ 527{
360 if($offset>strlen($str_data)) 528 if($offset>strlen($str_data))
361 throw new Exception("Not enough data"); 529 throw new Exception("Not enough data");
362} 530}
363 531
532/**
533 * Parse a string element out of the input stream
534 * @param $str_data (string) the input stream
535 * @param &$offset (integer) the current position in the input stream (will be updated)
536 * @param $dbg (integer) the current depth (for pretty printing)
537 * @returns (phpgats_String) the element read
538 */
364function phpgats_pS( $str_data, &$offset, $dbg ) 539function phpgats_pS( $str_data, &$offset, $dbg )
365{ 540{
366 $str_tmp = ""; 541 $str_tmp = "";
@@ -382,11 +557,25 @@ function phpgats_pS( $str_data, &$offset, $dbg )
382 return new phpgats_String($str_tmp); 557 return new phpgats_String($str_tmp);
383} 558}
384 559
560/**
561 * Parse an integer element out of the input stream
562 * @param $str_data (string) the input stream
563 * @param &$offset (integer) the current position in the input stream (will be updated)
564 * @param $dbg (integer) the current depth (for pretty printing)
565 * @returns (phpgats_Integer) the element read
566 */
385function phpgats_pI( $str_data, &$offset, $dbg ) 567function phpgats_pI( $str_data, &$offset, $dbg )
386{ 568{
387 return new phpgats_Integer(phpgats_readInt($str_data, $offset)); 569 return new phpgats_Integer(phpgats_readInt($str_data, $offset));
388} 570}
389 571
572/**
573 * Parse a float element out of the input stream
574 * @param $str_data (string) the input stream
575 * @param &$offset (integer) the current position in the input stream (will be updated)
576 * @param $dbg (integer) the current depth (for pretty printing)
577 * @returns (phpgats_Float) the element read
578 */
390function phpgats_pF( $str_data, &$offset, $dbg ) 579function phpgats_pF( $str_data, &$offset, $dbg )
391{ 580{
392 $str_tmp = ""; 581 $str_tmp = "";
@@ -416,6 +605,13 @@ function phpgats_pF( $str_data, &$offset, $dbg )
416 return new phpgats_Float( $e ); 605 return new phpgats_Float( $e );
417} 606}
418 607
608/**
609 * Parse a list element out of the input stream
610 * @param $str_data (string) the input stream
611 * @param &$offset (integer) the current position in the input stream (will be updated)
612 * @param $dbg (integer) the current depth (for pretty printing)
613 * @returns (phpgats_List) the element read
614 */
419function phpgats_pL( $str_data, &$offset, $dbg ) 615function phpgats_pL( $str_data, &$offset, $dbg )
420{ 616{
421 phpgats_pC( $str_data, $offset ); 617 phpgats_pC( $str_data, $offset );
@@ -432,6 +628,13 @@ function phpgats_pL( $str_data, &$offset, $dbg )
432 return $l_out; 628 return $l_out;
433} 629}
434 630
631/**
632 * Parse a dictionary element out of the input stream
633 * @param $str_data (string) the input stream
634 * @param &$offset (integer) the current position in the input stream (will be updated)
635 * @param $dbg (integer) the current depth (for pretty printing)
636 * @returns (phpgats_Dictionary) the element read
637 */
435function phpgats_pD( $str_data, &$offset, $dbg ) 638function phpgats_pD( $str_data, &$offset, $dbg )
436{ 639{
437 phpgats_pC( $str_data, $offset ); 640 phpgats_pC( $str_data, $offset );
@@ -449,6 +652,13 @@ function phpgats_pD( $str_data, &$offset, $dbg )
449 return $d_out; 652 return $d_out;
450} 653}
451 654
655/**
656 * The internal master recursive parse function
657 * @param $str_data (string) the input stream
658 * @param &$offset (integer) the current position in the input stream (will be updated)
659 * @param $dbg (integer) the current depth (for pretty printing)
660 * @returns (phpgats_Element) the element read
661 */
452function phpgats_pM( $str_data, &$offset, $dbg=0 ) 662function phpgats_pM( $str_data, &$offset, $dbg=0 )
453{ 663{
454 phpgats_pC( $str_data, $offset ); 664 phpgats_pC( $str_data, $offset );
@@ -507,6 +717,11 @@ function phpgats_pM( $str_data, &$offset, $dbg=0 )
507 } 717 }
508} 718}
509 719
720/**
721 * Call this function to parse a gats binary string into a phpgats_Element object
722 * @param $str_data (string) the binary gats data to be parsed
723 * @returns (phpgats_Element)
724 */
510function phpgats_parseGats( $str_data ) 725function phpgats_parseGats( $str_data )
511{ 726{
512 //print "parsing\n"; 727 //print "parsing\n";
@@ -534,12 +749,18 @@ function phpgats_parseGats( $str_data )
534 return phpgats_pM( $str_data, $offset ); 749 return phpgats_pM( $str_data, $offset );
535} 750}
536 751
752/**
753 * Call this function to generate a binary gats stream from the given phpgats_Element object
754 * @param $elem (phpgats_Element) the gats element object from which to generate a binary blob
755 * @returns (string) binary gats data
756 */
537function phpgats_writeGats( $elem ) 757function phpgats_writeGats( $elem )
538{ 758{
539 $str_out = $elem->encode(); 759 $str_out = $elem->encode();
540 $str_out = "\x01" . pack( "N", strlen($str_out)+5 ) . $str_out; 760 $str_out = "\x01" . pack( "N", strlen($str_out)+5 ) . $str_out;
541 return $str_out; 761 return $str_out;
542} 762}
763
543/* 764/*
544$l = new phpgats_List(); 765$l = new phpgats_List();
545$l->append( new phpgats_Float( 123000000000.0 ) ); 766$l->append( new phpgats_Float( 123000000000.0 ) );