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