msgformat_class.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include <unicode/unum.h>
  17. #include "msgformat_class.h"
  18. #include "php_intl.h"
  19. #include "msgformat_data.h"
  20. #include "msgformat_format.h"
  21. #include "msgformat_parse.h"
  22. #include "msgformat.h"
  23. #include "msgformat_attr.h"
  24. #include <zend_exceptions.h>
  25. zend_class_entry *MessageFormatter_ce_ptr = NULL;
  26. static zend_object_handlers MessageFormatter_handlers;
  27. /*
  28. * Auxiliary functions needed by objects of 'MessageFormatter' class
  29. */
  30. /* {{{ MessageFormatter_objects_dtor */
  31. static void MessageFormatter_object_dtor(void *object, zend_object_handle handle TSRMLS_DC )
  32. {
  33. zend_objects_destroy_object( object, handle TSRMLS_CC );
  34. }
  35. /* }}} */
  36. /* {{{ MessageFormatter_objects_free */
  37. void MessageFormatter_object_free( zend_object *object TSRMLS_DC )
  38. {
  39. MessageFormatter_object* mfo = (MessageFormatter_object*)object;
  40. zend_object_std_dtor( &mfo->zo TSRMLS_CC );
  41. msgformat_data_free( &mfo->mf_data TSRMLS_CC );
  42. efree( mfo );
  43. }
  44. /* }}} */
  45. /* {{{ MessageFormatter_object_create */
  46. zend_object_value MessageFormatter_object_create(zend_class_entry *ce TSRMLS_DC)
  47. {
  48. zend_object_value retval;
  49. MessageFormatter_object* intern;
  50. intern = ecalloc( 1, sizeof(MessageFormatter_object) );
  51. msgformat_data_init( &intern->mf_data TSRMLS_CC );
  52. zend_object_std_init( &intern->zo, ce TSRMLS_CC );
  53. object_properties_init(&intern->zo, ce);
  54. retval.handle = zend_objects_store_put(
  55. intern,
  56. MessageFormatter_object_dtor,
  57. (zend_objects_free_object_storage_t)MessageFormatter_object_free,
  58. NULL TSRMLS_CC );
  59. retval.handlers = &MessageFormatter_handlers;
  60. return retval;
  61. }
  62. /* }}} */
  63. /* {{{ MessageFormatter_object_clone */
  64. zend_object_value MessageFormatter_object_clone(zval *object TSRMLS_DC)
  65. {
  66. zend_object_value new_obj_val;
  67. zend_object_handle handle = Z_OBJ_HANDLE_P(object);
  68. MessageFormatter_object *mfo, *new_mfo;
  69. MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
  70. new_obj_val = MessageFormatter_ce_ptr->create_object(Z_OBJCE_P(object) TSRMLS_CC);
  71. new_mfo = (MessageFormatter_object *)zend_object_store_get_object_by_handle(new_obj_val.handle TSRMLS_CC);
  72. /* clone standard parts */
  73. zend_objects_clone_members(&new_mfo->zo, new_obj_val, &mfo->zo, handle TSRMLS_CC);
  74. /* clone formatter object */
  75. if (MSG_FORMAT_OBJECT(mfo) != NULL) {
  76. MSG_FORMAT_OBJECT(new_mfo) = umsg_clone(MSG_FORMAT_OBJECT(mfo),
  77. &INTL_DATA_ERROR_CODE(mfo));
  78. if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
  79. intl_errors_set(INTL_DATA_ERROR_P(mfo), INTL_DATA_ERROR_CODE(mfo),
  80. "Failed to clone MessageFormatter object", 0 TSRMLS_CC);
  81. zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Failed to clone MessageFormatter object");
  82. }
  83. } else {
  84. zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Cannot clone unconstructed MessageFormatter");
  85. }
  86. return new_obj_val;
  87. }
  88. /* }}} */
  89. /*
  90. * 'MessageFormatter' class registration structures & functions
  91. */
  92. /* {{{ arginfo */
  93. ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter___construct, 0, 0, 2)
  94. ZEND_ARG_INFO(0, locale)
  95. ZEND_ARG_INFO(0, pattern)
  96. ZEND_END_ARG_INFO()
  97. ZEND_BEGIN_ARG_INFO(arginfo_messageformatter_geterrormessage, 0)
  98. ZEND_END_ARG_INFO()
  99. ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_formatmessage, 0, 0, 3)
  100. ZEND_ARG_INFO(0, locale)
  101. ZEND_ARG_INFO(0, pattern)
  102. ZEND_ARG_INFO(0, args)
  103. ZEND_END_ARG_INFO()
  104. ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_format, 0, 0, 1)
  105. ZEND_ARG_INFO(0, args)
  106. ZEND_END_ARG_INFO()
  107. ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_setpattern, 0, 0, 1)
  108. ZEND_ARG_INFO(0, pattern)
  109. ZEND_END_ARG_INFO()
  110. ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_parse, 0, 0, 1)
  111. ZEND_ARG_INFO(0, source)
  112. ZEND_END_ARG_INFO()
  113. /* }}} */
  114. /* {{{ MessageFormatter_class_functions
  115. * Every 'MessageFormatter' class method has an entry in this table
  116. */
  117. static zend_function_entry MessageFormatter_class_functions[] = {
  118. PHP_ME( MessageFormatter, __construct, arginfo_messageformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
  119. ZEND_FENTRY( create, ZEND_FN( msgfmt_create ), arginfo_messageformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
  120. PHP_NAMED_FE( format, ZEND_FN( msgfmt_format ), arginfo_messageformatter_format )
  121. ZEND_FENTRY( formatMessage, ZEND_FN( msgfmt_format_message ), arginfo_messageformatter_formatmessage, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
  122. PHP_NAMED_FE( parse, ZEND_FN( msgfmt_parse ), arginfo_messageformatter_parse )
  123. ZEND_FENTRY( parseMessage, ZEND_FN( msgfmt_parse_message ), arginfo_messageformatter_formatmessage, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
  124. PHP_NAMED_FE( setPattern, ZEND_FN( msgfmt_set_pattern ), arginfo_messageformatter_setpattern )
  125. PHP_NAMED_FE( getPattern, ZEND_FN( msgfmt_get_pattern ), arginfo_messageformatter_geterrormessage )
  126. PHP_NAMED_FE( getLocale, ZEND_FN( msgfmt_get_locale ), arginfo_messageformatter_geterrormessage )
  127. PHP_NAMED_FE( getErrorCode, ZEND_FN( msgfmt_get_error_code ), arginfo_messageformatter_geterrormessage )
  128. PHP_NAMED_FE( getErrorMessage, ZEND_FN( msgfmt_get_error_message ), arginfo_messageformatter_geterrormessage )
  129. PHP_FE_END
  130. };
  131. /* }}} */
  132. /* {{{ msgformat_register_class
  133. * Initialize 'MessageFormatter' class
  134. */
  135. void msgformat_register_class( TSRMLS_D )
  136. {
  137. zend_class_entry ce;
  138. /* Create and register 'MessageFormatter' class. */
  139. INIT_CLASS_ENTRY( ce, "MessageFormatter", MessageFormatter_class_functions );
  140. ce.create_object = MessageFormatter_object_create;
  141. MessageFormatter_ce_ptr = zend_register_internal_class( &ce TSRMLS_CC );
  142. memcpy(&MessageFormatter_handlers, zend_get_std_object_handlers(),
  143. sizeof MessageFormatter_handlers);
  144. MessageFormatter_handlers.clone_obj = MessageFormatter_object_clone;
  145. /* Declare 'MessageFormatter' class properties. */
  146. if( !MessageFormatter_ce_ptr )
  147. {
  148. zend_error(E_ERROR, "Failed to register MessageFormatter class");
  149. return;
  150. }
  151. }
  152. /* }}} */
  153. /*
  154. * Local variables:
  155. * tab-width: 4
  156. * c-basic-offset: 4
  157. * End:
  158. * vim600: noet sw=4 ts=4 fdm=marker
  159. * vim<600: noet sw=4 ts=4
  160. */