]> granicus.if.org Git - php/commitdiff
Implement stream_get_contents, which is somewhat akin to file_get_contents,
authorWez Furlong <wez@php.net>
Wed, 8 Oct 2003 10:07:25 +0000 (10:07 +0000)
committerWez Furlong <wez@php.net>
Wed, 8 Oct 2003 10:07:25 +0000 (10:07 +0000)
except that it works on an already opened stream.

ext/standard/basic_functions.c
ext/standard/streamsfuncs.c
ext/standard/streamsfuncs.h

index 34e99c0d91b377cdb78ede30ca76e873525741ac..6a3ebbb82d57828e47c16ebbe869d4107c94849e 100644 (file)
@@ -715,6 +715,7 @@ function_entry basic_functions[] = {
        PHP_FE(stream_socket_accept,                                               third_arg_force_ref)
        PHP_FE(stream_socket_get_name,                                                                                  NULL)
        PHP_FE(stream_copy_to_stream,                                                                                   NULL)
+       PHP_FE(stream_get_contents,                                                                                             NULL)
        PHP_FE(fgetcsv,                                                                                                                 NULL)
        PHP_FE(flock,                                                                                                                   NULL)
        PHP_FE(get_meta_tags,                                                                                                   NULL)
index e6db9e8138c4fbb8148b044346ba4e3e31814b15..c0af60d8955efd4c0fe8a840b359d0f9e6e32065 100644 (file)
@@ -266,6 +266,38 @@ PHP_FUNCTION(stream_socket_get_name)
 }
 /* }}} */
 
+/* {{{ proto long stream_get_contents(resource source [, long maxlen ])
+   Reads all remaining bytes (or up to maxlen bytes) from a stream and returns them as a string. */
+PHP_FUNCTION(stream_get_contents)
+{
+       php_stream *stream;
+       zval *zsrc;
+       long maxlen = PHP_STREAM_COPY_ALL;
+       int len, newlen;
+       char *contents = NULL;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zsrc, &maxlen) == FAILURE) {
+               RETURN_FALSE;
+       }
+
+       php_stream_from_zval(stream, &zsrc);
+
+       if ((len = php_stream_copy_to_mem(stream, &contents, maxlen, 0)) > 0) {
+               
+               if (PG(magic_quotes_runtime)) {
+                       contents = php_addslashes(contents, len, &newlen, 1 TSRMLS_CC); /* 1 = free source string */
+                       len = newlen;
+               }
+
+               RETVAL_STRINGL(contents, len, 0);
+       } else if (len == 0) {
+               RETVAL_EMPTY_STRING();
+       } else {
+               RETVAL_FALSE;
+       }
+}
+/* }}} */
+
 /* {{{ proto long stream_copy_to_stream(resource source, resource dest [, long maxlen ])
    Reads up to maxlen bytes from source stream and writes them to dest stream. */
 PHP_FUNCTION(stream_copy_to_stream)
index bcd6870c94551a061127aadbf60c124bea2e85dd..8b308985e2000b86dd4e3049d70222d1d6568875 100644 (file)
@@ -28,6 +28,7 @@ PHP_FUNCTION(stream_socket_accept);
 PHP_FUNCTION(stream_socket_get_name);
 
 PHP_FUNCTION(stream_copy_to_stream);
+PHP_FUNCTION(stream_get_contents);
 
 PHP_FUNCTION(set_socket_blocking); /* deprecated */
 PHP_FUNCTION(stream_set_blocking);