pollita Mon Feb 17 21:53:24 2003 EDT
Modified files:
/php4/main/streams php_stream_filter_api.h
/php4/ext/standard file.c
Log:
Fix stream_filter_(ap|pre)pend to allow attaching on the read and/or write chains.
Automagically decide what to do if noone tells us.
Index: php4/main/streams/php_stream_filter_api.h
diff -u php4/main/streams/php_stream_filter_api.h:1.1
php4/main/streams/php_stream_filter_api.h:1.2
--- php4/main/streams/php_stream_filter_api.h:1.1 Mon Feb 17 20:22:21 2003
+++ php4/main/streams/php_stream_filter_api.h Mon Feb 17 21:53:23 2003
@@ -19,7 +19,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_stream_filter_api.h,v 1.1 2003/02/18 01:22:21 wez Exp $ */
+/* $Id: php_stream_filter_api.h,v 1.2 2003/02/18 02:53:23 pollita Exp $ */
/* The filter API works on the principle of "Bucket-Brigades". This is
* partially inspired by the Apache 2 method of doing things, although
@@ -33,6 +33,10 @@
* The first filter in the chain is invoked on the brigade and (depending on
* it's return value), the next filter is invoked and so on.
* */
+
+#define PHP_STREAM_FILTER_READ 0x0001
+#define PHP_STREAM_FILTER_WRITE 0x0002
+#define PHP_STREAM_FILTER_ALL (PHP_STREAM_FILTER_READ | PHP_STREAM_FILTER_WRITE)
typedef struct _php_stream_bucket php_stream_bucket;
typedef struct _php_stream_bucket_brigade php_stream_bucket_brigade;
Index: php4/ext/standard/file.c
diff -u php4/ext/standard/file.c:1.305 php4/ext/standard/file.c:1.306
--- php4/ext/standard/file.c:1.305 Mon Feb 17 20:22:21 2003
+++ php4/ext/standard/file.c Mon Feb 17 21:53:24 2003
@@ -21,7 +21,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: file.c,v 1.305 2003/02/18 01:22:21 wez Exp $ */
+/* $Id: file.c,v 1.306 2003/02/18 02:53:24 pollita Exp $ */
/* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */
@@ -179,6 +179,10 @@
REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_WARN",
PHP_STREAM_NOTIFY_SEVERITY_WARN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("STREAM_NOTIFY_SEVERITY_ERR",
PHP_STREAM_NOTIFY_SEVERITY_ERR, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("STREAM_FILTER_READ",
+PHP_STREAM_FILTER_READ, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("STREAM_FILTER_WRITE",
+PHP_STREAM_FILTER_WRITE, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("STREAM_FILTER_ALL",
+PHP_STREAM_FILTER_ALL, CONST_CS | CONST_PERSISTENT);
+
REGISTER_LONG_CONSTANT("FILE_USE_INCLUDE_PATH", PHP_FILE_USE_INCLUDE_PATH,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("FILE_IGNORE_NEW_LINES", PHP_FILE_IGNORE_NEW_LINES,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("FILE_SKIP_EMPTY_LINES", PHP_FILE_SKIP_EMPTY_LINES,
CONST_CS | CONST_PERSISTENT);
@@ -1252,31 +1256,60 @@
zval *zstream;
php_stream *stream;
char *filtername, *filterparams = NULL;
- int filternamelen, filterparamslen = 0;
+ int filternamelen, filterparamslen = 0, read_write = 0;
php_stream_filter *filter;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|s", &zstream,
- &filtername, &filternamelen, &filterparams,
&filterparamslen) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|ls", &zstream,
+ &filtername, &filternamelen, &read_write,
+&filterparams, &filterparamslen) == FAILURE) {
RETURN_FALSE;
}
php_stream_from_zval(stream, &zstream);
-
- filter = php_stream_filter_create(filtername, filterparams, filterparamslen,
php_stream_is_persistent(stream) TSRMLS_CC);
- if (filter == NULL) {
- RETURN_FALSE;
+
+ if ((read_write & PHP_STREAM_FILTER_ALL) == 0) {
+ /* Chain not specified.
+ * Examine stream->mode to determine which filters are needed
+ * There's no harm in attaching a filter to an unused chain,
+ * but why waste the memory and clock cycles?
+ */
+ if (strchr(stream->mode, 'r') || strchr(stream->mode, '+')) {
+ read_write |= PHP_STREAM_FILTER_READ;
+ }
+ if (strchr(stream->mode, 'w') || strchr(stream->mode, '+') ||
+strchr(stream->mode, 'a')) {
+ read_write |= PHP_STREAM_FILTER_WRITE;
+ }
}
- if (append) {
- php_stream_filter_append(&stream->readfilters, filter);
- } else {
- php_stream_filter_prepend(&stream->readfilters, filter);
+ if (read_write & PHP_STREAM_FILTER_READ) {
+ filter = php_stream_filter_create(filtername, filterparams,
+filterparamslen, php_stream_is_persistent(stream) TSRMLS_CC);
+ if (filter == NULL) {
+ RETURN_FALSE;
+ }
+
+ if (append) {
+ php_stream_filter_append(&stream->readfilters, filter);
+ } else {
+ php_stream_filter_prepend(&stream->readfilters, filter);
+ }
+ }
+
+ if (read_write & PHP_STREAM_FILTER_WRITE) {
+ filter = php_stream_filter_create(filtername, filterparams,
+filterparamslen, php_stream_is_persistent(stream) TSRMLS_CC);
+ if (filter == NULL) {
+ RETURN_FALSE;
+ }
+
+ if (append) {
+ php_stream_filter_append(&stream->writefilters, filter);
+ } else {
+ php_stream_filter_prepend(&stream->writefilters, filter);
+ }
}
RETURN_TRUE;
}
-/* {{{ proto bool stream_filter_prepend(resource stream, string filtername[, string
filterparams])
+/* {{{ proto bool stream_filter_prepend(resource stream, string filtername[, int
+read_write[, string filterparams]])
Prepend a filter to a stream */
PHP_FUNCTION(stream_filter_prepend)
{
@@ -1284,7 +1317,7 @@
}
/* }}} */
-/* {{{ proto bool stream_filter_append(resource stream, string filtername[, string
filterparams])
+/* {{{ proto bool stream_filter_append(resource stream, string filtername[, int
+read_write[, string filterparams]])
Append a filter to a stream */
PHP_FUNCTION(stream_filter_append)
{
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php