msgformat.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <unicode/ustring.h>
  20. #include <unicode/umsg.h>
  21. #include "php_intl.h"
  22. #include "msgformat_class.h"
  23. #include "intl_convert.h"
  24. /* {{{ */
  25. static void msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
  26. {
  27. const char* locale;
  28. char* pattern;
  29. int locale_len = 0, pattern_len = 0;
  30. UChar* spattern = NULL;
  31. int spattern_len = 0;
  32. zval* object;
  33. MessageFormatter_object* mfo;
  34. intl_error_reset( NULL TSRMLS_CC );
  35. object = return_value;
  36. /* Parse parameters. */
  37. if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "ss",
  38. &locale, &locale_len, &pattern, &pattern_len ) == FAILURE )
  39. {
  40. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  41. "msgfmt_create: unable to parse input parameters", 0 TSRMLS_CC );
  42. zval_dtor(return_value);
  43. RETURN_NULL();
  44. }
  45. INTL_CHECK_LOCALE_LEN_OBJ(locale_len, return_value);
  46. MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
  47. /* Convert pattern (if specified) to UTF-16. */
  48. if(pattern && pattern_len) {
  49. intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
  50. INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16");
  51. } else {
  52. spattern_len = 0;
  53. spattern = NULL;
  54. }
  55. if(locale_len == 0) {
  56. locale = intl_locale_get_default(TSRMLS_C);
  57. }
  58. #ifdef MSG_FORMAT_QUOTE_APOS
  59. if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
  60. INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format");
  61. }
  62. #endif
  63. if ((mfo)->mf_data.orig_format) {
  64. msgformat_data_free(&mfo->mf_data TSRMLS_CC);
  65. }
  66. (mfo)->mf_data.orig_format = estrndup(pattern, pattern_len);
  67. (mfo)->mf_data.orig_format_len = pattern_len;
  68. /* Create an ICU message formatter. */
  69. MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(mfo));
  70. if(spattern) {
  71. efree(spattern);
  72. }
  73. INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed");
  74. }
  75. /* }}} */
  76. /* {{{ proto MessageFormatter MesssageFormatter::create( string $locale, string $pattern )
  77. * Create formatter. }}} */
  78. /* {{{ proto MessageFormatter msgfmt_create( string $locale, string $pattern )
  79. * Create formatter.
  80. */
  81. PHP_FUNCTION( msgfmt_create )
  82. {
  83. object_init_ex( return_value, MessageFormatter_ce_ptr );
  84. msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  85. }
  86. /* }}} */
  87. /* {{{ proto void MessageFormatter::__construct( string $locale, string $pattern )
  88. * MessageFormatter object constructor.
  89. */
  90. PHP_METHOD( MessageFormatter, __construct )
  91. {
  92. return_value = getThis();
  93. msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  94. }
  95. /* }}} */
  96. /* {{{ proto int MessageFormatter::getErrorCode()
  97. * Get formatter's last error code. }}} */
  98. /* {{{ proto int msgfmt_get_error_code( MessageFormatter $nf )
  99. * Get formatter's last error code.
  100. */
  101. PHP_FUNCTION( msgfmt_get_error_code )
  102. {
  103. zval* object = NULL;
  104. MessageFormatter_object* mfo = NULL;
  105. /* Parse parameters. */
  106. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
  107. &object, MessageFormatter_ce_ptr ) == FAILURE )
  108. {
  109. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  110. "msgfmt_get_error_code: unable to parse input params", 0 TSRMLS_CC );
  111. RETURN_FALSE;
  112. }
  113. mfo = (MessageFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
  114. /* Return formatter's last error code. */
  115. RETURN_LONG( INTL_DATA_ERROR_CODE(mfo) );
  116. }
  117. /* }}} */
  118. /* {{{ proto string MessageFormatter::getErrorMessage( )
  119. * Get text description for formatter's last error code. }}} */
  120. /* {{{ proto string msgfmt_get_error_message( MessageFormatter $coll )
  121. * Get text description for formatter's last error code.
  122. */
  123. PHP_FUNCTION( msgfmt_get_error_message )
  124. {
  125. char* message = NULL;
  126. zval* object = NULL;
  127. MessageFormatter_object* mfo = NULL;
  128. /* Parse parameters. */
  129. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
  130. &object, MessageFormatter_ce_ptr ) == FAILURE )
  131. {
  132. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  133. "msgfmt_get_error_message: unable to parse input params", 0 TSRMLS_CC );
  134. RETURN_FALSE;
  135. }
  136. mfo = (MessageFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
  137. /* Return last error message. */
  138. message = intl_error_get_message( &mfo->mf_data.error TSRMLS_CC );
  139. RETURN_STRING( message, 0);
  140. }
  141. /* }}} */
  142. /*
  143. * Local variables:
  144. * tab-width: 4
  145. * c-basic-offset: 4
  146. * End:
  147. * vim600: noet sw=4 ts=4 fdm=marker
  148. * vim<600: noet sw=4 ts=4
  149. */