mirror of
https://github.com/php/php-src.git
synced 2025-08-16 05:58:45 +02:00
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).
This commit is contained in:
parent
8b5bc3ecd3
commit
f98ea4c46d
5 changed files with 69 additions and 0 deletions
|
@ -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)
|
||||
|
|
|
@ -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]])
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue