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: Mel Dafert (mel@dafert.at)                                  |
12    +----------------------------------------------------------------------+
13 */
14 
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18 
19 #include "../intl_cppshims.h"
20 
21 #include <unicode/dtptngen.h>
22 
23 extern "C" {
24 #define USE_DATETIMEPATTERNGENERATOR_POINTER 1
25 #include "datepatterngenerator_class.h"
26 #include "datepatterngenerator_arginfo.h"
27 #include <zend_exceptions.h>
28 #include <assert.h>
29 }
30 
31 using icu::DateTimePatternGenerator;
32 using icu::Locale;
33 
34 zend_class_entry *IntlDatePatternGenerator_ce_ptr;
35 zend_object_handlers IntlDatePatternGenerator_handlers;
36 
IntlDatePatternGenerator_object_clone(zend_object * object)37 static zend_object *IntlDatePatternGenerator_object_clone(zend_object *object)
38 {
39 	intl_error_reset(NULL);
40 
41 	IntlDatePatternGenerator_object *dtpgo_orig = php_intl_datepatterngenerator_fetch_object(object);
42 	intl_error_reset(DTPATTERNGEN_ERROR_P(dtpgo_orig));
43 
44 	zend_object *ret_val = IntlDatePatternGenerator_ce_ptr->create_object(object->ce);
45 	IntlDatePatternGenerator_object *dtpgo_new  = php_intl_datepatterngenerator_fetch_object(ret_val);
46 
47 	zend_objects_clone_members(&dtpgo_new->zo, &dtpgo_orig->zo);
48 
49 	if (dtpgo_orig->dtpg != NULL) {
50 		DateTimePatternGenerator *newDtpg = dtpgo_orig->dtpg->clone();
51 		if (!newDtpg) {
52 			zend_string *err_msg;
53 			intl_errors_set_code(DTPATTERNGEN_ERROR_P(dtpgo_orig),
54 				U_MEMORY_ALLOCATION_ERROR);
55 			intl_errors_set_custom_msg(DTPATTERNGEN_ERROR_P(dtpgo_orig),
56 				"Could not clone IntlDatePatternGenerator", 0);
57 			err_msg = intl_error_get_message(DTPATTERNGEN_ERROR_P(dtpgo_orig));
58 			zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
59 			zend_string_free(err_msg);
60 		} else {
61 			dtpgo_new->dtpg = newDtpg;
62 		}
63 	} else {
64 		zend_throw_exception(NULL, "Cannot clone unconstructed IntlDatePatternGenerator", 0);
65 	}
66 
67 	return ret_val;
68 }
69 
70 /*
71  * Initialize internals of IntlDatePatternGenerator_object not specific to zend standard objects.
72  */
IntlDatePatternGenerator_object_init(IntlDatePatternGenerator_object * co)73 static void IntlDatePatternGenerator_object_init(IntlDatePatternGenerator_object *co)
74 {
75 	intl_error_init(DTPATTERNGEN_ERROR_P(co));
76 	co->dtpg = NULL;
77 }
78 
IntlDatePatternGenerator_object_free(zend_object * object)79 static void IntlDatePatternGenerator_object_free(zend_object *object)
80 {
81 	IntlDatePatternGenerator_object* co = php_intl_datepatterngenerator_fetch_object(object);
82 
83 	if (co->dtpg) {
84 		delete co->dtpg;
85 		co->dtpg = NULL;
86 	}
87 	intl_error_reset(DTPATTERNGEN_ERROR_P(co));
88 
89 	zend_object_std_dtor(&co->zo);
90 }
91 
IntlDatePatternGenerator_object_create(zend_class_entry * ce)92 static zend_object *IntlDatePatternGenerator_object_create(zend_class_entry *ce)
93 {
94 	IntlDatePatternGenerator_object *intern =
95 		(IntlDatePatternGenerator_object*) zend_object_alloc(sizeof(IntlDatePatternGenerator_object), ce);
96 
97 	zend_object_std_init(&intern->zo, ce);
98 	object_properties_init(&intern->zo, ce);
99 	IntlDatePatternGenerator_object_init(intern);
100 
101 	intern->zo.handlers = &IntlDatePatternGenerator_handlers;
102 
103 	return &intern->zo;
104 }
105 
106 /*
107  * 'IntlDatePatternGenerator' class registration structures & functions
108  */
109 
110 /*
111  * Initialize 'IntlDatePatternGenerator' class
112  */
dateformat_register_IntlDatePatternGenerator_class(void)113 void dateformat_register_IntlDatePatternGenerator_class( void )
114 {
115 	/* Create and register 'IntlDatePatternGenerator' class. */
116 	IntlDatePatternGenerator_ce_ptr = register_class_IntlDatePatternGenerator();
117 	IntlDatePatternGenerator_ce_ptr->create_object = IntlDatePatternGenerator_object_create;
118 
119 	memcpy(&IntlDatePatternGenerator_handlers, &std_object_handlers,
120 		sizeof IntlDatePatternGenerator_handlers);
121 	IntlDatePatternGenerator_handlers.offset = XtOffsetOf(IntlDatePatternGenerator_object, zo);
122 	IntlDatePatternGenerator_handlers.clone_obj = IntlDatePatternGenerator_object_clone;
123 	IntlDatePatternGenerator_handlers.free_obj = IntlDatePatternGenerator_object_free;
124 }
125