collator_class.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  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: Vadim Savchuk <vsavchuk@productengine.com> |
  14. | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include "collator_class.h"
  18. #include "php_intl.h"
  19. #include "collator_attr.h"
  20. #include "collator_compare.h"
  21. #include "collator_sort.h"
  22. #include "collator_convert.h"
  23. #include "collator_locale.h"
  24. #include "collator_create.h"
  25. #include "collator_error.h"
  26. #include "intl_error.h"
  27. #include <unicode/ucol.h>
  28. zend_class_entry *Collator_ce_ptr = NULL;
  29. static zend_object_handlers Collator_handlers;
  30. /*
  31. * Auxiliary functions needed by objects of 'Collator' class
  32. */
  33. /* {{{ Collator_objects_free */
  34. void Collator_objects_free(zend_object *object )
  35. {
  36. Collator_object* co = php_intl_collator_fetch_object(object);
  37. zend_object_std_dtor(&co->zo );
  38. collator_object_destroy(co );
  39. }
  40. /* }}} */
  41. /* {{{ Collator_object_create */
  42. zend_object *Collator_object_create(zend_class_entry *ce )
  43. {
  44. Collator_object *intern = zend_object_alloc(sizeof(Collator_object), ce);
  45. intl_error_init(COLLATOR_ERROR_P(intern));
  46. zend_object_std_init(&intern->zo, ce );
  47. object_properties_init(&intern->zo, ce);
  48. intern->zo.handlers = &Collator_handlers;
  49. return &intern->zo;
  50. }
  51. /* }}} */
  52. /*
  53. * 'Collator' class registration structures & functions
  54. */
  55. /* {{{ Collator methods arguments info */
  56. /* NOTE: modifying 'collator_XX_args' do not forget to
  57. modify approptiate 'collator_XX_args' for
  58. the procedural API.
  59. */
  60. ZEND_BEGIN_ARG_INFO_EX( collator_0_args, 0, 0, 0 )
  61. ZEND_END_ARG_INFO()
  62. ZEND_BEGIN_ARG_INFO_EX( collator_1_arg, 0, 0, 1 )
  63. ZEND_ARG_INFO( 0, arg1 )
  64. ZEND_END_ARG_INFO()
  65. ZEND_BEGIN_ARG_INFO_EX( collator_2_args, 0, 0, 2 )
  66. ZEND_ARG_INFO( 0, arg1 )
  67. ZEND_ARG_INFO( 0, arg2 )
  68. ZEND_END_ARG_INFO()
  69. ZEND_BEGIN_ARG_INFO_EX( collator_sort_args, 0, 0, 1 )
  70. ZEND_ARG_ARRAY_INFO( 1, arr, 0 )
  71. ZEND_ARG_INFO( 0, flags )
  72. ZEND_END_ARG_INFO()
  73. ZEND_BEGIN_ARG_INFO_EX( collator_sort_with_sort_keys_args, 0, 0, 1 )
  74. ZEND_ARG_ARRAY_INFO( 1, arr, 0 )
  75. ZEND_END_ARG_INFO()
  76. /* }}} */
  77. /* {{{ Collator_class_functions
  78. * Every 'Collator' class method has an entry in this table
  79. */
  80. static const zend_function_entry Collator_class_functions[] = {
  81. PHP_ME( Collator, __construct, collator_1_arg, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
  82. ZEND_FENTRY( create, ZEND_FN( collator_create ), collator_1_arg, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
  83. PHP_NAMED_FE( compare, ZEND_FN( collator_compare ), collator_2_args )
  84. PHP_NAMED_FE( sort, ZEND_FN( collator_sort ), collator_sort_args )
  85. PHP_NAMED_FE( sortWithSortKeys, ZEND_FN( collator_sort_with_sort_keys ), collator_sort_with_sort_keys_args )
  86. PHP_NAMED_FE( asort, ZEND_FN( collator_asort ), collator_sort_args )
  87. PHP_NAMED_FE( getAttribute, ZEND_FN( collator_get_attribute ), collator_1_arg )
  88. PHP_NAMED_FE( setAttribute, ZEND_FN( collator_set_attribute ), collator_2_args )
  89. PHP_NAMED_FE( getStrength, ZEND_FN( collator_get_strength ), collator_0_args )
  90. PHP_NAMED_FE( setStrength, ZEND_FN( collator_set_strength ), collator_1_arg )
  91. PHP_NAMED_FE( getLocale, ZEND_FN( collator_get_locale ), collator_1_arg )
  92. PHP_NAMED_FE( getErrorCode, ZEND_FN( collator_get_error_code ), collator_0_args )
  93. PHP_NAMED_FE( getErrorMessage, ZEND_FN( collator_get_error_message ), collator_0_args )
  94. PHP_NAMED_FE( getSortKey, ZEND_FN( collator_get_sort_key ), collator_1_arg )
  95. PHP_FE_END
  96. };
  97. /* }}} */
  98. /* {{{ collator_register_Collator_class
  99. * Initialize 'Collator' class
  100. */
  101. void collator_register_Collator_class( void )
  102. {
  103. zend_class_entry ce;
  104. /* Create and register 'Collator' class. */
  105. INIT_CLASS_ENTRY( ce, "Collator", Collator_class_functions );
  106. ce.create_object = Collator_object_create;
  107. Collator_ce_ptr = zend_register_internal_class( &ce );
  108. memcpy(&Collator_handlers, &std_object_handlers,
  109. sizeof Collator_handlers);
  110. /* Collator has no usable clone semantics - ucol_cloneBinary/ucol_openBinary require binary buffer
  111. for which we don't have the place to keep */
  112. Collator_handlers.offset = XtOffsetOf(Collator_object, zo);
  113. Collator_handlers.clone_obj = NULL;
  114. Collator_handlers.free_obj = Collator_objects_free;
  115. /* Declare 'Collator' class properties. */
  116. if( !Collator_ce_ptr )
  117. {
  118. zend_error( E_ERROR,
  119. "Collator: attempt to create properties "
  120. "on a non-registered class." );
  121. return;
  122. }
  123. }
  124. /* }}} */
  125. /* {{{ void collator_object_init( Collator_object* co )
  126. * Initialize internals of Collator_object.
  127. * Must be called before any other call to 'collator_object_...' functions.
  128. */
  129. void collator_object_init( Collator_object* co )
  130. {
  131. if( !co )
  132. return;
  133. intl_error_init( COLLATOR_ERROR_P( co ) );
  134. }
  135. /* }}} */
  136. /* {{{ void collator_object_destroy( Collator_object* co )
  137. * Clean up mem allocted by internals of Collator_object
  138. */
  139. void collator_object_destroy( Collator_object* co )
  140. {
  141. if( !co )
  142. return;
  143. if( co->ucoll )
  144. {
  145. ucol_close( co->ucoll );
  146. co->ucoll = NULL;
  147. }
  148. intl_error_reset( COLLATOR_ERROR_P( co ) );
  149. }
  150. /* }}} */
  151. /*
  152. * Local variables:
  153. * tab-width: 4
  154. * c-basic-offset: 4
  155. * End:
  156. * vim600: noet sw=4 ts=4 fdm=marker
  157. * vim<600: noet sw=4 ts=4
  158. */