Back to index

php5  5.3.10
formatter_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 "formatter_class.h"
00025 #include "formatter_format.h"
00026 #include "intl_convert.h"
00027 
00028 /* {{{ proto mixed NumberFormatter::format( mixed $num[, int $type] )
00029  * Format a number. }}} */
00030 /* {{{ proto mixed numfmt_format( NumberFormatter $nf, mixed $num[, int type] )
00031  * Format a number.
00032  */
00033 PHP_FUNCTION( numfmt_format )
00034 {
00035        zval **number;
00036        long type = FORMAT_TYPE_DEFAULT;
00037        UChar format_buf[32];
00038        UChar* formatted = format_buf;
00039        int formatted_len = USIZE(format_buf);
00040        FORMATTER_METHOD_INIT_VARS;
00041 
00042        /* Parse parameters. */
00043        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OZ|l",
00044               &object, NumberFormatter_ce_ptr,  &number, &type ) == FAILURE )
00045        {
00046               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00047                      "numfmt_format: unable to parse input params", 0 TSRMLS_CC );
00048 
00049               RETURN_FALSE;
00050        }
00051 
00052        /* Fetch the object. */
00053        FORMATTER_METHOD_FETCH_OBJECT;
00054 
00055        if(type == FORMAT_TYPE_DEFAULT) {
00056               if(Z_TYPE_PP(number) == IS_STRING) {
00057                      convert_scalar_to_number_ex(number);
00058               }
00059 
00060               if(Z_TYPE_PP(number) == IS_LONG) {
00061                      /* take INT32 on 32-bit, int64 on 64-bit */
00062                      type = (sizeof(long) == 8)?FORMAT_TYPE_INT64:FORMAT_TYPE_INT32;
00063               } else if(Z_TYPE_PP(number) == IS_DOUBLE) {
00064                      type = FORMAT_TYPE_DOUBLE;
00065               } else {
00066                      type = FORMAT_TYPE_INT32;
00067               }
00068        }
00069 
00070        if(Z_TYPE_PP(number) != IS_DOUBLE && Z_TYPE_PP(number) != IS_LONG) {
00071               SEPARATE_ZVAL_IF_NOT_REF(number);
00072               convert_scalar_to_number( *number TSRMLS_CC );
00073        }
00074 
00075        switch(type) {
00076               case FORMAT_TYPE_INT32:
00077                      convert_to_long_ex(number);
00078                      formatted_len = unum_format(FORMATTER_OBJECT(nfo), (int32_t)Z_LVAL_PP(number), 
00079                             formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
00080                      if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
00081                             intl_error_reset(INTL_DATA_ERROR_P(nfo) TSRMLS_CC); 
00082                             formatted = eumalloc(formatted_len);
00083                             formatted_len = unum_format(FORMATTER_OBJECT(nfo), (int32_t)Z_LVAL_PP(number), 
00084                                    formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
00085                             if (U_FAILURE( INTL_DATA_ERROR_CODE(nfo) ) ) {
00086                                    efree(formatted);
00087                             }
00088                      }
00089                      INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
00090                      break;
00091 
00092               case FORMAT_TYPE_INT64:
00093               {
00094                      int64_t value = (Z_TYPE_PP(number) == IS_DOUBLE)?(int64_t)Z_DVAL_PP(number):Z_LVAL_PP(number);
00095                      formatted_len = unum_formatInt64(FORMATTER_OBJECT(nfo), value, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
00096                      if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
00097                             intl_error_reset(INTL_DATA_ERROR_P(nfo) TSRMLS_CC); 
00098                             formatted = eumalloc(formatted_len);
00099                             formatted_len = unum_formatInt64(FORMATTER_OBJECT(nfo), value, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
00100                             if (U_FAILURE( INTL_DATA_ERROR_CODE(nfo) ) ) {
00101                                    efree(formatted);
00102                             }
00103                      }
00104                      INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
00105               }
00106                      break;
00107 
00108               case FORMAT_TYPE_DOUBLE:
00109                      convert_to_double_ex(number);
00110                      formatted_len = unum_formatDouble(FORMATTER_OBJECT(nfo), Z_DVAL_PP(number), formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
00111                      if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
00112                             intl_error_reset(INTL_DATA_ERROR_P(nfo) TSRMLS_CC); 
00113                             formatted = eumalloc(formatted_len);
00114                             unum_formatDouble(FORMATTER_OBJECT(nfo), Z_DVAL_PP(number), formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
00115                             if (U_FAILURE( INTL_DATA_ERROR_CODE(nfo) ) ) {
00116                                    efree(formatted);
00117                             }
00118                      }
00119                      INTL_METHOD_CHECK_STATUS( nfo, "Number formatting failed" );
00120                      break;
00121 
00122               default:
00123                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported format type %ld", type);
00124                      RETURN_FALSE;
00125                      break;
00126        }
00127 
00128        INTL_METHOD_RETVAL_UTF8( nfo, formatted, formatted_len, ( formatted != format_buf ) );
00129 }
00130 /* }}} */
00131 
00132 /* {{{ proto mixed NumberFormatter::formatCurrency( double $num, string $currency )
00133  * Format a number as currency. }}} */
00134 /* {{{ proto mixed numfmt_format_currency( NumberFormatter $nf, double $num, string $currency )
00135  * Format a number as currency.
00136  */
00137 PHP_FUNCTION( numfmt_format_currency )
00138 {
00139        double     number;
00140        UChar      format_buf[32];
00141        UChar*     formatted     = format_buf;
00142        int        formatted_len = USIZE(format_buf);
00143        char*      currency      = NULL;
00144        int        currency_len  = 0;
00145        UChar*     scurrency     = NULL;
00146        int        scurrency_len = 0;
00147        FORMATTER_METHOD_INIT_VARS;
00148 
00149        /* Parse parameters. */
00150        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ods",
00151               &object, NumberFormatter_ce_ptr,  &number, &currency, &currency_len ) == FAILURE )
00152        {
00153               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00154                      "numfmt_format_currency: unable to parse input params", 0 TSRMLS_CC );
00155 
00156               RETURN_FALSE;
00157        }
00158 
00159        /* Fetch the object. */
00160        FORMATTER_METHOD_FETCH_OBJECT;
00161 
00162        /* Convert currency to UTF-16. */
00163        intl_convert_utf8_to_utf16(&scurrency, &scurrency_len, currency, currency_len, &INTL_DATA_ERROR_CODE(nfo));
00164        INTL_METHOD_CHECK_STATUS( nfo, "Currency conversion to UTF-16 failed" );
00165 
00166        /* Format the number using a fixed-length buffer. */
00167        formatted_len = unum_formatDoubleCurrency(FORMATTER_OBJECT(nfo), number, scurrency, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
00168 
00169        /* If the buffer turned out to be too small
00170         * then allocate another buffer dynamically
00171         * and use it to format the number.
00172         */
00173        if (INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR) {
00174               intl_error_reset(INTL_DATA_ERROR_P(nfo) TSRMLS_CC); 
00175               formatted = eumalloc(formatted_len);
00176               unum_formatDoubleCurrency(FORMATTER_OBJECT(nfo), number, scurrency, formatted, formatted_len, NULL, &INTL_DATA_ERROR_CODE(nfo));
00177        }
00178 
00179        if( U_FAILURE( INTL_DATA_ERROR_CODE((nfo)) ) ) {
00180               intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((nfo)) TSRMLS_CC );
00181               intl_errors_set_custom_msg( INTL_DATA_ERROR_P(nfo), "Number formatting failed", 0 TSRMLS_CC );
00182               RETVAL_FALSE;
00183               if (formatted != format_buf) {
00184                      efree(formatted);
00185               }
00186        } else {
00187               INTL_METHOD_RETVAL_UTF8( nfo, formatted, formatted_len, ( formatted != format_buf ) );
00188        }
00189 
00190        if(scurrency) {
00191               efree(scurrency);
00192        }
00193 }
00194 
00195 /* }}} */
00196 
00197 /*
00198  * Local variables:
00199  * tab-width: 4
00200  * c-basic-offset: 4
00201  * End:
00202  * vim600: noet sw=4 ts=4 fdm=marker
00203  * vim<600: noet sw=4 ts=4
00204  */