pollita Fri Sep 22 19:54:30 2006 UTC Modified files: /php-src/main php_streams.h /php-src/ext/standard streamsfuncs.c /php-src/main/streams streams.c Log: Update stream_copy_to_stream() for PHP6 http://cvs.php.net/viewvc.cgi/php-src/main/php_streams.h?r1=1.113&r2=1.114&diff_format=u Index: php-src/main/php_streams.h diff -u php-src/main/php_streams.h:1.113 php-src/main/php_streams.h:1.114 --- php-src/main/php_streams.h:1.113 Fri Apr 28 19:03:58 2006 +++ php-src/main/php_streams.h Fri Sep 22 19:54:30 2006 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_streams.h,v 1.113 2006/04/28 19:03:58 fmk Exp $ */ +/* $Id: php_streams.h,v 1.114 2006/09/22 19:54:30 pollita Exp $ */ #ifndef PHP_STREAMS_H #define PHP_STREAMS_H @@ -456,9 +456,12 @@ #define PHP_STREAM_COPY_ALL ((size_t)-1) BEGIN_EXTERN_C() +PHPAPI size_t _php_stream_ucopy_to_stream(php_stream *src, php_stream *dest, size_t maxlen, int maxchars STREAMS_DC TSRMLS_DC); PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC); -#define php_stream_copy_to_stream(src, dest, maxlen) _php_stream_copy_to_stream((src), (dest), (maxlen) STREAMS_CC TSRMLS_CC) - +/* Preserve "characters" semantics by having maxlen refer to maxchars in a unicode context */ +#define php_stream_copy_to_stream(src, dest, maxlen) ( ((src)->readbuf_type == IS_STRING) \ + ? _php_stream_copy_to_stream((src), (dest), (maxlen) STREAMS_CC TSRMLS_CC) \ + : _php_stream_ucopy_to_stream((src), (dest), -1, (maxlen) STREAMS_CC TSRMLS_CC) ) /* read all data from stream and put into a buffer. Caller must free buffer when done. * The copy will use mmap if available. */ http://cvs.php.net/viewvc.cgi/php-src/ext/standard/streamsfuncs.c?r1=1.88&r2=1.89&diff_format=u Index: php-src/ext/standard/streamsfuncs.c diff -u php-src/ext/standard/streamsfuncs.c:1.88 php-src/ext/standard/streamsfuncs.c:1.89 --- php-src/ext/standard/streamsfuncs.c:1.88 Fri Sep 22 18:42:33 2006 +++ php-src/ext/standard/streamsfuncs.c Fri Sep 22 19:54:30 2006 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: streamsfuncs.c,v 1.88 2006/09/22 18:42:33 pollita Exp $ */ +/* $Id: streamsfuncs.c,v 1.89 2006/09/22 19:54:30 pollita Exp $ */ #include "php.h" #include "php_globals.h" @@ -443,7 +443,7 @@ } /* }}} */ -/* {{{ proto long stream_copy_to_stream(resource source, resource dest [, long maxlen [, long pos]]) +/* {{{ proto long stream_copy_to_stream(resource source, resource dest [, long maxlen [, long pos]]) U Reads up to maxlen bytes from source stream and writes them to dest stream. */ PHP_FUNCTION(stream_copy_to_stream) { http://cvs.php.net/viewvc.cgi/php-src/main/streams/streams.c?r1=1.135&r2=1.136&diff_format=u Index: php-src/main/streams/streams.c diff -u php-src/main/streams/streams.c:1.135 php-src/main/streams/streams.c:1.136 --- php-src/main/streams/streams.c:1.135 Tue Sep 19 20:36:48 2006 +++ php-src/main/streams/streams.c Fri Sep 22 19:54:30 2006 @@ -19,7 +19,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: streams.c,v 1.135 2006/09/19 20:36:48 pollita Exp $ */ +/* $Id: streams.c,v 1.136 2006/09/22 19:54:30 pollita Exp $ */ #define _GNU_SOURCE #include "php.h" @@ -1568,7 +1568,7 @@ ucnv_resetFromUnicode(conv); - while ((b = php_stream_read_unicode(stream, inbuf_start, sizeof(inbuf_start))) > 0) { + while ((b = php_stream_read_unicode(stream, inbuf_start, 8192)) > 0) { char *outbuf = outbuf_start; const UChar *inbuf = inbuf_start; UErrorCode status = U_ZERO_ERROR; @@ -1733,14 +1733,97 @@ return len; } +/* Designed for copying UChars (taking into account both maxlen and maxchars) */ +PHPAPI size_t _php_stream_ucopy_to_stream(php_stream *src, php_stream *dest, size_t maxlen, int maxchars STREAMS_DC TSRMLS_DC) +{ + size_t haveread = 0; + php_stream_statbuf ssbuf; + + if (src->readbuf_type == IS_STRING) { + /* Called incorrectly, don't do that. */ + return _php_stream_copy_to_stream(src, dest, maxlen STREAMS_CC TSRMLS_CC); + } + + if (maxlen == 0 || maxchars == 0) { + return 0; + } + + if (maxlen == PHP_STREAM_COPY_ALL) { + maxlen = 0; + } + + if (php_stream_stat(src, &ssbuf) == 0) { + /* in the event that the source file is 0 bytes, return 1 to indicate success + * because opening the file to write had already created a copy */ + if (ssbuf.sb.st_size == 0 +#ifdef S_ISFIFO + && !S_ISFIFO(ssbuf.sb.st_mode) +#endif +#ifdef S_ISCHR + && !S_ISCHR(ssbuf.sb.st_mode) +#endif + ) { + return 1; + } + } + + while(1) { + UChar buf[CHUNK_SIZE]; + size_t readchunk = CHUNK_SIZE; + size_t didread; + + if (maxlen && (maxlen - haveread) < readchunk) { + readchunk = maxlen - haveread; + } + + didread = php_stream_read_unicode_ex(src, buf, readchunk, maxchars); + + if (didread) { + /* extra paranoid */ + size_t didwrite, towrite; + UChar *writeptr; + + if (maxchars > 0) { + /* Determine number of chars in this buf */ + maxchars -= u_countChar32(buf, didread); + } + + towrite = didread; + writeptr = buf; + haveread += didread; + + while(towrite) { + didwrite = php_stream_write_unicode(dest, writeptr, towrite); + if (didwrite == 0) { + return 0; /* error */ + } + + towrite -= didwrite; + writeptr += didwrite; + } + } else { + return haveread; + } + + if (maxchars == 0 || maxlen - haveread == 0) { + break; + } + } + + return haveread; +} + +/* Optimized for copying octets from source stream */ PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC) { - char buf[CHUNK_SIZE]; - size_t readchunk; size_t haveread = 0; - size_t didread; php_stream_statbuf ssbuf; + if (src->readbuf_type == IS_UNICODE) { + /* Called incorrectly, don't do that. */ + return _php_stream_ucopy_to_stream(src, dest, maxlen, -1 STREAMS_CC TSRMLS_CC); + } + if (maxlen == 0) { return 0; } @@ -1771,7 +1854,7 @@ p = php_stream_mmap_range(src, php_stream_tell(src), maxlen, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped); if (p) { - haveread = php_stream_write(dest, p, mapped); + mapped = php_stream_write(dest, p, mapped); php_stream_mmap_unmap(src); @@ -1780,10 +1863,13 @@ } while(1) { - readchunk = sizeof(buf); + char buf[CHUNK_SIZE]; + size_t readchunk = sizeof(buf); + size_t didread; - if (maxlen && (maxlen - haveread) < readchunk) + if (maxlen && (maxlen - haveread) < readchunk) { readchunk = maxlen - haveread; + } didread = php_stream_read(src, buf, readchunk); @@ -1813,8 +1899,8 @@ break; } } - return haveread; + return haveread; } /* }}} */
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php