Re: Re: memory streams
| From: | (Marcus Börger) | Date: | Sat, 16 Mar 2002 19:04:27 +0000 |
| Subject: | Re: Re: memory streams | ||
| References: | 1 2 3 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-81358@lists.php.net to get a copy of this message | ||
At 19:53 16.03.2002, Wez Furlong wrote:
>Damn; you're half right :-)
>
>Your seeker implementation should spot the offset=0 whence=SEEK_CUR
>case and return the current position.
>I've fixed the stdio stream implementation to do this.
>
>Please don't change the php_stream_ops structure, as there is no
>need.
>
>How are your memory streams implemented? (I was about to start on
>those!)
See them attached, i will commit them as soon as they work....
>--Wez.
>
>On 17/03/02, "Marcus Boerger" <520072483730-0001@t-online.de> wrote:
> > At the moment i am working at memory-streams....
> >
> > If have the following problem left:
> > I must extend
> > struct _php_stream_ops
> > with ftell....
> >
> > by the way php_stream_tell is wrong because it relies on seek and
> > that does return either 0 on success or an erroecode....
> >
> > regards
> > marcus
> >
> >
> >
> > --------->>> mailto:marcus.boerger@post.rwth-aachen.de
> > <<<------------
> > I don't want to start any blashphemous rumours
> > but i think that god's got a sick sense of humor
> > and when i die i expect to find him laughing.
> > Depeche Mode
> > --------------->>> http://www.marcus-boerger.de
> > <<<-------------------
>
>
>
>
>--
>PHP Development Mailing List <http://www.php.net/>
>To unsubscribe, visit: http://www.php.net/unsub.php
/* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2002 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Marcus Boerger <helly@php.net> | +----------------------------------------------------------------------+ */ #ifndef PHP_MEMORY_STREAM_H #define PHP_MEMORY_STREAM_H #include "php_streams.h" PHPAPI php_stream *php_memory_stream_create(); #endif /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: sw=4 ts=4 fdm=marker * vim<600: sw=4 ts=4 */ /* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2002 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: | | Marcus Boerger <helly@php.net> | +----------------------------------------------------------------------+ */ #define _GNU_SOURCE #include "php.h" #include "php_streams.h" /* {{{ ------- MEMORY stream implementation -------*/ typedef struct { char *data; size_t fpos; size_t fsize; } php_memory_stream_data; static size_t php_memory_stream_write(php_stream *stream, const char *buf, size_t count) {/* {{{ */ php_memory_stream_data *ms; assert(stream != NULL); ms = stream->abstract; assert(ms != NULL); if ( ms->fpos + count > ms->fsize) { char *tmp = erealloc( ms->data, ms->fpos + count); if ( !tmp) { count = ms->fsize - ms->fpos + 1; } else { ms->data = tmp; } } if ( count) { assert(buf!= NULL); memcpy( (char*)buf, ms->data+ms->fpos, count); ms->fpos += count; } return count; } /* }}} */ static size_t php_memory_stream_read(php_stream *stream, char *buf, size_t count) {/* {{{ */ php_memory_stream_data *ms; assert(stream != NULL); ms = stream->abstract; assert(ms != NULL); if (buf == NULL && count == 0) { /* check for EOF condition */ if (ms->fpos >= ms->fsize) { return EOF; } return 0; } if ( ms->fpos + count > ms->fsize) { count = ms->fsize - ms->fpos + 1; } if ( count) { assert(buf!= NULL); memcpy( buf, ms->data+ms->fpos, count); ms->fpos += count; } return count; } /* }}} */ static int php_memory_stream_close(php_stream *stream, int close_handle) {/* {{{ */ php_memory_stream_data *ms; assert(stream != NULL); ms = stream->abstract; assert(ms != NULL); if (ms->data) { efree(ms->data); } ms->data = NULL; ms->fsize = 0; ms->fpos = 0; } /* }}} */ static int php_memory_stream_flush(php_stream *stream) {/* {{{ */ // nothing to do here return 0; } /* }}} */ static int php_memory_stream_seek(php_stream *stream, off_t offset, int whence) {/* {{{ */ php_memory_stream_data *ms; assert(stream != NULL); ms = stream->abstract; assert(ms != NULL); if (offset == 0 && whence == SEEK_CUR) return ms->fpos; switch( whence) { case SEEK_CUR: if ( offset < 0) { if ( ms->fpos < -offset) { ms->fpos = 0; /*return EINVAL;*/ } else { ms->fpos = ms->fpos + offset; } } else { if ( ms->fpos < offset) { ms->fpos = ms->fsize; /*return EINVAL;*/ } else { ms->fpos = ms->fpos + offset; } } return 0; case SEEK_SET: if ( offset > ms->fsize) { ms->fpos = ms->fsize; /*return EINVAL;*/ } else { ms->fpos = offset; } return 0; case SEEK_END: if ( offset > 0) { ms->fpos = ms->fsize; /*return EINVAL;*/ } else if ( ms->fpos < -offset) { ms->fpos = 0; /*return EINVAL;*/ } else { ms->fpos = ms->fsize + offset; } return 0; default: return 0; return /*EINVAL*/; } } /* }}} */ php_stream_ops php_stream_memory_ops = { php_memory_stream_write, php_memory_stream_read, php_memory_stream_close, php_memory_stream_flush, php_memory_stream_seek, NULL, NULL, "MEMORY" }; PHPAPI php_stream *php_memory_stream_create() {/* {{{ */ php_memory_stream_data *self; self = emalloc(sizeof(*self)); assert( self != NULL); self->data = NULL; self->fpos = 0; self->fsize = 0; return php_stream_alloc(&php_stream_memory_ops, self, 0, "rwb"); } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */
/* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2002 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Marcus Boerger <helly@php.net> | +----------------------------------------------------------------------+ */ #ifndef PHP_MEMORY_STREAM_H #define PHP_MEMORY_STREAM_H #include "php_streams.h" PHPAPI php_stream *php_memory_stream_create(); #endif /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: sw=4 ts=4 fdm=marker * vim<600: sw=4 ts=4 */ /* +----------------------------------------------------------------------+ | PHP Version 4 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2002 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 2.02 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available at through the world-wide-web at | | http://www.php.net/license/2_02.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: | | Marcus Boerger <helly@php.net> | +----------------------------------------------------------------------+ */ #define _GNU_SOURCE #include "php.h" #include "php_streams.h" /* {{{ ------- MEMORY stream implementation -------*/ typedef struct { char *data; size_t fpos; size_t fsize; } php_memory_stream_data; static size_t php_memory_stream_write(php_stream *stream, const char *buf, size_t count) {/* {{{ */ php_memory_stream_data *ms; assert(stream != NULL); ms = stream->abstract; assert(ms != NULL); if ( ms->fpos + count > ms->fsize) { char *tmp = erealloc( ms->data, ms->fpos + count); if ( !tmp) { count = ms->fsize - ms->fpos + 1; } else { ms->data = tmp; } } if ( count) { assert(buf!= NULL); memcpy( (char*)buf, ms->data+ms->fpos, count); ms->fpos += count; } return count; } /* }}} */ static size_t php_memory_stream_read(php_stream *stream, char *buf, size_t count) {/* {{{ */ php_memory_stream_data *ms; assert(stream != NULL); ms = stream->abstract; assert(ms != NULL); if (buf == NULL && count == 0) { /* check for EOF condition */ if (ms->fpos >= ms->fsize) { return EOF; } return 0; } if ( ms->fpos + count > ms->fsize) { count = ms->fsize - ms->fpos + 1; } if ( count) { assert(buf!= NULL); memcpy( buf, ms->data+ms->fpos, count); ms->fpos += count; } return count; } /* }}} */ static int php_memory_stream_close(php_stream *stream, int close_handle) {/* {{{ */ php_memory_stream_data *ms; assert(stream != NULL); ms = stream->abstract; assert(ms != NULL); if (ms->data) { efree(ms->data); } ms->data = NULL; ms->fsize = 0; ms->fpos = 0; } /* }}} */ static int php_memory_stream_flush(php_stream *stream) {/* {{{ */ // nothing to do here return 0; } /* }}} */ static int php_memory_stream_seek(php_stream *stream, off_t offset, int whence) {/* {{{ */ php_memory_stream_data *ms; assert(stream != NULL); ms = stream->abstract; assert(ms != NULL); if (offset == 0 && whence == SEEK_CUR) return ms->fpos; switch( whence) { case SEEK_CUR: if ( offset < 0) { if ( ms->fpos < -offset) { ms->fpos = 0; /*return EINVAL;*/ } else { ms->fpos = ms->fpos + offset; } } else { if ( ms->fpos < offset) { ms->fpos = ms->fsize; /*return EINVAL;*/ } else { ms->fpos = ms->fpos + offset; } } return 0; case SEEK_SET: if ( offset > ms->fsize) { ms->fpos = ms->fsize; /*return EINVAL;*/ } else { ms->fpos = offset; } return 0; case SEEK_END: if ( offset > 0) { ms->fpos = ms->fsize; /*return EINVAL;*/ } else if ( ms->fpos < -offset) { ms->fpos = 0; /*return EINVAL;*/ } else { ms->fpos = ms->fsize + offset; } return 0; default: return 0; return /*EINVAL*/; } } /* }}} */ php_stream_ops php_stream_memory_ops = { php_memory_stream_write, php_memory_stream_read, php_memory_stream_close, php_memory_stream_flush, php_memory_stream_seek, NULL, NULL, "MEMORY" }; PHPAPI php_stream *php_memory_stream_create() {/* {{{ */ php_memory_stream_data *self; self = emalloc(sizeof(*self)); assert( self != NULL); self->data = NULL; self->fpos = 0; self->fsize = 0; return php_stream_alloc(&php_stream_memory_ops, self, 0, "rwb"); } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */