Back to index

php5  5.3.10
msgformat.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 #include <unicode/umsg.h>
00023 
00024 #include "php_intl.h"
00025 #include "msgformat_class.h"
00026 #include "intl_convert.h"
00027 
00028 /* {{{ */
00029 static void msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) 
00030 {
00031        char*       locale;
00032        char*       pattern;
00033        int         locale_len = 0, pattern_len = 0;
00034        UChar*      spattern     = NULL;
00035        int         spattern_len = 0;
00036        zval*       object;
00037        MessageFormatter_object* mfo;
00038        intl_error_reset( NULL TSRMLS_CC );
00039 
00040        object = return_value;
00041        /* Parse parameters. */
00042        if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "ss",
00043               &locale, &locale_len, &pattern, &pattern_len ) == FAILURE )
00044        {
00045               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00046                      "msgfmt_create: unable to parse input parameters", 0 TSRMLS_CC );
00047               zval_dtor(return_value);
00048               RETURN_NULL();
00049        }
00050 
00051        INTL_CHECK_LOCALE_LEN_OBJ(locale_len, return_value);
00052        MSG_FORMAT_METHOD_FETCH_OBJECT;
00053 
00054        /* Convert pattern (if specified) to UTF-16. */
00055        if(pattern && pattern_len) {
00056               intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
00057               INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16");
00058        } else {
00059               spattern_len = 0;
00060               spattern = NULL;
00061        }
00062 
00063        if(locale_len == 0) {
00064               locale = INTL_G(default_locale);
00065        }
00066 
00067        if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
00068               INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format");
00069        }
00070 
00071        (mfo)->mf_data.orig_format = estrndup(pattern, pattern_len);
00072        (mfo)->mf_data.orig_format_len = pattern_len;
00073        
00074        /* Create an ICU message formatter. */
00075        MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(mfo));
00076 
00077        if(spattern) {
00078               efree(spattern);
00079        }
00080 
00081        INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed");
00082 }
00083 /* }}} */
00084 
00085 /* {{{ proto MessageFormatter MesssageFormatter::create( string $locale, string $pattern )
00086  * Create formatter. }}} */
00087 /* {{{ proto MessageFormatter msgfmt_create( string $locale, string $pattern )
00088  * Create formatter.
00089  */
00090 PHP_FUNCTION( msgfmt_create )
00091 {
00092        object_init_ex( return_value, MessageFormatter_ce_ptr );
00093        msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
00094 }
00095 /* }}} */
00096 
00097 /* {{{ proto void MessageFormatter::__construct( string $locale, string $pattern )
00098  * MessageFormatter object constructor.
00099  */
00100 PHP_METHOD( MessageFormatter, __construct )
00101 {
00102        return_value = getThis();
00103        msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
00104 }
00105 /* }}} */
00106 
00107 /* {{{ proto int MessageFormatter::getErrorCode()
00108  * Get formatter's last error code. }}} */
00109 /* {{{ proto int msgfmt_get_error_code( MessageFormatter $nf )
00110  * Get formatter's last error code.
00111  */
00112 PHP_FUNCTION( msgfmt_get_error_code )
00113 {
00114        zval*                    object  = NULL;
00115        MessageFormatter_object*  mfo     = NULL;
00116 
00117        /* Parse parameters. */
00118        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
00119               &object, MessageFormatter_ce_ptr ) == FAILURE )
00120        {
00121               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00122                      "msgfmt_get_error_code: unable to parse input params", 0 TSRMLS_CC );
00123 
00124               RETURN_FALSE;
00125        }
00126 
00127        mfo = (MessageFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
00128 
00129        /* Return formatter's last error code. */
00130        RETURN_LONG( INTL_DATA_ERROR_CODE(mfo) );
00131 }
00132 /* }}} */
00133 
00134 /* {{{ proto string MessageFormatter::getErrorMessage( )
00135  * Get text description for formatter's last error code. }}} */
00136 /* {{{ proto string msgfmt_get_error_message( MessageFormatter $coll )
00137  * Get text description for formatter's last error code.
00138  */
00139 PHP_FUNCTION( msgfmt_get_error_message )
00140 {
00141        char*                    message = NULL;
00142        zval*                    object  = NULL;
00143        MessageFormatter_object*  mfo     = NULL;
00144 
00145        /* Parse parameters. */
00146        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
00147               &object, MessageFormatter_ce_ptr ) == FAILURE )
00148        {
00149               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00150                      "msgfmt_get_error_message: unable to parse input params", 0 TSRMLS_CC );
00151 
00152               RETURN_FALSE;
00153        }
00154 
00155        mfo = (MessageFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
00156 
00157        /* Return last error message. */
00158        message = intl_error_get_message( &mfo->mf_data.error TSRMLS_CC );
00159        RETURN_STRING( message, 0);
00160 }
00161 /* }}} */
00162 
00163 /*
00164  * Local variables:
00165  * tab-width: 4
00166  * c-basic-offset: 4
00167  * End:
00168  * vim600: noet sw=4 ts=4 fdm=marker
00169  * vim<600: noet sw=4 ts=4
00170  */