msgformat_attr.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "php_intl.h"
  20. #include "msgformat_class.h"
  21. #include "msgformat_attr.h"
  22. #include "intl_convert.h"
  23. #include <unicode/ustring.h>
  24. /* {{{ proto string MessageFormatter::getPattern( )
  25. * Get formatter pattern. }}} */
  26. /* {{{ proto string msgfmt_get_pattern( MessageFormatter $mf )
  27. * Get formatter pattern.
  28. */
  29. PHP_FUNCTION( msgfmt_get_pattern )
  30. {
  31. MSG_FORMAT_METHOD_INIT_VARS;
  32. /* Parse parameters. */
  33. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, MessageFormatter_ce_ptr ) == FAILURE )
  34. {
  35. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  36. "msgfmt_get_pattern: unable to parse input params", 0 TSRMLS_CC );
  37. RETURN_FALSE;
  38. }
  39. /* Fetch the object. */
  40. MSG_FORMAT_METHOD_FETCH_OBJECT;
  41. if(mfo->mf_data.orig_format) {
  42. RETURN_STRINGL(mfo->mf_data.orig_format, mfo->mf_data.orig_format_len, 1);
  43. }
  44. RETURN_FALSE;
  45. }
  46. /* }}} */
  47. /* {{{ proto bool MessageFormatter::setPattern( string $pattern )
  48. * Set formatter pattern. }}} */
  49. /* {{{ proto bool msgfmt_set_pattern( MessageFormatter $mf, string $pattern )
  50. * Set formatter pattern.
  51. */
  52. PHP_FUNCTION( msgfmt_set_pattern )
  53. {
  54. char* value = NULL;
  55. int value_len = 0;
  56. int spattern_len = 0;
  57. UChar* spattern = NULL;
  58. MSG_FORMAT_METHOD_INIT_VARS;
  59. /* Parse parameters. */
  60. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
  61. &object, MessageFormatter_ce_ptr, &value, &value_len ) == FAILURE )
  62. {
  63. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  64. "msgfmt_set_pattern: unable to parse input params", 0 TSRMLS_CC);
  65. RETURN_FALSE;
  66. }
  67. MSG_FORMAT_METHOD_FETCH_OBJECT;
  68. /* Convert given pattern to UTF-16. */
  69. intl_convert_utf8_to_utf16(&spattern, &spattern_len, value, value_len, &INTL_DATA_ERROR_CODE(mfo));
  70. INTL_METHOD_CHECK_STATUS(mfo, "Error converting pattern to UTF-16" );
  71. #ifdef MSG_FORMAT_QUOTE_APOS
  72. if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
  73. intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
  74. "msgfmt_set_pattern: error converting pattern to quote-friendly format", 0 TSRMLS_CC );
  75. RETURN_FALSE;
  76. }
  77. #endif
  78. /* TODO: add parse error information */
  79. umsg_applyPattern(MSG_FORMAT_OBJECT(mfo), spattern, spattern_len, NULL, &INTL_DATA_ERROR_CODE(mfo));
  80. if (spattern) {
  81. efree(spattern);
  82. }
  83. INTL_METHOD_CHECK_STATUS(mfo, "Error setting symbol value");
  84. if(mfo->mf_data.orig_format) {
  85. efree(mfo->mf_data.orig_format);
  86. }
  87. mfo->mf_data.orig_format = estrndup(value, value_len);
  88. mfo->mf_data.orig_format_len = value_len;
  89. /* invalidate cached format types */
  90. if (mfo->mf_data.arg_types) {
  91. zend_hash_destroy(mfo->mf_data.arg_types);
  92. efree(mfo->mf_data.arg_types);
  93. mfo->mf_data.arg_types = NULL;
  94. }
  95. RETURN_TRUE;
  96. }
  97. /* }}} */
  98. /* {{{ proto string MessageFormatter::getLocale()
  99. * Get formatter locale. }}} */
  100. /* {{{ proto string msgfmt_get_locale(MessageFormatter $mf)
  101. * Get formatter locale.
  102. */
  103. PHP_FUNCTION( msgfmt_get_locale )
  104. {
  105. char *loc;
  106. MSG_FORMAT_METHOD_INIT_VARS;
  107. /* Parse parameters. */
  108. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
  109. &object, MessageFormatter_ce_ptr ) == FAILURE )
  110. {
  111. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  112. "msgfmt_get_locale: unable to parse input params", 0 TSRMLS_CC );
  113. RETURN_FALSE;
  114. }
  115. /* Fetch the object. */
  116. MSG_FORMAT_METHOD_FETCH_OBJECT;
  117. loc = (char *)umsg_getLocale(MSG_FORMAT_OBJECT(mfo));
  118. RETURN_STRING(loc, 1);
  119. }
  120. /* }}} */
  121. /*
  122. * Local variables:
  123. * tab-width: 4
  124. * c-basic-offset: 4
  125. * End:
  126. * vim600: noet sw=4 ts=4 fdm=marker
  127. * vim<600: noet sw=4 ts=4
  128. */