Annotation of embedaddon/php/ext/intl/dateformat/dateformat.c, revision 1.1

1.1     ! misho       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: Kirti Velankar <kirtig@yahoo-inc.com>                       |
        !            14:    +----------------------------------------------------------------------+
        !            15: */
        !            16: #ifdef HAVE_CONFIG_H
        !            17: #include "config.h"
        !            18: #endif
        !            19: 
        !            20: #include <unicode/ustring.h>
        !            21: #include <unicode/udat.h>
        !            22: #include <unicode/ucal.h>
        !            23: 
        !            24: #include "php_intl.h"
        !            25: #include "intl_convert.h"
        !            26: #include "dateformat_class.h"
        !            27: #include "dateformat.h"
        !            28: 
        !            29: /* {{{ dateformat_register_constants
        !            30:  * Register constants common for the both (OO and procedural)
        !            31:  * APIs.
        !            32:  */
        !            33: void dateformat_register_constants( INIT_FUNC_ARGS )
        !            34: {
        !            35:        if( IntlDateFormatter_ce_ptr == NULL) {
        !            36:                zend_error(E_ERROR, "DateFormat class not defined");
        !            37:                return;
        !            38:        }
        !            39: 
        !            40:        #define DATEFORMATTER_EXPOSE_CONST(x) REGISTER_LONG_CONSTANT(#x, x, CONST_CS)
        !            41:        #define DATEFORMATTER_EXPOSE_CLASS_CONST(x) zend_declare_class_constant_long( IntlDateFormatter_ce_ptr, ZEND_STRS( #x ) - 1, UDAT_##x TSRMLS_CC );
        !            42:        #define DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST(name, value) zend_declare_class_constant_long( IntlDateFormatter_ce_ptr, ZEND_STRS( name ) - 1, value TSRMLS_CC );
        !            43: 
        !            44:        #define DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST(x) zend_declare_class_constant_long( IntlDateFormatter_ce_ptr, ZEND_STRS( #x ) - 1, UCAL_##x TSRMLS_CC );
        !            45: 
        !            46:        /* UDateFormatStyle constants */
        !            47:        DATEFORMATTER_EXPOSE_CLASS_CONST( FULL );
        !            48:        DATEFORMATTER_EXPOSE_CLASS_CONST( LONG );
        !            49:        DATEFORMATTER_EXPOSE_CLASS_CONST( MEDIUM );
        !            50:        DATEFORMATTER_EXPOSE_CLASS_CONST( SHORT );
        !            51:        DATEFORMATTER_EXPOSE_CLASS_CONST( NONE );
        !            52: 
        !            53: /*
        !            54:        DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "GREGORIAN", DATEF_GREGORIAN );
        !            55:        DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "CUSTOMARY", DATEF_CUSTOMARY );
        !            56:        DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "BUDDHIST", DATEF_BUDDHIST );
        !            57:        DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST( "JAPANESE_IMPERIAL", DATEF_JAPANESE_IMPERIAL );
        !            58: */
        !            59: 
        !            60:        DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST( GREGORIAN );
        !            61:        DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST( TRADITIONAL );
        !            62: 
        !            63:        #undef DATEFORMATTER_EXPOSE_UCAL_CLASS_CONST
        !            64:        #undef DATEFORMATTER_EXPOSE_CUSTOM_CLASS_CONST
        !            65:        #undef DATEFORMATTER_EXPOSE_CLASS_CONST
        !            66:        #undef DATEFORMATTER_EXPOSE_CONST
        !            67: }
        !            68: /* }}} */
        !            69: 
        !            70: /* {{{ */
        !            71: static void datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
        !            72: {
        !            73:     char*       locale;
        !            74:        int         locale_len = 0;
        !            75:        zval*       object;
        !            76:     long        date_type = 0;
        !            77:     long        time_type = 0;
        !            78:     long        calendar = UCAL_GREGORIAN;
        !            79:     char*       timezone_str = NULL;
        !            80:     int         timezone_str_len = 0;
        !            81:     char*       pattern_str = NULL;
        !            82:     int         pattern_str_len = 0;
        !            83:     UChar*      svalue = NULL;         /* UTF-16 pattern_str */
        !            84:     int         slength = 0;
        !            85:     UChar*      timezone_utf16 = NULL;         /* UTF-16 timezone_str */
        !            86:     int         timezone_utf16_len = 0;
        !            87:        UCalendar   ucal_obj = NULL;
        !            88:        IntlDateFormatter_object* dfo;
        !            89:        
        !            90:        intl_error_reset( NULL TSRMLS_CC );
        !            91:        object = return_value;
        !            92:        /* Parse parameters. */
        !            93:     if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "sll|sls",
        !            94:                &locale, &locale_len, &date_type, &time_type, &timezone_str, &timezone_str_len, &calendar,&pattern_str, &pattern_str_len ) == FAILURE )
        !            95:     {
        !            96:                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "datefmt_create: unable to parse input parameters", 0 TSRMLS_CC );
        !            97:                zval_dtor(return_value);
        !            98:                RETURN_NULL();
        !            99:     }
        !           100: 
        !           101:        INTL_CHECK_LOCALE_LEN_OBJ(locale_len, return_value);
        !           102:        DATE_FORMAT_METHOD_FETCH_OBJECT;
        !           103:        /* Convert pattern (if specified) to UTF-16. */
        !           104:        if( pattern_str && pattern_str_len>0 ){
        !           105:                intl_convert_utf8_to_utf16(&svalue, &slength, pattern_str, pattern_str_len, &INTL_DATA_ERROR_CODE(dfo));
        !           106:                INTL_CTOR_CHECK_STATUS(dfo, "datefmt_create: error converting pattern to UTF-16");
        !           107:        }
        !           108: 
        !           109:        /* Convert pattern (if specified) to UTF-16. */
        !           110:        if( timezone_str && timezone_str_len >0 ){
        !           111:                intl_convert_utf8_to_utf16(&timezone_utf16, &timezone_utf16_len, timezone_str, timezone_str_len, &INTL_DATA_ERROR_CODE(dfo));
        !           112:                INTL_CTOR_CHECK_STATUS(dfo, "datefmt_create: error converting timezone_str to UTF-16" );
        !           113:        }
        !           114: 
        !           115:        if(locale_len == 0) {
        !           116:                locale = INTL_G(default_locale);
        !           117:        }
        !           118: 
        !           119:        if( pattern_str && pattern_str_len>0 ){
        !           120:                DATE_FORMAT_OBJECT(dfo) = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, timezone_utf16, timezone_utf16_len, svalue, slength, &INTL_DATA_ERROR_CODE(dfo));
        !           121:        } else {
        !           122:                DATE_FORMAT_OBJECT(dfo) = udat_open(time_type, date_type, locale, timezone_utf16, timezone_utf16_len, svalue, slength, &INTL_DATA_ERROR_CODE(dfo));
        !           123:        }
        !           124: 
        !           125:     /* Set the calendar if passed */
        !           126:     if(!U_FAILURE(INTL_DATA_ERROR_CODE(dfo)) && calendar) {
        !           127:                ucal_obj = ucal_open( timezone_utf16, timezone_utf16_len, locale, calendar, &INTL_DATA_ERROR_CODE(dfo) );
        !           128:                if(!U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
        !           129:                        udat_setCalendar( DATE_FORMAT_OBJECT(dfo), ucal_obj );
        !           130:                }
        !           131:     }
        !           132: 
        !           133:        if(svalue)
        !           134:        {
        !           135:                efree(svalue);
        !           136:        }
        !           137:        if(timezone_utf16)
        !           138:        {
        !           139:                efree(timezone_utf16);
        !           140:        }
        !           141: 
        !           142:        INTL_CTOR_CHECK_STATUS(dfo, "datefmt_create: date formatter creation failed");
        !           143: 
        !           144:        /* Set the class variables */
        !           145:        dfo->date_type = date_type;
        !           146:        dfo->time_type = time_type;
        !           147:        dfo->calendar  = calendar;
        !           148:        if( timezone_str && timezone_str_len > 0){
        !           149:                dfo->timezone_id = estrndup( timezone_str, timezone_str_len);
        !           150:        }
        !           151: }
        !           152: /* }}} */
        !           153: 
        !           154: /* {{{ proto IntlDateFormatter IntlDateFormatter::create(string $locale, long date_type, long time_type[, string $timezone_str, long $calendar, string $pattern] )
        !           155:  * Create formatter. }}} */
        !           156: /* {{{ proto IntlDateFormatter datefmt_create(string $locale, long date_type, long time_type[, string $timezone_str, long $calendar, string $pattern] )
        !           157:  
        !           158:  * Create formatter.
        !           159:  */
        !           160: PHP_FUNCTION( datefmt_create )
        !           161: {
        !           162:     object_init_ex( return_value, IntlDateFormatter_ce_ptr );
        !           163:        datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
        !           164: }
        !           165: /* }}} */
        !           166: 
        !           167: /* {{{ proto void IntlDateFormatter::__construct(string $locale, long date_type, long time_type[, string $timezone_str, long $calendar, string $pattern])
        !           168:  * IntlDateFormatter object constructor.
        !           169:  */
        !           170: PHP_METHOD( IntlDateFormatter, __construct )
        !           171: {
        !           172:        return_value = getThis();
        !           173:        datefmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
        !           174: }
        !           175: /* }}} */
        !           176: 
        !           177: /* {{{ proto int IntlDateFormatter::getErrorCode()
        !           178:  * Get formatter's last error code. }}} */
        !           179: /* {{{ proto int datefmt_get_error_code( IntlDateFormatter $nf )
        !           180:  * Get formatter's last error code.
        !           181:  */
        !           182: PHP_FUNCTION( datefmt_get_error_code )
        !           183: {
        !           184:        DATE_FORMAT_METHOD_INIT_VARS;
        !           185: 
        !           186:        /* Parse parameters. */
        !           187:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
        !           188:                &object, IntlDateFormatter_ce_ptr ) == FAILURE )
        !           189:        {
        !           190:                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
        !           191:                        "datefmt_get_error_code: unable to parse input params", 0 TSRMLS_CC );
        !           192:                RETURN_FALSE;
        !           193:        }
        !           194: 
        !           195:        dfo = (IntlDateFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
        !           196: 
        !           197:        /* Return formatter's last error code. */
        !           198:        RETURN_LONG( INTL_DATA_ERROR_CODE(dfo) );
        !           199: }
        !           200: /* }}} */
        !           201: 
        !           202: /* {{{ proto string IntlDateFormatter::getErrorMessage( )
        !           203:  * Get text description for formatter's last error code. }}} */
        !           204: /* {{{ proto string datefmt_get_error_message( IntlDateFormatter $coll )
        !           205:  * Get text description for formatter's last error code.
        !           206:  */
        !           207: PHP_FUNCTION( datefmt_get_error_message )
        !           208: {
        !           209:        char*                    message = NULL;
        !           210:        DATE_FORMAT_METHOD_INIT_VARS;
        !           211: 
        !           212:        /* Parse parameters. */
        !           213:        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
        !           214:                &object, IntlDateFormatter_ce_ptr ) == FAILURE )
        !           215:        {
        !           216:                intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
        !           217:                        "datefmt_get_error_message: unable to parse input params", 0 TSRMLS_CC );
        !           218: 
        !           219:                RETURN_FALSE;
        !           220:        }
        !           221: 
        !           222:        dfo = (IntlDateFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
        !           223: 
        !           224:        /* Return last error message. */
        !           225:        message = intl_error_get_message( INTL_DATA_ERROR_P(dfo) TSRMLS_CC );
        !           226:        RETURN_STRING( message, 0);
        !           227: }
        !           228: /* }}} */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>