From: Sara Golemon Date: Tue, 19 Sep 2006 20:36:48 +0000 (+0000) Subject: Add INI controlled default stream encoding (unicode.stream_encoding). X-Git-Tag: RELEASE_1_0_0RC1~1619 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3f56da6ebae0b5ec521ed09276cbebbc2ddb3f05;p=php Add INI controlled default stream encoding (unicode.stream_encoding). Add convenience function stream_defualt_encoding() for setting it. --- diff --git a/Zend/zend.c b/Zend/zend.c index 8bd09c6779..079bea13eb 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -181,6 +181,7 @@ ZEND_INI_BEGIN() STD_ZEND_INI_ENTRY("unicode.script_encoding", NULL, ZEND_INI_ALL, OnUpdateEncoding, script_encoding_conv, zend_unicode_globals, unicode_globals) STD_ZEND_INI_ENTRY("unicode.http_input_encoding", NULL, ZEND_INI_ALL, OnUpdateEncoding, http_input_encoding_conv, zend_unicode_globals, unicode_globals) STD_ZEND_INI_ENTRY("unicode.filesystem_encoding", NULL, ZEND_INI_ALL, OnUpdateEncoding, filesystem_encoding_conv, zend_unicode_globals, unicode_globals) + STD_ZEND_INI_ENTRY("unicode.stream_encoding", "utf8", ZEND_INI_ALL, OnUpdateStringUnempty, stream_encoding, zend_unicode_globals, unicode_globals) ZEND_INI_END() diff --git a/Zend/zend_globals.h b/Zend/zend_globals.h index 2b8ca89d8e..710e52f3b6 100644 --- a/Zend/zend_globals.h +++ b/Zend/zend_globals.h @@ -276,6 +276,10 @@ struct _zend_unicode_globals { UConverter *utf8_conv; /* all-purpose UTF-8 converter */ UConverter *ascii_conv; /* all-purpose ASCII converter */ + char *stream_encoding; /* default stream encoding (contents, not FS entries) + Uses name of encoding rather than a real converter + because each stream needs its own instance */ + uint16_t from_error_mode; UChar from_subst_char[3]; uint16_t to_error_mode; diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c index 9b08f9c058..3dcac49748 100644 --- a/ext/standard/streamsfuncs.c +++ b/ext/standard/streamsfuncs.c @@ -1458,6 +1458,24 @@ PHP_FUNCTION(stream_socket_enable_crypto) } /* }}} */ + +/* {{{ proto bool stream_default_encoding(string encoding) U +Convenience wrapper for ini_set('unicode.stream_encoding', $encoding) */ +PHP_FUNCTION(stream_default_encoding) +{ + char *encoding; + int encoding_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoding, &encoding_len) == FAILURE) { + return; + } + + RETURN_BOOL(SUCCESS == zend_alter_ini_entry("unicode.stream_encoding", sizeof("unicode.stream_encoding"), + encoding, encoding_len, PHP_INI_ALL, PHP_INI_STAGE_RUNTIME)); +} +/* }}} */ + + /* {{{ proto void stream_encoding(resource stream[, string encoding]) Set character set for stream encoding UTODO: Return current encoding charset diff --git a/main/streams/streams.c b/main/streams/streams.c index 4176dcb850..2aa84d0ee1 100755 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -2306,7 +2306,7 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(char *path, char *mode, int optio /* Only apply implicit unicode.to. filter if the wrapper didn't do it for us */ if ((php_stream_filter_product(&stream->writefilters, IS_UNICODE) == IS_UNICODE) && (strchr(implicit_mode, 'w') || strchr(implicit_mode, 'a') || strchr(implicit_mode, '+'))) { - char *encoding = (context && context->output_encoding) ? context->output_encoding : "utf8"; + char *encoding = (context && context->output_encoding) ? context->output_encoding : UG(stream_encoding); /* UTODO: (Maybe?) Allow overriding the default error handlers on a per-stream basis via context params */ php_stream_encoding_apply(stream, 1, encoding, UG(from_error_mode), UG(from_subst_char)); @@ -2314,7 +2314,7 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(char *path, char *mode, int optio /* Only apply implicit unicode.from. filter if the wrapper didn't do it for us */ if ((stream->readbuf_type == IS_STRING) && (strchr(implicit_mode, 'r') || strchr(implicit_mode, '+'))) { - char *encoding = (context && context->input_encoding) ? context->input_encoding : "utf8"; + char *encoding = (context && context->input_encoding) ? context->input_encoding : UG(stream_encoding); /* UTODO: (Maybe?) Allow overriding the default error handlers on a per-stream basis via context params */ php_stream_encoding_apply(stream, 0, encoding, UG(to_error_mode), NULL);