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: Kirti Velankar <kirtig@yahoo-inc.com>                       |
12    +----------------------------------------------------------------------+
13 */
14 #include <unicode/unum.h>
15 
16 #include "dateformat_class.h"
17 #include "php_intl.h"
18 #include "dateformat_data.h"
19 #include "dateformat.h"
20 #include "dateformat_arginfo.h"
21 
22 #include <zend_exceptions.h>
23 
24 zend_class_entry *IntlDateFormatter_ce_ptr = NULL;
25 static zend_object_handlers IntlDateFormatter_handlers;
26 
27 /*
28  * Auxiliary functions needed by objects of 'IntlDateFormatter' class
29  */
30 
31 /* {{{ IntlDateFormatter_objects_dtor */
IntlDateFormatter_object_dtor(zend_object * object)32 static void IntlDateFormatter_object_dtor(zend_object *object )
33 {
34 	zend_objects_destroy_object( object );
35 }
36 /* }}} */
37 
38 /* {{{ IntlDateFormatter_objects_free */
IntlDateFormatter_object_free(zend_object * object)39 void IntlDateFormatter_object_free( zend_object *object )
40 {
41 	IntlDateFormatter_object* dfo = php_intl_dateformatter_fetch_object(object);
42 
43 	zend_object_std_dtor( &dfo->zo );
44 
45 	if (dfo->requested_locale) {
46 		efree( dfo->requested_locale );
47 	}
48 
49 	dateformat_data_free( &dfo->datef_data );
50 }
51 /* }}} */
52 
53 /* {{{ IntlDateFormatter_object_create */
IntlDateFormatter_object_create(zend_class_entry * ce)54 zend_object *IntlDateFormatter_object_create(zend_class_entry *ce)
55 {
56 	IntlDateFormatter_object*     intern;
57 
58 	intern = zend_object_alloc(sizeof(IntlDateFormatter_object), ce);
59 	dateformat_data_init( &intern->datef_data );
60 	zend_object_std_init( &intern->zo, ce );
61 	object_properties_init(&intern->zo, ce);
62 	intern->date_type			= 0;
63 	intern->time_type			= 0;
64 	intern->calendar			= -1;
65 	intern->requested_locale	= NULL;
66 
67 	intern->zo.handlers = &IntlDateFormatter_handlers;
68 
69 	return &intern->zo;
70 }
71 /* }}} */
72 
73 /* {{{ IntlDateFormatter_object_clone */
IntlDateFormatter_object_clone(zend_object * object)74 zend_object *IntlDateFormatter_object_clone(zend_object *object)
75 {
76 	IntlDateFormatter_object *dfo, *new_dfo;
77 	zend_object *new_obj;
78 
79 	dfo = php_intl_dateformatter_fetch_object(object);
80 	intl_error_reset(INTL_DATA_ERROR_P(dfo));
81 
82 	new_obj = IntlDateFormatter_ce_ptr->create_object(object->ce);
83 	new_dfo = php_intl_dateformatter_fetch_object(new_obj);
84 	/* clone standard parts */
85 	zend_objects_clone_members(&new_dfo->zo, &dfo->zo);
86 	/* clone formatter object */
87 	if (dfo->datef_data.udatf != NULL) {
88 		DATE_FORMAT_OBJECT(new_dfo) = udat_clone(DATE_FORMAT_OBJECT(dfo),  &INTL_DATA_ERROR_CODE(dfo));
89 		if (U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
90 			/* set up error in case error handler is interested */
91 			intl_errors_set(INTL_DATA_ERROR_P(dfo), INTL_DATA_ERROR_CODE(dfo),
92 					"Failed to clone IntlDateFormatter object", 0 );
93 			zend_throw_exception(NULL, "Failed to clone IntlDateFormatter object", 0);
94 		}
95 	} else {
96 		zend_throw_exception(NULL, "Cannot clone unconstructed IntlDateFormatter", 0);
97 	}
98 	return new_obj;
99 }
100 /* }}} */
101 
102 /*
103  * 'IntlDateFormatter' class registration structures & functions
104  */
105 
106 /* {{{ dateformat_register_class
107  * Initialize 'IntlDateFormatter' class
108  */
dateformat_register_IntlDateFormatter_class(void)109 void dateformat_register_IntlDateFormatter_class( void )
110 {
111 	zend_class_entry ce;
112 
113 	/* Create and register 'IntlDateFormatter' class. */
114 	INIT_CLASS_ENTRY( ce, "IntlDateFormatter", class_IntlDateFormatter_methods );
115 	ce.create_object = IntlDateFormatter_object_create;
116 	IntlDateFormatter_ce_ptr = zend_register_internal_class( &ce );
117 
118 	memcpy(&IntlDateFormatter_handlers, &std_object_handlers,
119 		sizeof IntlDateFormatter_handlers);
120 	IntlDateFormatter_handlers.offset = XtOffsetOf(IntlDateFormatter_object, zo);
121 	IntlDateFormatter_handlers.clone_obj = IntlDateFormatter_object_clone;
122 	IntlDateFormatter_handlers.dtor_obj = IntlDateFormatter_object_dtor;
123 	IntlDateFormatter_handlers.free_obj = IntlDateFormatter_object_free;
124 }
125 /* }}} */
126