msgformat.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. #include <unicode/ustring.h>
  18. #include <unicode/umsg.h>
  19. #include "php_intl.h"
  20. #include "msgformat_class.h"
  21. #include "msgformat_data.h"
  22. #include "intl_convert.h"
  23. /* {{{ */
  24. static int msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_error_handling *error_handling, bool *error_handling_replaced)
  25. {
  26. const char* locale;
  27. char* pattern;
  28. size_t locale_len = 0, pattern_len = 0;
  29. UChar* spattern = NULL;
  30. int spattern_len = 0;
  31. zval* object;
  32. MessageFormatter_object* mfo;
  33. UParseError parse_error;
  34. intl_error_reset( NULL );
  35. object = return_value;
  36. /* Parse parameters. */
  37. if( zend_parse_parameters( ZEND_NUM_ARGS(), "ss",
  38. &locale, &locale_len, &pattern, &pattern_len ) == FAILURE )
  39. {
  40. return FAILURE;
  41. }
  42. if (error_handling != NULL) {
  43. zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, error_handling);
  44. *error_handling_replaced = 1;
  45. }
  46. INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
  47. MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
  48. /* Convert pattern (if specified) to UTF-16. */
  49. if(pattern && pattern_len) {
  50. intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
  51. INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16");
  52. } else {
  53. spattern_len = 0;
  54. spattern = NULL;
  55. }
  56. if(locale_len == 0) {
  57. locale = intl_locale_get_default();
  58. }
  59. #ifdef MSG_FORMAT_QUOTE_APOS
  60. if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
  61. INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format");
  62. }
  63. #endif
  64. if ((mfo)->mf_data.orig_format) {
  65. msgformat_data_free(&mfo->mf_data);
  66. }
  67. (mfo)->mf_data.orig_format = estrndup(pattern, pattern_len);
  68. (mfo)->mf_data.orig_format_len = pattern_len;
  69. /* Create an ICU message formatter. */
  70. MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, &parse_error, &INTL_DATA_ERROR_CODE(mfo));
  71. if(spattern) {
  72. efree(spattern);
  73. }
  74. if (INTL_DATA_ERROR_CODE( mfo ) == U_PATTERN_SYNTAX_ERROR) {
  75. char *msg = NULL;
  76. smart_str parse_error_str;
  77. parse_error_str = intl_parse_error_to_string( &parse_error );
  78. spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
  79. smart_str_free( &parse_error_str );
  80. intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( mfo ) );
  81. intl_errors_set_custom_msg( INTL_DATA_ERROR_P( mfo ), msg, 1 );
  82. efree( msg );
  83. return FAILURE;
  84. }
  85. INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed");
  86. return SUCCESS;
  87. }
  88. /* }}} */
  89. /* {{{ Create formatter. */
  90. PHP_FUNCTION( msgfmt_create )
  91. {
  92. object_init_ex( return_value, MessageFormatter_ce_ptr );
  93. if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL, NULL) == FAILURE) {
  94. zval_ptr_dtor(return_value);
  95. RETURN_NULL();
  96. }
  97. }
  98. /* }}} */
  99. /* {{{ MessageFormatter object constructor. */
  100. PHP_METHOD( MessageFormatter, __construct )
  101. {
  102. zend_error_handling error_handling;
  103. bool error_handling_replaced = 0;
  104. return_value = ZEND_THIS;
  105. if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, &error_handling, &error_handling_replaced) == FAILURE) {
  106. if (!EG(exception)) {
  107. zend_string *err = intl_error_get_message(NULL);
  108. zend_throw_exception(IntlException_ce_ptr, ZSTR_VAL(err), intl_error_get_code(NULL));
  109. zend_string_release_ex(err, 0);
  110. }
  111. }
  112. if (error_handling_replaced) {
  113. zend_restore_error_handling(&error_handling);
  114. }
  115. }
  116. /* }}} */
  117. /* {{{ Get formatter's last error code. */
  118. PHP_FUNCTION( msgfmt_get_error_code )
  119. {
  120. zval* object = NULL;
  121. MessageFormatter_object* mfo = NULL;
  122. /* Parse parameters. */
  123. if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
  124. &object, MessageFormatter_ce_ptr ) == FAILURE )
  125. {
  126. RETURN_THROWS();
  127. }
  128. mfo = Z_INTL_MESSAGEFORMATTER_P( object );
  129. /* Return formatter's last error code. */
  130. RETURN_LONG( INTL_DATA_ERROR_CODE(mfo) );
  131. }
  132. /* }}} */
  133. /* {{{ Get text description for formatter's last error code. */
  134. PHP_FUNCTION( msgfmt_get_error_message )
  135. {
  136. zend_string* message = NULL;
  137. zval* object = NULL;
  138. MessageFormatter_object* mfo = NULL;
  139. /* Parse parameters. */
  140. if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
  141. &object, MessageFormatter_ce_ptr ) == FAILURE )
  142. {
  143. RETURN_THROWS();
  144. }
  145. mfo = Z_INTL_MESSAGEFORMATTER_P( object );
  146. /* Return last error message. */
  147. message = intl_error_get_message( &mfo->mf_data.error );
  148. RETURN_STR(message);
  149. }
  150. /* }}} */