1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
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 #ifndef INTL_DATA_H
20 #define INTL_DATA_H
21
22 #include <unicode/utypes.h>
23
24 #include "intl_error.h"
25
26 /* Mock object to generalize error handling in sub-modules.
27 Sub-module data structures should always have error as first element
28 for this to work!
29 */
30 typedef struct _intl_data {
31 intl_error error;
32 zend_object zo;
33 } intl_object;
34
35 #define INTL_METHOD_INIT_VARS(oclass, obj) \
36 zval* object = NULL; \
37 oclass##_object* obj = NULL; \
38 intl_error_reset( NULL );
39
40 #define INTL_DATA_ERROR(obj) (((intl_object *)(obj))->error)
41 #define INTL_DATA_ERROR_P(obj) (&(INTL_DATA_ERROR((obj))))
42 #define INTL_DATA_ERROR_CODE(obj) INTL_ERROR_CODE(INTL_DATA_ERROR((obj)))
43
44 #define INTL_METHOD_FETCH_OBJECT(oclass, obj) \
45 obj = Z_##oclass##_P( object ); \
46 intl_error_reset( INTL_DATA_ERROR_P(obj) ); \
47
48 /* Check status by error code, if error - exit */
49 #define INTL_CHECK_STATUS(err, msg) \
50 intl_error_set_code( NULL, (err) ); \
51 if( U_FAILURE((err)) ) \
52 { \
53 intl_error_set_custom_msg( NULL, msg, 0 ); \
54 RETURN_FALSE; \
55 }
56
57 /* Check status in object, if error return false */
58 #define INTL_METHOD_CHECK_STATUS(obj, msg) \
59 intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((obj)) ); \
60 if( U_FAILURE( INTL_DATA_ERROR_CODE((obj)) ) ) \
61 { \
62 intl_errors_set_custom_msg( INTL_DATA_ERROR_P((obj)), msg, 0 ); \
63 RETURN_FALSE; \
64 }
65
66 /* Check status in object, if error return null */
67 #define INTL_METHOD_CHECK_STATUS_OR_NULL(obj, msg) \
68 intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((obj)) ); \
69 if( U_FAILURE( INTL_DATA_ERROR_CODE((obj)) ) ) \
70 { \
71 intl_errors_set_custom_msg( INTL_DATA_ERROR_P((obj)), msg, 0 ); \
72 zval_ptr_dtor(return_value); \
73 RETURN_NULL(); \
74 }
75
76 /* Check status in object, if error return FAILURE */
77 #define INTL_CTOR_CHECK_STATUS(obj, msg) \
78 intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((obj)) ); \
79 if( U_FAILURE( INTL_DATA_ERROR_CODE((obj)) ) ) \
80 { \
81 intl_errors_set_custom_msg( INTL_DATA_ERROR_P((obj)), msg, 0 ); \
82 return FAILURE; \
83 }
84
85 #define INTL_METHOD_RETVAL_UTF8(obj, ustring, ulen, free_it) \
86 { \
87 zend_string *u8str; \
88 u8str = intl_convert_utf16_to_utf8(ustring, ulen, &INTL_DATA_ERROR_CODE((obj))); \
89 if((free_it)) { \
90 efree(ustring); \
91 } \
92 INTL_METHOD_CHECK_STATUS((obj), "Error converting value to UTF-8"); \
93 RETVAL_NEW_STR(u8str); \
94 }
95
96 #define INTL_MAX_LOCALE_LEN 80
97
98 #define INTL_CHECK_LOCALE_LEN(locale_len) \
99 if((locale_len) > INTL_MAX_LOCALE_LEN) { \
100 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, \
101 "Locale string too long, should be no longer than 80 characters", 0 ); \
102 RETURN_NULL(); \
103 }
104
105 #define INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len) \
106 if((locale_len) > INTL_MAX_LOCALE_LEN) { \
107 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, \
108 "Locale string too long, should be no longer than 80 characters", 0 ); \
109 return FAILURE; \
110 }
111
112 #endif // INTL_DATA_H