resourcebundle_class.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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: Hans-Peter Oeri (University of St.Gallen) <hp@oeri.ch> |
  12. +----------------------------------------------------------------------+
  13. */
  14. #include <stdlib.h>
  15. #include <unicode/ures.h>
  16. #include <unicode/uenum.h>
  17. #include <zend.h>
  18. #include <Zend/zend_exceptions.h>
  19. #include <Zend/zend_interfaces.h>
  20. #include <php.h>
  21. #include "php_intl.h"
  22. #include "intl_data.h"
  23. #include "intl_common.h"
  24. #include "resourcebundle/resourcebundle.h"
  25. #include "resourcebundle/resourcebundle_iterator.h"
  26. #include "resourcebundle/resourcebundle_class.h"
  27. #include "resourcebundle/resourcebundle_arginfo.h"
  28. zend_class_entry *ResourceBundle_ce_ptr = NULL;
  29. static zend_object_handlers ResourceBundle_object_handlers;
  30. /* {{{ ResourceBundle_object_free */
  31. static void ResourceBundle_object_free( zend_object *object )
  32. {
  33. ResourceBundle_object *rb = php_intl_resourcebundle_fetch_object(object);
  34. // only free local errors
  35. intl_error_reset( INTL_DATA_ERROR_P(rb) );
  36. if (rb->me) {
  37. ures_close( rb->me );
  38. }
  39. if (rb->child) {
  40. ures_close( rb->child );
  41. }
  42. zend_object_std_dtor( &rb->zend );
  43. }
  44. /* }}} */
  45. /* {{{ ResourceBundle_object_create */
  46. static zend_object *ResourceBundle_object_create( zend_class_entry *ce )
  47. {
  48. ResourceBundle_object *rb;
  49. rb = zend_object_alloc(sizeof(ResourceBundle_object), ce);
  50. zend_object_std_init( &rb->zend, ce );
  51. object_properties_init( &rb->zend, ce);
  52. intl_error_init( INTL_DATA_ERROR_P(rb) );
  53. rb->me = NULL;
  54. rb->child = NULL;
  55. rb->zend.handlers = &ResourceBundle_object_handlers;
  56. return &rb->zend;
  57. }
  58. /* }}} */
  59. /* {{{ ResourceBundle_ctor */
  60. static int resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_error_handling *error_handling, bool *error_handling_replaced)
  61. {
  62. const char *bundlename;
  63. size_t bundlename_len = 0;
  64. const char *locale;
  65. size_t locale_len = 0;
  66. bool fallback = 1;
  67. zval *object = return_value;
  68. ResourceBundle_object *rb = Z_INTL_RESOURCEBUNDLE_P( object );
  69. intl_error_reset( NULL );
  70. if( zend_parse_parameters( ZEND_NUM_ARGS(), "s!s!|b",
  71. &locale, &locale_len, &bundlename, &bundlename_len, &fallback ) == FAILURE )
  72. {
  73. return FAILURE;
  74. }
  75. if (error_handling != NULL) {
  76. zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, error_handling);
  77. *error_handling_replaced = 1;
  78. }
  79. if (rb->me) {
  80. zend_throw_error(NULL, "ResourceBundle object is already constructed");
  81. return FAILURE;
  82. }
  83. INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
  84. if (locale == NULL) {
  85. locale = intl_locale_get_default();
  86. }
  87. if (bundlename_len >= MAXPATHLEN) {
  88. zend_argument_value_error(2, "is too long");
  89. return FAILURE;
  90. }
  91. if (fallback) {
  92. rb->me = ures_open(bundlename, locale, &INTL_DATA_ERROR_CODE(rb));
  93. } else {
  94. rb->me = ures_openDirect(bundlename, locale, &INTL_DATA_ERROR_CODE(rb));
  95. }
  96. INTL_CTOR_CHECK_STATUS(rb, "resourcebundle_ctor: Cannot load libICU resource bundle");
  97. if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING ||
  98. INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) {
  99. char *pbuf;
  100. intl_errors_set_code(NULL, INTL_DATA_ERROR_CODE(rb));
  101. spprintf(&pbuf, 0, "resourcebundle_ctor: Cannot load libICU resource "
  102. "'%s' without fallback from %s to %s",
  103. bundlename ? bundlename : "(default data)", locale,
  104. ures_getLocaleByType(
  105. rb->me, ULOC_ACTUAL_LOCALE, &INTL_DATA_ERROR_CODE(rb)));
  106. intl_errors_set_custom_msg(INTL_DATA_ERROR_P(rb), pbuf, 1);
  107. efree(pbuf);
  108. return FAILURE;
  109. }
  110. return SUCCESS;
  111. }
  112. /* }}} */
  113. /* {{{ ResourceBundle object constructor */
  114. PHP_METHOD( ResourceBundle, __construct )
  115. {
  116. zend_error_handling error_handling;
  117. bool error_handling_replaced = 0;
  118. return_value = ZEND_THIS;
  119. if (resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, &error_handling, &error_handling_replaced) == FAILURE) {
  120. if (!EG(exception)) {
  121. zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
  122. }
  123. }
  124. if (error_handling_replaced) {
  125. zend_restore_error_handling(&error_handling);
  126. }
  127. }
  128. /* }}} */
  129. /* {{{ */
  130. PHP_FUNCTION( resourcebundle_create )
  131. {
  132. object_init_ex( return_value, ResourceBundle_ce_ptr );
  133. if (resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL, NULL) == FAILURE) {
  134. zval_ptr_dtor(return_value);
  135. RETURN_NULL();
  136. }
  137. }
  138. /* }}} */
  139. /* {{{ resourcebundle_array_fetch */
  140. static void resourcebundle_array_fetch(zend_object *object, zval *offset, zval *return_value, int fallback)
  141. {
  142. int32_t meindex = 0;
  143. char * mekey = NULL;
  144. bool is_numeric = 0;
  145. char *pbuf;
  146. ResourceBundle_object *rb;
  147. rb = php_intl_resourcebundle_fetch_object(object);
  148. intl_error_reset(NULL);
  149. intl_error_reset(INTL_DATA_ERROR_P(rb));
  150. if(Z_TYPE_P(offset) == IS_LONG) {
  151. is_numeric = 1;
  152. meindex = (int32_t)Z_LVAL_P(offset);
  153. rb->child = ures_getByIndex( rb->me, meindex, rb->child, &INTL_DATA_ERROR_CODE(rb) );
  154. } else if(Z_TYPE_P(offset) == IS_STRING) {
  155. mekey = Z_STRVAL_P(offset);
  156. rb->child = ures_getByKey(rb->me, mekey, rb->child, &INTL_DATA_ERROR_CODE(rb) );
  157. } else {
  158. intl_errors_set(INTL_DATA_ERROR_P(rb), U_ILLEGAL_ARGUMENT_ERROR,
  159. "resourcebundle_get: index should be integer or string", 0);
  160. RETURN_NULL();
  161. }
  162. intl_error_set_code( NULL, INTL_DATA_ERROR_CODE(rb) );
  163. if (U_FAILURE(INTL_DATA_ERROR_CODE(rb))) {
  164. if (is_numeric) {
  165. spprintf( &pbuf, 0, "Cannot load resource element %d", meindex );
  166. } else {
  167. spprintf( &pbuf, 0, "Cannot load resource element '%s'", mekey );
  168. }
  169. intl_errors_set_custom_msg( INTL_DATA_ERROR_P(rb), pbuf, 1 );
  170. efree(pbuf);
  171. RETURN_NULL();
  172. }
  173. if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING || INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) {
  174. UErrorCode icuerror;
  175. const char * locale = ures_getLocaleByType( rb->me, ULOC_ACTUAL_LOCALE, &icuerror );
  176. if (is_numeric) {
  177. spprintf( &pbuf, 0, "Cannot load element %d without fallback from to %s", meindex, locale );
  178. } else {
  179. spprintf( &pbuf, 0, "Cannot load element '%s' without fallback from to %s", mekey, locale );
  180. }
  181. intl_errors_set_custom_msg( INTL_DATA_ERROR_P(rb), pbuf, 1 );
  182. efree(pbuf);
  183. RETURN_NULL();
  184. }
  185. resourcebundle_extract_value( return_value, rb );
  186. }
  187. /* }}} */
  188. /* {{{ resourcebundle_array_get */
  189. zval *resourcebundle_array_get(zend_object *object, zval *offset, int type, zval *rv)
  190. {
  191. if(offset == NULL) {
  192. php_error( E_ERROR, "Cannot apply [] to ResourceBundle object" );
  193. }
  194. ZVAL_NULL(rv);
  195. resourcebundle_array_fetch(object, offset, rv, 1);
  196. return rv;
  197. }
  198. /* }}} */
  199. /* {{{ Get resource identified by numerical index or key name. */
  200. PHP_FUNCTION( resourcebundle_get )
  201. {
  202. bool fallback = 1;
  203. zval * offset;
  204. zval * object;
  205. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oz|b", &object, ResourceBundle_ce_ptr, &offset, &fallback ) == FAILURE) {
  206. RETURN_THROWS();
  207. }
  208. resourcebundle_array_fetch(Z_OBJ_P(object), offset, return_value, fallback);
  209. }
  210. /* }}} */
  211. /* {{{ resourcebundle_array_count */
  212. int resourcebundle_array_count(zend_object *object, zend_long *count)
  213. {
  214. ResourceBundle_object *rb = php_intl_resourcebundle_fetch_object(object);
  215. if (rb->me == NULL) {
  216. intl_errors_set(&rb->error, U_ILLEGAL_ARGUMENT_ERROR,
  217. "Found unconstructed ResourceBundle", 0);
  218. return 0;
  219. }
  220. *count = ures_getSize( rb->me );
  221. return SUCCESS;
  222. }
  223. /* }}} */
  224. /* {{{ Get resources count */
  225. PHP_FUNCTION( resourcebundle_count )
  226. {
  227. int32_t len;
  228. RESOURCEBUNDLE_METHOD_INIT_VARS;
  229. if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O", &object, ResourceBundle_ce_ptr ) == FAILURE ) {
  230. RETURN_THROWS();
  231. }
  232. RESOURCEBUNDLE_METHOD_FETCH_OBJECT;
  233. len = ures_getSize( rb->me );
  234. RETURN_LONG( len );
  235. }
  236. /* {{{ Get available locales from ResourceBundle name */
  237. PHP_FUNCTION( resourcebundle_locales )
  238. {
  239. char * bundlename;
  240. size_t bundlename_len = 0;
  241. const char * entry;
  242. int entry_len;
  243. UEnumeration *icuenum;
  244. UErrorCode icuerror = U_ZERO_ERROR;
  245. intl_errors_reset( NULL );
  246. if( zend_parse_parameters(ZEND_NUM_ARGS(), "s", &bundlename, &bundlename_len ) == FAILURE )
  247. {
  248. RETURN_THROWS();
  249. }
  250. if (bundlename_len >= MAXPATHLEN) {
  251. zend_argument_value_error(1, "is too long");
  252. RETURN_THROWS();
  253. }
  254. if(bundlename_len == 0) {
  255. // fetch default locales list
  256. bundlename = NULL;
  257. }
  258. icuenum = ures_openAvailableLocales( bundlename, &icuerror );
  259. INTL_CHECK_STATUS(icuerror, "Cannot fetch locales list");
  260. uenum_reset( icuenum, &icuerror );
  261. INTL_CHECK_STATUS(icuerror, "Cannot iterate locales list");
  262. array_init( return_value );
  263. while ((entry = uenum_next( icuenum, &entry_len, &icuerror ))) {
  264. add_next_index_stringl( return_value, (char *) entry, entry_len);
  265. }
  266. uenum_close( icuenum );
  267. }
  268. /* }}} */
  269. /* {{{ Get text description for ResourceBundle's last error code. */
  270. PHP_FUNCTION( resourcebundle_get_error_code )
  271. {
  272. RESOURCEBUNDLE_METHOD_INIT_VARS;
  273. if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
  274. &object, ResourceBundle_ce_ptr ) == FAILURE )
  275. {
  276. RETURN_THROWS();
  277. }
  278. rb = Z_INTL_RESOURCEBUNDLE_P( object );
  279. RETURN_LONG(INTL_DATA_ERROR_CODE(rb));
  280. }
  281. /* }}} */
  282. /* {{{ Get text description for ResourceBundle's last error. */
  283. PHP_FUNCTION( resourcebundle_get_error_message )
  284. {
  285. zend_string* message = NULL;
  286. RESOURCEBUNDLE_METHOD_INIT_VARS;
  287. if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
  288. &object, ResourceBundle_ce_ptr ) == FAILURE )
  289. {
  290. RETURN_THROWS();
  291. }
  292. rb = Z_INTL_RESOURCEBUNDLE_P( object );
  293. message = intl_error_get_message(INTL_DATA_ERROR_P(rb));
  294. RETURN_STR(message);
  295. }
  296. /* }}} */
  297. PHP_METHOD(ResourceBundle, getIterator) {
  298. if (zend_parse_parameters_none() == FAILURE) {
  299. return;
  300. }
  301. zend_create_internal_iterator_zval(return_value, ZEND_THIS);
  302. }
  303. /* {{{ resourcebundle_register_class
  304. * Initialize 'ResourceBundle' class
  305. */
  306. void resourcebundle_register_class( void )
  307. {
  308. ResourceBundle_ce_ptr = register_class_ResourceBundle(zend_ce_aggregate, zend_ce_countable);
  309. ResourceBundle_ce_ptr->create_object = ResourceBundle_object_create;
  310. ResourceBundle_ce_ptr->get_iterator = resourcebundle_get_iterator;
  311. ResourceBundle_object_handlers = std_object_handlers;
  312. ResourceBundle_object_handlers.offset = XtOffsetOf(ResourceBundle_object, zend);
  313. ResourceBundle_object_handlers.clone_obj = NULL; /* ICU ResourceBundle has no clone implementation */
  314. ResourceBundle_object_handlers.free_obj = ResourceBundle_object_free;
  315. ResourceBundle_object_handlers.read_dimension = resourcebundle_array_get;
  316. ResourceBundle_object_handlers.count_elements = resourcebundle_array_count;
  317. }
  318. /* }}} */