]> granicus.if.org Git - php/commitdiff
Added feature request #9173 (added stream_get_line(), this function will
authorIlia Alshanetsky <iliaa@php.net>
Sun, 9 Feb 2003 20:43:05 +0000 (20:43 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Sun, 9 Feb 2003 20:43:05 +0000 (20:43 +0000)
read either the specified number of bytes or until the ending string is
found).

ext/standard/basic_functions.c
ext/standard/file.c
ext/standard/file.h
main/php_streams.h
main/streams.c

index dd743bd79d82b04c6ea0e13e88b1c3e1110d508b..5c2f1880a457bfc88aa61e0aadd8427bcd4304dc 100644 (file)
@@ -681,6 +681,7 @@ function_entry basic_functions[] = {
        PHP_FALIAS(socket_set_blocking, stream_set_blocking,                                    NULL)
 
        PHP_FE(stream_get_meta_data,                                                                                    NULL)
+       PHP_FE(stream_get_line,                                                                                         NULL)
        PHP_FE(stream_register_wrapper,                                                                                 NULL)
        PHP_FE(stream_get_wrappers,                                                                                             NULL)
        PHP_FE(get_headers,                                                                                                     NULL)
index b369ed179ea5c126b9434482ef508a0ceb60ccd0..af486081b612278ef5ab72f98ce960e7ba257fd3 100644 (file)
@@ -1128,6 +1128,37 @@ PHP_FUNCTION(stream_filter_append)
        apply_filter_to_stream(1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
 }
 /* }}} */
+
+/* {{{ proto string stream_get_line(resource stream, int maxlen, string ending)
+   Read up to maxlen bytes from a stream or until the ending string is found */
+PHP_FUNCTION(stream_get_line)
+{
+       char *str;
+       int str_len;
+       long max_length;
+       zval *zstream;
+       char *buf;
+       size_t buf_size;
+       php_stream *stream;
+       
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rls", &zstream, &max_length, &str, &str_len) == FAILURE) {
+               RETURN_FALSE;
+       }
+
+       if (max_length < 0) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "The maximum allowed length must be greater then or equal to zero.");
+               RETURN_FALSE;
+       }
+
+       php_stream_from_zval(stream, &zstream);
+
+       if ((buf = php_stream_get_record(stream, max_length, &buf_size, str, str_len TSRMLS_CC))) {
+               RETURN_STRINGL(buf, buf_size, 0);
+       } else {
+               RETURN_FALSE;
+       }
+}
+
 /* }}} */
 
 /* {{{ proto resource fopen(string filename, string mode [, bool use_include_path [, resource context]])
index 28fca3b304e60ed9dedc36d5ec9f30ac1ecba076..ae379cb6a1194aed524a0e7729f0380712a47a68 100644 (file)
@@ -60,6 +60,7 @@ PHP_FUNCTION(stream_select);
 PHP_FUNCTION(stream_set_timeout);
 PHP_FUNCTION(stream_set_write_buffer);
 PHP_FUNCTION(stream_get_wrappers);
+PHP_FUNCTION(stream_get_line);
 PHP_FUNCTION(get_meta_tags);
 PHP_FUNCTION(flock);
 PHP_FUNCTION(fd_set);
index ad4ab3be5a6445c0a3962c50f2a093553f6c3f2d..0fbb867c06d90a6581a1b7f0773ea3d698394cd1 100755 (executable)
@@ -290,6 +290,7 @@ PHPAPI void php_stream_filter_append(php_stream *stream, php_stream_filter *filt
 PHPAPI php_stream_filter *php_stream_filter_remove(php_stream *stream, php_stream_filter *filter, int call_dtor TSRMLS_DC);
 PHPAPI void php_stream_filter_free(php_stream_filter *filter TSRMLS_DC);
 PHPAPI php_stream_filter *_php_stream_filter_alloc(php_stream_filter_ops *fops, void *abstract, int persistent STREAMS_DC TSRMLS_DC);
+PHPAPI char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t *returned_len, char *delim, size_t delim_len TSRMLS_DC);
 #define php_stream_filter_alloc(fops, thisptr, persistent) _php_stream_filter_alloc((fops), (thisptr), (persistent) STREAMS_CC TSRMLS_CC)
 #define php_stream_filter_alloc_rel(fops, thisptr, persistent) _php_stream_filter_alloc((fops), (thisptr), (persistent) STREAMS_REL_CC TSRMLS_CC)
 
index 5ed18c6d1106b318ced4e2e15e58a3af50250ac3..3d67b4a3f294a1c316c5236c2b8723ebfde5d353 100755 (executable)
@@ -29,6 +29,7 @@
 #include "php_open_temporary_file.h"
 #include "ext/standard/file.h"
 #include "ext/standard/basic_functions.h" /* for BG(mmap_file) (not strictly required) */
+#include "ext/standard/php_string.h" /* for php_memnstr, used by php_stream_get_record() */
 #ifdef HAVE_SYS_MMAN_H
 #include <sys/mman.h>
 #endif
@@ -2609,6 +2610,40 @@ PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash()
        return &url_stream_wrappers_hash;
 }
 
+PHPAPI char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t *returned_len, char *delim, size_t delim_len TSRMLS_DC)
+{
+       char *e, *buf;
+       size_t toread;
+
+       php_stream_fill_read_buffer(stream, maxlen TSRMLS_CC);
+
+       if (delim_len == 0) {
+               toread = maxlen;
+       } else {
+               if (delim_len == 1) {
+                       e = memchr(stream->readbuf, *delim, stream->readbuflen);
+               } else {
+                       e = php_memnstr(stream->readbuf, delim, delim_len, (stream->readbuf + stream->readbuflen));
+               }
+
+               if (!e) {
+                       toread = maxlen;
+               } else {
+                       toread = e - (char *) stream->readbuf;
+               }
+       }
+
+       buf = emalloc(toread + 1);
+       *returned_len = php_stream_read(stream, buf, toread);
+                       
+       if (*returned_len >= 0) {
+               return buf;
+       } else {
+               efree(buf);
+               return NULL;
+       }
+}
+
 /*
  * Local variables:
  * tab-width: 4