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 :
25 : #include "php_intl.h"
26 : #include "intl_error.h"
27 :
28 : ZEND_EXTERN_MODULE_GLOBALS( intl )
29 :
30 : /* {{{ intl_error* intl_g_error_get()
31 : * Return global error structure.
32 : */
33 : static intl_error* intl_g_error_get( TSRMLS_D )
34 46341 : {
35 46341 : return &INTL_G( g_error );
36 : }
37 : /* }}} */
38 :
39 : /* {{{ void intl_free_custom_error_msg( intl_error* err )
40 : * Free mem.
41 : */
42 : static void intl_free_custom_error_msg( intl_error* err TSRMLS_DC )
43 27476 : {
44 27476 : if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
45 0 : return;
46 :
47 27476 : if( !err->free_custom_error_message )
48 27439 : return;
49 :
50 37 : efree( err->custom_error_message );
51 :
52 37 : err->custom_error_message = NULL;
53 37 : err->free_custom_error_message = 0;
54 : }
55 : /* }}} */
56 :
57 : /* {{{ intl_error* intl_error_create()
58 : * Create and initialize internals of 'intl_error'.
59 : */
60 : intl_error* intl_error_create( TSRMLS_D )
61 0 : {
62 0 : intl_error* err = ecalloc( 1, sizeof( intl_error ) );
63 :
64 0 : intl_error_init( err TSRMLS_CC );
65 :
66 0 : return err;
67 : }
68 : /* }}} */
69 :
70 : /* {{{ void intl_error_init( intl_error* coll_error )
71 : * Initialize internals of 'intl_error'.
72 : */
73 : void intl_error_init( intl_error* err TSRMLS_DC )
74 17709 : {
75 17709 : if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
76 0 : return;
77 :
78 17709 : err->code = U_ZERO_ERROR;
79 17709 : err->custom_error_message = NULL;
80 17709 : err->free_custom_error_message = 0;
81 : }
82 : /* }}} */
83 :
84 : /* {{{ void intl_error_reset( intl_error* err )
85 : * Set last error code to 0 and unset last error message
86 : */
87 : void intl_error_reset( intl_error* err TSRMLS_DC )
88 26466 : {
89 26466 : if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
90 0 : return;
91 :
92 26466 : err->code = U_ZERO_ERROR;
93 :
94 26466 : intl_free_custom_error_msg( err TSRMLS_CC );
95 : }
96 : /* }}} */
97 :
98 : /* {{{ void intl_error_set_custom_msg( intl_error* err, char* msg, int copyMsg )
99 : * Set last error message to msg copying it if needed.
100 : */
101 : void intl_error_set_custom_msg( intl_error* err, char* msg, int copyMsg TSRMLS_DC )
102 1010 : {
103 1010 : if( !msg )
104 0 : return;
105 :
106 1010 : if(!err && INTL_G(error_level)) {
107 0 : php_error_docref(NULL TSRMLS_CC, INTL_G(error_level), "%s", msg);
108 : }
109 1010 : if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
110 0 : return;
111 :
112 : /* Free previous message if any */
113 1010 : intl_free_custom_error_msg( err TSRMLS_CC );
114 :
115 : /* Mark message copied if any */
116 1010 : err->free_custom_error_message = copyMsg;
117 :
118 : /* Set user's error text message */
119 1010 : err->custom_error_message = copyMsg ? estrdup( msg ) : msg;
120 : }
121 : /* }}} */
122 :
123 : /* {{{ const char* intl_error_get_message( intl_error* err )
124 : * Create output message in format "<intl_error_text>: <extra_user_error_text>".
125 : */
126 : char* intl_error_get_message( intl_error* err TSRMLS_DC )
127 248 : {
128 248 : const char* uErrorName = NULL;
129 248 : char* errMessage = 0;
130 :
131 248 : if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
132 0 : return estrdup( "" );
133 :
134 248 : uErrorName = u_errorName( err->code );
135 :
136 : /* Format output string */
137 248 : if( err->custom_error_message )
138 : {
139 138 : spprintf( &errMessage, 0, "%s: %s", err->custom_error_message, uErrorName );
140 : }
141 : else
142 : {
143 110 : spprintf( &errMessage, 0, "%s", uErrorName );
144 : }
145 :
146 248 : return errMessage;
147 : }
148 : /* }}} */
149 :
150 : /* {{{ void intl_error_set_code( intl_error* err, UErrorCode err_code )
151 : * Set last error code.
152 : */
153 : void intl_error_set_code( intl_error* err, UErrorCode err_code TSRMLS_DC )
154 4425 : {
155 4425 : if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
156 0 : return;
157 :
158 4425 : err->code = err_code;
159 : }
160 : /* }}} */
161 :
162 : /* {{{ void intl_error_get_code( intl_error* err )
163 : * Return last error code.
164 : */
165 : UErrorCode intl_error_get_code( intl_error* err TSRMLS_DC )
166 609 : {
167 609 : if( !err && !( err = intl_g_error_get( TSRMLS_C ) ) )
168 0 : return U_ZERO_ERROR;
169 :
170 609 : return err->code;
171 : }
172 : /* }}} */
173 :
174 : /* {{{ void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg )
175 : * Set error code and message.
176 : */
177 : void intl_error_set( intl_error* err, UErrorCode code, char* msg, int copyMsg TSRMLS_DC )
178 85 : {
179 85 : intl_error_set_code( err, code TSRMLS_CC );
180 85 : intl_error_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
181 85 : }
182 : /* }}} */
183 :
184 : /* {{{ void intl_errors_reset( intl_error* err )
185 : */
186 : void intl_errors_reset( intl_error* err TSRMLS_DC )
187 0 : {
188 0 : if(err) {
189 0 : intl_error_reset( err TSRMLS_CC );
190 : }
191 0 : intl_error_reset( NULL TSRMLS_CC );
192 0 : }
193 : /* }}} */
194 :
195 : /* {{{ void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg )
196 : */
197 : void intl_errors_set_custom_msg( intl_error* err, char* msg, int copyMsg TSRMLS_DC )
198 462 : {
199 462 : if(err) {
200 462 : intl_error_set_custom_msg( err, msg, copyMsg TSRMLS_CC );
201 : }
202 462 : intl_error_set_custom_msg( NULL, msg, copyMsg TSRMLS_CC );
203 462 : }
204 : /* }}} */
205 :
206 : /* {{{ intl_errors_set_code( intl_error* err, UErrorCode err_code )
207 : */
208 : void intl_errors_set_code( intl_error* err, UErrorCode err_code TSRMLS_DC )
209 0 : {
210 0 : if(err) {
211 0 : intl_error_set_code( err, err_code TSRMLS_CC );
212 : }
213 0 : intl_error_set_code( NULL, err_code TSRMLS_CC );
214 0 : }
215 : /* }}} */
216 :
217 : /*
218 : * Local variables:
219 : * tab-width: 4
220 : * c-basic-offset: 4
221 : * End:
222 : * vim600: noet sw=4 ts=4 fdm=marker
223 : * vim<600: noet sw=4 ts=4
224 : */
|