resourcebundle_class.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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: Hans-Peter Oeri (University of St.Gallen) <hp@oeri.ch> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include <stdlib.h>
  17. #include <unicode/ures.h>
  18. #include <unicode/uenum.h>
  19. #include <zend.h>
  20. #include <Zend/zend_exceptions.h>
  21. #include <Zend/zend_interfaces.h>
  22. #include <php.h>
  23. #include "php_intl.h"
  24. #include "intl_data.h"
  25. #include "resourcebundle/resourcebundle.h"
  26. #include "resourcebundle/resourcebundle_iterator.h"
  27. #include "resourcebundle/resourcebundle_class.h"
  28. zend_class_entry *ResourceBundle_ce_ptr = NULL;
  29. static zend_object_handlers ResourceBundle_object_handlers;
  30. /* {{{ ResourceBundle_object_dtor */
  31. static void ResourceBundle_object_destroy( void *object, zend_object_handle handle TSRMLS_DC )
  32. {
  33. ResourceBundle_object *rb = (ResourceBundle_object *) object;
  34. // only free local errors
  35. intl_error_reset( INTL_DATA_ERROR_P(rb) TSRMLS_CC );
  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( object TSRMLS_CC );
  43. efree(object);
  44. }
  45. /* }}} */
  46. /* {{{ ResourceBundle_object_create */
  47. static zend_object_value ResourceBundle_object_create( zend_class_entry *ce TSRMLS_DC )
  48. {
  49. zend_object_value retval;
  50. ResourceBundle_object *rb;
  51. rb = ecalloc( 1, sizeof(ResourceBundle_object) );
  52. zend_object_std_init( (zend_object *) rb, ce TSRMLS_CC );
  53. object_properties_init((zend_object *) rb, ce);
  54. intl_error_init( INTL_DATA_ERROR_P(rb) TSRMLS_CC );
  55. rb->me = NULL;
  56. rb->child = NULL;
  57. retval.handlers = &ResourceBundle_object_handlers;
  58. retval.handle = zend_objects_store_put( rb, ResourceBundle_object_destroy, NULL, NULL TSRMLS_CC );
  59. return retval;
  60. }
  61. /* }}} */
  62. /* {{{ ResourceBundle_ctor */
  63. static void resourcebundle_ctor(INTERNAL_FUNCTION_PARAMETERS)
  64. {
  65. const char *bundlename;
  66. int bundlename_len = 0;
  67. const char *locale;
  68. int locale_len = 0;
  69. zend_bool fallback = 1;
  70. zval *object = return_value;
  71. ResourceBundle_object *rb = (ResourceBundle_object *) zend_object_store_get_object( object TSRMLS_CC);
  72. intl_error_reset( NULL TSRMLS_CC );
  73. if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s!s!|b",
  74. &locale, &locale_len, &bundlename, &bundlename_len, &fallback ) == FAILURE )
  75. {
  76. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  77. "resourcebundle_ctor: unable to parse input parameters", 0 TSRMLS_CC );
  78. zval_dtor( return_value );
  79. RETURN_NULL();
  80. }
  81. INTL_CHECK_LOCALE_LEN_OBJ(locale_len, return_value);
  82. if (locale == NULL) {
  83. locale = intl_locale_get_default(TSRMLS_C);
  84. }
  85. if (bundlename_len >= MAXPATHLEN) {
  86. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "Bundle name too long", 0 TSRMLS_CC );
  87. zval_dtor(return_value);
  88. ZVAL_NULL(return_value);
  89. RETURN_NULL();
  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) TSRMLS_CC);
  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 TSRMLS_CC);
  107. efree(pbuf);
  108. zval_dtor(return_value);
  109. RETURN_NULL();
  110. }
  111. }
  112. /* }}} */
  113. /* {{{ arginfo_resourcebundle__construct */
  114. ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle___construct, 0, 0, 2 )
  115. ZEND_ARG_INFO( 0, locale )
  116. ZEND_ARG_INFO( 0, bundlename )
  117. ZEND_ARG_INFO( 0, fallback )
  118. ZEND_END_ARG_INFO()
  119. /* }}} */
  120. /* {{{ proto void ResourceBundle::__construct( string $locale [, string $bundlename [, bool $fallback = true ]] )
  121. * ResourceBundle object constructor
  122. */
  123. PHP_METHOD( ResourceBundle, __construct )
  124. {
  125. return_value = getThis();
  126. resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  127. }
  128. /* }}} */
  129. /* {{{ proto ResourceBundle ResourceBundle::create( string $locale [, string $bundlename [, bool $fallback = true ]] )
  130. proto ResourceBundle resourcebundle_create( string $locale [, string $bundlename [, bool $fallback = true ]] )
  131. */
  132. PHP_FUNCTION( resourcebundle_create )
  133. {
  134. object_init_ex( return_value, ResourceBundle_ce_ptr );
  135. resourcebundle_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  136. }
  137. /* }}} */
  138. /* {{{ resourcebundle_array_fetch */
  139. static void resourcebundle_array_fetch(zval *object, zval *offset, zval *return_value, int fallback TSRMLS_DC)
  140. {
  141. int32_t meindex = 0;
  142. char * mekey = NULL;
  143. zend_bool is_numeric = 0;
  144. char *pbuf;
  145. ResourceBundle_object *rb;
  146. intl_error_reset( NULL TSRMLS_CC );
  147. RESOURCEBUNDLE_METHOD_FETCH_OBJECT;
  148. if(Z_TYPE_P(offset) == IS_LONG) {
  149. is_numeric = 1;
  150. meindex = Z_LVAL_P(offset);
  151. rb->child = ures_getByIndex( rb->me, meindex, rb->child, &INTL_DATA_ERROR_CODE(rb) );
  152. } else if(Z_TYPE_P(offset) == IS_STRING) {
  153. mekey = Z_STRVAL_P(offset);
  154. rb->child = ures_getByKey(rb->me, mekey, rb->child, &INTL_DATA_ERROR_CODE(rb) );
  155. } else {
  156. intl_errors_set(INTL_DATA_ERROR_P(rb), U_ILLEGAL_ARGUMENT_ERROR,
  157. "resourcebundle_get: index should be integer or string", 0 TSRMLS_CC);
  158. RETURN_NULL();
  159. }
  160. intl_error_set_code( NULL, INTL_DATA_ERROR_CODE(rb) TSRMLS_CC );
  161. if (U_FAILURE(INTL_DATA_ERROR_CODE(rb))) {
  162. if (is_numeric) {
  163. spprintf( &pbuf, 0, "Cannot load resource element %d", meindex );
  164. } else {
  165. spprintf( &pbuf, 0, "Cannot load resource element '%s'", mekey );
  166. }
  167. intl_errors_set_custom_msg( INTL_DATA_ERROR_P(rb), pbuf, 1 TSRMLS_CC );
  168. efree(pbuf);
  169. RETURN_NULL();
  170. }
  171. if (!fallback && (INTL_DATA_ERROR_CODE(rb) == U_USING_FALLBACK_WARNING || INTL_DATA_ERROR_CODE(rb) == U_USING_DEFAULT_WARNING)) {
  172. UErrorCode icuerror;
  173. const char * locale = ures_getLocaleByType( rb->me, ULOC_ACTUAL_LOCALE, &icuerror );
  174. if (is_numeric) {
  175. spprintf( &pbuf, 0, "Cannot load element %d without fallback from to %s", meindex, locale );
  176. } else {
  177. spprintf( &pbuf, 0, "Cannot load element '%s' without fallback from to %s", mekey, locale );
  178. }
  179. intl_errors_set_custom_msg( INTL_DATA_ERROR_P(rb), pbuf, 1 TSRMLS_CC );
  180. efree(pbuf);
  181. RETURN_NULL();
  182. }
  183. resourcebundle_extract_value( return_value, rb TSRMLS_CC );
  184. }
  185. /* }}} */
  186. /* {{{ resourcebundle_array_get */
  187. zval *resourcebundle_array_get(zval *object, zval *offset, int type TSRMLS_DC)
  188. {
  189. zval *retval;
  190. if(offset == NULL) {
  191. php_error( E_ERROR, "Cannot apply [] to ResourceBundle object" );
  192. }
  193. MAKE_STD_ZVAL(retval);
  194. resourcebundle_array_fetch(object, offset, retval, 1 TSRMLS_CC);
  195. Z_DELREF_P(retval);
  196. return retval;
  197. }
  198. /* }}} */
  199. /* {{{ arginfo_resourcebundle_get */
  200. ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get, 0, 0, 1 )
  201. ZEND_ARG_INFO( 0, index )
  202. ZEND_ARG_INFO( 0, fallback )
  203. ZEND_END_ARG_INFO()
  204. /* }}} */
  205. /* {{{ proto mixed ResourceBundle::get( integer|string $resindex [, bool $fallback = true ] )
  206. * proto mixed resourcebundle_get( ResourceBundle $rb, integer|string $resindex [, bool $fallback = true ] )
  207. * Get resource identified by numerical index or key name.
  208. */
  209. PHP_FUNCTION( resourcebundle_get )
  210. {
  211. zend_bool fallback = 1;
  212. zval * offset;
  213. zval * object;
  214. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz|b", &object, ResourceBundle_ce_ptr, &offset, &fallback ) == FAILURE) {
  215. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  216. "resourcebundle_get: unable to parse input params", 0 TSRMLS_CC);
  217. RETURN_FALSE;
  218. }
  219. resourcebundle_array_fetch(object, offset, return_value, fallback TSRMLS_CC);
  220. }
  221. /* }}} */
  222. /* {{{ resourcebundle_array_count */
  223. int resourcebundle_array_count(zval *object, long *count TSRMLS_DC)
  224. {
  225. ResourceBundle_object *rb;
  226. RESOURCEBUNDLE_METHOD_FETCH_OBJECT_NO_CHECK;
  227. if (rb->me == NULL) {
  228. intl_errors_set(&rb->error, U_ILLEGAL_ARGUMENT_ERROR,
  229. "Found unconstructed ResourceBundle", 0 TSRMLS_CC);
  230. return 0;
  231. }
  232. *count = ures_getSize( rb->me );
  233. return SUCCESS;
  234. }
  235. /* }}} */
  236. /* {{{ arginfo_resourcebundle_count */
  237. ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_count, 0, 0, 0 )
  238. ZEND_END_ARG_INFO()
  239. /* }}} */
  240. /* {{{ proto int ResourceBundle::count()
  241. * proto int resourcebundle_count( ResourceBundle $bundle )
  242. * Get resources count
  243. */
  244. PHP_FUNCTION( resourcebundle_count )
  245. {
  246. int32_t len;
  247. RESOURCEBUNDLE_METHOD_INIT_VARS;
  248. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, ResourceBundle_ce_ptr ) == FAILURE ) {
  249. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  250. "resourcebundle_count: unable to parse input params", 0 TSRMLS_CC);
  251. RETURN_FALSE;
  252. }
  253. RESOURCEBUNDLE_METHOD_FETCH_OBJECT;
  254. len = ures_getSize( rb->me );
  255. RETURN_LONG( len );
  256. }
  257. /* {{{ arginfo_resourcebundle_getlocales */
  258. ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_getlocales, 0, 0, 1 )
  259. ZEND_ARG_INFO( 0, bundlename )
  260. ZEND_END_ARG_INFO()
  261. /* }}} */
  262. /* {{{ proto array ResourceBundle::getLocales( string $bundlename )
  263. * proto array resourcebundle_locales( string $bundlename )
  264. * Get available locales from ResourceBundle name
  265. */
  266. PHP_FUNCTION( resourcebundle_locales )
  267. {
  268. char * bundlename;
  269. int bundlename_len = 0;
  270. const char * entry;
  271. int entry_len;
  272. UEnumeration *icuenum;
  273. UErrorCode icuerror = U_ZERO_ERROR;
  274. intl_errors_reset( NULL TSRMLS_CC );
  275. if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &bundlename, &bundlename_len ) == FAILURE )
  276. {
  277. intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
  278. "resourcebundle_locales: unable to parse input params", 0 TSRMLS_CC);
  279. RETURN_FALSE;
  280. }
  281. if (bundlename_len >= MAXPATHLEN) {
  282. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "resourcebundle_locales: bundle name too long", 0 TSRMLS_CC );
  283. RETURN_FALSE;
  284. }
  285. if(bundlename_len == 0) {
  286. // fetch default locales list
  287. bundlename = NULL;
  288. }
  289. icuenum = ures_openAvailableLocales( bundlename, &icuerror );
  290. INTL_CHECK_STATUS(icuerror, "Cannot fetch locales list");
  291. uenum_reset( icuenum, &icuerror );
  292. INTL_CHECK_STATUS(icuerror, "Cannot iterate locales list");
  293. array_init( return_value );
  294. while ((entry = uenum_next( icuenum, &entry_len, &icuerror ))) {
  295. add_next_index_stringl( return_value, (char *) entry, entry_len, 1 );
  296. }
  297. uenum_close( icuenum );
  298. }
  299. /* }}} */
  300. /* {{{ arginfo_resourcebundle_get_error_code */
  301. ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get_error_code, 0, 0, 0 )
  302. ZEND_END_ARG_INFO()
  303. /* }}} */
  304. /* {{{ proto string ResourceBundle::getErrorCode( )
  305. * proto string resourcebundle_get_error_code( ResourceBundle $bundle )
  306. * Get text description for ResourceBundle's last error code.
  307. */
  308. PHP_FUNCTION( resourcebundle_get_error_code )
  309. {
  310. RESOURCEBUNDLE_METHOD_INIT_VARS;
  311. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
  312. &object, ResourceBundle_ce_ptr ) == FAILURE )
  313. {
  314. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  315. "resourcebundle_get_error_code: unable to parse input params", 0 TSRMLS_CC );
  316. RETURN_FALSE;
  317. }
  318. rb = (ResourceBundle_object *) zend_object_store_get_object( object TSRMLS_CC );
  319. RETURN_LONG(INTL_DATA_ERROR_CODE(rb));
  320. }
  321. /* }}} */
  322. /* {{{ arginfo_resourcebundle_get_error_message */
  323. ZEND_BEGIN_ARG_INFO_EX( arginfo_resourcebundle_get_error_message, 0, 0, 0 )
  324. ZEND_END_ARG_INFO()
  325. /* }}} */
  326. /* {{{ proto string ResourceBundle::getErrorMessage( )
  327. * proto string resourcebundle_get_error_message( ResourceBundle $bundle )
  328. * Get text description for ResourceBundle's last error.
  329. */
  330. PHP_FUNCTION( resourcebundle_get_error_message )
  331. {
  332. char* message = NULL;
  333. RESOURCEBUNDLE_METHOD_INIT_VARS;
  334. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
  335. &object, ResourceBundle_ce_ptr ) == FAILURE )
  336. {
  337. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  338. "resourcebundle_get_error_message: unable to parse input params", 0 TSRMLS_CC );
  339. RETURN_FALSE;
  340. }
  341. rb = (ResourceBundle_object *) zend_object_store_get_object( object TSRMLS_CC );
  342. message = (char *)intl_error_get_message(INTL_DATA_ERROR_P(rb) TSRMLS_CC);
  343. RETURN_STRING(message, 0);
  344. }
  345. /* }}} */
  346. /* {{{ ResourceBundle_class_functions
  347. * Every 'ResourceBundle' class method has an entry in this table
  348. */
  349. static zend_function_entry ResourceBundle_class_functions[] = {
  350. PHP_ME( ResourceBundle, __construct, arginfo_resourcebundle___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
  351. ZEND_NAMED_ME( create, ZEND_FN( resourcebundle_create ), arginfo_resourcebundle___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
  352. ZEND_NAMED_ME( get, ZEND_FN(resourcebundle_get), arginfo_resourcebundle_get, ZEND_ACC_PUBLIC )
  353. ZEND_NAMED_ME( count, ZEND_FN(resourcebundle_count), arginfo_resourcebundle_count, ZEND_ACC_PUBLIC )
  354. ZEND_NAMED_ME( getLocales, ZEND_FN(resourcebundle_locales), arginfo_resourcebundle_getlocales, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC )
  355. ZEND_NAMED_ME( getErrorCode, ZEND_FN(resourcebundle_get_error_code), arginfo_resourcebundle_get_error_code, ZEND_ACC_PUBLIC )
  356. ZEND_NAMED_ME( getErrorMessage, ZEND_FN(resourcebundle_get_error_message), arginfo_resourcebundle_get_error_message, ZEND_ACC_PUBLIC )
  357. PHP_FE_END
  358. };
  359. /* }}} */
  360. /* {{{ resourcebundle_register_class
  361. * Initialize 'ResourceBundle' class
  362. */
  363. void resourcebundle_register_class( TSRMLS_D )
  364. {
  365. zend_class_entry ce;
  366. INIT_CLASS_ENTRY( ce, "ResourceBundle", ResourceBundle_class_functions );
  367. ce.create_object = ResourceBundle_object_create;
  368. ce.get_iterator = resourcebundle_get_iterator;
  369. ResourceBundle_ce_ptr = zend_register_internal_class( &ce TSRMLS_CC );
  370. if( !ResourceBundle_ce_ptr )
  371. {
  372. zend_error(E_ERROR, "Failed to register ResourceBundle class");
  373. return;
  374. }
  375. ResourceBundle_object_handlers = std_object_handlers;
  376. ResourceBundle_object_handlers.clone_obj = NULL; /* ICU ResourceBundle has no clone implementation */
  377. ResourceBundle_object_handlers.read_dimension = resourcebundle_array_get;
  378. ResourceBundle_object_handlers.count_elements = resourcebundle_array_count;
  379. zend_class_implements(ResourceBundle_ce_ptr TSRMLS_CC, 1, zend_ce_traversable);
  380. }
  381. /* }}} */
  382. /*
  383. * Local variables:
  384. * tab-width: 4
  385. * c-basic-offset: 4
  386. * End:
  387. * vim600: noet sw=4 ts=4 fdm=marker
  388. * vim<600: noet sw=4 ts=4
  389. */