-temp streams are now clean (Thanks to Wez)

This commit is contained in:
Marcus Boerger 2002-03-18 04:57:06 +00:00
parent 391fdf858e
commit f83c6cc0a9
2 changed files with 129 additions and 192 deletions

View File

@ -19,7 +19,16 @@
#define _GNU_SOURCE
#include "php.h"
#include "php_streams.h"
/* Memory streams use a dynamic memory buffer to emulate a stream.
* You can use php_stream_memory_open to create a readonly stream
* from an existing memory buffer.
*/
/* Temp streams are streams that uses memory streams as long their
* size is less than a given memory amount. When a write operation
* exceeds that limit the content is written to a temporary file.
*/
/* {{{ ------- MEMORY stream implementation -------*/
@ -28,47 +37,46 @@ typedef struct {
size_t fpos;
size_t fsize;
size_t smax;
php_stream *file;
int mode;
} php_stream_temp_data;
} php_stream_memory_data;
static size_t php_stream_memory_write(php_stream *stream, const char *buf, size_t count STREAMS_DC) { /* {{{ */
php_stream_temp_data *ms;
static size_t php_stream_memory_write(php_stream *stream, const char *buf, size_t count) { /* {{{ */
php_stream_memory_data *ms;
assert(stream != NULL);
ms = stream->abstract;
assert(ms != NULL);
if ( ms->mode & TEMP_STREAM_READONLY) {
if (ms->mode & TEMP_STREAM_READONLY) {
return 0;
}
if ( ms->fpos + count > ms->fsize) {
if (ms->fpos + count > ms->fsize) {
char *tmp;
if ( !ms->data) {
tmp = emalloc( ms->fpos + count);
if (!ms->data) {
tmp = emalloc(ms->fpos + count);
} else {
tmp = erealloc( ms->data, ms->fpos + count);
tmp = erealloc(ms->data, ms->fpos + count);
}
if ( !tmp) {
if (!tmp) {
count = ms->fsize - ms->fpos + 1;
} else {
ms->data = tmp;
ms->fsize = ms->fpos + count;
}
}
if ( !ms->data)
count = 0;
if ( count) {
if (!ms->data)
count = 0;
if (count) {
assert(buf!= NULL);
memcpy( ms->data+ms->fpos, (char*)buf, count);
memcpy(ms->data+ms->fpos, (char*)buf, count);
ms->fpos += count;
}
return count;
} /* }}} */
static size_t php_stream_memory_read(php_stream *stream, char *buf, size_t count STREAMS_DC) { /* {{{ */
php_stream_temp_data *ms;
static size_t php_stream_memory_read(php_stream *stream, char *buf, size_t count) { /* {{{ */
php_stream_memory_data *ms;
assert(stream != NULL);
ms = stream->abstract;
@ -82,21 +90,21 @@ static size_t php_stream_memory_read(php_stream *stream, char *buf, size_t count
return 0;
}
if ( ms->fpos + count > ms->fsize) {
if (ms->fpos + count > ms->fsize) {
count = ms->fsize - ms->fpos;
}
if ( count) {
assert(data!= NULL);
if (count) {
assert(ms->data!= NULL);
assert(buf!= NULL);
memcpy( buf, ms->data+ms->fpos, count);
memcpy(buf, ms->data+ms->fpos, count);
ms->fpos += count;
}
return count;
} /* }}} */
static int php_stream_memory_close(php_stream *stream, int close_handle STREAMS_DC) { /* {{{ */
php_stream_temp_data *ms;
static int php_stream_memory_close(php_stream *stream, int close_handle) { /* {{{ */
php_stream_memory_data *ms;
assert(stream != NULL);
ms = stream->abstract;
@ -112,14 +120,14 @@ static int php_stream_memory_close(php_stream *stream, int close_handle STREAMS_
} /* }}} */
static int php_stream_memory_flush(php_stream *stream STREAMS_DC) { /* {{{ */
// nothing to do here
static int php_stream_memory_flush(php_stream *stream) { /* {{{ */
/* nothing to do here */
return 0;
} /* }}} */
static int php_stream_memory_seek(php_stream *stream, off_t offset, int whence STREAMS_DC) { /* {{{ */
php_stream_temp_data *ms;
static int php_stream_memory_seek(php_stream *stream, off_t offset, int whence) { /* {{{ */
php_stream_memory_data *ms;
assert(stream != NULL);
ms = stream->abstract;
@ -127,17 +135,17 @@ static int php_stream_memory_seek(php_stream *stream, off_t offset, int whence S
if (offset == 0 && whence == SEEK_CUR)
return ms->fpos;
switch( whence) {
switch(whence) {
case SEEK_CUR:
if ( offset < 0) {
if ( ms->fpos < -offset) {
if (offset < 0) {
if (ms->fpos < -offset) {
ms->fpos = 0;
/*return EINVAL;*/
} else {
ms->fpos = ms->fpos + offset;
}
} else {
if ( ms->fpos < offset) {
if (ms->fpos < offset) {
ms->fpos = ms->fsize;
/*return EINVAL;*/
} else {
@ -146,7 +154,7 @@ static int php_stream_memory_seek(php_stream *stream, off_t offset, int whence S
}
return 0;
case SEEK_SET:
if ( offset > ms->fsize) {
if (offset > ms->fsize) {
ms->fpos = ms->fsize;
/*return EINVAL;*/
} else {
@ -154,10 +162,10 @@ static int php_stream_memory_seek(php_stream *stream, off_t offset, int whence S
}
return 0;
case SEEK_END:
if ( offset > 0) {
if (offset > 0) {
ms->fpos = ms->fsize;
/*return EINVAL;*/
} else if ( ms->fpos < -offset) {
} else if (ms->fpos < -offset) {
ms->fpos = 0;
/*return EINVAL;*/
} else {
@ -170,11 +178,11 @@ static int php_stream_memory_seek(php_stream *stream, off_t offset, int whence S
}
} /* }}} */
static char *php_stream_memory_gets(php_stream *stream, char *buf, size_t maxlen STREAMS_DC) { /* {{{ */
static char *php_stream_memory_gets(php_stream *stream, char *buf, size_t maxlen) { /* {{{ */
size_t n = 1;
char *c = buf;
php_stream_temp_data *ms;
php_stream_memory_data *ms;
assert(stream != NULL);
ms = stream->abstract;
@ -184,7 +192,7 @@ static char *php_stream_memory_gets(php_stream *stream, char *buf, size_t maxlen
while(n < maxlen && ms->fpos<ms->fsize) {
n++;
if ( (*c = ms->data[ms->fpos++]) == '\n') {
if ((*c = ms->data[ms->fpos++]) == '\n') {
c++;
break;
}
@ -194,7 +202,7 @@ static char *php_stream_memory_gets(php_stream *stream, char *buf, size_t maxlen
return buf;
} /* }}} */
static int php_stream_memory_cast(php_stream *stream, int castas, void **ret STREAMS_DC) { /* {{{ */
static int php_stream_memory_cast(php_stream *stream, int castas, void **ret) { /* {{{ */
return FAILURE;
} /* }}} */
@ -208,30 +216,26 @@ php_stream_ops php_stream_memory_ops = {
};
PHPAPI php_stream *_php_stream_memory_create(int mode STREAMS_DC) { /* {{{ */
php_stream_temp_data *self;
php_stream_memory_data *self;
self = emalloc(sizeof(*self));
assert( self != NULL);
assert(self != NULL);
self->data = NULL;
self->fpos = 0;
self->fsize = 0;
self->smax = -1;
self->mode = mode;
return _php_stream_alloc(&php_stream_memory_ops, self, 0, "rwb" STREAMS_REL_CC);
return php_stream_alloc(&php_stream_memory_ops, self, 0, "rwb");
} /* }}} */
/* }}} */
PHPAPI php_stream *_php_stream_memory_open(int mode, char *buf, size_t length STREAMS_DC) { /* {{{ */
php_stream *stream;
php_stream_temp_data *ms;
php_stream_memory_data *ms;
if ( (stream = _php_stream_memory_create(mode STREAMS_REL_CC)) != NULL) {
if ( length) {
ms = stream->abstract;
ms->data = emalloc(length);
assert( buf != NULL);
memcpy(ms->data,buf,length);
if ((stream = _php_stream_memory_create(TEMP_STREAM_DEFAULT STREAMS_REL_CC)) != NULL) {
if (length) {
assert(buf != NULL);
php_stream_write(stream, buf, length);
}
ms->mode = mode;
}
@ -239,7 +243,7 @@ PHPAPI php_stream *_php_stream_memory_open(int mode, char *buf, size_t length ST
} /* }}} */
PHPAPI char *_php_stream_memory_get_buffer(php_stream *stream, size_t *length STREAMS_DC) { /* {{{ */
php_stream_temp_data *ms;
php_stream_memory_data *ms;
assert(stream != NULL);
ms = stream->abstract;
@ -250,130 +254,98 @@ PHPAPI char *_php_stream_memory_get_buffer(php_stream *stream, size_t *length ST
return ms->data;
} /* }}} */
/* }}} */
/* {{{ ------- TEMP stream implementation -------*/
static size_t php_stream_temp_write(php_stream *stream, const char *buf, size_t count STREAMS_DC) { /* {{{ */
php_stream_temp_data *ms;
typedef struct {
php_stream *innerstream;
size_t smax;
int mode;
} php_stream_temp_data;
static size_t php_stream_temp_write(php_stream *stream, const char *buf, size_t count) { /* {{{ */
php_stream_temp_data *ts;
assert(stream != NULL);
ms = stream->abstract;
assert(ms != NULL);
ts = stream->abstract;
assert(ts != NULL);
if ( ms->mode & TEMP_STREAM_READONLY) {
return 0;
}
if ( !ms->file) {
size_t ret, fpos;
php_stream *file;
if (php_stream_is(ts->innerstream, PHP_STREAM_IS_MEMORY)) {
size_t memsize;
char *membuf = _php_stream_memory_get_buffer(ts->innerstream, &memsize STREAMS_CC);
ret = php_stream_memory_write(stream,buf,count STREAMS_REL_CC);
if ( ms->fsize > ms->smax) {
fpos = ms->fpos;
ms->fpos = 0;
file = php_stream_fopen_tmpfile();
php_stream_copy_to_stream(stream, file, PHP_STREAM_COPY_ALL STREAMS_REL_CC);
php_stream_seek(file,fpos,SEEK_SET STREAMS_REL_CC);
ms->file = file;
ms->fsize = 0;
if ( ms->data) {
efree(ms->data);
}
ms->data = NULL;
if (memsize + count >= ts->smax) {
php_stream *file = php_stream_fopen_tmpfile();
php_stream_write(file, membuf, memsize);
php_stream_close(ts->innerstream);
ts->innerstream = file;
}
return ret;
} else {
return php_stream_write(ms->file,buf,count STREAMS_REL_CC);
}
return php_stream_write(ts->innerstream, buf, count);
} /* }}} */
static size_t php_stream_temp_read(php_stream *stream, char *buf, size_t count STREAMS_DC) { /* {{{ */
php_stream_temp_data *ms;
static size_t php_stream_temp_read(php_stream *stream, char *buf, size_t count) { /* {{{ */
php_stream_temp_data *ts;
assert(stream != NULL);
ms = stream->abstract;
assert(ms != NULL);
ts = stream->abstract;
assert(ts != NULL);
if ( !ms->file) {
return php_stream_memory_read(stream,buf,count STREAMS_REL_CC);
} else {
return php_stream_read(ms->file,buf,count STREAMS_REL_CC);
}
return php_stream_read(ts->innerstream, buf, count);
} /* }}} */
static int php_stream_temp_close(php_stream *stream, int close_handle STREAMS_DC) { /* {{{ */
php_stream_temp_data *ms;
static int php_stream_temp_close(php_stream *stream, int close_handle) { /* {{{ */
php_stream_temp_data *ts;
assert(stream != NULL);
ms = stream->abstract;
assert(ms != NULL);
ts = stream->abstract;
assert(ts != NULL);
if ( !ms->file) {
return php_stream_memory_close(stream,close_handle STREAMS_REL_CC);
} else {
int ret = php_stream_close(ms->file STREAMS_REL_CC);
ms->file = NULL;
php_stream_memory_close(stream,close_handle STREAMS_REL_CC);
return ret;
}
return php_stream_close(ts->innerstream);
} /* }}} */
static int php_stream_temp_flush(php_stream *stream STREAMS_DC) { /* {{{ */
php_stream_temp_data *ms;
static int php_stream_temp_flush(php_stream *stream) { /* {{{ */
php_stream_temp_data *ts;
assert(stream != NULL);
ms = stream->abstract;
assert(ms != NULL);
ts = stream->abstract;
assert(ts != NULL);
if ( !ms->file) {
return php_stream_memory_flush(stream STREAMS_REL_CC);
} else {
return php_stream_flush(ms->file STREAMS_REL_CC);
}
return php_stream_flush(ts->innerstream);
} /* }}} */
static int php_stream_temp_seek(php_stream *stream, off_t offset, int whence STREAMS_DC) { /* {{{ */
php_stream_temp_data *ms;
static int php_stream_temp_seek(php_stream *stream, off_t offset, int whence) { /* {{{ */
php_stream_temp_data *ts;
assert(stream != NULL);
ms = stream->abstract;
assert(ms != NULL);
ts = stream->abstract;
assert(ts != NULL);
if ( !ms->file) {
return php_stream_memory_seek(stream,offset,whence STREAMS_REL_CC);
} else {
return php_stream_seek(ms->file,offset,whence STREAMS_REL_CC);
}
return php_stream_seek(ts->innerstream, offset, whence);
} /* }}} */
char *php_stream_temp_gets(php_stream *stream, char *buf, size_t maxlen STREAMS_DC) { /* {{{ */
php_stream_temp_data *ms;
char *php_stream_temp_gets(php_stream *stream, char *buf, size_t maxlen) { /* {{{ */
php_stream_temp_data *ts;
assert(stream != NULL);
ms = stream->abstract;
assert(ms != NULL);
ts = stream->abstract;
assert(ts != NULL);
if ( !ms->file) {
return php_stream_memory_gets(stream,buf,maxlen STREAMS_REL_CC);
} else {
return php_stream_gets(ms->file,buf,maxlen STREAMS_REL_CC);
}
return php_stream_gets(ts->innerstream, buf, maxlen);
} /* }}} */
static int php_stream_temp_cast(php_stream *stream, int castas, void **ret STREAMS_DC) { /* {{{ */
php_stream_temp_data *ms;
static int php_stream_temp_cast(php_stream *stream, int castas, void **ret) { /* {{{ */
php_stream_temp_data *ts;
assert(stream != NULL);
ms = stream->abstract;
assert(ms != NULL);
ts = stream->abstract;
assert(ts != NULL);
if ( !ms->file) {
return php_stream_memory_cast(stream,castas,ret STREAMS_REL_CC);
} else {
return php_stream_cast(ms->file,castas,ret,0 STREAMS_REL_CC);
}
return php_stream_cast(ts->innerstream, castas, ret, 0);
} /* }}} */
php_stream_ops php_stream_temp_ops = {
@ -390,17 +362,12 @@ PHPAPI php_stream *_php_stream_temp_create(int mode, size_t max_memory_usage STR
php_stream *stream;
self = emalloc(sizeof(*self));
assert( self != NULL);
self->data = NULL;
self->fpos = 0;
self->fsize = 0;
assert(self != NULL);
self->smax = max_memory_usage;
self->mode = mode;
if ( max_memory_usage)
self->file = NULL;
stream = _php_stream_alloc(&php_stream_temp_ops, self, 0, "rwb" STREAMS_REL_CC);
if ( !max_memory_usage)
php_stream_temp_write(stream,NULL,0 STREAMS_REL_CC);
stream = php_stream_alloc(&php_stream_temp_ops, self, 0, "rwb");
self->innerstream = php_stream_memory_create(mode);
php_stream_temp_write(stream, NULL, 0);
return stream;
} /* }}} */
@ -408,10 +375,10 @@ PHPAPI php_stream *_php_stream_temp_open(int mode, size_t max_memory_usage, char
php_stream *stream;
php_stream_temp_data *ms;
if ( (stream = _php_stream_temp_create(mode & ~TEMP_STREAM_READONLY, max_memory_usage STREAMS_REL_CC)) != NULL) {
if ( length) {
if ((stream = _php_stream_temp_create(mode & ~TEMP_STREAM_READONLY, max_memory_usage STREAMS_REL_CC)) != NULL) {
if (length) {
assert(buf != NULL);
php_stream_temp_write(stream,buf,length STREAMS_REL_CC);
php_stream_temp_write(stream, buf, length);
}
ms = stream->abstract;
assert(ms != NULL);
@ -420,40 +387,6 @@ PHPAPI php_stream *_php_stream_temp_open(int mode, size_t max_memory_usage, char
return stream;
} /* }}} */
PHPAPI size_t _php_stream_temp_copy_buffer(php_stream *stream, char **buffer STREAMS_DC) { /* {{{ */
size_t fpos, size;
php_stream_temp_data *ms;
assert(stream != NULL);
ms = stream->abstract;
assert(ms != NULL);
assert(buffer != NULL);
assert(length != 0);
if ( !ms->file) {
*buffer = emalloc( ms->fsize);
if ( !*buffer) {
size = 0;
} else {
size = ms->fsize;
memcpy(*buffer,ms->data,size);
}
} else {
fpos = php_stream_tell(stream STREAMS_REL_CC);
php_stream_seek(stream,0,SEEK_END STREAMS_REL_CC);
size = php_stream_tell(stream STREAMS_REL_CC);
*buffer = emalloc( size);
if ( !*buffer) {
size = 0;
} else {
php_stream_seek(stream,0,SEEK_SET STREAMS_REL_CC);
size = php_stream_read(stream,*buffer,size STREAMS_REL_CC);
}
php_stream_seek(stream,fpos,SEEK_SET STREAMS_REL_CC);
}
return size;
} /* }}} */
/*
* Local variables:
* tab-width: 4

View File

@ -21,26 +21,30 @@
#include "php_streams.h"
#define php_stream_memory_create(mode) _php_stream_memory_create(mode STREAMS_REL_CC);
#define php_stream_memory_open( mode, buf, length) _php_stream_memory_open( mode, buf, length STREAMS_REL_CC);
#define php_stream_memory_get_buffer(stream, length) _hp_stream_memory_get_buffer(stream, length STREAMS_REL_CC);
#define php_stream_memory_create(mode) _php_stream_memory_create((mode) STREAMS_REL_CC);
#define php_stream_memory_open(mode, buf, length) _php_stream_memory_open((mode), (buf), (length) STREAMS_REL_CC);
#define php_stream_memory_get_buffer(stream, length) _php_stream_memory_get_buffer((stream), (length) STREAMS_REL_CC);
#define php_stream_temp_create(mode, max_memory_usage) _php_stream_temp_create(mode, max_memory_usage STREAMS_REL_CC);
#define php_stream_temp_open( mode, max_memory_usage, buf, length) _php_stream_temp_open( mode, max_memory_usage, buf, length STREAMS_REL_CC);
#define php_stream_temp_copy_buffer(stream, buffer) _php_stream_temp_copy_buffer(stream, bufferSTREAMS_REL_CC);
#define php_stream_temp_create(mode, max_memory_usage) _php_stream_temp_create((mode), (max_memory_usage) STREAMS_REL_CC);
#define php_stream_temp_open(mode, max_memory_usage, buf, length) _php_stream_temp_open((mode), (max_memory_usage), (buf), (length) STREAMS_REL_CC);
PHPAPI php_stream *_php_stream_memory_create(int mode STREAMS_REL_CC);
PHPAPI php_stream *_php_stream_memory_open( int mode, char *buf, size_t length STREAMS_REL_CC);
PHPAPI char *_php_stream_memory_get_buffer(php_stream *stream, size_t *length STREAMS_REL_CC);
PHPAPI php_stream *_php_stream_memory_create(int mode STREAMS_DC);
PHPAPI php_stream *_php_stream_memory_open(int mode, char *buf, size_t length STREAMS_DC);
PHPAPI char *_php_stream_memory_get_buffer(php_stream *stream, size_t *length STREAMS_DC);
PHPAPI php_stream *_php_stream_temp_create(int mode, size_t max_memory_usage STREAMS_REL_CC);
PHPAPI php_stream *_php_stream_temp_open( int mode, size_t max_memory_usage, char *buf, size_t length STREAMS_REL_CC);
PHPAPI size_t _php_stream_temp_copy_buffer(php_stream *stream, char **buffer STREAMS_REL_CC);
PHPAPI php_stream *_php_stream_temp_create(int mode, size_t max_memory_usage STREAMS_DC);
PHPAPI php_stream *_php_stream_temp_open(int mode, size_t max_memory_usage, char *buf, size_t length STREAMS_DC);
#define TEMP_STREAM_DEFAULT 0
#define TEMP_STREAM_READONLY 1
extern php_stream_ops php_stream_memory_ops;
extern php_stream_ops php_stream_temp_ops;
#define PHP_STREAM_IS_MEMORY &php_stream_memory_ops
#define PHP_STREAM_IS_TEMP &php_stream_temp_ops
#endif
/*