iliaa           Sun Feb  9 15:43:06 2003 EDT

  Modified files:              
    /php4/main  streams.c php_streams.h 
    /php4/ext/standard  file.c file.h basic_functions.c 
  Log:
  Added feature request #9173 (added stream_get_line(), this function will 
  read either the specified number of bytes or until the ending string is 
  found).
  
  
Index: php4/main/streams.c
diff -u php4/main/streams.c:1.144 php4/main/streams.c:1.145
--- php4/main/streams.c:1.144   Fri Feb  7 16:33:35 2003
+++ php4/main/streams.c Sun Feb  9 15:43:05 2003
@@ -20,7 +20,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: streams.c,v 1.144 2003/02/07 21:33:35 iliaa Exp $ */
+/* $Id: streams.c,v 1.145 2003/02/09 20:43:05 iliaa Exp $ */
 
 #define _GNU_SOURCE
 #include "php.h"
@@ -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
@@ -2607,6 +2608,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;
+       }
 }
 
 /*
Index: php4/main/php_streams.h
diff -u php4/main/php_streams.h:1.67 php4/main/php_streams.h:1.68
--- php4/main/php_streams.h:1.67        Fri Feb  7 17:49:21 2003
+++ php4/main/php_streams.h     Sun Feb  9 15:43:05 2003
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_streams.h,v 1.67 2003/02/07 22:49:21 iliaa Exp $ */
+/* $Id: php_streams.h,v 1.68 2003/02/09 20:43:05 iliaa Exp $ */
 
 #ifndef PHP_STREAMS_H
 #define PHP_STREAMS_H
@@ -290,6 +290,7 @@
 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: php4/ext/standard/file.c
diff -u php4/ext/standard/file.c:1.298 php4/ext/standard/file.c:1.299
--- php4/ext/standard/file.c:1.298      Sun Feb  9 15:35:54 2003
+++ php4/ext/standard/file.c    Sun Feb  9 15:43:05 2003
@@ -21,7 +21,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: file.c,v 1.298 2003/02/09 20:35:54 iliaa Exp $ */
+/* $Id: file.c,v 1.299 2003/02/09 20:43:05 iliaa Exp $ */
 
 /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */
 
@@ -1128,6 +1128,37 @@
        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: php4/ext/standard/file.h
diff -u php4/ext/standard/file.h:1.73 php4/ext/standard/file.h:1.74
--- php4/ext/standard/file.h:1.73       Fri Jan  3 03:02:36 2003
+++ php4/ext/standard/file.h    Sun Feb  9 15:43:05 2003
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: file.h,v 1.73 2003/01/03 08:02:36 pollita Exp $ */
+/* $Id: file.h,v 1.74 2003/02/09 20:43:05 iliaa Exp $ */
 
 /* Synced with php 3.0 revision 1.30 1999-06-16 [ssb] */
 
@@ -60,6 +60,7 @@
 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: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.575 
php4/ext/standard/basic_functions.c:1.576
--- php4/ext/standard/basic_functions.c:1.575   Sun Feb  9 14:09:50 2003
+++ php4/ext/standard/basic_functions.c Sun Feb  9 15:43:05 2003
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: basic_functions.c,v 1.575 2003/02/09 19:09:50 sniper Exp $ */
+/* $Id: basic_functions.c,v 1.576 2003/02/09 20:43:05 iliaa Exp $ */
 
 #include "php.h"
 #include "php_streams.h"
@@ -681,6 +681,7 @@
        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)



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to