Back to index

php5  5.3.10
formatter_main.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 "formatter_class.h"
00025 #include "intl_convert.h"
00026 
00027 /* {{{ */
00028 static void numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
00029 {
00030        char*       locale;
00031        char*       pattern = NULL;
00032        int         locale_len = 0, pattern_len = 0;
00033        long        style;
00034        UChar*      spattern     = NULL;
00035        int         spattern_len = 0;
00036        FORMATTER_METHOD_INIT_VARS;
00037 
00038        /* Parse parameters. */
00039        if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "sl|s",
00040               &locale, &locale_len, &style, &pattern, &pattern_len ) == FAILURE )
00041        {
00042               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00043                      "numfmt_create: unable to parse input parameters", 0 TSRMLS_CC );
00044               zval_dtor(return_value);
00045               RETURN_NULL();
00046        }
00047 
00048        INTL_CHECK_LOCALE_LEN_OBJ(locale_len, return_value);
00049        object = return_value;
00050        FORMATTER_METHOD_FETCH_OBJECT;
00051 
00052        /* Convert pattern (if specified) to UTF-16. */
00053        if(pattern && pattern_len) {
00054               intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(nfo));
00055               INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: error converting pattern to UTF-16");
00056        }
00057 
00058        if(locale_len == 0) {
00059               locale = INTL_G(default_locale);
00060        }
00061 
00062        /* Create an ICU number formatter. */
00063        FORMATTER_OBJECT(nfo) = unum_open(style, spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(nfo));
00064 
00065        if(spattern) {
00066               efree(spattern);
00067        }
00068 
00069        INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: number formatter creation failed");
00070 }
00071 /* }}} */
00072 
00073 /* {{{ proto NumberFormatter NumberFormatter::create( string $locale, int style[, string $pattern ] )
00074  * Create number formatter. }}} */
00075 /* {{{ proto NumberFormatter numfmt_create( string $locale, int style[, string $pattern ] )
00076  * Create number formatter.
00077  */
00078 PHP_FUNCTION( numfmt_create )
00079 {
00080        object_init_ex( return_value, NumberFormatter_ce_ptr );
00081        numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
00082 }
00083 /* }}} */
00084 
00085 /* {{{ proto void NumberFormatter::__construct( string $locale, int style[, string $pattern ] )
00086  * NumberFormatter object constructor.
00087  */
00088 PHP_METHOD( NumberFormatter, __construct )
00089 {
00090        return_value = getThis();
00091        numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
00092 }
00093 /* }}} */
00094 
00095 /* {{{ proto int NumberFormatter::getErrorCode()
00096  * Get formatter's last error code. }}} */
00097 /* {{{ proto int numfmt_get_error_code( NumberFormatter $nf )
00098  * Get formatter's last error code.
00099  */
00100 PHP_FUNCTION( numfmt_get_error_code )
00101 {
00102        FORMATTER_METHOD_INIT_VARS
00103 
00104        /* Parse parameters. */
00105        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
00106               &object, NumberFormatter_ce_ptr ) == FAILURE )
00107        {
00108               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00109                      "numfmt_get_error_code: unable to parse input params", 0 TSRMLS_CC );
00110 
00111               RETURN_FALSE;
00112        }
00113 
00114        nfo = (NumberFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
00115 
00116        /* Return formatter's last error code. */
00117        RETURN_LONG( INTL_DATA_ERROR_CODE(nfo) );
00118 }
00119 /* }}} */
00120 
00121 /* {{{ proto string NumberFormatter::getErrorMessage( )
00122  * Get text description for formatter's last error code. }}} */
00123 /* {{{ proto string numfmt_get_error_message( NumberFormatter $nf )
00124  * Get text description for formatter's last error code.
00125  */
00126 PHP_FUNCTION( numfmt_get_error_message )
00127 {
00128        char*                    message = NULL;
00129        FORMATTER_METHOD_INIT_VARS
00130 
00131        /* Parse parameters. */
00132        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
00133               &object, NumberFormatter_ce_ptr ) == FAILURE )
00134        {
00135               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00136                      "numfmt_get_error_message: unable to parse input params", 0 TSRMLS_CC );
00137 
00138               RETURN_FALSE;
00139        }
00140 
00141        nfo = (NumberFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
00142 
00143        /* Return last error message. */
00144        message = intl_error_get_message( INTL_DATA_ERROR_P(nfo) TSRMLS_CC );
00145        RETURN_STRING( message, 0);
00146 }
00147 /* }}} */
00148 
00149 /*
00150  * Local variables:
00151  * tab-width: 4
00152  * c-basic-offset: 4
00153  * End:
00154  * vim600: noet sw=4 ts=4 fdm=marker
00155  * vim<600: noet sw=4 ts=4
00156  */