pollita         Fri Mar 24 19:22:24 2006 UTC

  Modified files:              
    /php-src/ext/standard       streamsfuncs.c 
    /php-src/main       php_streams.h 
    /php-src/main/streams       streams.c 
  Log:
  Add php_stream_get_record_unicde() API call.
  Update stream_get_line() userspace function to handle unicode streams.
  
  
http://cvs.php.net/viewcvs.cgi/php-src/ext/standard/streamsfuncs.c?r1=1.68&r2=1.69&diff_format=u
Index: php-src/ext/standard/streamsfuncs.c
diff -u php-src/ext/standard/streamsfuncs.c:1.68 
php-src/ext/standard/streamsfuncs.c:1.69
--- php-src/ext/standard/streamsfuncs.c:1.68    Mon Mar 13 04:40:11 2006
+++ php-src/ext/standard/streamsfuncs.c Fri Mar 24 19:22:24 2006
@@ -17,7 +17,7 @@
   +----------------------------------------------------------------------+
 */
 
-/* $Id: streamsfuncs.c,v 1.68 2006/03/13 04:40:11 pollita Exp $ */
+/* $Id: streamsfuncs.c,v 1.69 2006/03/24 19:22:24 pollita Exp $ */
 
 #include "php.h"
 #include "php_globals.h"
@@ -1235,18 +1235,15 @@
 
 /* {{{ 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 */
-/* UTODO */
 PHP_FUNCTION(stream_get_line)
 {
-       char *str = NULL;
-       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, "rl|s", &zstream, 
&max_length, &str, &str_len) == FAILURE) {
+       zval **delim = NULL;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|Z", &zstream, 
&max_length, &delim) == FAILURE) {
                RETURN_FALSE;
        }
 
@@ -1260,10 +1257,34 @@
 
        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);
+       if (php_stream_reads_unicode(stream)) {
+               UChar *buf;
+
+               if (Z_TYPE_PP(delim) != IS_UNICODE) {
+                       convert_to_unicode_ex(delim);
+               }
+
+               /* maxchars == maxlength will prevent the otherwise generous 
maxlen == max_length * 2
+                  from allocating beyond what's requested */
+               buf = php_stream_get_record_unicode(stream, max_length * 2, 
max_length, &buf_size, Z_USTRVAL_PP(delim), Z_USTRLEN_PP(delim) TSRMLS_CC);
+               if (!buf) {
+                       RETURN_FALSE;
+               }
+
+               RETURN_UNICODEL(buf, buf_size, 0);
        } else {
-               RETURN_FALSE;
+               char *buf;
+
+               if (Z_TYPE_PP(delim) != IS_STRING) {
+                       convert_to_string_ex(delim);
+               }
+
+               buf = php_stream_get_record(stream, max_length, &buf_size, 
Z_STRVAL_PP(delim), Z_STRLEN_PP(delim) TSRMLS_CC);
+               if (!buf) {
+                       RETURN_FALSE;
+               }
+
+               RETURN_STRINGL(buf, buf_size, 0);
        }
 }
 
http://cvs.php.net/viewcvs.cgi/php-src/main/php_streams.h?r1=1.107&r2=1.108&diff_format=u
Index: php-src/main/php_streams.h
diff -u php-src/main/php_streams.h:1.107 php-src/main/php_streams.h:1.108
--- php-src/main/php_streams.h:1.107    Tue Mar 14 21:15:05 2006
+++ php-src/main/php_streams.h  Fri Mar 24 19:22:24 2006
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_streams.h,v 1.107 2006/03/14 21:15:05 pollita Exp $ */
+/* $Id: php_streams.h,v 1.108 2006/03/24 19:22:24 pollita Exp $ */
 
 #ifndef PHP_STREAMS_H
 #define PHP_STREAMS_H
@@ -329,6 +329,8 @@
                                                                                
                                        _php_stream_get_line((stream), 
(buf_type), ZSTR(buf), (maxlen), (maxchars), NULL TSRMLS_CC)
 
 PHPAPI char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t 
*returned_len, char *delim, size_t delim_len TSRMLS_DC);
+PHPAPI UChar *php_stream_get_record_unicode(php_stream *stream, size_t maxlen, 
size_t maxchars, size_t *returned_len, UChar *delim, size_t delim_len 
TSRMLS_DC);
+
 
 PHPAPI UChar *_php_stream_u_get_line(php_stream *stream, UChar *buf, int32_t 
*pmax_bytes, int32_t *pmax_chars, int *pis_unicode TSRMLS_DC);
 #define php_stream_u_get_line(stream, buf, maxlen_buf, maxlen_chars, buf_type) 
_php_stream_u_get_line((stream), (buf), (maxlen_buf), (maxlen_chars), 
(buf_type) TSRMLS_CC)
http://cvs.php.net/viewcvs.cgi/php-src/main/streams/streams.c?r1=1.109&r2=1.110&diff_format=u
Index: php-src/main/streams/streams.c
diff -u php-src/main/streams/streams.c:1.109 
php-src/main/streams/streams.c:1.110
--- php-src/main/streams/streams.c:1.109        Fri Mar 24 00:19:39 2006
+++ php-src/main/streams/streams.c      Fri Mar 24 19:22:24 2006
@@ -19,7 +19,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: streams.c,v 1.109 2006/03/24 00:19:39 pollita Exp $ */
+/* $Id: streams.c,v 1.110 2006/03/24 19:22:24 pollita Exp $ */
 
 #define _GNU_SOURCE
 #include "php.h"
@@ -1171,6 +1171,73 @@
        }
 }
 
+PHPAPI UChar *php_stream_get_record_unicode(php_stream *stream, size_t maxlen, 
size_t maxchars, size_t *returned_len, UChar *delim, size_t delim_len TSRMLS_DC)
+{
+       UChar *e, *buf;
+       size_t toread;
+       int skip = 0;
+
+       if (!php_stream_reads_unicode(stream)) {
+               return NULL;
+       }
+
+       php_stream_fill_read_buffer(stream, maxlen TSRMLS_CC);
+
+       if (delim_len == 0 || !delim) {
+               toread = maxlen;
+       } else {
+               if (delim_len == 1) {
+                       e = u_memchr(stream->readbuf.u + stream->readpos, 
*delim, stream->writepos - stream->readpos);
+               } else {
+                       e = u_strFindFirst(stream->readbuf.u + stream->readpos, 
stream->writepos - stream->readpos, delim, delim_len);
+               }
+
+               if (!e) {
+                       toread = maxlen;
+               } else {
+                       toread = e - (stream->readbuf.u + stream->readpos);
+                       skip = 1;
+               }
+       }
+
+       if (toread > maxlen && maxlen > 0) {
+               toread = maxlen;
+       }
+
+       if (U16_IS_SURROGATE(stream->readbuf.u[stream->readpos + toread - 1]) &&
+               U16_IS_SURROGATE_LEAD(stream->readbuf.u[stream->readpos + 
toread - 1])) {
+               /* Don't orphan */
+               toread--;
+       }
+
+       if (maxchars > 0) {
+               size_t ulen = u_countChar32(stream->readbuf.u + 
stream->readpos, toread);
+
+               if (maxchars > ulen) {
+                       int i = 0;
+                       UChar *s = stream->readbuf.u + stream->readpos;
+
+                       U16_FWD_N(s, i, toread, maxchars);
+                       toread = i;
+               }
+       }
+
+       buf = eumalloc(toread + 1);
+       *returned_len = php_stream_read_unicode(stream, buf, toread);
+
+       if (*returned_len >= 0) {
+               if (skip) {
+                       stream->readpos += delim_len;
+                       stream->position += delim_len;
+               }
+               buf[*returned_len] = 0;
+               return buf;
+       } else {
+               efree(buf);
+               return NULL;
+       }
+}
+
 /* Writes a buffer directly to a stream, using multiple of the chunk size */
 static size_t _php_stream_write_buffer(php_stream *stream, int buf_type, zstr 
buf, int buflen TSRMLS_DC)
 {

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

Reply via email to