Back to index

php5  5.3.10
msgformat_format.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | This source file is subject to version 3.01 of the PHP license,      |
00006    | that is bundled with this package in the file LICENSE, and is        |
00007    | available through the world-wide-web at the following url:           |
00008    | http://www.php.net/license/3_01.txt                                  |
00009    | If you did not receive a copy of the PHP license and are unable to   |
00010    | obtain it through the world-wide-web, please send a note to          |
00011    | license@php.net so we can mail you a copy immediately.               |
00012    +----------------------------------------------------------------------+
00013    | Authors: Stanislav Malyshev <stas@zend.com>                          |
00014    +----------------------------------------------------------------------+
00015  */
00016 
00017 #ifdef HAVE_CONFIG_H
00018 #include "config.h"
00019 #endif
00020 
00021 #include <unicode/ustring.h>
00022 
00023 #include "php_intl.h"
00024 #include "msgformat_class.h"
00025 #include "msgformat_format.h"
00026 #include "msgformat_data.h"
00027 #include "msgformat_helpers.h"
00028 #include "intl_convert.h"
00029 
00030 #ifndef Z_ADDREF_P
00031 #define Z_ADDREF_P(z) ((z)->refcount++)
00032 #endif
00033 
00034 /* {{{ */
00035 static void msgfmt_do_format(MessageFormatter_object *mfo, zval *args, zval *return_value TSRMLS_DC) 
00036 {
00037        zval **fargs;
00038        int count;
00039        UChar* formatted = NULL;
00040        int formatted_len = 0;
00041        HashPosition pos;
00042        int i;
00043 
00044        count = zend_hash_num_elements(Z_ARRVAL_P(args));
00045 
00046        if(count < umsg_format_arg_count(MSG_FORMAT_OBJECT(mfo))) {
00047               /* Not enough aguments for format! */
00048               intl_error_set( INTL_DATA_ERROR_P(mfo), U_ILLEGAL_ARGUMENT_ERROR,
00049                      "msgfmt_format: not enough parameters", 0 TSRMLS_CC );
00050               RETVAL_FALSE;
00051               return;
00052        }
00053 
00054        fargs = safe_emalloc(count, sizeof(zval *), 0);
00055 
00056        zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(args), &pos);
00057        for(i=0;i<count;i++) {
00058               zval **val;
00059               zend_hash_get_current_data_ex(Z_ARRVAL_P(args), (void **)&val, &pos);
00060               fargs[i] = *val;
00061               Z_ADDREF_P(fargs[i]);
00062               /* TODO: needs refcount increase here? */
00063               zend_hash_move_forward_ex(Z_ARRVAL_P(args), &pos);
00064        }
00065 
00066        umsg_format_helper(MSG_FORMAT_OBJECT(mfo), count, fargs, &formatted, &formatted_len, &INTL_DATA_ERROR_CODE(mfo) TSRMLS_CC);
00067 
00068        for(i=0;i<count;i++) {
00069               zval_ptr_dtor(&fargs[i]);
00070        }
00071 
00072        efree(fargs);
00073 
00074        if (formatted && U_FAILURE( INTL_DATA_ERROR_CODE(mfo) ) ) {
00075                      efree(formatted);
00076        }
00077 
00078        INTL_METHOD_CHECK_STATUS( mfo, "Number formatting failed" );
00079        INTL_METHOD_RETVAL_UTF8( mfo, formatted, formatted_len, 1 );
00080 }
00081 /* }}} */
00082 
00083 /* {{{ proto mixed MessageFormatter::format( array $args )
00084  * Format a message. }}} */
00085 /* {{{ proto mixed msgfmt_format( MessageFormatter $nf, array $args )
00086  * Format a message.
00087  */
00088 PHP_FUNCTION( msgfmt_format )
00089 {
00090        zval *args;
00091        MSG_FORMAT_METHOD_INIT_VARS;
00092 
00093 
00094        /* Parse parameters. */
00095        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa",
00096               &object, MessageFormatter_ce_ptr,  &args ) == FAILURE )
00097        {
00098               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00099                      "msgfmt_format: unable to parse input params", 0 TSRMLS_CC );
00100 
00101               RETURN_FALSE;
00102        }
00103 
00104        /* Fetch the object. */
00105        MSG_FORMAT_METHOD_FETCH_OBJECT;
00106 
00107        msgfmt_do_format(mfo, args, return_value TSRMLS_CC);
00108 }
00109 /* }}} */
00110 
00111 /* {{{ proto mixed MessageFormatter::formatMessage( string $locale, string $pattern, array $args )
00112  * Format a message. }}} */
00113 /* {{{ proto mixed msgfmt_format_message( string $locale, string $pattern, array $args )
00114  * Format a message.
00115  */
00116 PHP_FUNCTION( msgfmt_format_message )
00117 {
00118        zval       *args;
00119        UChar      *spattern = NULL;
00120        int         spattern_len = 0;
00121        char       *pattern = NULL;
00122        int         pattern_len = 0;
00123        char       *slocale = NULL;
00124        int         slocale_len = 0;
00125        MessageFormatter_object mf = {0};
00126        MessageFormatter_object *mfo = &mf;
00127 
00128        /* Parse parameters. */
00129        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "ssa",
00130                 &slocale, &slocale_len, &pattern, &pattern_len, &args ) == FAILURE )
00131        {
00132               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00133                      "msgfmt_format_message: unable to parse input params", 0 TSRMLS_CC );
00134 
00135               RETURN_FALSE;
00136        }
00137 
00138        msgformat_data_init(&mfo->mf_data TSRMLS_CC);
00139 
00140        if(pattern && pattern_len) {
00141               intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
00142               if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
00143               {
00144                      intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00145                             "msgfmt_format_message: error converting pattern to UTF-16", 0 TSRMLS_CC );
00146                      RETURN_FALSE;
00147               }
00148        } else {
00149               spattern_len = 0;
00150               spattern = NULL;
00151        }
00152 
00153        if(slocale_len == 0) {
00154               slocale = INTL_G(default_locale);
00155        }
00156 
00157        if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
00158               intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
00159                      "msgfmt_format_message: error converting pattern to quote-friendly format", 0 TSRMLS_CC );
00160               RETURN_FALSE;
00161        }
00162 
00163        /* Create an ICU message formatter. */
00164        MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo));
00165        if(spattern && spattern_len) {
00166               efree(spattern);
00167        }
00168        INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
00169 
00170        msgfmt_do_format(mfo, args, return_value TSRMLS_CC);
00171 
00172        /* drop the temporary formatter */
00173        msgformat_data_free(&mfo->mf_data TSRMLS_CC);
00174 }
00175 /* }}} */
00176 
00177 /*
00178  * Local variables:
00179  * tab-width: 4
00180  * c-basic-offset: 4
00181  * End:
00182  * vim600: noet sw=4 ts=4 fdm=marker
00183  * vim<600: noet sw=4 ts=4
00184  */