Back to index

php5  5.3.10
formatter_attr.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 "php_intl.h"
00022 #include "formatter_class.h"
00023 #include "formatter_attr.h"
00024 #include "intl_convert.h"
00025 
00026 #include <unicode/ustring.h>
00027 
00028 /* {{{ proto mixed NumberFormatter::getAttribute( int $attr )
00029  * Get formatter attribute value. }}} */
00030 /* {{{ proto mixed numfmt_get_attribute( NumberFormatter $nf, int $attr )
00031  * Get formatter attribute value.
00032  */
00033 PHP_FUNCTION( numfmt_get_attribute )
00034 {
00035        long attribute, value;
00036        FORMATTER_METHOD_INIT_VARS;
00037 
00038        /* Parse parameters. */
00039        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol",
00040               &object, NumberFormatter_ce_ptr, &attribute ) == FAILURE )
00041        {
00042               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00043                      "numfmt_get_attribute: unable to parse input params", 0 TSRMLS_CC );
00044 
00045               RETURN_FALSE;
00046        }
00047 
00048        /* Fetch the object. */
00049        FORMATTER_METHOD_FETCH_OBJECT;
00050 
00051        switch(attribute) {
00052               case UNUM_PARSE_INT_ONLY:
00053               case UNUM_GROUPING_USED:
00054               case UNUM_DECIMAL_ALWAYS_SHOWN:
00055               case UNUM_MAX_INTEGER_DIGITS:
00056               case UNUM_MIN_INTEGER_DIGITS:
00057               case UNUM_INTEGER_DIGITS:
00058               case UNUM_MAX_FRACTION_DIGITS:
00059               case UNUM_MIN_FRACTION_DIGITS:
00060               case UNUM_FRACTION_DIGITS:
00061               case UNUM_MULTIPLIER:
00062               case UNUM_GROUPING_SIZE:
00063               case UNUM_ROUNDING_MODE:
00064               case UNUM_FORMAT_WIDTH:
00065               case UNUM_PADDING_POSITION:
00066               case UNUM_SECONDARY_GROUPING_SIZE:
00067               case UNUM_SIGNIFICANT_DIGITS_USED:
00068               case UNUM_MIN_SIGNIFICANT_DIGITS:
00069               case UNUM_MAX_SIGNIFICANT_DIGITS:
00070               case UNUM_LENIENT_PARSE:
00071                      value = unum_getAttribute(FORMATTER_OBJECT(nfo), attribute);
00072                      if(value == -1) {
00073                             INTL_DATA_ERROR_CODE(nfo) = U_UNSUPPORTED_ERROR;
00074                      } else {
00075                             RETVAL_LONG(value);
00076                      }
00077                      break;
00078               case UNUM_ROUNDING_INCREMENT:
00079               {
00080                      double value_double = unum_getDoubleAttribute(FORMATTER_OBJECT(nfo), attribute);
00081                      if(value_double == -1) {
00082                             INTL_DATA_ERROR_CODE(nfo) = U_UNSUPPORTED_ERROR;
00083                      } else {
00084                             RETVAL_DOUBLE(value_double);
00085                      }
00086               }
00087                      break;
00088               default:
00089                      INTL_DATA_ERROR_CODE(nfo) = U_UNSUPPORTED_ERROR;
00090                      break;
00091        }
00092 
00093        INTL_METHOD_CHECK_STATUS( nfo, "Error getting attribute value" );
00094 }
00095 /* }}} */
00096 
00097 /* {{{ proto string NumberFormatter::getTextAttribute( int $attr )
00098  * Get formatter attribute value. }}} */
00099 /* {{{ proto string numfmt_get_text_attribute( NumberFormatter $nf, int $attr )
00100  * Get formatter attribute value.
00101  */
00102 PHP_FUNCTION( numfmt_get_text_attribute )
00103 {
00104        long   attribute;
00105        UChar  value_buf[64];
00106        int    value_buf_size = USIZE( value_buf );
00107        UChar* value  = value_buf;
00108        int    length = 0;
00109        FORMATTER_METHOD_INIT_VARS;
00110 
00111        /* Parse parameters. */
00112        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol",
00113               &object, NumberFormatter_ce_ptr, &attribute ) == FAILURE )
00114        {
00115               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00116                      "numfmt_get_text_attribute: unable to parse input params", 0 TSRMLS_CC );
00117 
00118               RETURN_FALSE;
00119        }
00120 
00121        /* Fetch the object. */
00122        FORMATTER_METHOD_FETCH_OBJECT;
00123 
00124        length = unum_getTextAttribute( FORMATTER_OBJECT(nfo), attribute, value, value_buf_size, &INTL_DATA_ERROR_CODE(nfo) );
00125        if(INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR && length >= value_buf_size) {
00126               ++length; /* to avoid U_STRING_NOT_TERMINATED_WARNING */
00127               INTL_DATA_ERROR_CODE(nfo) = U_ZERO_ERROR;
00128               value = eumalloc(length);
00129               length = unum_getTextAttribute( FORMATTER_OBJECT(nfo), attribute, value, length, &INTL_DATA_ERROR_CODE(nfo) );
00130               if(U_FAILURE(INTL_DATA_ERROR_CODE(nfo))) {
00131                      efree(value);
00132                      value = value_buf;
00133               }
00134        }
00135        INTL_METHOD_CHECK_STATUS( nfo, "Error getting attribute value" );
00136 
00137        INTL_METHOD_RETVAL_UTF8( nfo, value, length, ( value != value_buf ) );
00138 }
00139 /* }}} */
00140 
00141 /* {{{ proto bool NumberFormatter::setAttribute( int $attr, mixed $value )
00142  * Get formatter attribute value. }}} */
00143 /* {{{ proto bool numfmt_set_attribute( NumberFormatter $nf, int $attr, mixed $value )
00144  * Get formatter attribute value.
00145  */
00146 PHP_FUNCTION( numfmt_set_attribute )
00147 {
00148        long attribute;
00149        zval **value;
00150        FORMATTER_METHOD_INIT_VARS;
00151 
00152        /* Parse parameters. */
00153        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OlZ",
00154               &object, NumberFormatter_ce_ptr, &attribute, &value ) == FAILURE)
00155        {
00156               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00157                      "numfmt_set_attribute: unable to parse input params", 0 TSRMLS_CC );
00158 
00159               RETURN_FALSE;
00160        }
00161 
00162        /* Fetch the object. */
00163        FORMATTER_METHOD_FETCH_OBJECT;
00164 
00165        switch(attribute) {
00166               case UNUM_PARSE_INT_ONLY:
00167               case UNUM_GROUPING_USED:
00168               case UNUM_DECIMAL_ALWAYS_SHOWN:
00169               case UNUM_MAX_INTEGER_DIGITS:
00170               case UNUM_MIN_INTEGER_DIGITS:
00171               case UNUM_INTEGER_DIGITS:
00172               case UNUM_MAX_FRACTION_DIGITS:
00173               case UNUM_MIN_FRACTION_DIGITS:
00174               case UNUM_FRACTION_DIGITS:
00175               case UNUM_MULTIPLIER:
00176               case UNUM_GROUPING_SIZE:
00177               case UNUM_ROUNDING_MODE:
00178               case UNUM_FORMAT_WIDTH:
00179               case UNUM_PADDING_POSITION:
00180               case UNUM_SECONDARY_GROUPING_SIZE:
00181               case UNUM_SIGNIFICANT_DIGITS_USED:
00182               case UNUM_MIN_SIGNIFICANT_DIGITS:
00183               case UNUM_MAX_SIGNIFICANT_DIGITS:
00184               case UNUM_LENIENT_PARSE:
00185                      convert_to_long_ex(value);
00186                      unum_setAttribute(FORMATTER_OBJECT(nfo), attribute, Z_LVAL_PP(value));
00187                      break;
00188               case UNUM_ROUNDING_INCREMENT:
00189                      convert_to_double_ex(value);
00190                      unum_setDoubleAttribute(FORMATTER_OBJECT(nfo), attribute, Z_DVAL_PP(value));
00191                      break;
00192               default:
00193                      INTL_DATA_ERROR_CODE(nfo) = U_UNSUPPORTED_ERROR;
00194                      break;
00195        }
00196 
00197        INTL_METHOD_CHECK_STATUS( nfo, "Error setting attribute value" );
00198 
00199        RETURN_TRUE;
00200 }
00201 /* }}} */
00202 
00203 /* {{{ proto bool NumberFormatter::setTextAttribute( int $attr, string $value )
00204  * Get formatter attribute value. }}} */
00205 /* {{{ proto bool numfmt_set_text_attribute( NumberFormatter $nf, int $attr, string $value )
00206  * Get formatter attribute value.
00207  */
00208 PHP_FUNCTION( numfmt_set_text_attribute )
00209 {
00210        int slength = 0;
00211        UChar *svalue = NULL;
00212        long attribute;
00213        char *value;
00214        int len;
00215        FORMATTER_METHOD_INIT_VARS;
00216 
00217        /* Parse parameters. */
00218        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ols",
00219               &object, NumberFormatter_ce_ptr, &attribute, &value, &len ) == FAILURE)
00220        {
00221               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00222                      "numfmt_set_text_attribute: unable to parse input params", 0 TSRMLS_CC );
00223 
00224               RETURN_FALSE;
00225        }
00226 
00227        /* Fetch the object. */
00228        FORMATTER_METHOD_FETCH_OBJECT;
00229 
00230        /* Convert given attribute value to UTF-16. */
00231        intl_convert_utf8_to_utf16(&svalue, &slength, value, len, &INTL_DATA_ERROR_CODE(nfo));
00232        INTL_METHOD_CHECK_STATUS( nfo, "Error converting attribute value to UTF-16" );
00233 
00234        /* Actually set new attribute value. */
00235        unum_setTextAttribute(FORMATTER_OBJECT(nfo), attribute, svalue, slength, &INTL_DATA_ERROR_CODE(nfo));
00236        if (svalue) {
00237               efree(svalue);
00238        }
00239        INTL_METHOD_CHECK_STATUS( nfo, "Error setting text attribute" );
00240 
00241        RETURN_TRUE;
00242 }
00243 /* }}} */
00244 
00245 /* {{{ proto string NumberFormatter::getSymbol( int $attr )
00246  * Get formatter symbol value. }}} */
00247 /* {{{ proto string numfmt_get_symbol( NumberFormatter $nf, int $attr )
00248  * Get formatter symbol value.
00249  */
00250 PHP_FUNCTION( numfmt_get_symbol )
00251 {
00252        long symbol;
00253        UChar value_buf[4];
00254        UChar *value = value_buf;
00255        int length = USIZE(value_buf);
00256        FORMATTER_METHOD_INIT_VARS;
00257 
00258        /* Parse parameters. */
00259        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol",
00260               &object, NumberFormatter_ce_ptr, &symbol ) == FAILURE )
00261        {
00262               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00263                      "numfmt_get_symbol: unable to parse input params", 0 TSRMLS_CC );
00264 
00265               RETURN_FALSE;
00266        }
00267        
00268        if(symbol >= UNUM_FORMAT_SYMBOL_COUNT || symbol < 0) {
00269               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,  "numfmt_get_symbol: invalid symbol value", 0 TSRMLS_CC );
00270               RETURN_FALSE;
00271        }
00272 
00273        /* Fetch the object. */
00274        FORMATTER_METHOD_FETCH_OBJECT;
00275 
00276        length = unum_getSymbol(FORMATTER_OBJECT(nfo), symbol, value_buf, length, &INTL_DATA_ERROR_CODE(nfo));
00277        if(INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR && length >= USIZE( value_buf )) {
00278               ++length; /* to avoid U_STRING_NOT_TERMINATED_WARNING */
00279               INTL_DATA_ERROR_CODE(nfo) = U_ZERO_ERROR;
00280               value = eumalloc(length);
00281               length = unum_getSymbol(FORMATTER_OBJECT(nfo), symbol, value, length, &INTL_DATA_ERROR_CODE(nfo));
00282               if(U_FAILURE(INTL_DATA_ERROR_CODE(nfo))) {
00283                      efree(value);
00284                      value = value_buf;
00285               }
00286        }
00287        INTL_METHOD_CHECK_STATUS( nfo, "Error getting symbol value" );
00288 
00289        INTL_METHOD_RETVAL_UTF8( nfo, value, length, ( value_buf != value ) );
00290 }
00291 /* }}} */
00292 
00293 /* {{{ proto bool NumberFormatter::setSymbol( int $attr, string $symbol )
00294  * Set formatter symbol value. }}} */
00295 /* {{{ proto bool numfmt_set_symbol( NumberFormatter $nf, int $attr, string $symbol )
00296  * Set formatter symbol value.
00297  */
00298 PHP_FUNCTION( numfmt_set_symbol )
00299 {
00300        long       symbol;
00301        char*      value     = NULL;
00302        int        value_len = 0;
00303        UChar*     svalue  = 0;
00304        int        slength = 0;
00305        FORMATTER_METHOD_INIT_VARS;
00306 
00307        /* Parse parameters. */
00308        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ols",
00309               &object, NumberFormatter_ce_ptr, &symbol, &value, &value_len ) == FAILURE )
00310        {
00311               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00312                      "numfmt_set_symbol: unable to parse input params", 0 TSRMLS_CC );
00313 
00314               RETURN_FALSE;
00315        }
00316        
00317        if (symbol >= UNUM_FORMAT_SYMBOL_COUNT || symbol < 0) {
00318               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,  "numfmt_set_symbol: invalid symbol value", 0 TSRMLS_CC );
00319               RETURN_FALSE;
00320        }
00321 
00322        /* Fetch the object. */
00323        FORMATTER_METHOD_FETCH_OBJECT;
00324 
00325        /* Convert given symbol to UTF-16. */
00326        intl_convert_utf8_to_utf16(&svalue, &slength, value, value_len, &INTL_DATA_ERROR_CODE(nfo));
00327        INTL_METHOD_CHECK_STATUS( nfo, "Error converting symbol value to UTF-16" );
00328 
00329        /* Actually set the symbol. */
00330        unum_setSymbol(FORMATTER_OBJECT(nfo), symbol, svalue, slength, &INTL_DATA_ERROR_CODE(nfo));
00331        if (svalue) {
00332               efree(svalue);
00333        }
00334        INTL_METHOD_CHECK_STATUS( nfo, "Error setting symbol value" );
00335 
00336        RETURN_TRUE;
00337 }
00338 /* }}} */
00339 
00340 /* {{{ proto string NumberFormatter::getPattern( )
00341  * Get formatter pattern. }}} */
00342 /* {{{ proto string numfmt_get_pattern( NumberFormatter $nf )
00343  * Get formatter pattern.
00344  */
00345 PHP_FUNCTION( numfmt_get_pattern )
00346 {
00347        UChar  value_buf[64];
00348        int    length = USIZE( value_buf );
00349        UChar* value  = value_buf;
00350        FORMATTER_METHOD_INIT_VARS;
00351 
00352        /* Parse parameters. */
00353        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
00354               &object, NumberFormatter_ce_ptr ) == FAILURE )
00355        {
00356               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00357                      "numfmt_get_pattern: unable to parse input params", 0 TSRMLS_CC );
00358 
00359               RETURN_FALSE;
00360        }
00361 
00362        /* Fetch the object. */
00363        FORMATTER_METHOD_FETCH_OBJECT;
00364 
00365        length = unum_toPattern(FORMATTER_OBJECT(nfo), 0, value, length, &INTL_DATA_ERROR_CODE(nfo));
00366        if(INTL_DATA_ERROR_CODE(nfo) == U_BUFFER_OVERFLOW_ERROR && length >= USIZE( value_buf )) {
00367               ++length; /* to avoid U_STRING_NOT_TERMINATED_WARNING */
00368               INTL_DATA_ERROR_CODE(nfo) = U_ZERO_ERROR;
00369               value = eumalloc(length);
00370               length = unum_toPattern( FORMATTER_OBJECT(nfo), 0, value, length, &INTL_DATA_ERROR_CODE(nfo) );
00371               if(U_FAILURE(INTL_DATA_ERROR_CODE(nfo))) {
00372                      efree(value);
00373                      value = value_buf;
00374               }
00375        }
00376        INTL_METHOD_CHECK_STATUS( nfo, "Error getting formatter pattern" );
00377 
00378        INTL_METHOD_RETVAL_UTF8( nfo, value, length, ( value != value_buf ) );
00379 }
00380 /* }}} */
00381 
00382 /* {{{ proto bool NumberFormatter::setPattern( string $pattern )
00383  * Set formatter pattern. }}} */
00384 /* {{{ proto bool numfmt_set_pattern( NumberFormatter $nf, string $pattern )
00385  * Set formatter pattern.
00386  */
00387 PHP_FUNCTION( numfmt_set_pattern )
00388 {
00389        char*       value = NULL;
00390        int         value_len = 0;
00391        int         slength = 0;
00392        UChar*     svalue  = NULL;
00393        FORMATTER_METHOD_INIT_VARS;
00394 
00395        /* Parse parameters. */
00396        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
00397               &object, NumberFormatter_ce_ptr, &value, &value_len ) == FAILURE )
00398        {
00399               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00400                      "numfmt_set_pattern: unable to parse input params", 0 TSRMLS_CC );
00401 
00402               RETURN_FALSE;
00403        }
00404 
00405        FORMATTER_METHOD_FETCH_OBJECT;
00406 
00407        /* Convert given pattern to UTF-16. */
00408        intl_convert_utf8_to_utf16(&svalue, &slength, value, value_len, &INTL_DATA_ERROR_CODE(nfo));
00409        INTL_METHOD_CHECK_STATUS( nfo, "Error converting pattern to UTF-16" );
00410 
00411        /* TODO: add parse error information */
00412        unum_applyPattern(FORMATTER_OBJECT(nfo), 0, svalue, slength, NULL, &INTL_DATA_ERROR_CODE(nfo));
00413        if (svalue) {
00414               efree(svalue);
00415        }
00416        INTL_METHOD_CHECK_STATUS( nfo, "Error setting pattern value" );
00417 
00418        RETURN_TRUE;
00419 }
00420 /* }}} */
00421 
00422 /* {{{ proto string NumberFormatter::getLocale([int type])
00423  * Get formatter locale. }}} */
00424 /* {{{ proto string numfmt_get_locale( NumberFormatter $nf[, int type] )
00425  * Get formatter locale.
00426  */
00427 PHP_FUNCTION( numfmt_get_locale )
00428 {
00429        long type = ULOC_ACTUAL_LOCALE;
00430        char* loc;
00431        FORMATTER_METHOD_INIT_VARS;
00432 
00433        /* Parse parameters. */
00434        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|l",
00435               &object, NumberFormatter_ce_ptr, &type ) == FAILURE )
00436        {
00437               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00438                      "numfmt_get_locale: unable to parse input params", 0 TSRMLS_CC );
00439 
00440               RETURN_FALSE;
00441        }
00442 
00443        /* Fetch the object. */
00444        FORMATTER_METHOD_FETCH_OBJECT;
00445 
00446        loc = (char *)unum_getLocaleByType(FORMATTER_OBJECT(nfo), type, &INTL_DATA_ERROR_CODE(nfo));
00447        INTL_METHOD_CHECK_STATUS( nfo, "Error getting locale" );
00448        RETURN_STRING(loc, 1);
00449 }
00450 /* }}} */
00451 
00452 /*
00453  * Local variables:
00454  * tab-width: 4
00455  * c-basic-offset: 4
00456  * End:
00457  * vim600: noet sw=4 ts=4 fdm=marker
00458  * vim<600: noet sw=4 ts=4
00459  */