xref: /PHP-5.5/ext/intl/intl_error.c (revision 9762609c)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Vadim Savchuk <vsavchuk@productengine.com>                  |
14    |          Dmitry Lakhtyuk <dlakhtyuk@productengine.com>               |
15    |          Stanislav Malyshev <stas@zend.com>                          |
16    +----------------------------------------------------------------------+
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include <php.h>
24 #include <zend_exceptions.h>
25 
26 #include "php_intl.h"
27 #include "intl_error.h"
28 #include "intl_convert.h"
29 
ZEND_EXTERN_MODULE_GLOBALS(intl)30 ZEND_EXTERN_MODULE_GLOBALS( intl )
31 
32 static zend_class_entry *IntlException_ce_ptr;
33 
34 /* {{{ intl_error* intl_g_error_get()
35  * Return global error structure.
36  */
37 static intl_error* intl_g_error_get( TSRMLS_D )
38 {
39 	return &INTL_G( g_error );
40 }
41 /* }}} */
42 
43 /* {{{ void intl_free_custom_error_msg( intl_error* err )
44  * Free mem.
45  */
intl_free_custom_error_msg(intl_error * err TSRMLS_DC)46 static void intl_free_custom_error_msg( intl_error* err TSRMLS_DC )
47 {
48 	if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
49 		return;
50 
51 	if(err->free_custom_error_message ) {
52 		efree( err->custom_error_message );
53 	}
54 
55 	err->custom_error_message      = NULL;
56 	err->free_custom_error_message = 0;
57 }
58 /* }}} */
59 
60 /* {{{ intl_error* intl_error_create()
61  * Create and initialize  internals of 'intl_error'.
62  */
intl_error_create(TSRMLS_D)63 intl_error* intl_error_create( TSRMLS_D )
64 {
65 	intl_error* err = ecalloc( 1, sizeof( intl_error ) );
66 
67 	intl_error_init( err TSRMLS_CC );
68 
69 	return err;
70 }
71 /* }}} */
72 
73 /* {{{ void intl_error_init( intl_error* coll_error )
74  * Initialize internals of 'intl_error'.
75  */
intl_error_init(intl_error * err TSRMLS_DC)76 void intl_error_init( intl_error* err TSRMLS_DC )
77 {
78 	if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
79 		return;
80 
81 	err->code                      = U_ZERO_ERROR;
82 	err->custom_error_message      = NULL;
83 	err->free_custom_error_message = 0;
84 }
85 /* }}} */
86 
87 /* {{{ void intl_error_reset( intl_error* err )
88  * Set last error code to 0 and unset last error message
89  */
intl_error_reset(intl_error * err TSRMLS_DC)90 void intl_error_reset( intl_error* err TSRMLS_DC )
91 {
92 	if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
93 		return;
94 
95 	err->code = U_ZERO_ERROR;
96 
97 	intl_free_custom_error_msg( err TSRMLS_CC );
98 }
99 /* }}} */
100 
101 /* {{{ void intl_error_set_custom_msg( intl_error* err, char* msg, int copyMsg )
102  * Set last error message to msg copying it if needed.
103  */
intl_error_set_custom_msg(intl_error * err,char * msg,int copyMsg TSRMLS_DC)104 void intl_error_set_custom_msg( intl_error* err, char* msg, int copyMsg TSRMLS_DC )
105 {
106 	if( !msg )
107 		return;
108 
109 	if( !err ) {
110 		if( INTL_G( error_level ) )
111 			php_error_docref( NULL TSRMLS_CC, INTL_G( error_level ), "%s", msg );
112 		if( INTL_G( use_exceptions ) )
113 			zend_throw_exception_ex( IntlException_ce_ptr, 0 TSRMLS_CC, "%s", msg );
114 	}
115 	if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
116 		return;
117 
118 	/* Free previous message if any */
119 	intl_free_custom_error_msg( err TSRMLS_CC );
120 
121 	/* Mark message copied if any */
122 	err->free_custom_error_message = copyMsg;
123 
124 	/* Set user's error text message */
125 	err->custom_error_message = copyMsg ? estrdup( msg ) : msg;
126 }
127 /* }}} */
128 
129 /* {{{ const char* intl_error_get_message( intl_error* err )
130  * Create output message in format "<intl_error_text>: <extra_user_error_text>".
131  */
intl_error_get_message(intl_error * err TSRMLS_DC)132 char* intl_error_get_message( intl_error* err TSRMLS_DC )
133 {
134 	const char* uErrorName = NULL;
135 	char*       errMessage = 0;
136 
137 	if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
138 		return estrdup( "" );
139 
140 	uErrorName = u_errorName( err->code );
141 
142 	/* Format output string */
143 	if( err->custom_error_message )
144 	{
145 		spprintf( &errMessage, 0, "%s: %s", err->custom_error_message, uErrorName );
146 	}
147 	else
148 	{
149 		spprintf( &errMessage, 0, "%s", uErrorName );
150 	}
151 
152 	return errMessage;
153 }
154 /* }}} */
155 
156 /* {{{ void intl_error_set_code( intl_error* err, UErrorCode err_code )
157  * Set last error code.
158  */
intl_error_set_code(intl_error * err,UErrorCode err_code TSRMLS_DC)159 void intl_error_set_code( intl_error* err, UErrorCode err_code TSRMLS_DC )
160 {
161 	if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
162 		return;
163 
164 	err->code = err_code;
165 }
166 /* }}} */
167 
168 /* {{{ void intl_error_get_code( intl_error* err )
169  * Return last error code.
170  */
intl_error_get_code(intl_error * err TSRMLS_DC)171 UErrorCode intl_error_get_code( intl_error* err TSRMLS_DC )
172 {
173 	if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
174 		return U_ZERO_ERROR;
175 
176 	return err->code;
177 }
178 /* }}} */
179 
180 /* {{{ void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg )
181  * Set error code and message.
182  */
intl_error_set(intl_error * err,UErrorCode code,char * msg,int copyMsg TSRMLS_DC)183 void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC )
184 {
185 	intl_error_set_code( err, code TSRMLS_CC );
186 	intl_error_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
187 }
188 /* }}} */
189 
190 /* {{{ void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg )
191  * Set error code and message.
192  */
intl_errors_set(intl_error * err,UErrorCode code,char * msg,int copyMsg TSRMLS_DC)193 void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC )
194 {
195 	intl_errors_set_code( err, code TSRMLS_CC );
196 	intl_errors_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
197 }
198 /* }}} */
199 
200 /* {{{ void intl_errors_reset( intl_error* err )
201  */
intl_errors_reset(intl_error * err TSRMLS_DC)202 void intl_errors_reset( intl_error* err TSRMLS_DC )
203 {
204 	if(err) {
205 		intl_error_reset( err TSRMLS_CC );
206 	}
207 	intl_error_reset( NULL TSRMLS_CC );
208 }
209 /* }}} */
210 
211 /* {{{ void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg )
212  */
intl_errors_set_custom_msg(intl_error * err,char * msg,int copyMsg TSRMLS_DC)213 void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg TSRMLS_DC )
214 {
215 	if(err) {
216 		intl_error_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
217 	}
218 	intl_error_set_custom_msg( NULL, msg, copyMsg TSRMLS_CC );
219 }
220 /* }}} */
221 
222 /* {{{ intl_errors_set_code( intl_error* err, UErrorCode err_code )
223  */
intl_errors_set_code(intl_error * err,UErrorCode err_code TSRMLS_DC)224 void intl_errors_set_code( intl_error* err, UErrorCode err_code TSRMLS_DC )
225 {
226 	if(err) {
227 		intl_error_set_code( err, err_code TSRMLS_CC );
228 	}
229 	intl_error_set_code( NULL, err_code TSRMLS_CC );
230 }
231 /* }}} */
232 
intl_register_IntlException_class(TSRMLS_D)233 void intl_register_IntlException_class( TSRMLS_D )
234 {
235 	zend_class_entry ce,
236 					 *default_exception_ce;
237 
238 	default_exception_ce = zend_exception_get_default( TSRMLS_C );
239 
240 	/* Create and register 'IntlException' class. */
241 	INIT_CLASS_ENTRY_EX( ce, "IntlException", sizeof( "IntlException" ) - 1, NULL );
242 	IntlException_ce_ptr = zend_register_internal_class_ex( &ce,
243 		default_exception_ce, NULL TSRMLS_CC );
244 	IntlException_ce_ptr->create_object = default_exception_ce->create_object;
245 }
246 
intl_parse_error_to_string(UParseError * pe)247 smart_str intl_parse_error_to_string( UParseError* pe )
248 {
249 	smart_str  ret = {0};
250 	char       *buf;
251 	int        u8len;
252 	UErrorCode status;
253 	int        any = 0;
254 
255 	assert( pe != NULL );
256 
257 	smart_str_appends( &ret, "parse error " );
258 	if( pe->line > 0 )
259 	{
260 		smart_str_appends( &ret, "on line " );
261 		smart_str_append_long( &ret, (long ) pe->line );
262 		any = 1;
263 	}
264 	if( pe->offset >= 0 ) {
265 		if( any )
266 			smart_str_appends( &ret, ", " );
267 		else
268 			smart_str_appends( &ret, "at " );
269 
270 		smart_str_appends( &ret, "offset " );
271 		smart_str_append_long( &ret, (long ) pe->offset );
272 		any = 1;
273 	}
274 
275 	if (pe->preContext[0] != 0 ) {
276 		if( any )
277 			smart_str_appends( &ret, ", " );
278 
279 		smart_str_appends( &ret, "after \"" );
280 		intl_convert_utf16_to_utf8( &buf, &u8len, pe->preContext, -1, &status );
281 		if( U_FAILURE( status ) )
282 		{
283 			smart_str_appends( &ret, "(could not convert parser error pre-context to UTF-8)" );
284 		}
285 		else {
286 			smart_str_appendl( &ret, buf, u8len );
287 			efree( buf );
288 		}
289 		smart_str_appends( &ret, "\"" );
290 		any = 1;
291 	}
292 
293 	if( pe->postContext[0] != 0 )
294 	{
295 		if( any )
296 			smart_str_appends( &ret, ", " );
297 
298 		smart_str_appends( &ret, "before or at \"" );
299 		intl_convert_utf16_to_utf8( &buf, &u8len, pe->postContext, -1, &status );
300 		if( U_FAILURE( status ) )
301 		{
302 			smart_str_appends( &ret, "(could not convert parser error post-context to UTF-8)" );
303 		}
304 		else
305 		{
306 			smart_str_appendl( &ret, buf, u8len );
307 			efree( buf );
308 		}
309 		smart_str_appends( &ret, "\"" );
310 		any = 1;
311 	}
312 
313 	if( !any )
314 	{
315 		smart_str_free( &ret );
316 		smart_str_appends( &ret, "no parse error" );
317 	}
318 
319 	smart_str_0( &ret );
320 	return ret;
321 }
322 
323 /*
324  * Local variables:
325  * tab-width: 4
326  * c-basic-offset: 4
327  * End:
328  * vim600: noet sw=4 ts=4 fdm=marker
329  * vim<600: noet sw=4 ts=4
330  */
331