intl_data.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. +----------------------------------------------------------------------+
  3. | This source file is subject to version 3.01 of the PHP license, |
  4. | that is bundled with this package in the file LICENSE, and is |
  5. | available through the world-wide-web at the following url: |
  6. | https://www.php.net/license/3_01.txt |
  7. | If you did not receive a copy of the PHP license and are unable to |
  8. | obtain it through the world-wide-web, please send a note to |
  9. | license@php.net so we can mail you a copy immediately. |
  10. +----------------------------------------------------------------------+
  11. | Authors: Vadim Savchuk <vsavchuk@productengine.com> |
  12. | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> |
  13. | Stanislav Malyshev <stas@zend.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifndef INTL_DATA_H
  17. #define INTL_DATA_H
  18. #include <unicode/utypes.h>
  19. #include "intl_error.h"
  20. /* Mock object to generalize error handling in sub-modules.
  21. Sub-module data structures should always have error as first element
  22. for this to work!
  23. */
  24. typedef struct _intl_data {
  25. intl_error error;
  26. zend_object zo;
  27. } intl_object;
  28. #define INTL_METHOD_INIT_VARS(oclass, obj) \
  29. zval* object = NULL; \
  30. oclass##_object* obj = NULL; \
  31. intl_error_reset( NULL );
  32. #define INTL_DATA_ERROR(obj) (((intl_object *)(obj))->error)
  33. #define INTL_DATA_ERROR_P(obj) (&(INTL_DATA_ERROR((obj))))
  34. #define INTL_DATA_ERROR_CODE(obj) INTL_ERROR_CODE(INTL_DATA_ERROR((obj)))
  35. #define INTL_METHOD_FETCH_OBJECT(oclass, obj) \
  36. obj = Z_##oclass##_P( object ); \
  37. intl_error_reset( INTL_DATA_ERROR_P(obj) ); \
  38. /* Check status by error code, if error return false */
  39. #define INTL_CHECK_STATUS(err, msg) \
  40. intl_error_set_code( NULL, (err) ); \
  41. if( U_FAILURE((err)) ) \
  42. { \
  43. intl_error_set_custom_msg( NULL, msg, 0 ); \
  44. RETURN_FALSE; \
  45. }
  46. /* Check status by error code, if error return null */
  47. #define INTL_CHECK_STATUS_OR_NULL(err, msg) \
  48. intl_error_set_code( NULL, (err) ); \
  49. if( U_FAILURE((err)) ) \
  50. { \
  51. intl_error_set_custom_msg( NULL, msg, 0 ); \
  52. RETURN_NULL(); \
  53. }
  54. /* Check status in object, if error return false */
  55. #define INTL_METHOD_CHECK_STATUS(obj, msg) \
  56. intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((obj)) ); \
  57. if( U_FAILURE( INTL_DATA_ERROR_CODE((obj)) ) ) \
  58. { \
  59. intl_errors_set_custom_msg( INTL_DATA_ERROR_P((obj)), msg, 0 ); \
  60. RETURN_FALSE; \
  61. }
  62. /* Check status in object, if error goto a label */
  63. #define INTL_METHOD_CHECK_STATUS_OR_GOTO(obj, msg, label) \
  64. intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((obj)) ); \
  65. if( U_FAILURE( INTL_DATA_ERROR_CODE((obj)) ) ) \
  66. { \
  67. intl_errors_set_custom_msg( INTL_DATA_ERROR_P((obj)), msg, 0 ); \
  68. RETVAL_FALSE; \
  69. goto label; \
  70. }
  71. /* Check status in object, if error return null */
  72. #define INTL_METHOD_CHECK_STATUS_OR_NULL(obj, msg) \
  73. intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((obj)) ); \
  74. if( U_FAILURE( INTL_DATA_ERROR_CODE((obj)) ) ) \
  75. { \
  76. intl_errors_set_custom_msg( INTL_DATA_ERROR_P((obj)), msg, 0 ); \
  77. zval_ptr_dtor(return_value); \
  78. RETURN_NULL(); \
  79. }
  80. /* Check status in object, if error return FAILURE */
  81. #define INTL_CTOR_CHECK_STATUS(obj, msg) \
  82. intl_error_set_code( NULL, INTL_DATA_ERROR_CODE((obj)) ); \
  83. if( U_FAILURE( INTL_DATA_ERROR_CODE((obj)) ) ) \
  84. { \
  85. intl_errors_set_custom_msg( INTL_DATA_ERROR_P((obj)), msg, 0 ); \
  86. return FAILURE; \
  87. }
  88. #define INTL_METHOD_RETVAL_UTF8(obj, ustring, ulen, free_it) \
  89. { \
  90. zend_string *u8str; \
  91. u8str = intl_convert_utf16_to_utf8(ustring, ulen, &INTL_DATA_ERROR_CODE((obj))); \
  92. if((free_it)) { \
  93. efree(ustring); \
  94. } \
  95. INTL_METHOD_CHECK_STATUS((obj), "Error converting value to UTF-8"); \
  96. RETVAL_NEW_STR(u8str); \
  97. }
  98. #define INTL_MAX_LOCALE_LEN (ULOC_FULLNAME_CAPACITY-1)
  99. #define INTL_CHECK_LOCALE_LEN(locale_len) \
  100. if((locale_len) > INTL_MAX_LOCALE_LEN) { \
  101. char *_msg; \
  102. spprintf(&_msg, 0, "Locale string too long, should be no longer than %d characters", INTL_MAX_LOCALE_LEN); \
  103. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, _msg, 1); \
  104. efree(_msg); \
  105. RETURN_NULL(); \
  106. }
  107. #define INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len) \
  108. if((locale_len) > INTL_MAX_LOCALE_LEN) { \
  109. char *_msg; \
  110. spprintf(&_msg, 0, "Locale string too long, should be no longer than %d characters", INTL_MAX_LOCALE_LEN); \
  111. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, _msg, 1); \
  112. efree(_msg); \
  113. return FAILURE; \
  114. }
  115. #endif // INTL_DATA_H