This source file includes following definitions.
- ZEND_EXTERN_MODULE_GLOBALS
- intl_free_custom_error_msg
- intl_error_create
- intl_error_init
- intl_error_reset
- intl_error_set_custom_msg
- intl_error_get_message
- intl_error_set_code
- intl_error_get_code
- intl_error_set
- intl_errors_set
- intl_errors_reset
- intl_errors_set_custom_msg
- intl_errors_set_code
- intl_register_IntlException_class
- intl_parse_error_to_string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30 ZEND_EXTERN_MODULE_GLOBALS( intl )
31
32 static zend_class_entry *IntlException_ce_ptr;
33
34
35
36
37 static intl_error* intl_g_error_get( TSRMLS_D )
38 {
39 return &INTL_G( g_error );
40 }
41
42
43
44
45
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
61
62
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
74
75
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
88
89
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
102
103
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
119 intl_free_custom_error_msg( err TSRMLS_CC );
120
121
122 err->free_custom_error_message = copyMsg;
123
124
125 err->custom_error_message = copyMsg ? estrdup( msg ) : msg;
126 }
127
128
129
130
131
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
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
157
158
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
169
170
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
181
182
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
191
192
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
201
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
212
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
223
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
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
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
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
325
326
327
328
329
330