cvs: /php4/ext/zlib zlib.c

From: Date: Sat, 04 Sep 1999 21:51:54 +0000
Subject: cvs: /php4/ext/zlib zlib.c
Groups: php.version4 
Request: Send a blank email to php-version4+get-4099@lists.php.net to get a copy of this message
sr Sat Sep 4 17:51:54 1999 EDT Modified files: /php4/ext/zlib zlib.c Log: First try with new resource handling in zlib module. Index: php4/ext/zlib/zlib.c diff -u php4/ext/zlib/zlib.c:1.10 php4/ext/zlib/zlib.c:1.11 --- php4/ext/zlib/zlib.c:1.10 Sat Sep 4 09:18:57 1999 +++ php4/ext/zlib/zlib.c Sat Sep 4 17:51:54 1999 @@ -16,7 +16,7 @@ | Stefan Röhrich <sr@linux.de> | +----------------------------------------------------------------------+ */ -/* $Id: zlib.c,v 1.10 1999/09/04 13:18:57 zeev Exp $ */ +/* $Id: zlib.c,v 1.11 1999/09/04 21:51:54 sr Exp $ */ #if !PHP_31 && defined(THREAD_SAFE) #undef THREAD_SAFE #endif @@ -136,10 +136,10 @@ }; #if defined(COMPILE_DL) -DLEXPORT php3_module_entry *get_module(void) { return &php3_zlib_module_entry; } +DLEXPORT php3_module_entry *get_module(void) { return &php_zlib_module_entry; } #endif -static void php3i_destructor_gzclose(gzFile *zp) { +static void phpi_destructor_gzclose(gzFile *zp) { (void)gzclose(zp); } @@ -163,7 +163,7 @@ return FAILURE; } #endif - ZLIB_GLOBAL(le_zp) = register_list_destructors(php3i_destructor_gzclose,NULL); + ZLIB_GLOBAL(le_zp) = register_list_destructors(phpi_destructor_gzclose,NULL); return SUCCESS; } @@ -376,7 +376,6 @@ pval *arg1, *arg2, *arg3; gzFile *zp; char *p; - int id; int use_include_path = 0; ZLIB_TLS_VARS; @@ -412,9 +411,8 @@ RETURN_FALSE; } ZLIB_GLOBAL(gzgetss_state)=0; - id = php3_list_insert(zp,ZLIB_GLOBAL(le_zp)); efree(p); - RETURN_LONG(id); + ZEND_REGISTER_RESOURCE(return_value, zp, ZLIB_GLOBAL(le_zp)); } /* }}} */ @@ -422,21 +420,14 @@ Close an open .gz-file pointer */ PHP_FUNCTION(gzclose) { pval *arg1; - int id, type; gzFile *zp; ZLIB_TLS_VARS; - + if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_long(arg1); - id=arg1->value.lval; - zp = php3_list_find(id,&type); - if (!zp || (type!=ZLIB_GLOBAL(le_zp))) { - php_error(E_WARNING,"Unable to find gz-file identifier %d",id); - RETURN_FALSE; - } - php3_list_delete(id); + ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", ZLIB_GLOBAL(le_zp)); + zend_list_delete(arg1->value.lval); RETURN_TRUE; } /* }}} */ @@ -446,20 +437,13 @@ PHP_FUNCTION(gzeof) { pval *arg1; gzFile *zp; - int id, type; ZLIB_TLS_VARS; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; - } - convert_to_long(arg1); - id = arg1->value.lval; - zp = php3_list_find(id,&type); - if ((!zp || (type!=ZLIB_GLOBAL(le_zp)))) { - php_error(E_WARNING,"Unable to find gz-file identifier %d",id); - /* we're at the eof if the file doesn't exist */ - RETURN_TRUE; } + ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", ZLIB_GLOBAL(le_zp)); + if ((gzeof(zp))) { RETURN_TRUE; } else { @@ -473,23 +457,18 @@ PHP_FUNCTION(gzgets) { pval *arg1, *arg2; gzFile *zp; - int id, len, type; + int len; char *buf; ZLIB_TLS_VARS; if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &arg1, &arg2) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_long(arg1); convert_to_long(arg2); - id = arg1->value.lval; len = arg2->value.lval; - zp = php3_list_find(id,&type); - if (!zp || (type!=ZLIB_GLOBAL(le_zp))) { - php_error(E_WARNING,"Unable to find gz-file identifier %d",id); - RETURN_FALSE; - } + ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", ZLIB_GLOBAL(le_zp)); + buf = emalloc(sizeof(char) * (len + 1)); /* needed because recv doesnt put a null at the end*/ memset(buf,0,len+1); @@ -514,21 +493,16 @@ PHP_FUNCTION(gzgetc) { pval *arg1; gzFile *zp; - int id, type, c; + int c; char *buf; ZLIB_TLS_VARS; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_long(arg1); - id = arg1->value.lval; - zp = php3_list_find(id,&type); - if (!zp || (type!=ZLIB_GLOBAL(le_zp))) { - php_error(E_WARNING,"Unable to find gz-file identifier %d",id); - RETURN_FALSE; - } + ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", ZLIB_GLOBAL(le_zp)); + buf = emalloc(sizeof(char) * 2); if ((c=gzgetc(zp)) == (-1)) { efree(buf); @@ -551,7 +525,7 @@ { pval *fd, *bytes; gzFile *zp; - int id, len, br, type; + int len, br; char *buf, *p, *rbuf, *rp, c, lc; ZLIB_TLS_VARS; @@ -559,17 +533,11 @@ WRONG_PARAM_COUNT; } - convert_to_long(fd); convert_to_long(bytes); - id = fd->value.lval; len = bytes->value.lval; - zp = php3_list_find(id,&type); - if (!zp || (type!=ZLIB_GLOBAL(le_zp))) { - php_error(E_WARNING, "Unable to find gz-file identifier %d", id); - RETURN_FALSE; - } + ZEND_FETCH_RESOURCE(zp, gzFile *, fd, -1, "Zlib file", ZLIB_GLOBAL(le_zp)); buf = emalloc(sizeof(char) * (len + 1)); /*needed because recv doesnt set null char at end*/ @@ -667,7 +635,7 @@ PHP_FUNCTION(gzwrite) { pval *arg1, *arg2, *arg3=NULL; gzFile *zp; - int ret,id,type; + int ret; int num_bytes; ZLIB_TLS_VARS; @@ -692,15 +660,8 @@ /* NOTREACHED */ break; } - convert_to_long(arg1); - id = arg1->value.lval; + ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", ZLIB_GLOBAL(le_zp)); - zp = php3_list_find(id,&type); - if (!zp || (type!=ZLIB_GLOBAL(le_zp))) { - php_error(E_WARNING,"Unable to find gz-file identifier %d",id); - RETURN_FALSE; - } - /* strip slashes only if the length wasn't specified explicitly */ if (!arg3 && PG(magic_quotes_runtime)) { php_stripslashes(arg2->value.str.val,&num_bytes); @@ -719,20 +680,15 @@ Rewind the position of a .gz-file pointer */ PHP_FUNCTION(gzrewind) { pval *arg1; - int id,type; gzFile *zp; ZLIB_TLS_VARS; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_long(arg1); - id = arg1->value.lval; - zp = php3_list_find(id,&type); - if (!zp || (type!=ZLIB_GLOBAL(le_zp))) { - php_error(E_WARNING,"Unable to find gz-file identifier %d",id); - RETURN_FALSE; - } + + ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", ZLIB_GLOBAL(le_zp)); + gzrewind(zp); RETURN_TRUE; } @@ -742,21 +698,16 @@ Get .gz-file pointer's read/write position */ PHP_FUNCTION(gztell) { pval *arg1; - int id, type; long pos; gzFile *zp; ZLIB_TLS_VARS; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; - } - convert_to_long(arg1); - id = arg1->value.lval; - zp = php3_list_find(id,&type); - if (!zp || (type!=ZLIB_GLOBAL(le_zp))) { - php_error(E_WARNING,"Unable to find gz-file identifier %d",id); - RETURN_FALSE; } + + ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", ZLIB_GLOBAL(le_zp)); + pos = gztell(zp); RETURN_LONG(pos); } @@ -766,7 +717,7 @@ Seek on a file pointer */ PHP_FUNCTION(gzseek) { pval *arg1, *arg2; - int ret,id,type; + int ret; long pos; gzFile *zp; ZLIB_TLS_VARS; @@ -774,15 +725,11 @@ if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &arg1, &arg2) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_long(arg1); convert_to_long(arg2); pos = arg2->value.lval; - id = arg1->value.lval; - zp = php3_list_find(id,&type); - if (!zp || (type!=ZLIB_GLOBAL(le_zp))) { - php_error(E_WARNING,"Unable to find gz-file identifier %d",id); - RETURN_FALSE; - } + + ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", ZLIB_GLOBAL(le_zp)); + ret = gzseek(zp,pos,SEEK_SET); RETURN_LONG(ret); } @@ -848,26 +795,22 @@ pval *arg1; gzFile *zp; char buf[8192]; - int id, size, b, type; + int size, b; ZLIB_TLS_VARS; if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg1) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_long(arg1); - id = arg1->value.lval; - zp = php3_list_find(id,&type); - if (!zp || (type!=ZLIB_GLOBAL(le_zp))) { - php_error(E_WARNING,"Unable to find gz-file identifier %d",id); - RETURN_FALSE; - } + + ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", ZLIB_GLOBAL(le_zp)); + size = 0; while((b = gzread(zp, buf, sizeof(buf))) > 0) { PHPWRITE(buf,b); size += b ; } /* gzclose(zp); */ - php3_list_delete(id); + zend_list_delete(arg1->value.lval); RETURN_LONG(size); } /* }}} */ @@ -878,22 +821,17 @@ { pval *arg1, *arg2; gzFile *zp; - int id, len, type; + int len; ZLIB_TLS_VARS; if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &arg1, &arg2) == FAILURE) { WRONG_PARAM_COUNT; } - convert_to_long(arg1); convert_to_long(arg2); - id = arg1->value.lval; len = arg2->value.lval; - zp = php3_list_find(id,&type); - if (!zp || (type!=ZLIB_GLOBAL(le_zp))) { - php_error(E_WARNING,"Unable to find gz-file identifier %d",id); - RETURN_FALSE; - } + ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", ZLIB_GLOBAL(le_zp)); + return_value->value.str.val = emalloc(sizeof(char) * (len + 1)); /* needed because recv doesnt put a null at the end*/

« previous php.version4 (#4099) next »