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 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 ZEND_PARSE_PARAMETERS_START(3, 3)
90 Z_PARAM_STRING(slocale, slocale_len)
91 Z_PARAM_STRING(pattern, pattern_len)
92 Z_PARAM_STRING(source, src_len)
93 ZEND_PARSE_PARAMETERS_END();
94
95 INTL_CHECK_LOCALE_LEN(slocale_len);
96 memset(mfo, 0, sizeof(*mfo));
97 msgformat_data_init(&mfo->mf_data);
98
99 if(pattern && pattern_len) {
100 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
101 if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
102 {
103 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
104 "msgfmt_parse_message: error converting pattern to UTF-16", 0 );
105 RETURN_FALSE;
106 }
107 } else {
108 spattern_len = 0;
109 spattern = NULL;
110 }
111
112 if(slocale_len == 0) {
113 slocale = (char *)intl_locale_get_default();
114 }
115
116 #ifdef MSG_FORMAT_QUOTE_APOS
117 if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
118 intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
119 "msgfmt_parse_message: error converting pattern to quote-friendly format", 0 );
120 RETURN_FALSE;
121 }
122 #endif
123
124 /* Create an ICU message formatter. */
125 MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo));
126 if(spattern && spattern_len) {
127 efree(spattern);
128 }
129 INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
130
131 msgfmt_do_parse(mfo, source, src_len, return_value);
132
133 /* drop the temporary formatter */
134 msgformat_data_free(&mfo->mf_data);
135 }
136 /* }}} */
137