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 zend_class_entry *IntlException_ce_ptr;
33
34
35
36
37 static intl_error* intl_g_error_get( void )
38 {
39 return &INTL_G( g_error );
40 }
41
42
43
44
45
46 static void intl_free_custom_error_msg( intl_error* err )
47 {
48 if( !err && !( err = intl_g_error_get( ) ) )
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( void )
64 {
65 intl_error* err = ecalloc( 1, sizeof( intl_error ) );
66
67 intl_error_init( err );
68
69 return err;
70 }
71
72
73
74
75
76 void intl_error_init( intl_error* err )
77 {
78 if( !err && !( err = intl_g_error_get( ) ) )
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 )
91 {
92 if( !err && !( err = intl_g_error_get( ) ) )
93 return;
94
95 err->code = U_ZERO_ERROR;
96
97 intl_free_custom_error_msg( err );
98 }
99
100
101
102
103
104 void intl_error_set_custom_msg( intl_error* err, const char* msg, int copyMsg )
105 {
106 if( !msg )
107 return;
108
109 if( !err ) {
110 if( INTL_G( error_level ) )
111 php_error_docref( NULL, INTL_G( error_level ), "%s", msg );
112 if( INTL_G( use_exceptions ) )
113 zend_throw_exception_ex( IntlException_ce_ptr, 0, "%s", msg );
114 }
115 if( !err && !( err = intl_g_error_get( ) ) )
116 return;
117
118
119 intl_free_custom_error_msg( err );
120
121
122 err->free_custom_error_message = copyMsg;
123
124
125 err->custom_error_message = copyMsg ? estrdup( msg ) : (char *) msg;
126 }
127
128
129
130
131
132 zend_string * intl_error_get_message( intl_error* err )
133 {
134 const char *uErrorName = NULL;
135 zend_string *errMessage = 0;
136
137 if( !err && !( err = intl_g_error_get( ) ) )
138 return ZSTR_EMPTY_ALLOC();
139
140 uErrorName = u_errorName( err->code );
141
142
143 if( err->custom_error_message )
144 {
145 errMessage = strpprintf(0, "%s: %s", err->custom_error_message, uErrorName );
146 }
147 else
148 {
149 errMessage = strpprintf(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 )
160 {
161 if( !err && !( err = intl_g_error_get( ) ) )
162 return;
163
164 err->code = err_code;
165 }
166
167
168
169
170
171 UErrorCode intl_error_get_code( intl_error* err )
172 {
173 if( !err && !( err = intl_g_error_get( ) ) )
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, const char* msg, int copyMsg )
184 {
185 intl_error_set_code( err, code );
186 intl_error_set_custom_msg( err, msg, copyMsg );
187 }
188
189
190
191
192
193 void intl_errors_set( intl_error* err, UErrorCode code, const char* msg, int copyMsg )
194 {
195 intl_errors_set_code( err, code );
196 intl_errors_set_custom_msg( err, msg, copyMsg );
197 }
198
199
200
201
202 void intl_errors_reset( intl_error* err )
203 {
204 if(err) {
205 intl_error_reset( err );
206 }
207 intl_error_reset( NULL );
208 }
209
210
211
212
213 void intl_errors_set_custom_msg( intl_error* err, const char* msg, int copyMsg )
214 {
215 if(err) {
216 intl_error_set_custom_msg( err, msg, copyMsg );
217 }
218 intl_error_set_custom_msg( NULL, msg, copyMsg );
219 }
220
221
222
223
224 void intl_errors_set_code( intl_error* err, UErrorCode err_code )
225 {
226 if(err) {
227 intl_error_set_code( err, err_code );
228 }
229 intl_error_set_code( NULL, err_code );
230 }
231
232
233 void intl_register_IntlException_class( void )
234 {
235 zend_class_entry ce;
236
237
238 INIT_CLASS_ENTRY_EX( ce, "IntlException", sizeof( "IntlException" ) - 1, NULL );
239 IntlException_ce_ptr = zend_register_internal_class_ex( &ce,
240 zend_ce_exception );
241 IntlException_ce_ptr->create_object = zend_ce_exception->create_object;
242 }
243
244 smart_str intl_parse_error_to_string( UParseError* pe )
245 {
246 smart_str ret = {0};
247 zend_string *u8str;
248 UErrorCode status;
249 int any = 0;
250
251 assert( pe != NULL );
252
253 smart_str_appends( &ret, "parse error " );
254 if( pe->line > 0 )
255 {
256 smart_str_appends( &ret, "on line " );
257 smart_str_append_long( &ret, (zend_long ) pe->line );
258 any = 1;
259 }
260 if( pe->offset >= 0 ) {
261 if( any )
262 smart_str_appends( &ret, ", " );
263 else
264 smart_str_appends( &ret, "at " );
265
266 smart_str_appends( &ret, "offset " );
267 smart_str_append_long( &ret, (zend_long ) pe->offset );
268 any = 1;
269 }
270
271 if (pe->preContext[0] != 0 ) {
272 if( any )
273 smart_str_appends( &ret, ", " );
274
275 smart_str_appends( &ret, "after \"" );
276 u8str = intl_convert_utf16_to_utf8(pe->preContext, -1, &status );
277 if( !u8str )
278 {
279 smart_str_appends( &ret, "(could not convert parser error pre-context to UTF-8)" );
280 }
281 else {
282 smart_str_append( &ret, u8str );
283 zend_string_release( u8str );
284 }
285 smart_str_appends( &ret, "\"" );
286 any = 1;
287 }
288
289 if( pe->postContext[0] != 0 )
290 {
291 if( any )
292 smart_str_appends( &ret, ", " );
293
294 smart_str_appends( &ret, "before or at \"" );
295 u8str = intl_convert_utf16_to_utf8(pe->postContext, -1, &status );
296 if( !u8str )
297 {
298 smart_str_appends( &ret, "(could not convert parser error post-context to UTF-8)" );
299 }
300 else
301 {
302 smart_str_append( &ret, u8str );
303 zend_string_release( u8str );
304 }
305 smart_str_appends( &ret, "\"" );
306 any = 1;
307 }
308
309 if( !any )
310 {
311 smart_str_free( &ret );
312 smart_str_appends( &ret, "no parse error" );
313 }
314
315 smart_str_0( &ret );
316 return ret;
317 }
318
319
320
321
322
323
324
325
326