diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2013-03-14 05:42:27 +0000 |
---|---|---|
committer | <> | 2013-04-03 16:25:08 +0000 |
commit | c4dd7a1a684490673e25aaf4fabec5df138854c4 (patch) | |
tree | 4d57c44caae4480efff02b90b9be86f44bf25409 /ext/intl/msgformat | |
download | php2-master.tar.gz |
Imported from /home/lorry/working-area/delta_php2/php-5.4.13.tar.bz2.HEADphp-5.4.13master
Diffstat (limited to 'ext/intl/msgformat')
-rw-r--r-- | ext/intl/msgformat/msgformat.c | 176 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat.h | 27 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat_attr.c | 145 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat_attr.h | 26 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat_class.c | 192 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat_class.h | 55 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat_data.c | 102 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat_data.h | 44 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat_format.c | 186 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat_format.h | 25 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat_helpers.cpp | 212 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat_helpers.h | 25 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat_parse.c | 161 | ||||
-rw-r--r-- | ext/intl/msgformat/msgformat_parse.h | 25 |
14 files changed, 1401 insertions, 0 deletions
diff --git a/ext/intl/msgformat/msgformat.c b/ext/intl/msgformat/msgformat.c new file mode 100644 index 0000000..e3fb942 --- /dev/null +++ b/ext/intl/msgformat/msgformat.c @@ -0,0 +1,176 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <unicode/ustring.h> +#include <unicode/umsg.h> + +#include "php_intl.h" +#include "msgformat_class.h" +#include "intl_convert.h" + +/* {{{ */ +static void msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS) +{ + char* locale; + char* pattern; + int locale_len = 0, pattern_len = 0; + UChar* spattern = NULL; + int spattern_len = 0; + zval* object; + MessageFormatter_object* mfo; + intl_error_reset( NULL TSRMLS_CC ); + + object = return_value; + /* Parse parameters. */ + if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "ss", + &locale, &locale_len, &pattern, &pattern_len ) == FAILURE ) + { + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_create: unable to parse input parameters", 0 TSRMLS_CC ); + zval_dtor(return_value); + RETURN_NULL(); + } + + INTL_CHECK_LOCALE_LEN_OBJ(locale_len, return_value); + MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK; + + /* Convert pattern (if specified) to UTF-16. */ + if(pattern && pattern_len) { + intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo)); + INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16"); + } else { + spattern_len = 0; + spattern = NULL; + } + + if(locale_len == 0) { + locale = INTL_G(default_locale); + } + +#ifdef MSG_FORMAT_QUOTE_APOS + if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) { + INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format"); + } +#endif + + if ((mfo)->mf_data.orig_format) { + msgformat_data_free(&mfo->mf_data TSRMLS_CC); + } + + (mfo)->mf_data.orig_format = estrndup(pattern, pattern_len); + (mfo)->mf_data.orig_format_len = pattern_len; + + /* Create an ICU message formatter. */ + MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(mfo)); + + if(spattern) { + efree(spattern); + } + + INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed"); +} +/* }}} */ + +/* {{{ proto MessageFormatter MesssageFormatter::create( string $locale, string $pattern ) + * Create formatter. }}} */ +/* {{{ proto MessageFormatter msgfmt_create( string $locale, string $pattern ) + * Create formatter. + */ +PHP_FUNCTION( msgfmt_create ) +{ + object_init_ex( return_value, MessageFormatter_ce_ptr ); + msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); +} +/* }}} */ + +/* {{{ proto void MessageFormatter::__construct( string $locale, string $pattern ) + * MessageFormatter object constructor. + */ +PHP_METHOD( MessageFormatter, __construct ) +{ + return_value = getThis(); + msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU); +} +/* }}} */ + +/* {{{ proto int MessageFormatter::getErrorCode() + * Get formatter's last error code. }}} */ +/* {{{ proto int msgfmt_get_error_code( MessageFormatter $nf ) + * Get formatter's last error code. + */ +PHP_FUNCTION( msgfmt_get_error_code ) +{ + zval* object = NULL; + MessageFormatter_object* mfo = NULL; + + /* Parse parameters. */ + if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", + &object, MessageFormatter_ce_ptr ) == FAILURE ) + { + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_get_error_code: unable to parse input params", 0 TSRMLS_CC ); + + RETURN_FALSE; + } + + mfo = (MessageFormatter_object *) zend_object_store_get_object( object TSRMLS_CC ); + + /* Return formatter's last error code. */ + RETURN_LONG( INTL_DATA_ERROR_CODE(mfo) ); +} +/* }}} */ + +/* {{{ proto string MessageFormatter::getErrorMessage( ) + * Get text description for formatter's last error code. }}} */ +/* {{{ proto string msgfmt_get_error_message( MessageFormatter $coll ) + * Get text description for formatter's last error code. + */ +PHP_FUNCTION( msgfmt_get_error_message ) +{ + char* message = NULL; + zval* object = NULL; + MessageFormatter_object* mfo = NULL; + + /* Parse parameters. */ + if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", + &object, MessageFormatter_ce_ptr ) == FAILURE ) + { + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_get_error_message: unable to parse input params", 0 TSRMLS_CC ); + + RETURN_FALSE; + } + + mfo = (MessageFormatter_object *) zend_object_store_get_object( object TSRMLS_CC ); + + /* Return last error message. */ + message = intl_error_get_message( &mfo->mf_data.error TSRMLS_CC ); + RETURN_STRING( message, 0); +} +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/ext/intl/msgformat/msgformat.h b/ext/intl/msgformat/msgformat.h new file mode 100644 index 0000000..205c706 --- /dev/null +++ b/ext/intl/msgformat/msgformat.h @@ -0,0 +1,27 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifndef MSG_FORMATTER_H +#define MSG_FORMATTER_H + +#include <php.h> + +PHP_FUNCTION( msgfmt_create ); +PHP_FUNCTION( msgfmt_get_error_code ); +PHP_FUNCTION( msgfmt_get_error_message ); +PHP_METHOD( MessageFormatter, __construct ); + +#endif // MSG_FORMAT_H diff --git a/ext/intl/msgformat/msgformat_attr.c b/ext/intl/msgformat/msgformat_attr.c new file mode 100644 index 0000000..ed2dae2 --- /dev/null +++ b/ext/intl/msgformat/msgformat_attr.c @@ -0,0 +1,145 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php_intl.h" +#include "msgformat_class.h" +#include "msgformat_attr.h" +#include "intl_convert.h" + +#include <unicode/ustring.h> + + +/* {{{ proto string MessageFormatter::getPattern( ) + * Get formatter pattern. }}} */ +/* {{{ proto string msgfmt_get_pattern( MessageFormatter $mf ) + * Get formatter pattern. + */ +PHP_FUNCTION( msgfmt_get_pattern ) +{ + MSG_FORMAT_METHOD_INIT_VARS; + + /* Parse parameters. */ + if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &object, MessageFormatter_ce_ptr ) == FAILURE ) + { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_get_pattern: unable to parse input params", 0 TSRMLS_CC ); + RETURN_FALSE; + } + + /* Fetch the object. */ + MSG_FORMAT_METHOD_FETCH_OBJECT; + + if(mfo->mf_data.orig_format) { + RETURN_STRINGL(mfo->mf_data.orig_format, mfo->mf_data.orig_format_len, 1); + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool MessageFormatter::setPattern( string $pattern ) + * Set formatter pattern. }}} */ +/* {{{ proto bool msgfmt_set_pattern( MessageFormatter $mf, string $pattern ) + * Set formatter pattern. + */ +PHP_FUNCTION( msgfmt_set_pattern ) +{ + char* value = NULL; + int value_len = 0; + int spattern_len = 0; + UChar* spattern = NULL; + MSG_FORMAT_METHOD_INIT_VARS; + + /* Parse parameters. */ + if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", + &object, MessageFormatter_ce_ptr, &value, &value_len ) == FAILURE ) + { + intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_set_pattern: unable to parse input params", 0 TSRMLS_CC); + RETURN_FALSE; + } + + MSG_FORMAT_METHOD_FETCH_OBJECT; + + /* Convert given pattern to UTF-16. */ + intl_convert_utf8_to_utf16(&spattern, &spattern_len, value, value_len, &INTL_DATA_ERROR_CODE(mfo)); + INTL_METHOD_CHECK_STATUS(mfo, "Error converting pattern to UTF-16" ); + +#ifdef MSG_FORMAT_QUOTE_APOS + if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) { + intl_error_set( NULL, U_INVALID_FORMAT_ERROR, + "msgfmt_set_pattern: error converting pattern to quote-friendly format", 0 TSRMLS_CC ); + RETURN_FALSE; + } +#endif + + /* TODO: add parse error information */ + umsg_applyPattern(MSG_FORMAT_OBJECT(mfo), spattern, spattern_len, NULL, &INTL_DATA_ERROR_CODE(mfo)); + if (spattern) { + efree(spattern); + } + INTL_METHOD_CHECK_STATUS(mfo, "Error setting symbol value"); + + if(mfo->mf_data.orig_format) { + efree(mfo->mf_data.orig_format); + } + mfo->mf_data.orig_format = estrndup(value, value_len); + mfo->mf_data.orig_format_len = value_len; + + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto string MessageFormatter::getLocale() + * Get formatter locale. }}} */ +/* {{{ proto string msgfmt_get_locale(MessageFormatter $mf) + * Get formatter locale. + */ +PHP_FUNCTION( msgfmt_get_locale ) +{ + char *loc; + MSG_FORMAT_METHOD_INIT_VARS; + + /* Parse parameters. */ + if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", + &object, MessageFormatter_ce_ptr ) == FAILURE ) + { + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_get_locale: unable to parse input params", 0 TSRMLS_CC ); + + RETURN_FALSE; + } + + /* Fetch the object. */ + MSG_FORMAT_METHOD_FETCH_OBJECT; + + loc = (char *)umsg_getLocale(MSG_FORMAT_OBJECT(mfo)); + RETURN_STRING(loc, 1); +} +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/ext/intl/msgformat/msgformat_attr.h b/ext/intl/msgformat/msgformat_attr.h new file mode 100644 index 0000000..898c445 --- /dev/null +++ b/ext/intl/msgformat/msgformat_attr.h @@ -0,0 +1,26 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifndef MSG_FORMAT_ATTR_H +#define MSG_FORMAT_ATTR_H + +#include <php.h> + +PHP_FUNCTION( msgfmt_set_pattern ); +PHP_FUNCTION( msgfmt_get_pattern ); +PHP_FUNCTION( msgfmt_get_locale ); + +#endif // MSG_FORMAT_ATTR_H diff --git a/ext/intl/msgformat/msgformat_class.c b/ext/intl/msgformat/msgformat_class.c new file mode 100644 index 0000000..bb3b55f --- /dev/null +++ b/ext/intl/msgformat/msgformat_class.c @@ -0,0 +1,192 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#include <unicode/unum.h> + +#include "msgformat_class.h" +#include "php_intl.h" +#include "msgformat_data.h" +#include "msgformat_format.h" +#include "msgformat_parse.h" +#include "msgformat.h" +#include "msgformat_attr.h" + +#include <zend_exceptions.h> + +zend_class_entry *MessageFormatter_ce_ptr = NULL; +static zend_object_handlers MessageFormatter_handlers; + +/* + * Auxiliary functions needed by objects of 'MessageFormatter' class + */ + +/* {{{ MessageFormatter_objects_dtor */ +static void MessageFormatter_object_dtor(void *object, zend_object_handle handle TSRMLS_DC ) +{ + zend_objects_destroy_object( object, handle TSRMLS_CC ); +} +/* }}} */ + +/* {{{ MessageFormatter_objects_free */ +void MessageFormatter_object_free( zend_object *object TSRMLS_DC ) +{ + MessageFormatter_object* mfo = (MessageFormatter_object*)object; + + zend_object_std_dtor( &mfo->zo TSRMLS_CC ); + + msgformat_data_free( &mfo->mf_data TSRMLS_CC ); + + efree( mfo ); +} +/* }}} */ + +/* {{{ MessageFormatter_object_create */ +zend_object_value MessageFormatter_object_create(zend_class_entry *ce TSRMLS_DC) +{ + zend_object_value retval; + MessageFormatter_object* intern; + + intern = ecalloc( 1, sizeof(MessageFormatter_object) ); + msgformat_data_init( &intern->mf_data TSRMLS_CC ); + zend_object_std_init( &intern->zo, ce TSRMLS_CC ); + object_properties_init(&intern->zo, ce); + + retval.handle = zend_objects_store_put( + intern, + MessageFormatter_object_dtor, + (zend_objects_free_object_storage_t)MessageFormatter_object_free, + NULL TSRMLS_CC ); + + retval.handlers = &MessageFormatter_handlers; + + return retval; +} +/* }}} */ + +/* {{{ MessageFormatter_object_clone */ +zend_object_value MessageFormatter_object_clone(zval *object TSRMLS_DC) +{ + zend_object_value new_obj_val; + zend_object_handle handle = Z_OBJ_HANDLE_P(object); + MessageFormatter_object *mfo, *new_mfo; + + MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK; + new_obj_val = MessageFormatter_ce_ptr->create_object(Z_OBJCE_P(object) TSRMLS_CC); + new_mfo = (MessageFormatter_object *)zend_object_store_get_object_by_handle(new_obj_val.handle TSRMLS_CC); + /* clone standard parts */ + zend_objects_clone_members(&new_mfo->zo, new_obj_val, &mfo->zo, handle TSRMLS_CC); + + /* clone formatter object */ + if (MSG_FORMAT_OBJECT(mfo) != NULL) { + MSG_FORMAT_OBJECT(new_mfo) = umsg_clone(MSG_FORMAT_OBJECT(mfo), + &INTL_DATA_ERROR_CODE(mfo)); + + if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) { + intl_errors_set(INTL_DATA_ERROR_P(mfo), INTL_DATA_ERROR_CODE(mfo), + "Failed to clone MessageFormatter object", 0 TSRMLS_CC); + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Failed to clone MessageFormatter object"); + } + } else { + zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Cannot clone unconstructed MessageFormatter"); + } + return new_obj_val; +} +/* }}} */ + +/* + * 'MessageFormatter' class registration structures & functions + */ + +/* {{{ arginfo */ +ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter___construct, 0, 0, 2) + ZEND_ARG_INFO(0, locale) + ZEND_ARG_INFO(0, pattern) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(arginfo_messageformatter_geterrormessage, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_formatmessage, 0, 0, 3) + ZEND_ARG_INFO(0, locale) + ZEND_ARG_INFO(0, pattern) + ZEND_ARG_INFO(0, args) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_format, 0, 0, 1) + ZEND_ARG_INFO(0, args) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_setpattern, 0, 0, 1) + ZEND_ARG_INFO(0, pattern) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_parse, 0, 0, 1) + ZEND_ARG_INFO(0, source) +ZEND_END_ARG_INFO() +/* }}} */ + +/* {{{ MessageFormatter_class_functions + * Every 'MessageFormatter' class method has an entry in this table + */ +static zend_function_entry MessageFormatter_class_functions[] = { + PHP_ME( MessageFormatter, __construct, arginfo_messageformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR ) + ZEND_FENTRY( create, ZEND_FN( msgfmt_create ), arginfo_messageformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC ) + PHP_NAMED_FE( format, ZEND_FN( msgfmt_format ), arginfo_messageformatter_format ) + ZEND_FENTRY( formatMessage, ZEND_FN( msgfmt_format_message ), arginfo_messageformatter_formatmessage, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC ) + PHP_NAMED_FE( parse, ZEND_FN( msgfmt_parse ), arginfo_messageformatter_parse ) + ZEND_FENTRY( parseMessage, ZEND_FN( msgfmt_parse_message ), arginfo_messageformatter_formatmessage, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC ) + PHP_NAMED_FE( setPattern, ZEND_FN( msgfmt_set_pattern ), arginfo_messageformatter_setpattern ) + PHP_NAMED_FE( getPattern, ZEND_FN( msgfmt_get_pattern ), arginfo_messageformatter_geterrormessage ) + PHP_NAMED_FE( getLocale, ZEND_FN( msgfmt_get_locale ), arginfo_messageformatter_geterrormessage ) + PHP_NAMED_FE( getErrorCode, ZEND_FN( msgfmt_get_error_code ), arginfo_messageformatter_geterrormessage ) + PHP_NAMED_FE( getErrorMessage, ZEND_FN( msgfmt_get_error_message ), arginfo_messageformatter_geterrormessage ) + PHP_FE_END +}; +/* }}} */ + +/* {{{ msgformat_register_class + * Initialize 'MessageFormatter' class + */ +void msgformat_register_class( TSRMLS_D ) +{ + zend_class_entry ce; + + /* Create and register 'MessageFormatter' class. */ + INIT_CLASS_ENTRY( ce, "MessageFormatter", MessageFormatter_class_functions ); + ce.create_object = MessageFormatter_object_create; + MessageFormatter_ce_ptr = zend_register_internal_class( &ce TSRMLS_CC ); + + memcpy(&MessageFormatter_handlers, zend_get_std_object_handlers(), + sizeof MessageFormatter_handlers); + MessageFormatter_handlers.clone_obj = MessageFormatter_object_clone; + + /* Declare 'MessageFormatter' class properties. */ + if( !MessageFormatter_ce_ptr ) + { + zend_error(E_ERROR, "Failed to register MessageFormatter class"); + return; + } +} +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/ext/intl/msgformat/msgformat_class.h b/ext/intl/msgformat/msgformat_class.h new file mode 100644 index 0000000..337e04e --- /dev/null +++ b/ext/intl/msgformat/msgformat_class.h @@ -0,0 +1,55 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifndef MSG_FORMAT_CLASS_H +#define MSG_FORMAT_CLASS_H + +#include <php.h> + +#include <unicode/uconfig.h> + +#include "../intl_common.h" +#include "../intl_error.h" +#include "../intl_data.h" +#include "msgformat_data.h" + +typedef struct { + zend_object zo; + msgformat_data mf_data; +} MessageFormatter_object; + +void msgformat_register_class( TSRMLS_D ); +extern zend_class_entry *MessageFormatter_ce_ptr; + +/* Auxiliary macros */ + +#define MSG_FORMAT_METHOD_INIT_VARS INTL_METHOD_INIT_VARS(MessageFormatter, mfo) +#define MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK INTL_METHOD_FETCH_OBJECT(MessageFormatter, mfo) +#define MSG_FORMAT_METHOD_FETCH_OBJECT \ + MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK; \ + if (MSG_FORMAT_OBJECT(mfo) == NULL) { \ + intl_errors_set(&mfo->mf_data.error, U_ILLEGAL_ARGUMENT_ERROR, \ + "Found unconstructed MessageFormatter", 0 TSRMLS_CC); \ + RETURN_FALSE; \ + } + +#define MSG_FORMAT_OBJECT(mfo) (mfo)->mf_data.umsgf + +#if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM < 48 +# define MSG_FORMAT_QUOTE_APOS 1 +#endif + +#endif // #ifndef MSG_FORMAT_CLASS_H diff --git a/ext/intl/msgformat/msgformat_data.c b/ext/intl/msgformat/msgformat_data.c new file mode 100644 index 0000000..527c1d4 --- /dev/null +++ b/ext/intl/msgformat/msgformat_data.c @@ -0,0 +1,102 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <unicode/ustring.h> +#include "msgformat_data.h" + +#include "msgformat_class.h" + +/* {{{ void msgformat_data_init( msgformat_data* mf_data ) + * Initialize internals of msgformat_data. + */ +void msgformat_data_init( msgformat_data* mf_data TSRMLS_DC ) +{ + if( !mf_data ) + return; + + mf_data->umsgf = NULL; + mf_data->orig_format = NULL; + intl_error_reset( &mf_data->error TSRMLS_CC ); +} +/* }}} */ + +/* {{{ void msgformat_data_free( msgformat_data* mf_data ) + * Clean up memory allocated for msgformat_data + */ +void msgformat_data_free( msgformat_data* mf_data TSRMLS_DC ) +{ + if( !mf_data ) + return; + + if( mf_data->umsgf ) + umsg_close( mf_data->umsgf ); + + if(mf_data->orig_format) { + efree(mf_data->orig_format); + mf_data->orig_format = NULL; + } + + mf_data->umsgf = NULL; + intl_error_reset( &mf_data->error TSRMLS_CC ); +} +/* }}} */ + +/* {{{ msgformat_data* msgformat_data_create() + * Allocate memory for msgformat_data and initialize it with default values. + */ +msgformat_data* msgformat_data_create( TSRMLS_D ) +{ + msgformat_data* mf_data = ecalloc( 1, sizeof(msgformat_data) ); + + msgformat_data_init( mf_data TSRMLS_CC ); + + return mf_data; +} +/* }}} */ + +#ifdef MSG_FORMAT_QUOTE_APOS +int msgformat_fix_quotes(UChar **spattern, uint32_t *spattern_len, UErrorCode *ec) +{ + if(*spattern && *spattern_len && u_strchr(*spattern, (UChar)'\'')) { + UChar *npattern = emalloc(sizeof(UChar)*(2*(*spattern_len)+1)); + uint32_t npattern_len; + npattern_len = umsg_autoQuoteApostrophe(*spattern, *spattern_len, npattern, 2*(*spattern_len)+1, ec); + efree(*spattern); + if( U_FAILURE(*ec) ) + { + return FAILURE; + } + npattern = erealloc(npattern, sizeof(UChar)*(npattern_len+1)); + *spattern = npattern; + *spattern_len = npattern_len; + } + return SUCCESS; +} +#endif + + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/ext/intl/msgformat/msgformat_data.h b/ext/intl/msgformat/msgformat_data.h new file mode 100644 index 0000000..6479888 --- /dev/null +++ b/ext/intl/msgformat/msgformat_data.h @@ -0,0 +1,44 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifndef MSG_FORMAT_DATA_H +#define MSG_FORMAT_DATA_H + +#include <php.h> + +#include "../intl_error.h" + +#include <unicode/umsg.h> + +typedef struct { + // error hangling + intl_error error; + + // formatter handling + UMessageFormat* umsgf; + char* orig_format; + ulong orig_format_len; +} msgformat_data; + +msgformat_data* msgformat_data_create( TSRMLS_D ); +void msgformat_data_init( msgformat_data* mf_data TSRMLS_DC ); +void msgformat_data_free( msgformat_data* mf_data TSRMLS_DC ); + +#ifdef MSG_FORMAT_QUOTE_APOS +int msgformat_fix_quotes(UChar **spattern, uint32_t *spattern_len, UErrorCode *ec); +#endif + +#endif // MSG_FORMAT_DATA_H diff --git a/ext/intl/msgformat/msgformat_format.c b/ext/intl/msgformat/msgformat_format.c new file mode 100644 index 0000000..9a18ac0 --- /dev/null +++ b/ext/intl/msgformat/msgformat_format.c @@ -0,0 +1,186 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <unicode/ustring.h> + +#include "php_intl.h" +#include "msgformat_class.h" +#include "msgformat_format.h" +#include "msgformat_data.h" +#include "msgformat_helpers.h" +#include "intl_convert.h" + +#ifndef Z_ADDREF_P +#define Z_ADDREF_P(z) ((z)->refcount++) +#endif + +/* {{{ */ +static void msgfmt_do_format(MessageFormatter_object *mfo, zval *args, zval *return_value TSRMLS_DC) +{ + zval **fargs; + int count; + UChar* formatted = NULL; + int formatted_len = 0; + HashPosition pos; + int i; + + count = zend_hash_num_elements(Z_ARRVAL_P(args)); + + if(count < umsg_format_arg_count(MSG_FORMAT_OBJECT(mfo))) { + /* Not enough aguments for format! */ + intl_error_set( INTL_DATA_ERROR_P(mfo), U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_format: not enough parameters", 0 TSRMLS_CC ); + RETVAL_FALSE; + return; + } + + fargs = safe_emalloc(count, sizeof(zval *), 0); + + zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(args), &pos); + for(i=0;i<count;i++) { + zval **val; + zend_hash_get_current_data_ex(Z_ARRVAL_P(args), (void **)&val, &pos); + fargs[i] = *val; + Z_ADDREF_P(fargs[i]); + /* TODO: needs refcount increase here? */ + zend_hash_move_forward_ex(Z_ARRVAL_P(args), &pos); + } + + umsg_format_helper(MSG_FORMAT_OBJECT(mfo), count, fargs, &formatted, &formatted_len, &INTL_DATA_ERROR_CODE(mfo) TSRMLS_CC); + + for(i=0;i<count;i++) { + zval_ptr_dtor(&fargs[i]); + } + + efree(fargs); + + if (formatted && U_FAILURE( INTL_DATA_ERROR_CODE(mfo) ) ) { + efree(formatted); + } + + INTL_METHOD_CHECK_STATUS( mfo, "Number formatting failed" ); + INTL_METHOD_RETVAL_UTF8( mfo, formatted, formatted_len, 1 ); +} +/* }}} */ + +/* {{{ proto mixed MessageFormatter::format( array $args ) + * Format a message. }}} */ +/* {{{ proto mixed msgfmt_format( MessageFormatter $nf, array $args ) + * Format a message. + */ +PHP_FUNCTION( msgfmt_format ) +{ + zval *args; + MSG_FORMAT_METHOD_INIT_VARS; + + + /* Parse parameters. */ + if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa", + &object, MessageFormatter_ce_ptr, &args ) == FAILURE ) + { + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_format: unable to parse input params", 0 TSRMLS_CC ); + + RETURN_FALSE; + } + + /* Fetch the object. */ + MSG_FORMAT_METHOD_FETCH_OBJECT; + + msgfmt_do_format(mfo, args, return_value TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto mixed MessageFormatter::formatMessage( string $locale, string $pattern, array $args ) + * Format a message. }}} */ +/* {{{ proto mixed msgfmt_format_message( string $locale, string $pattern, array $args ) + * Format a message. + */ +PHP_FUNCTION( msgfmt_format_message ) +{ + zval *args; + UChar *spattern = NULL; + int spattern_len = 0; + char *pattern = NULL; + int pattern_len = 0; + char *slocale = NULL; + int slocale_len = 0; + MessageFormatter_object mf = {0}; + MessageFormatter_object *mfo = &mf; + + /* Parse parameters. */ + if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "ssa", + &slocale, &slocale_len, &pattern, &pattern_len, &args ) == FAILURE ) + { + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_format_message: unable to parse input params", 0 TSRMLS_CC ); + + RETURN_FALSE; + } + + msgformat_data_init(&mfo->mf_data TSRMLS_CC); + + if(pattern && pattern_len) { + intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo)); + if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) ) + { + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_format_message: error converting pattern to UTF-16", 0 TSRMLS_CC ); + RETURN_FALSE; + } + } else { + spattern_len = 0; + spattern = NULL; + } + + if(slocale_len == 0) { + slocale = INTL_G(default_locale); + } + +#ifdef MSG_FORMAT_QUOTE_APOS + if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) { + intl_error_set( NULL, U_INVALID_FORMAT_ERROR, + "msgfmt_format_message: error converting pattern to quote-friendly format", 0 TSRMLS_CC ); + RETURN_FALSE; + } +#endif + + /* Create an ICU message formatter. */ + MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo)); + if(spattern && spattern_len) { + efree(spattern); + } + INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed"); + + msgfmt_do_format(mfo, args, return_value TSRMLS_CC); + + /* drop the temporary formatter */ + msgformat_data_free(&mfo->mf_data TSRMLS_CC); +} +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/ext/intl/msgformat/msgformat_format.h b/ext/intl/msgformat/msgformat_format.h new file mode 100644 index 0000000..b74deab --- /dev/null +++ b/ext/intl/msgformat/msgformat_format.h @@ -0,0 +1,25 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifndef MSG_FORMAT_FORMAT_H +#define MSG_FORMAT_FORMAT_H + +#include <php.h> + +PHP_FUNCTION( msgfmt_format ); +PHP_FUNCTION( msgfmt_format_message ); + +#endif // MSG_FORMAT_FORMAT_H diff --git a/ext/intl/msgformat/msgformat_helpers.cpp b/ext/intl/msgformat/msgformat_helpers.cpp new file mode 100644 index 0000000..1895de2 --- /dev/null +++ b/ext/intl/msgformat/msgformat_helpers.cpp @@ -0,0 +1,212 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <math.h> +#include <unicode/msgfmt.h> +#include <unicode/chariter.h> + +extern "C" { +#include "php_intl.h" +#include "msgformat_class.h" +#include "msgformat_format.h" +#include "msgformat_helpers.h" +#include "intl_convert.h" +} + +U_NAMESPACE_BEGIN +/** + * This class isolates our access to private internal methods of + * MessageFormat. It is never instantiated; it exists only for C++ + * access management. + */ +class MessageFormatAdapter { +public: + static const Formattable::Type* getArgTypeList(const MessageFormat& m, + int32_t& count); +}; +const Formattable::Type* +MessageFormatAdapter::getArgTypeList(const MessageFormat& m, + int32_t& count) { + return m.getArgTypeList(count); +} +U_NAMESPACE_END + +U_CFUNC int32_t umsg_format_arg_count(UMessageFormat *fmt) +{ + int32_t fmt_count = 0; + MessageFormatAdapter::getArgTypeList(*(const MessageFormat*)fmt, fmt_count); + return fmt_count; +} + +U_CFUNC void umsg_format_helper(UMessageFormat *fmt, int arg_count, zval **args, UChar **formatted, int *formatted_len, UErrorCode *status TSRMLS_DC) +{ + int fmt_count = 0; + const Formattable::Type* argTypes = + MessageFormatAdapter::getArgTypeList(*(const MessageFormat*)fmt, fmt_count); + Formattable* fargs = new Formattable[fmt_count ? fmt_count : 1]; + + for(int32_t i = 0; i < fmt_count; ++i) { + UChar *stringVal = NULL; + int stringLen = 0; + int64_t tInt64 = 0; + + switch(argTypes[i]) { + case Formattable::kDate: + convert_to_long_ex(&args[i]); + fargs[i].setDate(U_MILLIS_PER_SECOND * (double)Z_LVAL_P(args[i])); + break; + + case Formattable::kDouble: + convert_to_double_ex(&args[i]); + fargs[i].setDouble(Z_DVAL_P(args[i])); + break; + + case Formattable::kLong: + convert_to_long_ex(&args[i]); + fargs[i].setLong(Z_LVAL_P(args[i])); + break; + + case Formattable::kInt64: + if(Z_TYPE_P(args[i]) == IS_DOUBLE) { + tInt64 = (int64_t)Z_DVAL_P(args[i]); + } else if(Z_TYPE_P(args[i]) == IS_LONG) { + tInt64 = (int64_t)Z_LVAL_P(args[i]); + } else { + SEPARATE_ZVAL_IF_NOT_REF(&args[i]); + convert_scalar_to_number( args[i] TSRMLS_CC ); + tInt64 = (Z_TYPE_P(args[i]) == IS_DOUBLE)?(int64_t)Z_DVAL_P(args[i]):Z_LVAL_P(args[i]); + } + fargs[i].setInt64(tInt64); + break; + + case Formattable::kString: + convert_to_string_ex(&args[i]); + intl_convert_utf8_to_utf16(&stringVal, &stringLen, Z_STRVAL_P(args[i]), Z_STRLEN_P(args[i]), status); + if(U_FAILURE(*status)){ + delete[] fargs; + return; + } + fargs[i].setString(stringVal); + efree(stringVal); + break; + + case Formattable::kArray: + case Formattable::kObject: + *status = U_UNSUPPORTED_ERROR; + delete[] fargs; + return; + } + } + + UnicodeString resultStr; + FieldPosition fieldPosition(0); + + /* format the message */ + ((const MessageFormat*)fmt)->format(fargs, fmt_count, resultStr, fieldPosition, *status); + + delete[] fargs; + + if(U_FAILURE(*status)){ + return; + } + + *formatted_len = resultStr.length(); + *formatted = eumalloc(*formatted_len+1); + resultStr.extract(*formatted, *formatted_len+1, *status); +} + +#define cleanup_zvals() for(int j=i;j>=0;j--) { zval_ptr_dtor((*args)+i); } + +U_CFUNC void umsg_parse_helper(UMessageFormat *fmt, int *count, zval ***args, UChar *source, int source_len, UErrorCode *status) +{ + UnicodeString srcString(source, source_len); + Formattable *fargs = ((const MessageFormat*)fmt)->parse(srcString, *count, *status); + + if(U_FAILURE(*status)) { + return; + } + + *args = (zval **)safe_emalloc(*count, sizeof(zval *), 0); + + // assign formattables to varargs + for(int32_t i = 0; i < *count; i++) { + int64_t aInt64; + double aDate; + UnicodeString temp; + char *stmp; + int stmp_len; + + ALLOC_INIT_ZVAL((*args)[i]); + + switch(fargs[i].getType()) { + case Formattable::kDate: + aDate = ((double)fargs[i].getDate())/U_MILLIS_PER_SECOND; + if(aDate > LONG_MAX || aDate < -LONG_MAX) { + ZVAL_DOUBLE((*args)[i], aDate<0?ceil(aDate):floor(aDate)); + } else { + ZVAL_LONG((*args)[i], (long)aDate); + } + break; + + case Formattable::kDouble: + ZVAL_DOUBLE((*args)[i], (double)fargs[i].getDouble()); + break; + + case Formattable::kLong: + ZVAL_LONG((*args)[i], fargs[i].getLong()); + break; + + case Formattable::kInt64: + aInt64 = fargs[i].getInt64(); + if(aInt64 > LONG_MAX || aInt64 < -LONG_MAX) { + ZVAL_DOUBLE((*args)[i], (double)aInt64); + } else { + ZVAL_LONG((*args)[i], (long)aInt64); + } + break; + + case Formattable::kString: + fargs[i].getString(temp); + intl_convert_utf16_to_utf8(&stmp, &stmp_len, temp.getBuffer(), temp.length(), status); + if(U_FAILURE(*status)) { + cleanup_zvals(); + return; + } + ZVAL_STRINGL((*args)[i], stmp, stmp_len, 0); + break; + + case Formattable::kObject: + case Formattable::kArray: + *status = U_ILLEGAL_ARGUMENT_ERROR; + cleanup_zvals(); + break; + } + } + delete[] fargs; +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/ext/intl/msgformat/msgformat_helpers.h b/ext/intl/msgformat/msgformat_helpers.h new file mode 100644 index 0000000..30c7e39 --- /dev/null +++ b/ext/intl/msgformat/msgformat_helpers.h @@ -0,0 +1,25 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifndef MSG_FORMAT_HELPERS_H +#define MSG_FORMAT_HELPERS_H + +int32_t umsg_format_arg_count(UMessageFormat *fmt); +void umsg_format_helper(UMessageFormat *fmt, int arg_count, zval **args, + UChar **formatted, int *formatted_len, UErrorCode *status TSRMLS_DC); +void umsg_parse_helper(UMessageFormat *fmt, int *count, zval ***args, + UChar *source, int source_len, UErrorCode *status); +#endif // MSG_FORMAT_HELPERS_H diff --git a/ext/intl/msgformat/msgformat_parse.c b/ext/intl/msgformat/msgformat_parse.c new file mode 100644 index 0000000..f540b1d --- /dev/null +++ b/ext/intl/msgformat/msgformat_parse.c @@ -0,0 +1,161 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <unicode/ustring.h> + +#include "php_intl.h" +#include "msgformat_class.h" +#include "msgformat_parse.h" +#include "msgformat_data.h" +#include "msgformat_helpers.h" +#include "intl_convert.h" + +/* {{{ */ +static void msgfmt_do_parse(MessageFormatter_object *mfo, char *source, int src_len, zval *return_value TSRMLS_DC) +{ + zval **fargs; + int count = 0; + int i; + UChar *usource = NULL; + int usrc_len = 0; + + intl_convert_utf8_to_utf16(&usource, &usrc_len, source, src_len, &INTL_DATA_ERROR_CODE(mfo)); + INTL_METHOD_CHECK_STATUS(mfo, "Converting parse string failed"); + + umsg_parse_helper(MSG_FORMAT_OBJECT(mfo), &count, &fargs, usource, usrc_len, &INTL_DATA_ERROR_CODE(mfo)); + if (usource) { + efree(usource); + } + INTL_METHOD_CHECK_STATUS(mfo, "Parsing failed"); + + array_init(return_value); + for(i=0;i<count;i++) { + add_next_index_zval(return_value, fargs[i]); + } + efree(fargs); +} +/* }}} */ + +/* {{{ proto array MessageFormatter::parse( string $source ) + * Parse a message }}} */ +/* {{{ proto array msgfmt_parse( MessageFormatter $nf, string $source ) + * Parse a message. + */ +PHP_FUNCTION( msgfmt_parse ) +{ + char *source; + int source_len; + MSG_FORMAT_METHOD_INIT_VARS; + + + /* Parse parameters. */ + if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", + &object, MessageFormatter_ce_ptr, &source, &source_len ) == FAILURE ) + { + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_parse: unable to parse input params", 0 TSRMLS_CC ); + + RETURN_FALSE; + } + + /* Fetch the object. */ + MSG_FORMAT_METHOD_FETCH_OBJECT; + + msgfmt_do_parse(mfo, source, source_len, return_value TSRMLS_CC); +} +/* }}} */ + +/* {{{ proto array MessageFormatter::formatMessage( string $locale, string $pattern, string $source ) + * Parse a message. }}} */ +/* {{{ proto array numfmt_parse_message( string $locale, string $pattern, string $source ) + * Parse a message. + */ +PHP_FUNCTION( msgfmt_parse_message ) +{ + UChar *spattern = NULL; + int spattern_len = 0; + char *pattern = NULL; + int pattern_len = 0; + char *slocale = NULL; + int slocale_len = 0; + char *source = NULL; + int src_len = 0; + MessageFormatter_object mf = {0}; + MessageFormatter_object *mfo = &mf; + + /* Parse parameters. */ + if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "sss", + &slocale, &slocale_len, &pattern, &pattern_len, &source, &src_len ) == FAILURE ) + { + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_parse_message: unable to parse input params", 0 TSRMLS_CC ); + + RETURN_FALSE; + } + + msgformat_data_init(&mfo->mf_data TSRMLS_CC); + + if(pattern && pattern_len) { + intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo)); + if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) ) + { + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, + "msgfmt_parse_message: error converting pattern to UTF-16", 0 TSRMLS_CC ); + RETURN_FALSE; + } + } else { + spattern_len = 0; + spattern = NULL; + } + + if(slocale_len == 0) { + slocale = INTL_G(default_locale); + } + +#ifdef MSG_FORMAT_QUOTE_APOS + if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) { + intl_error_set( NULL, U_INVALID_FORMAT_ERROR, + "msgfmt_parse_message: error converting pattern to quote-friendly format", 0 TSRMLS_CC ); + RETURN_FALSE; + } +#endif + + /* Create an ICU message formatter. */ + MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo)); + if(spattern && spattern_len) { + efree(spattern); + } + INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed"); + + msgfmt_do_parse(mfo, source, src_len, return_value TSRMLS_CC); + + /* drop the temporary formatter */ + msgformat_data_free(&mfo->mf_data TSRMLS_CC); +} +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/ext/intl/msgformat/msgformat_parse.h b/ext/intl/msgformat/msgformat_parse.h new file mode 100644 index 0000000..a937235 --- /dev/null +++ b/ext/intl/msgformat/msgformat_parse.h @@ -0,0 +1,25 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Stanislav Malyshev <stas@zend.com> | + +----------------------------------------------------------------------+ + */ + +#ifndef MSG_FORMAT_PARSE_H +#define MSG_FORMAT_PARSE_H + +#include <php.h> + +PHP_FUNCTION( msgfmt_parse ); +PHP_FUNCTION( msgfmt_parse_message ); + +#endif // MSG_FORMAT_PARSE_H |