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