msgformat_parse.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "php_intl.h"
  21. #include "msgformat_class.h"
  22. #include "msgformat_parse.h"
  23. #include "msgformat_data.h"
  24. #include "msgformat_helpers.h"
  25. #include "intl_convert.h"
  26. /* {{{ */
  27. static void msgfmt_do_parse(MessageFormatter_object *mfo, char *source, int src_len, zval *return_value TSRMLS_DC)
  28. {
  29. zval **fargs;
  30. int count = 0;
  31. int i;
  32. UChar *usource = NULL;
  33. int usrc_len = 0;
  34. intl_convert_utf8_to_utf16(&usource, &usrc_len, source, src_len, &INTL_DATA_ERROR_CODE(mfo));
  35. INTL_METHOD_CHECK_STATUS(mfo, "Converting parse string failed");
  36. umsg_parse_helper(MSG_FORMAT_OBJECT(mfo), &count, &fargs, usource, usrc_len, &INTL_DATA_ERROR_CODE(mfo));
  37. if (usource) {
  38. efree(usource);
  39. }
  40. INTL_METHOD_CHECK_STATUS(mfo, "Parsing failed");
  41. array_init(return_value);
  42. for(i=0;i<count;i++) {
  43. add_next_index_zval(return_value, fargs[i]);
  44. }
  45. efree(fargs);
  46. }
  47. /* }}} */
  48. /* {{{ proto array MessageFormatter::parse( string $source )
  49. * Parse a message }}} */
  50. /* {{{ proto array msgfmt_parse( MessageFormatter $nf, string $source )
  51. * Parse a message.
  52. */
  53. PHP_FUNCTION( msgfmt_parse )
  54. {
  55. char *source;
  56. int source_len;
  57. MSG_FORMAT_METHOD_INIT_VARS;
  58. /* Parse parameters. */
  59. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
  60. &object, MessageFormatter_ce_ptr, &source, &source_len ) == FAILURE )
  61. {
  62. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  63. "msgfmt_parse: unable to parse input params", 0 TSRMLS_CC );
  64. RETURN_FALSE;
  65. }
  66. /* Fetch the object. */
  67. MSG_FORMAT_METHOD_FETCH_OBJECT;
  68. msgfmt_do_parse(mfo, source, source_len, return_value TSRMLS_CC);
  69. }
  70. /* }}} */
  71. /* {{{ proto array MessageFormatter::formatMessage( string $locale, string $pattern, string $source )
  72. * Parse a message. }}} */
  73. /* {{{ proto array numfmt_parse_message( string $locale, string $pattern, string $source )
  74. * Parse a message.
  75. */
  76. PHP_FUNCTION( msgfmt_parse_message )
  77. {
  78. UChar *spattern = NULL;
  79. int spattern_len = 0;
  80. char *pattern = NULL;
  81. int pattern_len = 0;
  82. const char *slocale = NULL;
  83. int slocale_len = 0;
  84. char *source = NULL;
  85. int src_len = 0;
  86. MessageFormatter_object mf = {0};
  87. MessageFormatter_object *mfo = &mf;
  88. /* Parse parameters. */
  89. if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "sss",
  90. &slocale, &slocale_len, &pattern, &pattern_len, &source, &src_len ) == FAILURE )
  91. {
  92. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  93. "msgfmt_parse_message: unable to parse input params", 0 TSRMLS_CC );
  94. RETURN_FALSE;
  95. }
  96. msgformat_data_init(&mfo->mf_data TSRMLS_CC);
  97. if(pattern && pattern_len) {
  98. intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
  99. if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
  100. {
  101. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  102. "msgfmt_parse_message: error converting pattern to UTF-16", 0 TSRMLS_CC );
  103. RETURN_FALSE;
  104. }
  105. } else {
  106. spattern_len = 0;
  107. spattern = NULL;
  108. }
  109. if(slocale_len == 0) {
  110. slocale = intl_locale_get_default(TSRMLS_C);
  111. }
  112. #ifdef MSG_FORMAT_QUOTE_APOS
  113. if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
  114. intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
  115. "msgfmt_parse_message: error converting pattern to quote-friendly format", 0 TSRMLS_CC );
  116. RETURN_FALSE;
  117. }
  118. #endif
  119. /* Create an ICU message formatter. */
  120. MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo));
  121. if(spattern && spattern_len) {
  122. efree(spattern);
  123. }
  124. INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
  125. msgfmt_do_parse(mfo, source, src_len, return_value TSRMLS_CC);
  126. /* drop the temporary formatter */
  127. msgformat_data_free(&mfo->mf_data TSRMLS_CC);
  128. }
  129. /* }}} */
  130. /*
  131. * Local variables:
  132. * tab-width: 4
  133. * c-basic-offset: 4
  134. * End:
  135. * vim600: noet sw=4 ts=4 fdm=marker
  136. * vim<600: noet sw=4 ts=4
  137. */