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.

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