diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2011-02-17 23:13:29 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2011-02-17 23:13:29 +0000 |
| commit | 20d9ff5d3c4ac09f1f489eef4b0a4297983d9abd (patch) | |
| tree | d01655340a0f61ae544e746642653be648f809ae | |
| parent | 7292af7c475c61920987ec498144a3fd1e90f1e8 (diff) | |
| download | libgats-20d9ff5d3c4ac09f1f489eef4b0a4297983d9abd.tar.gz libgats-20d9ff5d3c4ac09f1f489eef4b0a4297983d9abd.tar.bz2 libgats-20d9ff5d3c4ac09f1f489eef4b0a4297983d9abd.tar.xz libgats-20d9ff5d3c4ac09f1f489eef4b0a4297983d9abd.zip | |
Ok, php can read and write gats style integers now.
| -rw-r--r-- | php/int.php | 108 | ||||
| -rw-r--r-- | src/tests/int.cpp | 30 |
2 files changed, 134 insertions, 4 deletions
diff --git a/php/int.php b/php/int.php new file mode 100644 index 0000000..478af73 --- /dev/null +++ b/php/int.php | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | /* | ||
| 4 | function writeInta( $iIn ) | ||
| 5 | { | ||
| 6 | $ret = ""; | ||
| 7 | if( $iIn < 0 ) | ||
| 8 | { | ||
| 9 | $iIn = -$iIn; | ||
| 10 | $b = ($iIn&0x3f); | ||
| 11 | if( $iIn > $b ) | ||
| 12 | $b |= 0x80 | 0x40; | ||
| 13 | else | ||
| 14 | $b |= 0x40; | ||
| 15 | } | ||
| 16 | else | ||
| 17 | { | ||
| 18 | $b = ($iIn&0x3f); | ||
| 19 | if( $iIn > $b ) | ||
| 20 | $b |= 0x80; | ||
| 21 | } | ||
| 22 | |||
| 23 | $ret .= chr( $b ); | ||
| 24 | $iIn = $iIn >> 6; | ||
| 25 | |||
| 26 | while( $iIn > 0 ) | ||
| 27 | { | ||
| 28 | $b = ($iIn&0x7f); | ||
| 29 | if( $iIn > $b ) | ||
| 30 | $b |= 0x80; | ||
| 31 | $ret .= chr( $b ); | ||
| 32 | $iIn = $iIn >> 7; | ||
| 33 | } | ||
| 34 | |||
| 35 | return $ret; | ||
| 36 | } */ | ||
| 37 | |||
| 38 | function writeInt( $iIn ) | ||
| 39 | { | ||
| 40 | $ret = ""; | ||
| 41 | if( gmp_cmp( $iIn, 0 ) < 0 ) | ||
| 42 | { | ||
| 43 | $iIn = gmp_mul( $iIn, -1 ); | ||
| 44 | $b = gmp_intval( gmp_and( $iIn, 0x3f ) ); | ||
| 45 | if( gmp_cmp( $iIn, $b ) > 0 ) | ||
| 46 | $b |= 0x80 | 0x40; | ||
| 47 | else | ||
| 48 | $b |= 0x40; | ||
| 49 | } | ||
| 50 | else | ||
| 51 | { | ||
| 52 | $b = gmp_intval( gmp_and( $iIn, 0x3f ) ); | ||
| 53 | if( gmp_cmp( $iIn, $b ) > 0 ) | ||
| 54 | $b |= 0x80; | ||
| 55 | } | ||
| 56 | |||
| 57 | $ret .= chr( $b ); | ||
| 58 | $iIn = gmp_div( $iIn, 64 ); | ||
| 59 | |||
| 60 | while( gmp_cmp( $iIn, 0 ) > 0 ) | ||
| 61 | { | ||
| 62 | $b = gmp_intval( gmp_and( $iIn, 0x7f ) ); | ||
| 63 | if( gmp_cmp( $iIn, $b ) > 0 ) | ||
| 64 | $b |= 0x80; | ||
| 65 | $ret .= chr( $b ); | ||
| 66 | $iIn = gmp_div( $iIn, 128 ); | ||
| 67 | } | ||
| 68 | |||
| 69 | return $ret; | ||
| 70 | } | ||
| 71 | |||
| 72 | function readInt( $sIn, &$pos ) | ||
| 73 | { | ||
| 74 | $neg = false; | ||
| 75 | |||
| 76 | $b = ord($sIn[$pos++]); | ||
| 77 | if( ($b&0x40) == 0x40 ) | ||
| 78 | $neg = true; | ||
| 79 | $iOut = gmp_init( $b&0x3f ); | ||
| 80 | $mult = gmp_init( 64 ); | ||
| 81 | while( ($b&0x80) ) | ||
| 82 | { | ||
| 83 | $b = ord($sIn[$pos++]); | ||
| 84 | $iOut = gmp_or( $iOut, gmp_mul( $b&0x7f, $mult ) ); | ||
| 85 | $mult = gmp_mul( $mult, 128 ); | ||
| 86 | } | ||
| 87 | if( $neg == true ) | ||
| 88 | $iOut = gmp_mul( $iOut, -1 ); | ||
| 89 | |||
| 90 | return $iOut; | ||
| 91 | } | ||
| 92 | |||
| 93 | function teststr( $str ) | ||
| 94 | { | ||
| 95 | $pos = 0; | ||
| 96 | $enc = writeInt( $str ); | ||
| 97 | $out = gmp_strval( readInt( $enc, $pos ) ); | ||
| 98 | print( $str . " => " . $out ); | ||
| 99 | if( $out == $str ) | ||
| 100 | print(", correct.\n"); | ||
| 101 | else | ||
| 102 | print(", incorrect.\n"); | ||
| 103 | } | ||
| 104 | //print( bin2hex( writeInt("3898993999921") ) . "\n" ); | ||
| 105 | teststr("-7"); | ||
| 106 | |||
| 107 | |||
| 108 | ?> | ||
diff --git a/src/tests/int.cpp b/src/tests/int.cpp index 348e179..c19df9c 100644 --- a/src/tests/int.cpp +++ b/src/tests/int.cpp | |||
| @@ -2,14 +2,36 @@ | |||
| 2 | 2 | ||
| 3 | #include <bu/sio.h> | 3 | #include <bu/sio.h> |
| 4 | #include <bu/membuf.h> | 4 | #include <bu/membuf.h> |
| 5 | #include <stdlib.h> | ||
| 5 | 6 | ||
| 6 | using namespace Bu; | 7 | using namespace Bu; |
| 7 | 8 | ||
| 8 | int main() | 9 | void hexdump( char *dat, int iSize ) |
| 9 | { | 10 | { |
| 10 | MemBuf mb; | 11 | static const char *hex="0123456789ABCDEF"; |
| 11 | int64_t i = -6; | 12 | printf("----\n"); |
| 13 | for( int j = 0; j < iSize; j += 8 ) | ||
| 14 | { | ||
| 15 | for( int k = j; /*k < iSize &&*/ k < j+8; k++ ) | ||
| 16 | printf((k<iSize)?"%c%c ":" ", hex[(dat[k]>>4)&0x0F], hex[dat[k]&0x0F] ); | ||
| 17 | printf("| "); | ||
| 18 | for( int k = j; k < iSize && k < j+8; k++ ) | ||
| 19 | printf("%c ", (dat[k]>13&&dat[k]<127)?(dat[k]):('.') ); | ||
| 20 | printf("\n"); | ||
| 21 | } | ||
| 22 | printf("----\n"); | ||
| 23 | } | ||
| 12 | 24 | ||
| 25 | int main( int argc, char *argv[] ) | ||
| 26 | { | ||
| 27 | for( int j = 1; j < argc; j++ ) | ||
| 28 | { | ||
| 29 | int64_t i = strtoll( argv[j], NULL, 10 ); | ||
| 30 | MemBuf mb; | ||
| 31 | Gats::Integer::writePackedInt( mb, i ); | ||
| 32 | hexdump( mb.getString().getStr(), mb.getString().getSize() ); | ||
| 33 | } | ||
| 34 | /* | ||
| 13 | sio << "Before: " << i << sio.nl; | 35 | sio << "Before: " << i << sio.nl; |
| 14 | Gats::Integer::writePackedInt( mb, i ); | 36 | Gats::Integer::writePackedInt( mb, i ); |
| 15 | mb.write("aaa", 3 ); | 37 | mb.write("aaa", 3 ); |
| @@ -19,7 +41,7 @@ int main() | |||
| 19 | char buf[4]; | 41 | char buf[4]; |
| 20 | buf[mb.read( buf, 3 )] = '\0'; | 42 | buf[mb.read( buf, 3 )] = '\0'; |
| 21 | sio << "Extra: \"" << buf << "\"" << sio.nl; | 43 | sio << "Extra: \"" << buf << "\"" << sio.nl; |
| 22 | 44 | */ | |
| 23 | return 0; | 45 | return 0; |
| 24 | } | 46 | } |
| 25 | 47 | ||
