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