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
|
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Vadim Savchuk <vsavchuk@productengine.com> |
| Dmitry Lakhtyuk <dlakhtyuk@productengine.com> |
| Stanislav Malyshev <stas@zend.com> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <php.h>
#include <zend_exceptions.h>
#include "php_intl.h"
#include "intl_error.h"
#include "intl_convert.h"
ZEND_EXTERN_MODULE_GLOBALS( intl )
static zend_class_entry *IntlException_ce_ptr;
/* {{{ intl_error* intl_g_error_get()
* Return global error structure.
*/
static intl_error* intl_g_error_get( TSRMLS_D )
{
return &INTL_G( g_error );
}
/* }}} */
/* {{{ void intl_free_custom_error_msg( intl_error* err )
* Free mem.
*/
static void intl_free_custom_error_msg( intl_error* err TSRMLS_DC )
{
if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
return;
if(err->free_custom_error_message ) {
efree( err->custom_error_message );
}
err->custom_error_message = NULL;
err->free_custom_error_message = 0;
}
/* }}} */
/* {{{ intl_error* intl_error_create()
* Create and initialize internals of 'intl_error'.
*/
intl_error* intl_error_create( TSRMLS_D )
{
intl_error* err = ecalloc( 1, sizeof( intl_error ) );
intl_error_init( err TSRMLS_CC );
return err;
}
/* }}} */
/* {{{ void intl_error_init( intl_error* coll_error )
* Initialize internals of 'intl_error'.
*/
void intl_error_init( intl_error* err TSRMLS_DC )
{
if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
return;
err->code = U_ZERO_ERROR;
err->custom_error_message = NULL;
err->free_custom_error_message = 0;
}
/* }}} */
/* {{{ void intl_error_reset( intl_error* err )
* Set last error code to 0 and unset last error message
*/
void intl_error_reset( intl_error* err TSRMLS_DC )
{
if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
return;
err->code = U_ZERO_ERROR;
intl_free_custom_error_msg( err TSRMLS_CC );
}
/* }}} */
/* {{{ void intl_error_set_custom_msg( intl_error* err, char* msg, int copyMsg )
* Set last error message to msg copying it if needed.
*/
void intl_error_set_custom_msg( intl_error* err, char* msg, int copyMsg TSRMLS_DC )
{
if( !msg )
return;
if( !err ) {
if( INTL_G( error_level ) )
php_error_docref( NULL TSRMLS_CC, INTL_G( error_level ), "%s", msg );
if( INTL_G( use_exceptions ) )
zend_throw_exception_ex( IntlException_ce_ptr, 0 TSRMLS_CC, "%s", msg );
}
if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
return;
/* Free previous message if any */
intl_free_custom_error_msg( err TSRMLS_CC );
/* Mark message copied if any */
err->free_custom_error_message = copyMsg;
/* Set user's error text message */
err->custom_error_message = copyMsg ? estrdup( msg ) : msg;
}
/* }}} */
/* {{{ const char* intl_error_get_message( intl_error* err )
* Create output message in format "<intl_error_text>: <extra_user_error_text>".
*/
zend_string * intl_error_get_message( intl_error* err TSRMLS_DC )
{
const char *uErrorName = NULL;
zend_string *errMessage = 0;
if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
return STR_EMPTY_ALLOC();
uErrorName = u_errorName( err->code );
/* Format output string */
if( err->custom_error_message )
{
errMessage = strpprintf(0, "%s: %s", err->custom_error_message, uErrorName );
}
else
{
errMessage = strpprintf(0, "%s", uErrorName );
}
return errMessage;
}
/* }}} */
/* {{{ void intl_error_set_code( intl_error* err, UErrorCode err_code )
* Set last error code.
*/
void intl_error_set_code( intl_error* err, UErrorCode err_code TSRMLS_DC )
{
if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
return;
err->code = err_code;
}
/* }}} */
/* {{{ void intl_error_get_code( intl_error* err )
* Return last error code.
*/
UErrorCode intl_error_get_code( intl_error* err TSRMLS_DC )
{
if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
return U_ZERO_ERROR;
return err->code;
}
/* }}} */
/* {{{ void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg )
* Set error code and message.
*/
void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC )
{
intl_error_set_code( err, code TSRMLS_CC );
intl_error_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
}
/* }}} */
/* {{{ void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg )
* Set error code and message.
*/
void intl_errors_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC )
{
intl_errors_set_code( err, code TSRMLS_CC );
intl_errors_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
}
/* }}} */
/* {{{ void intl_errors_reset( intl_error* err )
*/
void intl_errors_reset( intl_error* err TSRMLS_DC )
{
if(err) {
intl_error_reset( err TSRMLS_CC );
}
intl_error_reset( NULL TSRMLS_CC );
}
/* }}} */
/* {{{ void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg )
*/
void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg TSRMLS_DC )
{
if(err) {
intl_error_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
}
intl_error_set_custom_msg( NULL, msg, copyMsg TSRMLS_CC );
}
/* }}} */
/* {{{ intl_errors_set_code( intl_error* err, UErrorCode err_code )
*/
void intl_errors_set_code( intl_error* err, UErrorCode err_code TSRMLS_DC )
{
if(err) {
intl_error_set_code( err, err_code TSRMLS_CC );
}
intl_error_set_code( NULL, err_code TSRMLS_CC );
}
/* }}} */
void intl_register_IntlException_class( TSRMLS_D )
{
zend_class_entry ce,
*default_exception_ce;
default_exception_ce = zend_exception_get_default( TSRMLS_C );
/* Create and register 'IntlException' class. */
INIT_CLASS_ENTRY_EX( ce, "IntlException", sizeof( "IntlException" ) - 1, NULL );
IntlException_ce_ptr = zend_register_internal_class_ex( &ce,
default_exception_ce TSRMLS_CC );
IntlException_ce_ptr->create_object = default_exception_ce->create_object;
}
smart_str intl_parse_error_to_string( UParseError* pe )
{
smart_str ret = {0};
char *buf;
int u8len;
UErrorCode status;
int any = 0;
assert( pe != NULL );
smart_str_appends( &ret, "parse error " );
if( pe->line > 0 )
{
smart_str_appends( &ret, "on line " );
smart_str_append_long( &ret, (zend_long ) pe->line );
any = 1;
}
if( pe->offset >= 0 ) {
if( any )
smart_str_appends( &ret, ", " );
else
smart_str_appends( &ret, "at " );
smart_str_appends( &ret, "offset " );
smart_str_append_long( &ret, (zend_long ) pe->offset );
any = 1;
}
if (pe->preContext[0] != 0 ) {
if( any )
smart_str_appends( &ret, ", " );
smart_str_appends( &ret, "after \"" );
intl_convert_utf16_to_utf8( &buf, &u8len, pe->preContext, -1, &status );
if( U_FAILURE( status ) )
{
smart_str_appends( &ret, "(could not convert parser error pre-context to UTF-8)" );
}
else {
smart_str_appendl( &ret, buf, u8len );
efree( buf );
}
smart_str_appends( &ret, "\"" );
any = 1;
}
if( pe->postContext[0] != 0 )
{
if( any )
smart_str_appends( &ret, ", " );
smart_str_appends( &ret, "before or at \"" );
intl_convert_utf16_to_utf8( &buf, &u8len, pe->postContext, -1, &status );
if( U_FAILURE( status ) )
{
smart_str_appends( &ret, "(could not convert parser error post-context to UTF-8)" );
}
else
{
smart_str_appendl( &ret, buf, u8len );
efree( buf );
}
smart_str_appends( &ret, "\"" );
any = 1;
}
if( !any )
{
smart_str_free( &ret );
smart_str_appends( &ret, "no parse error" );
}
smart_str_0( &ret );
return ret;
}
/*
* 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
*/
|