PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/intl/transliterator - transliterator_class.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 74 102 72.5 %
Date: 2014-10-22 Functions: 10 11 90.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | This source file is subject to version 3.01 of the PHP license,      |
       6             :    | that is bundled with this package in the file LICENSE, and is        |
       7             :    | available through the world-wide-web at the following url:           |
       8             :    | http://www.php.net/license/3_01.txt                                  |
       9             :    | If you did not receive a copy of the PHP license and are unable to   |
      10             :    | obtain it through the world-wide-web, please send a note to          |
      11             :    | license@php.net so we can mail you a copy immediately.               |
      12             :    +----------------------------------------------------------------------+
      13             :    | Authors: Gustavo Lopes <cataphract@php.net>                          |
      14             :    +----------------------------------------------------------------------+
      15             :  */
      16             : 
      17             : #include "transliterator_class.h"
      18             : #include "php_intl.h"
      19             : #include "transliterator_methods.h"
      20             : #include "intl_error.h"
      21             : #include "intl_convert.h"
      22             : #include "intl_data.h"
      23             : 
      24             : #include <unicode/utrans.h>
      25             : 
      26             : zend_class_entry *Transliterator_ce_ptr = NULL;
      27             : 
      28             : zend_object_handlers Transliterator_handlers;
      29             : 
      30             : /* {{{ int transliterator_object_construct( zval *object, UTransliterator *utrans, UErrorCode *status TSRMLS_DC )
      31             :  * Initialize internals of Transliterator_object.
      32             :  */
      33          21 : int transliterator_object_construct( zval *object,
      34             :                                                                          UTransliterator *utrans,
      35             :                                                                          UErrorCode *status TSRMLS_DC )
      36             : {
      37             :         const UChar           *ustr_id;
      38             :         int32_t               ustr_id_len;
      39             :         char                  *str_id;
      40             :         int                   str_id_len;
      41             :         Transliterator_object *to;
      42             : 
      43          21 :         TRANSLITERATOR_METHOD_FETCH_OBJECT_NO_CHECK;
      44             : 
      45             :         assert( to->utrans == NULL );
      46             :         /* this assignment must happen before any return with failure because the
      47             :          * caller relies on it always being made (so it can just destroy the object
      48             :          * to close the transliterator) */
      49          21 :         to->utrans = utrans;
      50             : 
      51          21 :         ustr_id = utrans_getUnicodeID( utrans, &ustr_id_len );
      52          21 :         intl_convert_utf16_to_utf8( &str_id, &str_id_len, ustr_id, (int ) ustr_id_len, status );
      53          21 :         if( U_FAILURE( *status ) )
      54             :         {
      55           0 :                 return FAILURE;
      56             :         }
      57             : 
      58          21 :         zend_update_property_stringl(Transliterator_ce_ptr, object,
      59             :                 "id", sizeof( "id" ) - 1, str_id, str_id_len TSRMLS_CC );
      60          21 :         efree( str_id );
      61          21 :         return SUCCESS;
      62             : }
      63             : /* }}} */
      64             : 
      65             : /*
      66             :  * Auxiliary functions needed by objects of 'Transliterator' class
      67             :  */
      68             : 
      69             : /* {{{ void transliterator_object_init( Transliterator_object* to )
      70             :  * Initialize internals of Transliterator_object.
      71             :  */
      72          28 : static void transliterator_object_init( Transliterator_object* to TSRMLS_DC )
      73             : {
      74          28 :         if( !to )
      75           0 :                 return;
      76             : 
      77          28 :         intl_error_init( TRANSLITERATOR_ERROR_P( to ) TSRMLS_CC );
      78             : }
      79             : /* }}} */
      80             : 
      81             : /* {{{ void transliterator_object_destroy( Transliterator_object* to )
      82             :  * Clean up mem allocted by internals of Transliterator_object
      83             :  */
      84          28 : static void transliterator_object_destroy( Transliterator_object* to TSRMLS_DC )
      85             : {
      86          28 :         if( !to )
      87           0 :                 return;
      88             : 
      89          28 :         if( to->utrans )
      90             :         {
      91          21 :                 utrans_close( to->utrans );
      92          21 :                 to->utrans = NULL;
      93             :         }
      94             : 
      95          28 :         intl_error_reset( TRANSLITERATOR_ERROR_P( to ) TSRMLS_CC );
      96             : }
      97             : /* }}} */
      98             : 
      99             : /* {{{ Transliterator_objects_dtor */
     100          25 : static void Transliterator_objects_dtor(
     101             :         zend_object *object TSRMLS_DC )
     102             : {
     103          25 :         zend_objects_destroy_object( object TSRMLS_CC );
     104          25 : }
     105             : /* }}} */
     106             : 
     107             : /* {{{ Transliterator_objects_free */
     108          28 : static void Transliterator_objects_free( zend_object *object TSRMLS_DC )
     109             : {
     110          28 :         Transliterator_object* to = php_intl_transliterator_fetch_object(object);
     111             : 
     112          28 :         zend_object_std_dtor( &to->zo TSRMLS_CC );
     113             : 
     114          28 :         transliterator_object_destroy( to TSRMLS_CC );
     115          28 : }
     116             : /* }}} */
     117             : 
     118             : /* {{{ Transliterator_object_create */
     119          28 : static zend_object *Transliterator_object_create(
     120             :         zend_class_entry *ce TSRMLS_DC )
     121             : {
     122             :         Transliterator_object* intern;
     123             : 
     124          28 :         intern = ecalloc( 1, sizeof( Transliterator_object ) + sizeof(zval) * (ce->default_properties_count - 1));
     125             : 
     126          28 :         zend_object_std_init( &intern->zo, ce TSRMLS_CC );
     127          28 :     object_properties_init( &intern->zo, ce );
     128          28 :         transliterator_object_init( intern TSRMLS_CC );
     129             : 
     130          28 :         intern->zo.handlers = &Transliterator_handlers;
     131             : 
     132          28 :         return &intern->zo;
     133             : }
     134             : /* }}} */
     135             : 
     136             : /*
     137             :  * Object handlers for Transliterator class (and subclasses)
     138             :  */
     139             : 
     140             : /* {{{ clone handler for Transliterator */
     141           1 : static zend_object *Transliterator_clone_obj( zval *object TSRMLS_DC )
     142             : {
     143             :         Transliterator_object *to_orig,
     144             :                               *to_new;
     145             :         zend_object               *ret_val;
     146           1 :         intl_error_reset( NULL TSRMLS_CC );
     147             : 
     148           1 :         to_orig = Z_INTL_TRANSLITERATOR_P( object );
     149           1 :         intl_error_reset( INTL_DATA_ERROR_P( to_orig ) TSRMLS_CC );
     150           1 :         ret_val = Transliterator_ce_ptr->create_object( Z_OBJCE_P( object ) TSRMLS_CC );
     151           1 :         to_new  = php_intl_transliterator_fetch_object( ret_val );
     152             : 
     153           1 :         zend_objects_clone_members( &to_new->zo, &to_orig->zo TSRMLS_CC );
     154             : 
     155           1 :         if( to_orig->utrans != NULL )
     156             :         {
     157           1 :                 UTransliterator *utrans = NULL;
     158             :                 zval                    tempz; /* dummy zval to pass to transliterator_object_construct */
     159             : 
     160             :                 /* guaranteed to return NULL if it fails */
     161           1 :                 utrans = utrans_clone( to_orig->utrans, TRANSLITERATOR_ERROR_CODE_P( to_orig ) );
     162             : 
     163           1 :                 if( U_FAILURE( TRANSLITERATOR_ERROR_CODE( to_orig ) ) )
     164           0 :                         goto err;
     165             : 
     166           1 :                 ZVAL_OBJ(&tempz, ret_val);
     167           1 :                 transliterator_object_construct( &tempz, utrans,
     168             :                         TRANSLITERATOR_ERROR_CODE_P( to_orig ) TSRMLS_CC );
     169             : 
     170           1 :                 if( U_FAILURE( TRANSLITERATOR_ERROR_CODE( to_orig ) ) )
     171             :                 {
     172             :                         zend_string *err_msg;
     173             : err:
     174             : 
     175           0 :                         if( utrans != NULL )
     176           0 :                                 transliterator_object_destroy( to_new TSRMLS_CC );
     177             : 
     178             :                         /* set the error anyway, in case in the future we decide not to
     179             :                          * throw an error. It also helps build the error message */
     180           0 :                         intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( to_orig ) TSRMLS_CC );
     181           0 :                         intl_errors_set_custom_msg( TRANSLITERATOR_ERROR_P( to_orig ),
     182             :                                 "Could not clone transliterator", 0 TSRMLS_CC );
     183             : 
     184           0 :                         err_msg = intl_error_get_message( TRANSLITERATOR_ERROR_P( to_orig ) TSRMLS_CC );
     185           0 :                         php_error_docref( NULL TSRMLS_CC, E_ERROR, "%s", err_msg->val );
     186             :                         zend_string_free( err_msg ); /* if it's changed into a warning */
     187             :                         /* do not destroy tempz; we need to return something */
     188             :                 }
     189             :         }
     190             :         else
     191             :         {
     192             :                 /* We shouldn't have unconstructed objects in the first place */
     193           0 :                 php_error_docref( NULL TSRMLS_CC, E_WARNING,
     194             :                         "Cloning unconstructed transliterator." );
     195             :         }
     196             : 
     197           1 :         return ret_val;
     198             : }
     199             : /* }}} */
     200             : 
     201             : #define TRANSLITERATOR_PROPERTY_HANDLER_PROLOG  \
     202             :         zval tmp_member;                                                        \
     203             :         if( Z_TYPE_P( member ) != IS_STRING )           \
     204             :         {                                                                                       \
     205             :                 tmp_member = *member;                                   \
     206             :                 zval_copy_ctor( &tmp_member );                      \
     207             :                 convert_to_string( &tmp_member );           \
     208             :                 member = &tmp_member;                                       \
     209             :                 cache_slot = NULL;                                              \
     210             :     }
     211             : 
     212             : #define TRANSLITERATOR_PROPERTY_HANDLER_EPILOG  \
     213             :         if( member == &tmp_member )                                 \
     214             :         {                                                                                       \
     215             :                 zval_dtor( &tmp_member );                           \
     216             :         }
     217             : 
     218             : /* {{{ get_property_ptr_ptr handler */
     219           0 : static zval *Transliterator_get_property_ptr_ptr( zval *object, zval *member, int type, void **cache_slot TSRMLS_DC )
     220             : {
     221             :         zval *retval;
     222             : 
     223           0 :         TRANSLITERATOR_PROPERTY_HANDLER_PROLOG;
     224             : 
     225           0 :         if(zend_binary_strcmp( "id", sizeof( "id" ) - 1,
     226           0 :                 Z_STRVAL_P( member ), Z_STRLEN_P( member ) ) == 0 )
     227             :         {
     228           0 :                 retval = NULL; /* fallback to read_property */
     229             :         }
     230             :         else
     231             :         {
     232           0 :                 retval = std_object_handlers.get_property_ptr_ptr( object, member, type, cache_slot TSRMLS_CC );
     233             :         }
     234             : 
     235           0 :         TRANSLITERATOR_PROPERTY_HANDLER_EPILOG;
     236             : 
     237           0 :         return retval;
     238             : }
     239             : /* }}} */
     240             : 
     241             : /* {{{ read_property handler */
     242           7 : static zval *Transliterator_read_property( zval *object, zval *member, int type, void **cache_slot, zval *rv TSRMLS_DC )
     243             : {
     244             :         zval *retval;
     245             : 
     246           7 :         TRANSLITERATOR_PROPERTY_HANDLER_PROLOG;
     247             : 
     248           7 :         if( ( type != BP_VAR_R && type != BP_VAR_IS ) &&
     249           0 :                 ( zend_binary_strcmp( "id", sizeof( "id" ) - 1,
     250           0 :                 Z_STRVAL_P( member ), Z_STRLEN_P( member ) ) == 0 ) )
     251             :         {
     252           0 :                 php_error_docref0( NULL TSRMLS_CC, E_WARNING, "The property \"id\" is read-only" );
     253           0 :                 retval = &EG( uninitialized_zval );
     254             :         }
     255             :         else
     256             :         {
     257           7 :                 retval = std_object_handlers.read_property( object, member, type, cache_slot, rv TSRMLS_CC );
     258             :         }
     259             : 
     260           7 :         TRANSLITERATOR_PROPERTY_HANDLER_EPILOG;
     261             : 
     262           7 :         return retval;
     263             : }
     264             : 
     265             : /* }}} */
     266             : 
     267             : /* {{{ write_property handler */
     268          21 : static void Transliterator_write_property( zval *object, zval *member, zval *value,
     269             :         void **cache_slot TSRMLS_DC )
     270             : {
     271          21 :         TRANSLITERATOR_PROPERTY_HANDLER_PROLOG;
     272             : 
     273          21 :         if( ( EG( scope ) != Transliterator_ce_ptr ) &&
     274           0 :                 ( zend_binary_strcmp( "id", sizeof( "id" ) - 1,
     275           0 :                 Z_STRVAL_P( member ), Z_STRLEN_P( member ) ) == 0 ) )
     276             :         {
     277           0 :                 php_error_docref0( NULL TSRMLS_CC, E_WARNING, "The property \"id\" is read-only" );
     278             :         }
     279             :         else
     280             :         {
     281          21 :                 std_object_handlers.write_property( object, member, value, cache_slot TSRMLS_CC );
     282             :         }
     283             : 
     284          21 :         TRANSLITERATOR_PROPERTY_HANDLER_EPILOG;
     285          21 : }
     286             : /* }}} */
     287             : 
     288             : /*
     289             :  * 'Transliterator' class registration structures & functions
     290             :  */
     291             : 
     292             : /* {{{ Transliterator methods arguments info */
     293             : 
     294             : ZEND_BEGIN_ARG_INFO_EX( ainfo_trans_void, 0, 0, 0 )
     295             : ZEND_END_ARG_INFO()
     296             : 
     297             : ZEND_BEGIN_ARG_INFO_EX( ainfo_trans_create, 0, 0, 1 )
     298             :         ZEND_ARG_INFO( 0, id )
     299             :         ZEND_ARG_INFO( 0, direction )
     300             : ZEND_END_ARG_INFO()
     301             : 
     302             : ZEND_BEGIN_ARG_INFO_EX( ainfo_trans_create_from_rules, 0, 0, 1 )
     303             :         ZEND_ARG_INFO( 0, rules )
     304             :         ZEND_ARG_INFO( 0, direction )
     305             : ZEND_END_ARG_INFO()
     306             : 
     307             : ZEND_BEGIN_ARG_INFO_EX( ainfo_trans_create_inverse, 0, 0, 1 )
     308             :         ZEND_ARG_OBJ_INFO( 0, orig_trans, Transliterator, 0 )
     309             : ZEND_END_ARG_INFO()
     310             : 
     311             : ZEND_BEGIN_ARG_INFO_EX( ainfo_trans_me_transliterate, 0, 0, 1 )
     312             :         ZEND_ARG_INFO( 0, subject )
     313             :         ZEND_ARG_INFO( 0, start )
     314             :         ZEND_ARG_INFO( 0, end )
     315             : ZEND_END_ARG_INFO()
     316             : 
     317             : ZEND_BEGIN_ARG_INFO_EX( ainfo_trans_error, 0, 0, 1 )
     318             :         ZEND_ARG_OBJ_INFO( 0, trans, Transliterator, 0 )
     319             : ZEND_END_ARG_INFO()
     320             : 
     321             : /* }}} */
     322             : 
     323             : /* {{{ Transliterator_class_functions
     324             :  * Every 'Transliterator' class method has an entry in this table
     325             :  */
     326             : zend_function_entry Transliterator_class_functions[] = {
     327             :         PHP_ME( Transliterator,                 __construct,                                            ainfo_trans_void,                               ZEND_ACC_PRIVATE | ZEND_ACC_CTOR | ZEND_ACC_FINAL )
     328             :         PHP_ME_MAPPING( create,                 transliterator_create,                          ainfo_trans_create,                             ZEND_ACC_STATIC |ZEND_ACC_PUBLIC )
     329             :         PHP_ME_MAPPING( createFromRules,transliterator_create_from_rules,       ainfo_trans_create_from_rules,  ZEND_ACC_STATIC | ZEND_ACC_PUBLIC )
     330             :         PHP_ME_MAPPING( createInverse,  transliterator_create_inverse,          ainfo_trans_void,                               ZEND_ACC_PUBLIC )
     331             :         PHP_ME_MAPPING( listIDs,                transliterator_list_ids,                        ainfo_trans_void,                               ZEND_ACC_STATIC | ZEND_ACC_PUBLIC )
     332             :         PHP_ME_MAPPING( transliterate,  transliterator_transliterate,           ainfo_trans_me_transliterate,   ZEND_ACC_PUBLIC )
     333             :         PHP_ME_MAPPING( getErrorCode,   transliterator_get_error_code,          ainfo_trans_void,                               ZEND_ACC_PUBLIC )
     334             :         PHP_ME_MAPPING( getErrorMessage,transliterator_get_error_message,       ainfo_trans_void,                               ZEND_ACC_PUBLIC )
     335             :         PHP_FE_END
     336             : };
     337             : /* }}} */
     338             : 
     339             : /* {{{ transliterator_register_Transliterator_class
     340             :  * Initialize 'Transliterator' class
     341             :  */
     342       20423 : void transliterator_register_Transliterator_class( TSRMLS_D )
     343             : {
     344             :         zend_class_entry ce;
     345             : 
     346             :         /* Create and register 'Transliterator' class. */
     347       20423 :         INIT_CLASS_ENTRY( ce, "Transliterator", Transliterator_class_functions );
     348       20423 :         ce.create_object = Transliterator_object_create;
     349       20423 :         Transliterator_ce_ptr = zend_register_internal_class( &ce TSRMLS_CC );
     350       20423 :         memcpy( &Transliterator_handlers, zend_get_std_object_handlers(),
     351             :                 sizeof Transliterator_handlers );
     352       20423 :         Transliterator_handlers.offset = XtOffsetOf(Transliterator_object, zo);
     353       20423 :         Transliterator_handlers.dtor_obj = Transliterator_objects_dtor;
     354       20423 :         Transliterator_handlers.free_obj = Transliterator_objects_free;
     355       20423 :         Transliterator_handlers.clone_obj = Transliterator_clone_obj;
     356       20423 :         Transliterator_handlers.get_property_ptr_ptr = Transliterator_get_property_ptr_ptr;
     357       20423 :         Transliterator_handlers.read_property = Transliterator_read_property;
     358       20423 :         Transliterator_handlers.write_property = Transliterator_write_property;
     359             : 
     360             :         /* Declare 'Transliterator' class properties */
     361       20423 :         if( !Transliterator_ce_ptr )
     362             :         {
     363           0 :                 zend_error( E_ERROR,
     364             :                         "Transliterator: attempt to create properties "
     365             :                         "on a non-registered class." );
     366           0 :                 return;
     367             :         }
     368       20423 :         zend_declare_property_null( Transliterator_ce_ptr,
     369             :                 "id", sizeof( "id" ) - 1, ZEND_ACC_PUBLIC TSRMLS_CC );
     370             : 
     371             :         /* constants are declared in transliterator_register_constants, called from MINIT */
     372             : 
     373             : }
     374             : /* }}} */
     375             : 
     376             : /*
     377             :  * Local variables:
     378             :  * tab-width: 4
     379             :  * c-basic-offset: 4
     380             :  * End:
     381             :  * vim600: noet sw=4 ts=4 fdm=marker
     382             :  * vim<600: noet sw=4 ts=4
     383             :  */

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:50 +0000 (21 hours ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.