1 /*
2 +----------------------------------------------------------------------+
3 | This source file is subject to version 3.01 of the PHP license, |
4 | that is bundled with this package in the file LICENSE, and is |
5 | available through the world-wide-web at the following url: |
6 | https://www.php.net/license/3_01.txt |
7 | If you did not receive a copy of the PHP license and are unable to |
8 | obtain it through the world-wide-web, please send a note to |
9 | license@php.net so we can mail you a copy immediately. |
10 +----------------------------------------------------------------------+
11 | Authors: Stanislav Malyshev <stas@zend.com> |
12 +----------------------------------------------------------------------+
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18
19 #include <unicode/ustring.h>
20
21 #include "php_intl.h"
22 #include "msgformat_class.h"
23 #include "msgformat_data.h"
24 #include "msgformat_helpers.h"
25 #include "intl_convert.h"
26
27 /* {{{ */
msgfmt_do_parse(MessageFormatter_object * mfo,char * source,size_t src_len,zval * return_value)28 static void msgfmt_do_parse(MessageFormatter_object *mfo, char *source, size_t src_len, zval *return_value)
29 {
30 zval *fargs;
31 int count = 0;
32 int i;
33 UChar *usource = NULL;
34 int usrc_len = 0;
35
36 intl_convert_utf8_to_utf16(&usource, &usrc_len, source, src_len, &INTL_DATA_ERROR_CODE(mfo));
37 INTL_METHOD_CHECK_STATUS(mfo, "Converting parse string failed");
38
39 umsg_parse_helper(MSG_FORMAT_OBJECT(mfo), &count, &fargs, usource, usrc_len, &INTL_DATA_ERROR_CODE(mfo));
40 if (usource) {
41 efree(usource);
42 }
43 INTL_METHOD_CHECK_STATUS(mfo, "Parsing failed");
44
45 array_init(return_value);
46 for(i=0;i<count;i++) {
47 add_next_index_zval(return_value, &fargs[i]);
48 }
49 efree(fargs);
50 }
51 /* }}} */
52
53 /* {{{ Parse a message */
PHP_FUNCTION(msgfmt_parse)54 PHP_FUNCTION( msgfmt_parse )
55 {
56 char *source;
57 size_t source_len;
58 MSG_FORMAT_METHOD_INIT_VARS;
59
60
61 /* Parse parameters. */
62 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os",
63 &object, MessageFormatter_ce_ptr, &source, &source_len ) == FAILURE )
64 {
65 RETURN_THROWS();
66 }
67
68 /* Fetch the object. */
69 MSG_FORMAT_METHOD_FETCH_OBJECT;
70
71 msgfmt_do_parse(mfo, source, source_len, return_value);
72 }
73 /* }}} */
74
75 /* {{{ Parse a message. */
PHP_FUNCTION(msgfmt_parse_message)76 PHP_FUNCTION( msgfmt_parse_message )
77 {
78 UChar *spattern = NULL;
79 int spattern_len = 0;
80 char *pattern = NULL;
81 size_t pattern_len = 0;
82 const char *slocale = NULL;
83 size_t slocale_len = 0;
84 char *source = NULL;
85 size_t src_len = 0;
86 MessageFormatter_object mf;
87 MessageFormatter_object *mfo = &mf;
88
89 /* Parse parameters. */
90 if( zend_parse_parameters( ZEND_NUM_ARGS(), "sss",
91 &slocale, &slocale_len, &pattern, &pattern_len, &source, &src_len ) == FAILURE )
92 {
93 RETURN_THROWS();
94 }
95
96 INTL_CHECK_LOCALE_LEN(slocale_len);
97 memset(mfo, 0, sizeof(*mfo));
98 msgformat_data_init(&mfo->mf_data);
99
100 if(pattern && pattern_len) {
101 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
102 if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
103 {
104 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
105 "msgfmt_parse_message: error converting pattern to UTF-16", 0 );
106 RETURN_FALSE;
107 }
108 } else {
109 spattern_len = 0;
110 spattern = NULL;
111 }
112
113 if(slocale_len == 0) {
114 slocale = intl_locale_get_default();
115 }
116
117 #ifdef MSG_FORMAT_QUOTE_APOS
118 if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
119 intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
120 "msgfmt_parse_message: error converting pattern to quote-friendly format", 0 );
121 RETURN_FALSE;
122 }
123 #endif
124
125 /* Create an ICU message formatter. */
126 MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo));
127 if(spattern && spattern_len) {
128 efree(spattern);
129 }
130 INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
131
132 msgfmt_do_parse(mfo, source, src_len, return_value);
133
134 /* drop the temporary formatter */
135 msgformat_data_free(&mfo->mf_data);
136 }
137 /* }}} */
138