You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.2 KiB

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