how to make nasty code less nasty (obtaining sqlite db handle)

From: Date: Thu, 03 Feb 2005 22:03:45 +0000
Subject: how to make nasty code less nasty (obtaining sqlite db handle)
Groups: php.internals 
Request: Send a blank email to internals+get-14597@lists.php.net to get a copy of this message
I've written an extension which needs to accept a Resource which is an SQLite database handle, and call a C function which will be using that db handle to issue queries. What I've done works, but is kinda nasty because there doesn't appear to be a clean way to obtain that database handle nor, from what I can tell, determine the resource type to expect. Is there a nicer way of doing this? PHP_FUNCTION(my_PHP_function) { int id; int dataLen; int resourceType; char * pData; zval * zdb; void * hDB; void ** phDB; /* Ensure we got the correct number of parameters */ if (ZEND_NUM_ARGS() != 2) { WRONG_PARAM_COUNT; } /* Retrieve the parameters */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zdb, &pData, &dataLen) == FAILURE) { return; } /* * Voodoo to retrieve the sqlite database handle from the resource. * How do I validate that resourceType is reasonable? */ id = zdb->value.lval; if ((phDB = zend_list_find(id, &resourceType)) == NULL) { return; } /* * This is nasty too. We "know" that the first field in the private * structure is the database handle. Just extract it. */ hDB = *phDB; RETURN_STRING(my_C_function(hDB, pData), TRUE); } Thanks, Derrell

« previous php.internals (#14597) next »