Back to index

php5  5.3.10
intl_error.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | This source file is subject to version 3.01 of the PHP license,      |
00006    | that is bundled with this package in the file LICENSE, and is        |
00007    | available through the world-wide-web at the following url:           |
00008    | http://www.php.net/license/3_01.txt                                  |
00009    | If you did not receive a copy of the PHP license and are unable to   |
00010    | obtain it through the world-wide-web, please send a note to          |
00011    | license@php.net so we can mail you a copy immediately.               |
00012    +----------------------------------------------------------------------+
00013    | Authors: Vadim Savchuk <vsavchuk@productengine.com>                  |
00014    |          Dmitry Lakhtyuk <dlakhtyuk@productengine.com>               |
00015    |          Stanislav Malyshev <stas@zend.com>                          |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 #include "config.h"
00021 #endif
00022 
00023 #include <php.h>
00024 
00025 #include "php_intl.h"
00026 #include "intl_error.h"
00027 
00028 ZEND_EXTERN_MODULE_GLOBALS( intl )
00029 
00030 /* {{{ intl_error* intl_g_error_get()
00031  * Return global error structure.
00032  */
00033 static intl_error* intl_g_error_get( TSRMLS_D )
00034 {
00035        return &INTL_G( g_error );
00036 }
00037 /* }}} */
00038 
00039 /* {{{ void intl_free_custom_error_msg( intl_error* err )
00040  * Free mem.
00041  */
00042 static void intl_free_custom_error_msg( intl_error* err TSRMLS_DC )
00043 {
00044        if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
00045               return;
00046 
00047        if(err->free_custom_error_message ) {
00048               efree( err->custom_error_message );
00049        }
00050 
00051        err->custom_error_message      = NULL;
00052        err->free_custom_error_message = 0;
00053 }
00054 /* }}} */
00055 
00056 /* {{{ intl_error* intl_error_create()
00057  * Create and initialize  internals of 'intl_error'.
00058  */
00059 intl_error* intl_error_create( TSRMLS_D )
00060 {
00061        intl_error* err = ecalloc( 1, sizeof( intl_error ) );
00062 
00063        intl_error_init( err TSRMLS_CC );
00064 
00065        return err;
00066 }
00067 /* }}} */
00068 
00069 /* {{{ void intl_error_init( intl_error* coll_error )
00070  * Initialize internals of 'intl_error'.
00071  */
00072 void intl_error_init( intl_error* err TSRMLS_DC )
00073 {
00074        if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
00075               return;
00076 
00077        err->code                      = U_ZERO_ERROR;
00078        err->custom_error_message      = NULL;
00079        err->free_custom_error_message = 0;
00080 }
00081 /* }}} */
00082 
00083 /* {{{ void intl_error_reset( intl_error* err )
00084  * Set last error code to 0 and unset last error message
00085  */
00086 void intl_error_reset( intl_error* err TSRMLS_DC )
00087 {
00088        if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
00089               return;
00090 
00091        err->code = U_ZERO_ERROR;
00092 
00093        intl_free_custom_error_msg( err TSRMLS_CC );
00094 }
00095 /* }}} */
00096 
00097 /* {{{ void intl_error_set_custom_msg( intl_error* err, char* msg, int copyMsg )
00098  * Set last error message to msg copying it if needed.
00099  */
00100 void intl_error_set_custom_msg( intl_error* err, char* msg, int copyMsg TSRMLS_DC )
00101 {
00102        if( !msg )
00103               return;
00104 
00105        if(!err && INTL_G(error_level)) {
00106               php_error_docref(NULL TSRMLS_CC, INTL_G(error_level), "%s", msg);            
00107        }
00108        if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
00109               return;
00110 
00111        /* Free previous message if any */
00112        intl_free_custom_error_msg( err TSRMLS_CC );
00113 
00114        /* Mark message copied if any */
00115        err->free_custom_error_message = copyMsg;
00116 
00117        /* Set user's error text message */
00118        err->custom_error_message = copyMsg ? estrdup( msg ) : msg;
00119 }
00120 /* }}} */
00121 
00122 /* {{{ const char* intl_error_get_message( intl_error* err )
00123  * Create output message in format "<intl_error_text>: <extra_user_error_text>".
00124  */
00125 char* intl_error_get_message( intl_error* err TSRMLS_DC )
00126 {
00127        const char* uErrorName = NULL;
00128        char*       errMessage = 0;
00129 
00130        if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
00131               return estrdup( "" );
00132 
00133        uErrorName = u_errorName( err->code );
00134 
00135        /* Format output string */
00136        if( err->custom_error_message )
00137        {
00138               spprintf( &errMessage, 0, "%s: %s", err->custom_error_message, uErrorName );
00139        }
00140        else
00141        {
00142               spprintf( &errMessage, 0, "%s", uErrorName );
00143        }
00144 
00145        return errMessage;
00146 }
00147 /* }}} */
00148 
00149 /* {{{ void intl_error_set_code( intl_error* err, UErrorCode err_code )
00150  * Set last error code.
00151  */
00152 void intl_error_set_code( intl_error* err, UErrorCode err_code TSRMLS_DC )
00153 {
00154        if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
00155               return;
00156 
00157        err->code = err_code;
00158 }
00159 /* }}} */
00160 
00161 /* {{{ void intl_error_get_code( intl_error* err )
00162  * Return last error code.
00163  */
00164 UErrorCode intl_error_get_code( intl_error* err TSRMLS_DC )
00165 {
00166        if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
00167               return U_ZERO_ERROR;
00168 
00169        return err->code;
00170 }
00171 /* }}} */
00172 
00173 /* {{{ void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg )
00174  * Set error code and message.
00175  */
00176 void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC )
00177 {
00178        intl_error_set_code( err, code TSRMLS_CC );
00179        intl_error_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
00180 }
00181 /* }}} */
00182 
00183 /* {{{ void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg )
00184  * Set error code and message.
00185  */
00186 void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC )
00187 {
00188        intl_errors_set_code( err, code TSRMLS_CC );
00189        intl_errors_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
00190 }
00191 /* }}} */
00192 
00193 /* {{{ void intl_errors_reset( intl_error* err )
00194  */
00195 void intl_errors_reset( intl_error* err TSRMLS_DC )
00196 {
00197        if(err) {
00198               intl_error_reset( err TSRMLS_CC );
00199        }
00200        intl_error_reset( NULL TSRMLS_CC );
00201 }
00202 /* }}} */
00203 
00204 /* {{{ void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg )
00205  */
00206 void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg TSRMLS_DC )
00207 {
00208        if(err) {
00209               intl_error_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
00210        }
00211        intl_error_set_custom_msg( NULL, msg, copyMsg TSRMLS_CC );
00212 }
00213 /* }}} */
00214 
00215 /* {{{ intl_errors_set_code( intl_error* err, UErrorCode err_code )
00216  */
00217 void intl_errors_set_code( intl_error* err, UErrorCode err_code TSRMLS_DC )
00218 {
00219        if(err) {
00220               intl_error_set_code( err, err_code TSRMLS_CC );
00221        }
00222        intl_error_set_code( NULL, err_code TSRMLS_CC );
00223 }
00224 /* }}} */
00225 
00226 /*
00227  * Local variables:
00228  * tab-width: 4
00229  * c-basic-offset: 4
00230  * End:
00231  * vim600: noet sw=4 ts=4 fdm=marker
00232  * vim<600: noet sw=4 ts=4
00233  */