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/msgformat - msgformat_format.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 53 61 86.9 %
Date: 2014-10-24 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       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: Stanislav Malyshev <stas@zend.com>                          |
      14             :    +----------------------------------------------------------------------+
      15             :  */
      16             : 
      17             : #ifdef HAVE_CONFIG_H
      18             : #include "config.h"
      19             : #endif
      20             : 
      21             : #include <unicode/ustring.h>
      22             : 
      23             : #include "php_intl.h"
      24             : #include "msgformat_class.h"
      25             : #include "msgformat_format.h"
      26             : #include "msgformat_data.h"
      27             : #include "msgformat_helpers.h"
      28             : #include "intl_convert.h"
      29             : 
      30             : #ifndef Z_ADDREF_P
      31             : #define Z_ADDREF_P(z) ((z)->refcount++)
      32             : #endif
      33             : 
      34             : /* {{{ */
      35          31 : static void msgfmt_do_format(MessageFormatter_object *mfo, zval *args, zval *return_value TSRMLS_DC) 
      36             : {
      37             :         zval **fargs;
      38             :         int count;
      39          31 :         UChar* formatted = NULL;
      40          31 :         int formatted_len = 0;
      41             :         HashPosition pos;
      42             :         int i;
      43             : 
      44          31 :         count = zend_hash_num_elements(Z_ARRVAL_P(args));
      45             : 
      46          31 :         if(count < umsg_format_arg_count(MSG_FORMAT_OBJECT(mfo))) {
      47             :                 /* Not enough aguments for format! */
      48           2 :                 intl_error_set( INTL_DATA_ERROR_P(mfo), U_ILLEGAL_ARGUMENT_ERROR,
      49             :                         "msgfmt_format: not enough parameters", 0 TSRMLS_CC );
      50           2 :                 RETVAL_FALSE;
      51           2 :                 return;
      52             :         }
      53             : 
      54          29 :         fargs = safe_emalloc(count, sizeof(zval *), 0);
      55             : 
      56          29 :         zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(args), &pos);
      57         107 :         for(i=0;i<count;i++) {
      58             :                 zval **val;
      59          78 :                 zend_hash_get_current_data_ex(Z_ARRVAL_P(args), (void **)&val, &pos);
      60          78 :                 fargs[i] = *val;
      61          78 :                 Z_ADDREF_P(fargs[i]);
      62             :                 /* TODO: needs refcount increase here? */
      63          78 :                 zend_hash_move_forward_ex(Z_ARRVAL_P(args), &pos);
      64             :         }
      65             : 
      66          29 :         umsg_format_helper(MSG_FORMAT_OBJECT(mfo), count, fargs, &formatted, &formatted_len, &INTL_DATA_ERROR_CODE(mfo) TSRMLS_CC);
      67             : 
      68         107 :         for(i=0;i<count;i++) {
      69          78 :                 zval_ptr_dtor(&fargs[i]);
      70             :         }
      71             : 
      72          29 :         efree(fargs);
      73             : 
      74          29 :         if (formatted && U_FAILURE( INTL_DATA_ERROR_CODE(mfo) ) ) {
      75           0 :                         efree(formatted);
      76             :         }
      77             : 
      78          29 :         INTL_METHOD_CHECK_STATUS( mfo, "Number formatting failed" );
      79          29 :         INTL_METHOD_RETVAL_UTF8( mfo, formatted, formatted_len, 1 );
      80             : }
      81             : /* }}} */
      82             : 
      83             : /* {{{ proto mixed MessageFormatter::format( array $args )
      84             :  * Format a message. }}} */
      85             : /* {{{ proto mixed msgfmt_format( MessageFormatter $nf, array $args )
      86             :  * Format a message.
      87             :  */
      88          22 : PHP_FUNCTION( msgfmt_format )
      89             : {
      90             :         zval *args;
      91          22 :         MSG_FORMAT_METHOD_INIT_VARS;
      92             : 
      93             : 
      94             :         /* Parse parameters. */
      95          22 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa",
      96             :                 &object, MessageFormatter_ce_ptr,  &args ) == FAILURE )
      97             :         {
      98           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
      99             :                         "msgfmt_format: unable to parse input params", 0 TSRMLS_CC );
     100             : 
     101           1 :                 RETURN_FALSE;
     102             :         }
     103             : 
     104             :         /* Fetch the object. */
     105          21 :         MSG_FORMAT_METHOD_FETCH_OBJECT;
     106             : 
     107          21 :         msgfmt_do_format(mfo, args, return_value TSRMLS_CC);
     108             : }
     109             : /* }}} */
     110             : 
     111             : /* {{{ proto mixed MessageFormatter::formatMessage( string $locale, string $pattern, array $args )
     112             :  * Format a message. }}} */
     113             : /* {{{ proto mixed msgfmt_format_message( string $locale, string $pattern, array $args )
     114             :  * Format a message.
     115             :  */
     116          11 : PHP_FUNCTION( msgfmt_format_message )
     117             : {
     118             :         zval       *args;
     119          11 :         UChar      *spattern = NULL;
     120          11 :         int         spattern_len = 0;
     121          11 :         char       *pattern = NULL;
     122          11 :         int         pattern_len = 0;
     123          11 :         char       *slocale = NULL;
     124          11 :         int         slocale_len = 0;
     125          11 :         MessageFormatter_object mf = {0};
     126          11 :         MessageFormatter_object *mfo = &mf;
     127             : 
     128             :         /* Parse parameters. */
     129          11 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "ssa",
     130             :                   &slocale, &slocale_len, &pattern, &pattern_len, &args ) == FAILURE )
     131             :         {
     132           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     133             :                         "msgfmt_format_message: unable to parse input params", 0 TSRMLS_CC );
     134             : 
     135           1 :                 RETURN_FALSE;
     136             :         }
     137             : 
     138          10 :         msgformat_data_init(&mfo->mf_data TSRMLS_CC);
     139             : 
     140          20 :         if(pattern && pattern_len) {
     141          10 :                 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
     142          10 :                 if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
     143             :                 {
     144           0 :                         intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     145             :                                 "msgfmt_format_message: error converting pattern to UTF-16", 0 TSRMLS_CC );
     146           0 :                         RETURN_FALSE;
     147             :                 }
     148             :         } else {
     149           0 :                 spattern_len = 0;
     150           0 :                 spattern = NULL;
     151             :         }
     152             : 
     153          10 :         if(slocale_len == 0) {
     154           0 :                 slocale = INTL_G(default_locale);
     155             :         }
     156             : 
     157             : #ifdef MSG_FORMAT_QUOTE_APOS
     158          10 :         if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
     159           0 :                 intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
     160             :                         "msgfmt_format_message: error converting pattern to quote-friendly format", 0 TSRMLS_CC );
     161           0 :                 RETURN_FALSE;
     162             :         }
     163             : #endif
     164             : 
     165             :         /* Create an ICU message formatter. */
     166          10 :         MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo));
     167          10 :         if(spattern && spattern_len) {
     168          10 :                 efree(spattern);
     169             :         }
     170          10 :         INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
     171             : 
     172          10 :         msgfmt_do_format(mfo, args, return_value TSRMLS_CC);
     173             : 
     174             :         /* drop the temporary formatter */
     175          10 :         msgformat_data_free(&mfo->mf_data TSRMLS_CC);
     176             : }
     177             : /* }}} */
     178             : 
     179             : /*
     180             :  * Local variables:
     181             :  * tab-width: 4
     182             :  * c-basic-offset: 4
     183             :  * End:
     184             :  * vim600: noet sw=4 ts=4 fdm=marker
     185             :  * vim<600: noet sw=4 ts=4
     186             :  */

Generated by: LCOV version 1.10

Generated at Fri, 24 Oct 2014 05:21:48 +0000 (7 days ago)

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