formatter_main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "formatter_class.h"
  22. #include "intl_convert.h"
  23. /* {{{ */
  24. static void numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
  25. {
  26. const char* locale;
  27. char* pattern = NULL;
  28. int locale_len = 0, pattern_len = 0;
  29. long style;
  30. UChar* spattern = NULL;
  31. int spattern_len = 0;
  32. FORMATTER_METHOD_INIT_VARS;
  33. /* Parse parameters. */
  34. if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "sl|s",
  35. &locale, &locale_len, &style, &pattern, &pattern_len ) == FAILURE )
  36. {
  37. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  38. "numfmt_create: unable to parse input parameters", 0 TSRMLS_CC );
  39. zval_dtor(return_value);
  40. RETURN_NULL();
  41. }
  42. INTL_CHECK_LOCALE_LEN_OBJ(locale_len, return_value);
  43. object = return_value;
  44. FORMATTER_METHOD_FETCH_OBJECT_NO_CHECK;
  45. /* Convert pattern (if specified) to UTF-16. */
  46. if(pattern && pattern_len) {
  47. intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(nfo));
  48. INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: error converting pattern to UTF-16");
  49. }
  50. if(locale_len == 0) {
  51. locale = intl_locale_get_default(TSRMLS_C);
  52. }
  53. /* Create an ICU number formatter. */
  54. FORMATTER_OBJECT(nfo) = unum_open(style, spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(nfo));
  55. if(spattern) {
  56. efree(spattern);
  57. }
  58. INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: number formatter creation failed");
  59. }
  60. /* }}} */
  61. /* {{{ proto NumberFormatter NumberFormatter::create( string $locale, int style[, string $pattern ] )
  62. * Create number formatter. }}} */
  63. /* {{{ proto NumberFormatter numfmt_create( string $locale, int style[, string $pattern ] )
  64. * Create number formatter.
  65. */
  66. PHP_FUNCTION( numfmt_create )
  67. {
  68. object_init_ex( return_value, NumberFormatter_ce_ptr );
  69. numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  70. }
  71. /* }}} */
  72. /* {{{ proto void NumberFormatter::__construct( string $locale, int style[, string $pattern ] )
  73. * NumberFormatter object constructor.
  74. */
  75. PHP_METHOD( NumberFormatter, __construct )
  76. {
  77. return_value = getThis();
  78. numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  79. }
  80. /* }}} */
  81. /* {{{ proto int NumberFormatter::getErrorCode()
  82. * Get formatter's last error code. }}} */
  83. /* {{{ proto int numfmt_get_error_code( NumberFormatter $nf )
  84. * Get formatter's last error code.
  85. */
  86. PHP_FUNCTION( numfmt_get_error_code )
  87. {
  88. FORMATTER_METHOD_INIT_VARS
  89. /* Parse parameters. */
  90. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
  91. &object, NumberFormatter_ce_ptr ) == FAILURE )
  92. {
  93. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  94. "numfmt_get_error_code: unable to parse input params", 0 TSRMLS_CC );
  95. RETURN_FALSE;
  96. }
  97. nfo = (NumberFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
  98. /* Return formatter's last error code. */
  99. RETURN_LONG( INTL_DATA_ERROR_CODE(nfo) );
  100. }
  101. /* }}} */
  102. /* {{{ proto string NumberFormatter::getErrorMessage( )
  103. * Get text description for formatter's last error code. }}} */
  104. /* {{{ proto string numfmt_get_error_message( NumberFormatter $nf )
  105. * Get text description for formatter's last error code.
  106. */
  107. PHP_FUNCTION( numfmt_get_error_message )
  108. {
  109. char* message = NULL;
  110. FORMATTER_METHOD_INIT_VARS
  111. /* Parse parameters. */
  112. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
  113. &object, NumberFormatter_ce_ptr ) == FAILURE )
  114. {
  115. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  116. "numfmt_get_error_message: unable to parse input params", 0 TSRMLS_CC );
  117. RETURN_FALSE;
  118. }
  119. nfo = (NumberFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
  120. /* Return last error message. */
  121. message = intl_error_get_message( INTL_DATA_ERROR_P(nfo) TSRMLS_CC );
  122. RETURN_STRING( message, 0);
  123. }
  124. /* }}} */
  125. /*
  126. * Local variables:
  127. * tab-width: 4
  128. * c-basic-offset: 4
  129. * End:
  130. * vim600: noet sw=4 ts=4 fdm=marker
  131. * vim<600: noet sw=4 ts=4
  132. */