Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:14597 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 57737 invoked by uid 1010); 3 Feb 2005 22:03:56 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 57678 invoked from network); 3 Feb 2005 22:03:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Feb 2005 22:03:55 -0000 X-Host-Fingerprint: 66.92.75.243 dsl092-075-243.bos1.dsl.speakeasy.net Linux 2.4/2.6 Received: from ([66.92.75.243:3781] helo=amber.vis-av.com) by pb1.pair.com (ecelerity HEAD (r4105:4106)) with SMTP id FF/E8-10528-5CF92024 for ; Thu, 03 Feb 2005 17:03:50 -0500 Received: (qmail 21551 invoked from network); 3 Feb 2005 22:03:45 -0000 Received: from unknown (HELO random.internal) (192.168.1.9) by amber.internal with SMTP; 3 Feb 2005 22:03:45 -0000 Received: (nullmailer pid 6705 invoked by uid 0); Thu, 03 Feb 2005 22:03:45 -0000 To: internals Reply-To: Derrell.Lipman@UnwiredUniverse.com Date: Thu, 03 Feb 2005 17:03:45 -0500 Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Corporate Culture, linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Subject: how to make nasty code less nasty (obtaining sqlite db handle) From: Derrell.Lipman@UnwiredUniverse.com 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