Annotation of embedaddon/php/ext/intl/formatter/formatter_parse.c, revision 1.1.1.1
1.1 misho 1: /*
2: +----------------------------------------------------------------------+
3: | PHP Version 5 |
4: +----------------------------------------------------------------------+
5: | This source file is subject to version 3.01 of the PHP license, |
6: | that is bundled with this package in the file LICENSE, and is |
7: | available through the world-wide-web at the following url: |
8: | http://www.php.net/license/3_01.txt |
9: | If you did not receive a copy of the PHP license and are unable to |
10: | obtain it through the world-wide-web, please send a note to |
11: | license@php.net so we can mail you a copy immediately. |
12: +----------------------------------------------------------------------+
13: | Authors: Stanislav Malyshev <stas@zend.com> |
14: +----------------------------------------------------------------------+
15: */
16:
17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
21: #include <unicode/ustring.h>
22: #include <locale.h>
23:
24: #include "php_intl.h"
25: #include "formatter_class.h"
26: #include "formatter_format.h"
27: #include "formatter_parse.h"
28: #include "intl_convert.h"
29:
30: #define ICU_LOCALE_BUG 1
31:
32: /* {{{ proto mixed NumberFormatter::parse( string $str[, int $type, int &$position ])
33: * Parse a number. }}} */
34: /* {{{ proto mixed numfmt_parse( NumberFormatter $nf, string $str[, int $type, int &$position ])
35: * Parse a number.
36: */
37: PHP_FUNCTION( numfmt_parse )
38: {
39: long type = FORMAT_TYPE_DOUBLE;
40: UChar* sstr = NULL;
41: int sstr_len = 0;
42: char* str = NULL;
43: int str_len;
44: int32_t val32, position = 0;
45: int64_t val64;
46: double val_double;
47: int32_t* position_p = NULL;
48: zval *zposition = NULL;
49: char *oldlocale;
50: FORMATTER_METHOD_INIT_VARS;
51:
52: /* Parse parameters. */
53: if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|lz!",
54: &object, NumberFormatter_ce_ptr, &str, &str_len, &type, &zposition ) == FAILURE )
55: {
56: intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
57: "number_parse: unable to parse input params", 0 TSRMLS_CC );
58:
59: RETURN_FALSE;
60: }
61:
62: /* Fetch the object. */
63: FORMATTER_METHOD_FETCH_OBJECT;
64:
65: /* Convert given string to UTF-16. */
66: intl_convert_utf8_to_utf16(&sstr, &sstr_len, str, str_len, &INTL_DATA_ERROR_CODE(nfo));
67: INTL_METHOD_CHECK_STATUS( nfo, "String conversion to UTF-16 failed" );
68:
69: if(zposition) {
70: convert_to_long(zposition);
71: position = (int32_t)Z_LVAL_P( zposition );
72: position_p = &position;
73: }
74:
75: #if ICU_LOCALE_BUG && defined(LC_NUMERIC)
76: oldlocale = setlocale(LC_NUMERIC, "C");
77: #endif
78:
79: switch(type) {
80: case FORMAT_TYPE_INT32:
81: val32 = unum_parse(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
82: RETVAL_LONG(val32);
83: break;
84: case FORMAT_TYPE_INT64:
85: val64 = unum_parseInt64(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
86: if(val64 > LONG_MAX || val64 < -LONG_MAX) {
87: RETVAL_DOUBLE(val64);
88: } else {
89: val32 = (int32_t)val64;
90: RETVAL_LONG(val32);
91: }
92: break;
93: case FORMAT_TYPE_DOUBLE:
94: val_double = unum_parseDouble(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, &INTL_DATA_ERROR_CODE(nfo));
95: RETVAL_DOUBLE(val_double);
96: break;
97: default:
98: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported format type %ld", type);
99: RETVAL_FALSE;
100: break;
101: }
102: #if ICU_LOCALE_BUG && defined(LC_NUMERIC)
103: setlocale(LC_NUMERIC, oldlocale);
104: #endif
105: if(zposition) {
106: zval_dtor(zposition);
107: ZVAL_LONG(zposition, position);
108: }
109:
110: if (sstr) {
111: efree(sstr);
112: }
113:
114: INTL_METHOD_CHECK_STATUS( nfo, "Number parsing failed" );
115: }
116: /* }}} */
117:
118: /* {{{ proto double NumberFormatter::parseCurrency( string $str, string $¤cy[, int $&position] )
119: * Parse a number as currency. }}} */
120: /* {{{ proto double numfmt_parse_currency( NumberFormatter $nf, string $str, string $¤cy[, int $&position] )
121: * Parse a number as currency.
122: */
123: PHP_FUNCTION( numfmt_parse_currency )
124: {
125: double number;
126: UChar currency[5] = {0};
127: UChar* sstr = NULL;
128: int sstr_len = 0;
129: char *currency_str = NULL;
130: int currency_len = 0;
131: char *str;
132: int str_len;
133: int32_t* position_p = NULL;
134: int32_t position = 0;
135: zval *zcurrency, *zposition = NULL;
136: FORMATTER_METHOD_INIT_VARS;
137:
138: /* Parse parameters. */
139: if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osz|z!",
140: &object, NumberFormatter_ce_ptr, &str, &str_len, &zcurrency, &zposition ) == FAILURE )
141: {
142: intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
143: "number_parse_currency: unable to parse input params", 0 TSRMLS_CC );
144:
145: RETURN_FALSE;
146: }
147:
148: /* Fetch the object. */
149: FORMATTER_METHOD_FETCH_OBJECT;
150:
151: /* Convert given string to UTF-16. */
152: intl_convert_utf8_to_utf16(&sstr, &sstr_len, str, str_len, &INTL_DATA_ERROR_CODE(nfo));
153: INTL_METHOD_CHECK_STATUS( nfo, "String conversion to UTF-16 failed" );
154:
155: if(zposition) {
156: convert_to_long(zposition);
157: position = (int32_t)Z_LVAL_P( zposition );
158: position_p = &position;
159: }
160:
161: number = unum_parseDoubleCurrency(FORMATTER_OBJECT(nfo), sstr, sstr_len, position_p, currency, &INTL_DATA_ERROR_CODE(nfo));
162: if(zposition) {
163: zval_dtor(zposition);
164: ZVAL_LONG(zposition, position);
165: }
166: if (sstr) {
167: efree(sstr);
168: }
169: INTL_METHOD_CHECK_STATUS( nfo, "Number parsing failed" );
170:
171: /* Convert parsed currency to UTF-8 and pass it back to caller. */
172: intl_convert_utf16_to_utf8(¤cy_str, ¤cy_len, currency, u_strlen(currency), &INTL_DATA_ERROR_CODE(nfo));
173: INTL_METHOD_CHECK_STATUS( nfo, "Currency conversion to UTF-8 failed" );
174: zval_dtor( zcurrency );
175: ZVAL_STRINGL(zcurrency, currency_str, currency_len, 0);
176:
177: RETVAL_DOUBLE( number );
178: }
179: /* }}} */
180:
181: /*
182: * Local variables:
183: * tab-width: 4
184: * c-basic-offset: 4
185: * End:
186: * vim600: noet sw=4 ts=4 fdm=marker
187: * vim<600: noet sw=4 ts=4
188: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>