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: Scott MacVicar <scottmac@php.net> |
12 +----------------------------------------------------------------------+
13 */
14
15 #include "spoofchecker_class.h"
16 #include "spoofchecker_arginfo.h"
17 #include "php_intl.h"
18 #include "intl_error.h"
19
20 #include <unicode/uspoof.h>
21
22 zend_class_entry *Spoofchecker_ce_ptr = NULL;
23 static zend_object_handlers Spoofchecker_handlers;
24
25 /*
26 * Auxiliary functions needed by objects of 'Spoofchecker' class
27 */
28
29 /* {{{ Spoofchecker_objects_free */
Spoofchecker_objects_free(zend_object * object)30 void Spoofchecker_objects_free(zend_object *object)
31 {
32 Spoofchecker_object* co = php_intl_spoofchecker_fetch_object(object);
33
34 zend_object_std_dtor(&co->zo);
35
36 spoofchecker_object_destroy(co);
37 }
38 /* }}} */
39
40 /* {{{ Spoofchecker_object_create */
Spoofchecker_object_create(zend_class_entry * ce)41 zend_object *Spoofchecker_object_create(zend_class_entry *ce)
42 {
43 Spoofchecker_object* intern;
44
45 intern = zend_object_alloc(sizeof(Spoofchecker_object), ce);
46 intl_error_init(SPOOFCHECKER_ERROR_P(intern));
47 zend_object_std_init(&intern->zo, ce);
48 object_properties_init(&intern->zo, ce);
49
50 intern->zo.handlers = &Spoofchecker_handlers;
51
52 return &intern->zo;
53 }
54 /* }}} */
55
56 /*
57 * 'Spoofchecker' class registration structures & functions
58 */
59
60 /* {{{ Spoofchecker_class_functions
61 * Every 'Spoofchecker' class method has an entry in this table
62 */
63
spoofchecker_clone_obj(zend_object * object)64 static zend_object *spoofchecker_clone_obj(zend_object *object) /* {{{ */
65 {
66 zend_object *new_obj_val;
67 Spoofchecker_object *sfo, *new_sfo;
68
69 sfo = php_intl_spoofchecker_fetch_object(object);
70 intl_error_reset(SPOOFCHECKER_ERROR_P(sfo));
71
72 new_obj_val = Spoofchecker_ce_ptr->create_object(object->ce);
73 new_sfo = php_intl_spoofchecker_fetch_object(new_obj_val);
74 /* clone standard parts */
75 zend_objects_clone_members(&new_sfo->zo, &sfo->zo);
76 /* clone internal object */
77 new_sfo->uspoof = uspoof_clone(sfo->uspoof, SPOOFCHECKER_ERROR_CODE_P(new_sfo));
78 if(U_FAILURE(SPOOFCHECKER_ERROR_CODE(new_sfo))) {
79 /* set up error in case error handler is interested */
80 intl_error_set( NULL, SPOOFCHECKER_ERROR_CODE(new_sfo), "Failed to clone SpoofChecker object", 0 );
81 Spoofchecker_objects_free(&new_sfo->zo); /* free new object */
82 zend_error(E_ERROR, "Failed to clone SpoofChecker object");
83 }
84 return new_obj_val;
85 }
86 /* }}} */
87
88 /* {{{ spoofchecker_register_Spoofchecker_class
89 * Initialize 'Spoofchecker' class
90 */
spoofchecker_register_Spoofchecker_class(void)91 void spoofchecker_register_Spoofchecker_class(void)
92 {
93 zend_class_entry ce;
94
95 /* Create and register 'Spoofchecker' class. */
96 INIT_CLASS_ENTRY(ce, "Spoofchecker", class_Spoofchecker_methods);
97 ce.create_object = Spoofchecker_object_create;
98 Spoofchecker_ce_ptr = zend_register_internal_class(&ce);
99
100 memcpy(&Spoofchecker_handlers, &std_object_handlers,
101 sizeof Spoofchecker_handlers);
102 Spoofchecker_handlers.offset = XtOffsetOf(Spoofchecker_object, zo);
103 Spoofchecker_handlers.clone_obj = spoofchecker_clone_obj;
104 Spoofchecker_handlers.free_obj = Spoofchecker_objects_free;
105 }
106 /* }}} */
107
108 /* {{{ void spoofchecker_object_init( Spoofchecker_object* co )
109 * Initialize internals of Spoofchecker_object.
110 * Must be called before any other call to 'spoofchecker_object_...' functions.
111 */
spoofchecker_object_init(Spoofchecker_object * co)112 void spoofchecker_object_init(Spoofchecker_object* co)
113 {
114 if (!co) {
115 return;
116 }
117
118 intl_error_init(SPOOFCHECKER_ERROR_P(co));
119 }
120 /* }}} */
121
122 /* {{{ void spoofchecker_object_destroy( Spoofchecker_object* co )
123 * Clean up mem allocted by internals of Spoofchecker_object
124 */
spoofchecker_object_destroy(Spoofchecker_object * co)125 void spoofchecker_object_destroy(Spoofchecker_object* co)
126 {
127 if (!co) {
128 return;
129 }
130
131 if (co->uspoof) {
132 uspoof_close(co->uspoof);
133 co->uspoof = NULL;
134 }
135
136 intl_error_reset(SPOOFCHECKER_ERROR_P(co));
137 }
138 /* }}} */
139