xref: /PHP-8.0/ext/intl/collator/collator_class.c (revision f00bcfbb)
1 /*
2    +----------------------------------------------------------------------+
3    | This source file is subject to version 3.01 of the PHP license,      |
4    | that is bundled with this package in the file LICENSE, and is        |
5    | available through the world-wide-web at the following url:           |
6    | http://www.php.net/license/3_01.txt                                  |
7    | If you did not receive a copy of the PHP license and are unable to   |
8    | obtain it through the world-wide-web, please send a note to          |
9    | license@php.net so we can mail you a copy immediately.               |
10    +----------------------------------------------------------------------+
11    | Authors: Vadim Savchuk <vsavchuk@productengine.com>                  |
12    |          Dmitry Lakhtyuk <dlakhtyuk@productengine.com>               |
13    +----------------------------------------------------------------------+
14  */
15 
16 #include "collator_class.h"
17 #include "php_intl.h"
18 #include "collator_sort.h"
19 #include "collator_convert.h"
20 #include "collator_arginfo.h"
21 #include "intl_error.h"
22 
23 #include <unicode/ucol.h>
24 
25 zend_class_entry *Collator_ce_ptr = NULL;
26 static zend_object_handlers Collator_handlers;
27 
28 /*
29  * Auxiliary functions needed by objects of 'Collator' class
30  */
31 
32 /* {{{ Collator_objects_free */
Collator_objects_free(zend_object * object)33 void Collator_objects_free(zend_object *object )
34 {
35 	Collator_object* co = php_intl_collator_fetch_object(object);
36 
37 	zend_object_std_dtor(&co->zo );
38 
39 	collator_object_destroy(co );
40 }
41 /* }}} */
42 
43 /* {{{ Collator_object_create */
Collator_object_create(zend_class_entry * ce)44 zend_object *Collator_object_create(zend_class_entry *ce )
45 {
46 	Collator_object *intern = zend_object_alloc(sizeof(Collator_object), ce);
47 	intl_error_init(COLLATOR_ERROR_P(intern));
48 	zend_object_std_init(&intern->zo, ce );
49 	object_properties_init(&intern->zo, ce);
50 
51 	intern->zo.handlers = &Collator_handlers;
52 
53 	return &intern->zo;
54 }
55 /* }}} */
56 
57 /*
58  * 'Collator' class registration structures & functions
59  */
60 
61 /* {{{ collator_register_Collator_class
62  * Initialize 'Collator' class
63  */
collator_register_Collator_class(void)64 void collator_register_Collator_class( void )
65 {
66 	zend_class_entry ce;
67 
68 	/* Create and register 'Collator' class. */
69 	INIT_CLASS_ENTRY( ce, "Collator", class_Collator_methods );
70 	ce.create_object = Collator_object_create;
71 	Collator_ce_ptr = zend_register_internal_class( &ce );
72 
73 	memcpy(&Collator_handlers, &std_object_handlers,
74 		sizeof Collator_handlers);
75 	/* Collator has no usable clone semantics - ucol_cloneBinary/ucol_openBinary require binary buffer
76 	   for which we don't have the place to keep */
77 	Collator_handlers.offset = XtOffsetOf(Collator_object, zo);
78 	Collator_handlers.clone_obj = NULL;
79 	Collator_handlers.free_obj = Collator_objects_free;
80 
81 	/* Declare 'Collator' class properties. */
82 	if( !Collator_ce_ptr )
83 	{
84 		zend_error( E_ERROR,
85 			"Collator: attempt to create properties "
86 			"on a non-registered class." );
87 		return;
88 	}
89 }
90 /* }}} */
91 
92 /* {{{ void collator_object_init( Collator_object* co )
93  * Initialize internals of Collator_object.
94  * Must be called before any other call to 'collator_object_...' functions.
95  */
collator_object_init(Collator_object * co)96 void collator_object_init( Collator_object* co )
97 {
98 	if( !co )
99 		return;
100 
101 	intl_error_init( COLLATOR_ERROR_P( co ) );
102 }
103 /* }}} */
104 
105 /* {{{ void collator_object_destroy( Collator_object* co )
106  * Clean up mem allocted by internals of Collator_object
107  */
collator_object_destroy(Collator_object * co)108 void collator_object_destroy( Collator_object* co )
109 {
110 	if( !co )
111 		return;
112 
113 	if( co->ucoll )
114 	{
115 		ucol_close( co->ucoll );
116 		co->ucoll = NULL;
117 	}
118 
119 	intl_error_reset( COLLATOR_ERROR_P( co ) );
120 }
121 /* }}} */
122