forked from CloCkWeRX/sdo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SDO_CPPException.cpp
executable file
·365 lines (309 loc) · 10.8 KB
/
SDO_CPPException.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
+----------------------------------------------------------------------+
| (c) Copyright IBM Corporation 2005, 2006. |
| All Rights Reserved. |
+----------------------------------------------------------------------+
| |
| Licensed under the Apache License, Version 2.0 (the "License"); you |
| may not use this file except in compliance with the License. You may |
| obtain a copy of the License at |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| implied. See the License for the specific language governing |
| permissions and limitations under the License. |
+----------------------------------------------------------------------+
| Author: Caroline Maynard |
+----------------------------------------------------------------------+
*/
static char rcs_id[] = "$Id: SDO_CPPException.cpp 221678 2006-10-13 11:08:47Z cem $";
#ifdef PHP_WIN32
#include <iostream>
#include <math.h>
#include "zend_config.w32.h"
#endif
#include "php.h"
#include "zend_exceptions.h"
#include "php_sdo_int.h"
#include "commonj/sdo/SDORuntimeException.h"
#define CLASS_NAME "SDO_CPPException"
static zend_object_handlers sdo_cppexception_object_handlers;
/* {{{ sdo_throw_runtimeexception
* maps a C++ SDO exception to a PHP SDO exception and throws it
*/
zval *sdo_throw_runtimeexception(SDORuntimeException *e TSRMLS_DC)
{
zval *z_cause;
zval *z_sdo_exception;
zend_class_entry *exception_class;
const char *message = e->getMessageText();
const char *exception_type = e->getEClassName();
MAKE_STD_ZVAL(z_cause);
sdo_cppexception_new (z_cause, e TSRMLS_CC);
if (strcmp(exception_type, "SDOUnsupportedOperationException") == 0)
exception_class = sdo_unsupportedoperationexception_class_entry;
else if (strcmp(exception_type, "SDOPropertyNotFoundException") == 0 ||
strcmp(exception_type, "SDOPathNotFoundException") == 0)
exception_class = sdo_propertynotfoundexception_class_entry;
else if (strcmp(exception_type, "SDOTypeNotFoundException") == 0)
exception_class = sdo_typenotfoundexception_class_entry;
else if (strcmp(exception_type, "SDOIndexOutOfRangeException") == 0)
exception_class = sdo_indexoutofboundsexception_class_entry;
else if (strcmp(exception_type, "SDOInvalidConversionException") == 0)
exception_class = sdo_invalidconversionexception_class_entry;
else if (strcmp(exception_type, "SDOPropertyNotSetException") == 0)
exception_class = sdo_propertynotsetexception_class_entry;
else
exception_class = sdo_exception_class_entry;
z_sdo_exception = sdo_throw_exception (exception_class, message, 0, z_cause TSRMLS_CC);
zval_ptr_dtor(&z_cause);
return z_sdo_exception;
}
/* }}} */
/* {{{ sdo_cppexception_object_create
*/
static zend_object_value sdo_cppexception_create_object(zend_class_entry *ce TSRMLS_DC)
{
zend_object *object;
zval *tmp; /* this must be passed to hash_copy, but doesn't seem to be used */
zval z_object;
z_object.value.obj = zend_objects_new(&object, ce TSRMLS_CC);
z_object.value.obj.handlers = &sdo_cppexception_object_handlers;
ALLOC_HASHTABLE(object->properties);
zend_hash_init(object->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
// zend_hash_copy(object->properties, &ce->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
#if PHP_VERSION_ID < 50399
zend_hash_copy(object->properties, &ce->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &tmp, sizeof(zval *));
#else
object_properties_init(object, ce);
#endif
return z_object.value.obj;
}
/* }}} */
/* {{{ sdo_cppexception_get_property
*/
static void sdo_cppexception_get_property(zval *me, /* const */ char *name, long name_len, zval *return_value TSRMLS_DC)
{
zval *value;
value = zend_read_property(sdo_cppexception_class_entry, me,
name, name_len, 0 TSRMLS_CC);
*return_value = *value;
zval_copy_ctor(return_value);
INIT_PZVAL(return_value);
}
/* }}} */
/* {{{ sdo_cppexception_cast_object
*/
#if PHP_MAJOR_VERSION > 5 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 1)
static int sdo_cppexception_cast_object(zval *readobj, zval *writeobj, int type TSRMLS_DC)
{
int should_free = 0;
#else
static int sdo_cppexception_cast_object(zval *readobj, zval *writeobj, int type, int should_free TSRMLS_DC)
{
#endif
ostringstream print_buf;
zval free_obj;
int rc = SUCCESS;
if (should_free) {
free_obj = *writeobj;
}
sdo_cppexception_get_property(readobj, "string", sizeof("string") - 1, writeobj TSRMLS_CC);
switch(type) {
case IS_STRING:
convert_to_string(writeobj);
break;
case IS_BOOL:
convert_to_boolean(writeobj);
break;
case IS_LONG:
convert_to_long(writeobj);
break;
case IS_DOUBLE:
convert_to_double(writeobj);
break;
default:
rc = FAILURE;
}
if (should_free) {
zval_dtor(&free_obj);
}
return rc;
}
/* }}} */
/* {{{ sdo_cppexception_minit
* This clas is not a PHP Exception, merely a wrapper for the data
* from a C++ SDORuntimeException. It has similar properties to a
* PHP Exception, but there's no debug trace.
*/
void sdo_cppexception_minit(zend_class_entry *ce TSRMLS_DC)
{
sdo_cppexception_class_entry =
zend_register_internal_class(ce TSRMLS_CC);
sdo_cppexception_class_entry->create_object = sdo_cppexception_create_object;
memcpy(&sdo_cppexception_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
sdo_cppexception_object_handlers.cast_object = sdo_cppexception_cast_object;
zend_declare_property_string(sdo_cppexception_class_entry,
"class", sizeof("class") - 1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_string(sdo_cppexception_class_entry,
"file", sizeof("file") - 1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_long(sdo_cppexception_class_entry,
"line", sizeof("line") - 1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_string(sdo_cppexception_class_entry,
"function", sizeof("function") - 1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_string(sdo_cppexception_class_entry,
"message", sizeof("message") - 1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_long(sdo_cppexception_class_entry,
"severity", sizeof("severity") - 1, E_ERROR, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_string(sdo_cppexception_class_entry,
"string", sizeof("string") - 1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
}
/* }}} */
/* {{{ sdo_cppexception_new
* Instantiate an SDO_CPPException from an SDORuntimeException
*/
void sdo_cppexception_new(zval *me, SDORuntimeException *cpp_exception TSRMLS_DC)
{
const char *eclass = cpp_exception->getEClassName();
const char *function = cpp_exception->getFunctionName();
const char *message = cpp_exception->getMessageText();
const char *file = cpp_exception->getFileName();
long line = cpp_exception->getLineNumber();
// char *class_name, *space;
long severity;
ostringstream print_buf;
std::string print_string;
switch(cpp_exception->getSeverity()) {
case SDORuntimeException::Normal:
severity = E_NOTICE;
break;
case SDORuntimeException::Warning:
severity = E_WARNING;
break;
case SDORuntimeException::Error:
case SDORuntimeException::Severe:
default:
severity = E_ERROR;
break;
}
if (object_init_ex(me, sdo_cppexception_class_entry) == FAILURE) {
/* const */ char *space, *class_name = get_active_class_name(&space TSRMLS_CC);
// class_name = get_active_class_name(&space TSRMLS_CC);
php_error(E_ERROR, "%s%s%s(): internal error (%i) - failed to instantiate %s object",
class_name, space, get_active_function_name(TSRMLS_C), __LINE__, CLASS_NAME);
return;
}
if (eclass) {
zend_update_property_string(sdo_cppexception_class_entry, me,
"class", sizeof("class")-1, (char *)eclass TSRMLS_CC);
}
if (file) {
zend_update_property_string(sdo_cppexception_class_entry, me,
"file", sizeof("file")-1, (char *)file TSRMLS_CC);
}
zend_update_property_long(sdo_cppexception_class_entry, me,
"line", sizeof("line")-1, line TSRMLS_CC);
if (function) {
zend_update_property_string(sdo_cppexception_class_entry, me,
"function", sizeof("function")-1, (char *)function TSRMLS_CC);
}
if (message) {
zend_update_property_string(sdo_cppexception_class_entry, me,
"message", sizeof("message")-1, (char *)message TSRMLS_CC);
}
zend_update_property_long(sdo_cppexception_class_entry, me,
"severity", sizeof("severity")-1, severity TSRMLS_CC);
print_buf << *cpp_exception;
print_string = print_buf.str();
zend_update_property_stringl (sdo_cppexception_class_entry, me,
"string", sizeof("string") - 1,
(char *)print_string.c_str(), print_string.length() TSRMLS_CC);
}
/* }}} */
/* {{{ SDO_CPPException::getClass
*/
PHP_METHOD(SDO_CPPException, getClass)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
sdo_cppexception_get_property(
getThis(), "class", sizeof("class") - 1, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ SDO_CPPException::getFile
*/
PHP_METHOD(SDO_CPPException, getFile)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
sdo_cppexception_get_property(
getThis(), "file", sizeof("file") - 1, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ SDO_CPPException::getLine
*/
PHP_METHOD(SDO_CPPException, getLine)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
sdo_cppexception_get_property(
getThis(), "line", sizeof("line") - 1, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ SDO_CPPException::getFunction
*/
PHP_METHOD(SDO_CPPException, getFunction)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
sdo_cppexception_get_property(
getThis(), "function", sizeof("function") - 1, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ SDO_CPPException::getMessage
*/
PHP_METHOD(SDO_CPPException, getMessage)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
sdo_cppexception_get_property(
getThis(), "message", sizeof("message") - 1, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ SDO_CPPException::getSeverity
*/
PHP_METHOD(SDO_CPPException, getSeverity)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
sdo_cppexception_get_property(
getThis(), "severity", sizeof("severity") - 1, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ SDO_CPPException::__toString
*/
PHP_METHOD(SDO_CPPException, __toString)
{
#if PHP_MAJOR_VERSION > 5 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 1)
sdo_cppexception_cast_object(getThis(), return_value, IS_STRING TSRMLS_CC);
#else
sdo_cppexception_cast_object(getThis(), return_value, IS_STRING, 0 TSRMLS_CC);
#endif
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/