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 <math.h>
22 : #include <unicode/msgfmt.h>
23 : #include <unicode/chariter.h>
24 :
25 : extern "C" {
26 : #include "php_intl.h"
27 : #include "msgformat_class.h"
28 : #include "msgformat_format.h"
29 : #include "msgformat_helpers.h"
30 : #include "intl_convert.h"
31 : }
32 :
33 : U_NAMESPACE_BEGIN
34 : /**
35 : * This class isolates our access to private internal methods of
36 : * MessageFormat. It is never instantiated; it exists only for C++
37 : * access management.
38 : */
39 : class MessageFormatAdapter {
40 : public:
41 : static const Formattable::Type* getArgTypeList(const MessageFormat& m,
42 : int32_t& count);
43 : };
44 : const Formattable::Type*
45 : MessageFormatAdapter::getArgTypeList(const MessageFormat& m,
46 54 : int32_t& count) {
47 54 : return m.getArgTypeList(count);
48 : }
49 : U_NAMESPACE_END
50 :
51 28 : U_CFUNC int32_t umsg_format_arg_count(UMessageFormat *fmt)
52 : {
53 28 : int32_t fmt_count = 0;
54 28 : MessageFormatAdapter::getArgTypeList(*(const MessageFormat*)fmt, fmt_count);
55 28 : return fmt_count;
56 : }
57 :
58 26 : U_CFUNC void umsg_format_helper(UMessageFormat *fmt, int arg_count, zval **args, UChar **formatted, int *formatted_len, UErrorCode *status TSRMLS_DC)
59 : {
60 26 : int fmt_count = 0;
61 : const Formattable::Type* argTypes =
62 26 : MessageFormatAdapter::getArgTypeList(*(const MessageFormat*)fmt, fmt_count);
63 26 : Formattable* fargs = new Formattable[fmt_count ? fmt_count : 1];
64 :
65 86 : for(int32_t i = 0; i < fmt_count; ++i) {
66 60 : UChar *stringVal = NULL;
67 60 : int stringLen = 0;
68 60 : int64_t tInt64 = 0;
69 :
70 60 : switch(argTypes[i]) {
71 : case Formattable::kDate:
72 0 : convert_to_long_ex(&args[i]);
73 0 : fargs[i].setDate(U_MILLIS_PER_SECOND * (double)Z_LVAL_P(args[i]));
74 0 : break;
75 :
76 : case Formattable::kDouble:
77 28 : convert_to_double_ex(&args[i]);
78 28 : fargs[i].setDouble(Z_DVAL_P(args[i]));
79 28 : break;
80 :
81 : case Formattable::kLong:
82 32 : convert_to_long_ex(&args[i]);
83 32 : fargs[i].setLong(Z_LVAL_P(args[i]));
84 32 : break;
85 :
86 : case Formattable::kInt64:
87 0 : if(Z_TYPE_P(args[i]) == IS_DOUBLE) {
88 0 : tInt64 = (int64_t)Z_DVAL_P(args[i]);
89 0 : } else if(Z_TYPE_P(args[i]) == IS_LONG) {
90 0 : tInt64 = (int64_t)Z_LVAL_P(args[i]);
91 : } else {
92 0 : SEPARATE_ZVAL_IF_NOT_REF(&args[i]);
93 0 : convert_scalar_to_number( args[i] TSRMLS_CC );
94 0 : tInt64 = (Z_TYPE_P(args[i]) == IS_DOUBLE)?(int64_t)Z_DVAL_P(args[i]):Z_LVAL_P(args[i]);
95 : }
96 0 : fargs[i].setInt64(tInt64);
97 0 : break;
98 :
99 : case Formattable::kString:
100 0 : convert_to_string_ex(&args[i]);
101 0 : intl_convert_utf8_to_utf16(&stringVal, &stringLen, Z_STRVAL_P(args[i]), Z_STRLEN_P(args[i]), status);
102 0 : if(U_FAILURE(*status)){
103 0 : delete[] fargs;
104 0 : return;
105 : }
106 0 : fargs[i].setString(stringVal);
107 0 : efree(stringVal);
108 0 : break;
109 :
110 : case Formattable::kArray:
111 : case Formattable::kObject:
112 0 : *status = U_UNSUPPORTED_ERROR;
113 0 : delete[] fargs;
114 0 : return;
115 : }
116 : }
117 :
118 26 : UnicodeString resultStr;
119 26 : FieldPosition fieldPosition(0);
120 :
121 : /* format the message */
122 26 : ((const MessageFormat*)fmt)->format(fargs, fmt_count, resultStr, fieldPosition, *status);
123 :
124 26 : delete[] fargs;
125 :
126 26 : if(U_FAILURE(*status)){
127 0 : return;
128 : }
129 :
130 26 : *formatted_len = resultStr.length();
131 26 : *formatted = eumalloc(*formatted_len+1);
132 26 : resultStr.extract(*formatted, *formatted_len+1, *status);
133 : }
134 :
135 : #define cleanup_zvals() for(int j=i;j>=0;j--) { zval_ptr_dtor((*args)+i); }
136 :
137 20 : U_CFUNC void umsg_parse_helper(UMessageFormat *fmt, int *count, zval ***args, UChar *source, int source_len, UErrorCode *status)
138 : {
139 20 : UnicodeString srcString(source, source_len);
140 20 : Formattable *fargs = ((const MessageFormat*)fmt)->parse(srcString, *count, *status);
141 :
142 20 : if(U_FAILURE(*status)) {
143 0 : return;
144 : }
145 :
146 20 : *args = (zval **)safe_emalloc(*count, sizeof(zval *), 0);
147 :
148 : // assign formattables to varargs
149 20 : for(int32_t i = 0; i < *count; i++) {
150 : int64_t aInt64;
151 : double aDate;
152 52 : UnicodeString temp;
153 : char *stmp;
154 : int stmp_len;
155 :
156 52 : ALLOC_INIT_ZVAL((*args)[i]);
157 :
158 52 : switch(fargs[i].getType()) {
159 : case Formattable::kDate:
160 0 : aDate = ((double)fargs[i].getDate())/U_MILLIS_PER_SECOND;
161 0 : if(aDate > LONG_MAX || aDate < -LONG_MAX) {
162 0 : ZVAL_DOUBLE((*args)[i], aDate<0?ceil(aDate):floor(aDate));
163 : } else {
164 0 : ZVAL_LONG((*args)[i], (long)aDate);
165 : }
166 0 : break;
167 :
168 : case Formattable::kDouble:
169 16 : ZVAL_DOUBLE((*args)[i], (double)fargs[i].getDouble());
170 16 : break;
171 :
172 : case Formattable::kLong:
173 36 : ZVAL_LONG((*args)[i], fargs[i].getLong());
174 36 : break;
175 :
176 : case Formattable::kInt64:
177 0 : aInt64 = fargs[i].getInt64();
178 0 : if(aInt64 > LONG_MAX || aInt64 < -LONG_MAX) {
179 0 : ZVAL_DOUBLE((*args)[i], (double)aInt64);
180 : } else {
181 0 : ZVAL_LONG((*args)[i], (long)aInt64);
182 : }
183 0 : break;
184 :
185 : case Formattable::kString:
186 0 : fargs[i].getString(temp);
187 0 : intl_convert_utf16_to_utf8(&stmp, &stmp_len, temp.getBuffer(), temp.length(), status);
188 0 : if(U_FAILURE(*status)) {
189 0 : cleanup_zvals();
190 : return;
191 : }
192 0 : ZVAL_STRINGL((*args)[i], stmp, stmp_len, 0);
193 0 : break;
194 :
195 : case Formattable::kObject:
196 : case Formattable::kArray:
197 0 : *status = U_ILLEGAL_ARGUMENT_ERROR;
198 0 : cleanup_zvals();
199 : break;
200 : }
201 : }
202 20 : delete[] fargs;
203 : }
204 :
205 : /*
206 : * Local variables:
207 : * tab-width: 4
208 : * c-basic-offset: 4
209 : * End:
210 : * vim600: noet sw=4 ts=4 fdm=marker
211 : * vim<600: noet sw=4 ts=4
212 : */
|