diff options
author | Ilia Alshanetsky <iliaa@php.net> | 2003-02-09 20:43:05 +0000 |
---|---|---|
committer | Ilia Alshanetsky <iliaa@php.net> | 2003-02-09 20:43:05 +0000 |
commit | f98ea4c46d06ab206ee93b53fb48744fa3888bc9 (patch) | |
tree | 7492b3c578cc288fcce4ad76c837693be1197eff /main/streams.c | |
parent | 8b5bc3ecd3c6049ad930012fe6a4cbe1f02182b7 (diff) | |
download | php-git-f98ea4c46d06ab206ee93b53fb48744fa3888bc9.tar.gz |
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).
Diffstat (limited to 'main/streams.c')
-rwxr-xr-x | main/streams.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/main/streams.c b/main/streams.c index 5ed18c6d11..3d67b4a3f2 100755 --- a/main/streams.c +++ b/main/streams.c @@ -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 |