Add zend_throw_exception_ex() which allows to format exception messages.

#
# Since we don't have any portable way of printing into a dynamic buffer i
# used a stack buffer of 1K (just like the error printing) and used a dynamic
# buffer in case the necessary function is available.
#
This commit is contained in:
Marcus Boerger 2003-08-28 22:56:41 +00:00
parent 12376a2270
commit fa70708d15
4 changed files with 96 additions and 0 deletions

View file

@ -144,6 +144,53 @@ ZEND_API zend_class_entry *zend_exception_get_default(void)
return default_exception_ptr;
}
ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...)
{
zval *ex;
va_list arg;
#ifdef _GNU_SOURCE
char *message;
va_start(arg, format);
vasprintf(message, format, arg);
va_end(arg);
#else
char message[1024];
va_start(arg, format);
vsnprintf(message, sizeof(message), format, arg);
va_end(arg);
#endif
MAKE_STD_ZVAL(ex);
if (exception_ce) {
if (!instanceof_function(exception_ce, default_exception_ptr TSRMLS_CC)) {
zend_error(E_NOTICE, "Exceptions must be derived from exception");
exception_ce = default_exception_ptr;
}
} else {
exception_ce = default_exception_ptr;
}
object_init_ex(ex, exception_ce);
if (message) {
zend_update_property_string(exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
}
if (code) {
zend_update_property_long(exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
}
#ifdef _GNU_SOURCE
free(message);
#endif
EG(exception) = ex;
}
/* at the moment we can't use zend_throw_exception_ex because we don't have a protable
* vsnprintf that tells us the number of characters needed nor do we have spprintf from
* php or asprintf from glibc always.
*/
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC)
{
zval *ex;

View file

@ -30,6 +30,7 @@ ZEND_API void zend_register_default_classes(TSRMLS_D);
/* exception_ce NULL or zend_exception_get_default() or a derived class
* message NULL or the message of the exception */
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC);
ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...);
/* show an exception using zend_error(E_ERROR,...) */
ZEND_API void zend_exception_error(zval *exception TSRMLS_DC);

View file

@ -144,6 +144,53 @@ ZEND_API zend_class_entry *zend_exception_get_default(void)
return default_exception_ptr;
}
ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...)
{
zval *ex;
va_list arg;
#ifdef _GNU_SOURCE
char *message;
va_start(arg, format);
vasprintf(message, format, arg);
va_end(arg);
#else
char message[1024];
va_start(arg, format);
vsnprintf(message, sizeof(message), format, arg);
va_end(arg);
#endif
MAKE_STD_ZVAL(ex);
if (exception_ce) {
if (!instanceof_function(exception_ce, default_exception_ptr TSRMLS_CC)) {
zend_error(E_NOTICE, "Exceptions must be derived from exception");
exception_ce = default_exception_ptr;
}
} else {
exception_ce = default_exception_ptr;
}
object_init_ex(ex, exception_ce);
if (message) {
zend_update_property_string(exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
}
if (code) {
zend_update_property_long(exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
}
#ifdef _GNU_SOURCE
free(message);
#endif
EG(exception) = ex;
}
/* at the moment we can't use zend_throw_exception_ex because we don't have a protable
* vsnprintf that tells us the number of characters needed nor do we have spprintf from
* php or asprintf from glibc always.
*/
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC)
{
zval *ex;

View file

@ -30,6 +30,7 @@ ZEND_API void zend_register_default_classes(TSRMLS_D);
/* exception_ce NULL or zend_exception_get_default() or a derived class
* message NULL or the message of the exception */
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC);
ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...);
/* show an exception using zend_error(E_ERROR,...) */
ZEND_API void zend_exception_error(zval *exception TSRMLS_DC);