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 :
23 : #include "php_intl.h"
24 : #include "msgformat_class.h"
25 : #include "msgformat_parse.h"
26 : #include "msgformat_data.h"
27 : #include "msgformat_helpers.h"
28 : #include "intl_convert.h"
29 :
30 : /* {{{ */
31 : static void msgfmt_do_parse(MessageFormatter_object *mfo, char *source, int src_len, zval *return_value TSRMLS_DC)
32 20 : {
33 : zval **fargs;
34 20 : int count = 0;
35 : int i;
36 20 : UChar *usource = NULL;
37 20 : int usrc_len = 0;
38 :
39 20 : intl_convert_utf8_to_utf16(&usource, &usrc_len, source, src_len, &INTL_DATA_ERROR_CODE(mfo));
40 20 : INTL_METHOD_CHECK_STATUS(mfo, "Converting parse string failed");
41 :
42 20 : umsg_parse_helper(MSG_FORMAT_OBJECT(mfo), &count, &fargs, usource, usrc_len, &INTL_DATA_ERROR_CODE(mfo));
43 20 : efree(usource);
44 20 : INTL_METHOD_CHECK_STATUS(mfo, "Parsing failed");
45 :
46 20 : array_init(return_value);
47 72 : for(i=0;i<count;i++) {
48 52 : add_next_index_zval(return_value, fargs[i]);
49 : }
50 20 : efree(fargs);
51 : }
52 : /* }}} */
53 :
54 : /* {{{ proto array MessageFormatter::parse( string $source )
55 : * Parse a message }}} */
56 : /* {{{ proto array msgfmt_parse( MessageFormatter $nf, string $source )
57 : * Parse a message.
58 : */
59 : PHP_FUNCTION( msgfmt_parse )
60 10 : {
61 : char *source;
62 : int source_len;
63 10 : MSG_FORMAT_METHOD_INIT_VARS;
64 :
65 :
66 : /* Parse parameters. */
67 10 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
68 : &object, MessageFormatter_ce_ptr, &source, &source_len ) == FAILURE )
69 : {
70 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
71 : "msgfmt_parse: unable to parse input params", 0 TSRMLS_CC );
72 :
73 0 : RETURN_FALSE;
74 : }
75 :
76 : /* Fetch the object. */
77 10 : MSG_FORMAT_METHOD_FETCH_OBJECT;
78 :
79 10 : msgfmt_do_parse(mfo, source, source_len, return_value TSRMLS_CC);
80 : }
81 : /* }}} */
82 :
83 : /* {{{ proto array MessageFormatter::formatMessage( string $locale, string $pattern, string $source )
84 : * Parse a message. }}} */
85 : /* {{{ proto array numfmt_parse_message( string $locale, string $pattern, string $source )
86 : * Parse a message.
87 : */
88 : PHP_FUNCTION( msgfmt_parse_message )
89 10 : {
90 10 : UChar *spattern = NULL;
91 10 : int spattern_len = 0;
92 10 : char *pattern = NULL;
93 10 : int pattern_len = 0;
94 10 : char *slocale = NULL;
95 10 : int slocale_len = 0;
96 10 : char *source = NULL;
97 10 : int src_len = 0;
98 10 : MessageFormatter_object mf = {0};
99 10 : MessageFormatter_object *mfo = &mf;
100 :
101 : /* Parse parameters. */
102 10 : if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "sss",
103 : &slocale, &slocale_len, &pattern, &pattern_len, &source, &src_len ) == FAILURE )
104 : {
105 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
106 : "msgfmt_parse_message: unable to parse input params", 0 TSRMLS_CC );
107 :
108 0 : RETURN_FALSE;
109 : }
110 :
111 10 : msgformat_data_init(&mfo->mf_data TSRMLS_CC);
112 :
113 20 : if(pattern && pattern_len) {
114 10 : intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
115 10 : if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
116 : {
117 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
118 : "msgfmt_parse_message: error converting pattern to UTF-16", 0 TSRMLS_CC );
119 0 : RETURN_FALSE;
120 : }
121 : } else {
122 0 : spattern_len = 0;
123 0 : spattern = NULL;
124 : }
125 :
126 10 : if(slocale_len == 0) {
127 0 : slocale = INTL_G(default_locale);
128 : }
129 :
130 10 : if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
131 0 : intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
132 : "msgfmt_parse_message: error converting pattern to quote-friendly format", 0 TSRMLS_CC );
133 0 : RETURN_FALSE;
134 : }
135 :
136 : /* Create an ICU message formatter. */
137 10 : MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo));
138 10 : if(spattern && spattern_len) {
139 10 : efree(spattern);
140 : }
141 10 : INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
142 :
143 10 : msgfmt_do_parse(mfo, source, src_len, return_value TSRMLS_CC);
144 :
145 : /* drop the temporary formatter */
146 10 : msgformat_data_free(&mfo->mf_data TSRMLS_CC);
147 : }
148 : /* }}} */
149 :
150 : /*
151 : * Local variables:
152 : * tab-width: 4
153 : * c-basic-offset: 4
154 : * End:
155 : * vim600: noet sw=4 ts=4 fdm=marker
156 : * vim<600: noet sw=4 ts=4
157 : */
|