Back to index

php5  5.3.10
msgformat_parse.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | This source file is subject to version 3.01 of the PHP license,      |
00006    | that is bundled with this package in the file LICENSE, and is        |
00007    | available through the world-wide-web at the following url:           |
00008    | http://www.php.net/license/3_01.txt                                  |
00009    | If you did not receive a copy of the PHP license and are unable to   |
00010    | obtain it through the world-wide-web, please send a note to          |
00011    | license@php.net so we can mail you a copy immediately.               |
00012    +----------------------------------------------------------------------+
00013    | Authors: Stanislav Malyshev <stas@zend.com>                          |
00014    +----------------------------------------------------------------------+
00015  */
00016 
00017 #ifdef HAVE_CONFIG_H
00018 #include "config.h"
00019 #endif
00020 
00021 #include <unicode/ustring.h>
00022 
00023 #include "php_intl.h"
00024 #include "msgformat_class.h"
00025 #include "msgformat_parse.h"
00026 #include "msgformat_data.h"
00027 #include "msgformat_helpers.h"
00028 #include "intl_convert.h"
00029 
00030 /* {{{ */
00031 static void msgfmt_do_parse(MessageFormatter_object *mfo, char *source, int src_len, zval *return_value TSRMLS_DC) 
00032 {
00033        zval **fargs;
00034        int count = 0;
00035        int i;
00036        UChar *usource = NULL;
00037        int usrc_len = 0;
00038 
00039        intl_convert_utf8_to_utf16(&usource, &usrc_len, source, src_len, &INTL_DATA_ERROR_CODE(mfo));
00040        INTL_METHOD_CHECK_STATUS(mfo, "Converting parse string failed");
00041 
00042        umsg_parse_helper(MSG_FORMAT_OBJECT(mfo), &count, &fargs, usource, usrc_len, &INTL_DATA_ERROR_CODE(mfo));
00043        if (usource) {
00044               efree(usource);
00045        }
00046        INTL_METHOD_CHECK_STATUS(mfo, "Parsing failed");
00047 
00048        array_init(return_value);
00049        for(i=0;i<count;i++) {
00050               add_next_index_zval(return_value, fargs[i]);
00051        }
00052        efree(fargs);
00053 }
00054 /* }}} */
00055 
00056 /* {{{ proto array MessageFormatter::parse( string $source )
00057  * Parse a message }}} */
00058 /* {{{ proto array msgfmt_parse( MessageFormatter $nf, string $source )
00059  * Parse a message.
00060  */
00061 PHP_FUNCTION( msgfmt_parse )
00062 {
00063        char *source;
00064        int source_len;
00065        MSG_FORMAT_METHOD_INIT_VARS;
00066 
00067 
00068        /* Parse parameters. */
00069        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
00070               &object, MessageFormatter_ce_ptr,  &source, &source_len ) == FAILURE )
00071        {
00072               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00073                      "msgfmt_parse: unable to parse input params", 0 TSRMLS_CC );
00074 
00075               RETURN_FALSE;
00076        }
00077 
00078        /* Fetch the object. */
00079        MSG_FORMAT_METHOD_FETCH_OBJECT;
00080 
00081        msgfmt_do_parse(mfo, source, source_len, return_value TSRMLS_CC);
00082 }
00083 /* }}} */
00084 
00085 /* {{{ proto array MessageFormatter::formatMessage( string $locale, string $pattern, string $source )
00086  * Parse a message. }}} */
00087 /* {{{ proto array numfmt_parse_message( string $locale, string $pattern, string $source )
00088  * Parse a message.
00089  */
00090 PHP_FUNCTION( msgfmt_parse_message )
00091 {
00092        UChar      *spattern = NULL;
00093        int         spattern_len = 0;
00094        char       *pattern = NULL;
00095        int         pattern_len = 0;
00096        char       *slocale = NULL;
00097        int         slocale_len = 0;
00098        char       *source = NULL;
00099        int         src_len = 0;
00100        MessageFormatter_object mf = {0};
00101        MessageFormatter_object *mfo = &mf;
00102 
00103        /* Parse parameters. */
00104        if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "sss",
00105                 &slocale, &slocale_len, &pattern, &pattern_len, &source, &src_len ) == FAILURE )
00106        {
00107               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00108                      "msgfmt_parse_message: unable to parse input params", 0 TSRMLS_CC );
00109 
00110               RETURN_FALSE;
00111        }
00112 
00113        msgformat_data_init(&mfo->mf_data TSRMLS_CC);
00114 
00115        if(pattern && pattern_len) {
00116               intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
00117               if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
00118               {
00119                      intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00120                             "msgfmt_parse_message: error converting pattern to UTF-16", 0 TSRMLS_CC );
00121                      RETURN_FALSE;
00122               }
00123        } else {
00124               spattern_len = 0;
00125               spattern = NULL;
00126        }
00127 
00128        if(slocale_len == 0) {
00129               slocale = INTL_G(default_locale);
00130        }
00131 
00132        if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
00133               intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
00134                      "msgfmt_parse_message: error converting pattern to quote-friendly format", 0 TSRMLS_CC );
00135               RETURN_FALSE;
00136        }
00137 
00138        /* Create an ICU message formatter. */
00139        MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo));
00140        if(spattern && spattern_len) {
00141               efree(spattern);
00142        }
00143        INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
00144 
00145        msgfmt_do_parse(mfo, source, src_len, return_value TSRMLS_CC);
00146 
00147        /* drop the temporary formatter */
00148        msgformat_data_free(&mfo->mf_data TSRMLS_CC);
00149 }
00150 /* }}} */
00151 
00152 /*
00153  * Local variables:
00154  * tab-width: 4
00155  * c-basic-offset: 4
00156  * End:
00157  * vim600: noet sw=4 ts=4 fdm=marker
00158  * vim<600: noet sw=4 ts=4
00159  */