Changeset 242 for trunk/ext

Show
Ignore:
Timestamp:
03/03/2007 11:28:42 (21 months ago)
Author:
indeyets
Message:

(PHP stuff)
* added basic support for error-handling (an exception is thrown on parse error)
* ranamed nan() to nanphp(), so extension compiles fine on osx
* small zts fix

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/ext/php/phpext.c

    r111 r242  
    1515 
    1616#include "php.h" 
     17#include "zend_exceptions.h" 
    1718#include "php_ini.h" 
    1819#include "ext/standard/info.h" 
    1920#include "php_syck.h" 
    2021 
    21 static double zero()    { return 0.0; } 
     22static double zero()    { return 0.0; } 
    2223static double one() { return 1.0; } 
    2324static double inf() { return one() / zero(); } 
    24 static double nan() { return zero() / zero(); } 
     25static double nanphp() { return zero() / zero(); } 
    2526 
    2627/* {{{ syck_functions[] 
     
    4243        "syck", 
    4344        syck_functions, 
    44         NULL,            /* module init function */ 
    45         NULL,            /* module shutdown function */ 
    46         NULL,            /* request init function */ 
    47         NULL,            /* request shutdown function */ 
     45        NULL,                   /* module init function */ 
     46        NULL,                   /* module shutdown function */ 
     47        NULL,                   /* request init function */ 
     48        NULL,                   /* request shutdown function */ 
    4849        PHP_MINFO(syck), /* module info function */ 
    4950#if ZEND_MODULE_API_NO >= 20010901 
     
    6263/* Remove comments and fill if you need to have entries in php.ini 
    6364PHP_INI_BEGIN() 
    64     STD_PHP_INI_ENTRY("syck.global_value",      "42", PHP_INI_ALL, OnUpdateInt, global_value, zend_syck_globals, syck_globals) 
    65     STD_PHP_INI_ENTRY("syck.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_syck_globals, syck_globals) 
     65        STD_PHP_INI_ENTRY("syck.global_value",    "42", PHP_INI_ALL, OnUpdateInt, global_value, zend_syck_globals, syck_globals) 
     66        STD_PHP_INI_ENTRY("syck.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_syck_globals, syck_globals) 
    6667PHP_INI_END() 
    6768*/ 
     
    8687 
    8788static zend_function_entry mergekey_functions[] = { 
    88   PHP_FALIAS(mergekey,          mergekey_init,              NULL) 
     89  PHP_FALIAS(mergekey,            mergekey_init,                          NULL) 
    8990  { NULL, NULL, NULL } 
    9091}; 
     
    9697  object_init_ex(getThis(), &merge_key_entry); 
    9798}  
    98                                                  
     99                                                                                                 
    99100 
    100101static void destroy_MergeKey_resource(zend_rsrc_list_entry *resource TSRMLS_DC) 
     
    110111PHP_MINIT_FUNCTION(syck) 
    111112{ 
    112     le_mergekeyp = zend_register_list_destructors_ex(destroy_MergeKey_resource, NULL, "MergeKey", module_number); 
     113        le_mergekeyp = zend_register_list_destructors_ex(destroy_MergeKey_resource, NULL, "MergeKey", module_number); 
    113114 
    114115        INIT_CLASS_ENTRY(merge_key_entry, "mergekey", mergekey_functions); 
     
    168169 
    169170 
    170 SYMID 
    171 php_syck_handler(p, n) 
    172     SyckParser *p; 
    173     SyckNode *n; 
    174 { 
    175     SYMID oid; 
    176     zval *o, *o2, *o3; 
    177     unsigned int i; 
     171SYMID php_syck_handler(SyckParser *p, SyckNode *n) 
     172{ 
     173        SYMID oid; 
     174        zval *o, *o2, *o3; 
     175        unsigned int i; 
    178176 
    179177        MAKE_STD_ZVAL(o); 
    180     switch (n->kind) 
    181     { 
    182         case syck_str_kind: 
    183             if ( n->type_id == NULL || strcmp( n->type_id, "str" ) == 0 ) 
    184             { 
     178        switch (n->kind) 
     179        { 
     180                case syck_str_kind: 
     181                        if ( n->type_id == NULL || strcmp( n->type_id, "str" ) == 0 ) 
     182                        { 
    185183                                ZVAL_STRINGL( o, n->data.str->ptr, n->data.str->len, 1); 
    186             } 
    187             else if ( strcmp( n->type_id, "null" ) == 0 ) 
    188             { 
    189                 ZVAL_NULL( o ); 
    190             } 
    191             else if ( strcmp( n->type_id, "bool#yes" ) == 0 ) 
    192             { 
     184                        } 
     185                        else if ( strcmp( n->type_id, "null" ) == 0 ) 
     186                        { 
     187                                ZVAL_NULL( o ); 
     188                        } 
     189                        else if ( strcmp( n->type_id, "bool#yes" ) == 0 ) 
     190                        { 
    193191                                ZVAL_BOOL( o, 1 ); 
    194             } 
    195             else if ( strcmp( n->type_id, "bool#no" ) == 0 ) 
    196             { 
     192                        } 
     193                        else if ( strcmp( n->type_id, "bool#no" ) == 0 ) 
     194                        { 
    197195                                ZVAL_BOOL( o, 0 ); 
    198             } 
    199             else if ( strcmp( n->type_id, "int#hex" ) == 0 ) 
    200             { 
     196                        } 
     197                        else if ( strcmp( n->type_id, "int#hex" ) == 0 ) 
     198                        { 
    201199                                long intVal = strtol( n->data.str->ptr, NULL, 16 ); 
    202200                                ZVAL_LONG( o, intVal ); 
    203             } 
    204             else if ( strcmp( n->type_id, "int#oct" ) == 0 ) 
    205             { 
     201                        } 
     202                        else if ( strcmp( n->type_id, "int#oct" ) == 0 ) 
     203                        { 
    206204                                long intVal = strtol( n->data.str->ptr, NULL, 8 ); 
    207205                                ZVAL_LONG( o, intVal ); 
    208             } 
    209             else if ( strcmp( n->type_id, "int" ) == 0 ) 
    210             { 
     206                        } 
     207                        else if ( strcmp( n->type_id, "int" ) == 0 ) 
     208                        { 
    211209                                long intVal = strtol( n->data.str->ptr, NULL, 10 ); 
    212210                                ZVAL_LONG( o, intVal ); 
    213             } 
    214             else if ( strcmp( n->type_id, "float" ) == 0 ) 
    215             { 
    216                 double f; 
    217                 syck_str_blow_away_commas( n ); 
    218                 f = strtod( n->data.str->ptr, NULL ); 
     211                        } 
     212                        else if ( strcmp( n->type_id, "float" ) == 0 ) 
     213                        { 
     214                                double f; 
     215                                syck_str_blow_away_commas( n ); 
     216                                f = strtod( n->data.str->ptr, NULL ); 
    219217                                ZVAL_DOUBLE( o, f ); 
    220             } 
    221             else if ( strcmp( n->type_id, "float#nan" ) == 0 ) 
    222             { 
    223                 ZVAL_DOUBLE( o, nan() ); 
    224             } 
    225             else if ( strcmp( n->type_id, "float#inf" ) == 0 ) 
    226             { 
    227                 ZVAL_DOUBLE( o, inf() ); 
    228             } 
    229             else if ( strcmp( n->type_id, "float#neginf" ) == 0 ) 
    230             { 
    231                 ZVAL_DOUBLE( o, -inf() ); 
    232             } 
    233             else if ( strcmp( n->type_id, "merge" ) == 0 ) 
    234             { 
     218                        } 
     219                        else if ( strcmp( n->type_id, "float#nan" ) == 0 ) 
     220                        { 
     221                                ZVAL_DOUBLE( o, nanphp() ); 
     222                        } 
     223                        else if ( strcmp( n->type_id, "float#inf" ) == 0 ) 
     224                        { 
     225                                ZVAL_DOUBLE( o, inf() ); 
     226                        } 
     227                        else if ( strcmp( n->type_id, "float#neginf" ) == 0 ) 
     228                        { 
     229                                ZVAL_DOUBLE( o, -inf() ); 
     230                        } 
     231                        else if ( strcmp( n->type_id, "merge" ) == 0 ) 
     232                        { 
     233                                TSRMLS_FETCH(); 
    235234                                MAKE_STD_ZVAL( o ); 
    236235                                object_init_ex( o, &merge_key_entry ); 
    237             } 
    238             else 
    239             { 
     236                        } 
     237                        else 
     238                        { 
    240239                                ZVAL_STRINGL(o, n->data.str->ptr, n->data.str->len, 1); 
    241             } 
    242         break; 
    243  
    244         case syck_seq_kind: 
     240                        } 
     241                break; 
     242 
     243                case syck_seq_kind: 
    245244                        array_init(o); 
    246             for ( i = 0; i < n->data.list->idx; i++ ) 
    247             { 
    248                 oid = syck_seq_read( n, i ); 
    249                 syck_lookup_sym( p, oid, &o2 ); 
     245                        for ( i = 0; i < n->data.list->idx; i++ ) 
     246                        { 
     247                                oid = syck_seq_read( n, i ); 
     248                                syck_lookup_sym( p, oid, &o2 ); 
    250249                                add_index_zval( o, i, o2 ); 
    251             } 
    252         break; 
    253  
    254         case syck_map_kind: 
     250                        } 
     251                break; 
     252 
     253                case syck_map_kind: 
    255254                        array_init(o); 
    256             for ( i = 0; i < n->data.pairs->idx; i++ ) 
    257             { 
    258                 oid = syck_map_read( n, map_key, i ); 
    259                 syck_lookup_sym( p, oid, &o2 ); 
    260                 oid = syck_map_read( n, map_value, i ); 
    261                 syck_lookup_sym( p, oid, &o3 ); 
     255                        for ( i = 0; i < n->data.pairs->idx; i++ ) 
     256                        { 
     257                                oid = syck_map_read( n, map_key, i ); 
     258                                syck_lookup_sym( p, oid, &o2 ); 
     259                                oid = syck_map_read( n, map_value, i ); 
     260                                syck_lookup_sym( p, oid, &o3 ); 
    262261                                if ( o2->type == IS_STRING ) 
    263262                                { 
    264263                                        add_assoc_zval( o, o2->value.str.val, o3 ); 
    265264                                } 
    266             } 
    267         break; 
    268     } 
    269     oid = syck_add_sym( p, o ); 
    270     return oid; 
     265                        } 
     266                break; 
     267        } 
     268        oid = syck_add_sym( p, o ); 
     269        return oid; 
     270} 
     271 
     272void php_syck_ehandler(SyckParser *p, char *str) 
     273{ 
     274        TSRMLS_FETCH(); 
     275        zend_class_entry *exc = zend_get_error_exception(); 
     276        zend_throw_error_exception(exc, str, 1, 1 TSRMLS_CC); 
    271277} 
    272278 
     
    287293 
    288294        parser = syck_new_parser(); 
    289     syck_parser_str( parser, arg, arg_len, NULL ); 
    290     syck_parser_handler( parser, php_syck_handler ); 
    291     syck_parser_implicit_typing( parser, 1 ); 
    292     syck_parser_taguri_expansion( parser, 0 ); 
    293     v = syck_parse( parser ); 
    294     syck_lookup_sym( parser, v, &obj ); 
    295     syck_free_parser( parser ); 
    296  
    297         *return_value = *obj; 
    298         zval_copy_ctor(return_value); 
     295 
     296        syck_parser_handler( parser, php_syck_handler ); 
     297        syck_parser_error_handler( parser, php_syck_ehandler ); 
     298 
     299        syck_parser_implicit_typing( parser, 1 ); 
     300        syck_parser_taguri_expansion( parser, 0 ); 
     301 
     302        syck_parser_str( parser, arg, arg_len, NULL ); 
     303 
     304        v = syck_parse( parser ); 
     305 
     306        if (1 == syck_lookup_sym( parser, v, &obj )) { 
     307                *return_value = *obj; 
     308                zval_copy_ctor(return_value); 
     309        } 
     310 
     311        syck_free_parser( parser ); 
    299312} 
    300313/* }}} */