cvs: php4 /ext/dba dba.c php_dba.h
| From: | Jani Taskinen | Date: | Fri, 23 Aug 2002 22:24:02 +0000 |
| Subject: | cvs: php4 /ext/dba dba.c php_dba.h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-13614@lists.php.net to get a copy of this message | ||
sniper Fri Aug 23 18:24:02 2002 EDT
Modified files:
/php4/ext/dba dba.c php_dba.h
Log:
Made this extension to use the resource stuff
Index: php4/ext/dba/dba.c
diff -u php4/ext/dba/dba.c:1.43 php4/ext/dba/dba.c:1.44
--- php4/ext/dba/dba.c:1.43 Wed Jun 26 03:51:59 2002
+++ php4/ext/dba/dba.c Fri Aug 23 18:24:02 2002
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: dba.c,v 1.43 2002/06/26 07:51:59 derick Exp $ */
+/* $Id: dba.c,v 1.44 2002/08/23 22:24:02 sniper Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -55,20 +55,19 @@
};
/* }}} */
-static PHP_MINIT_FUNCTION(dba);
-static PHP_MSHUTDOWN_FUNCTION(dba);
-static PHP_MINFO_FUNCTION(dba);
+PHP_MINIT_FUNCTION(dba);
+PHP_MINFO_FUNCTION(dba);
zend_module_entry dba_module_entry = {
- STANDARD_MODULE_HEADER,
+ STANDARD_MODULE_HEADER,
"dba",
- dba_functions,
+ dba_functions,
PHP_MINIT(dba),
- PHP_MSHUTDOWN(dba),
NULL,
- NULL,
+ NULL,
+ NULL,
PHP_MINFO(dba),
- NO_VERSION_YET,
+ NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};
@@ -93,9 +92,9 @@
/* {{{ macromania */
#define DBA_ID_PARS \
- pval **id; \
+ zval **id; \
dba_info *info = NULL; \
- int type, ac = ZEND_NUM_ARGS()
+ int ac = ZEND_NUM_ARGS()
/* these are used to get the standard arguments */
@@ -105,24 +104,14 @@
}
#define DBA_GET2 \
- pval **key; \
+ zval **key; \
if(ac != 2 || zend_get_parameters_ex(ac, &key, &id) != SUCCESS) { \
WRONG_PARAM_COUNT; \
} \
convert_to_string_ex(key)
-#define DBA_IF_NOT_CORRECT_TYPE(link_id) \
- info = zend_list_find(link_id, &type); \
- if(!info || (type != GLOBAL(le_db) && type != GLOBAL(le_pdb)))
-
#define DBA_ID_GET \
- convert_to_long_ex(id); \
- DBA_IF_NOT_CORRECT_TYPE(Z_LVAL_PP(id)) { \
- php_error(E_WARNING, "%s(): Unable to find DBA identifier %d", \
- get_active_function_name(TSRMLS_C), \
- Z_LVAL_PP(id)); \
- RETURN_FALSE; \
- }
+ ZEND_FETCH_RESOURCE2(info, dba_info *, id, -1, "DBA identifier", le_db, le_pdb);
#define DBA_ID_GET1 DBA_ID_PARS; DBA_GET1; DBA_ID_GET
#define DBA_ID_GET2 DBA_ID_PARS; DBA_GET2; DBA_ID_GET
@@ -143,8 +132,6 @@
RETURN_FALSE; \
}
-#define GLOBAL(a) a
-
/* }}} */
/* {{{ globals */
@@ -173,7 +160,6 @@
static int le_db;
static int le_pdb;
-static HashTable ht_keys;
/* }}} */
/* {{{ dba_close
@@ -198,20 +184,18 @@
/* {{{ PHP_MINIT_FUNCTION
*/
-static PHP_MINIT_FUNCTION(dba)
+PHP_MINIT_FUNCTION(dba)
{
- zend_hash_init(&ht_keys, 0, NULL, NULL, 1);
- GLOBAL(le_db) = zend_register_list_destructors_ex(dba_close_rsrc, NULL, "dba",
module_number);
- GLOBAL(le_pdb) = zend_register_list_destructors_ex(NULL, dba_close_rsrc, "dba
persistent", module_number);
+ le_db = zend_register_list_destructors_ex(dba_close_rsrc, NULL, "dba", module_number);
+ le_pdb = zend_register_list_destructors_ex(NULL, dba_close_rsrc, "dba persistent",
module_number);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
-static PHP_MSHUTDOWN_FUNCTION(dba)
+PHP_MSHUTDOWN_FUNCTION(dba)
{
- zend_hash_destroy(&ht_keys);
return SUCCESS;
}
/* }}} */
@@ -220,7 +204,7 @@
/* {{{ PHP_MINFO_FUNCTION
*/
-static PHP_MINFO_FUNCTION(dba)
+PHP_MINFO_FUNCTION(dba)
{
dba_handler *hptr;
smart_str handlers = {0};
@@ -242,13 +226,13 @@
php_info_print_table_end();
}
/* }}} */
-
+
/* {{{ php_dba_update
*/
static void php_dba_update(INTERNAL_FUNCTION_PARAMETERS, int mode)
{
DBA_ID_PARS;
- pval **val, **key;
+ zval **val, **key;
if(ac != 3 || zend_get_parameters_ex(ac, &key, &val, &id) != SUCCESS) {
WRONG_PARAM_COUNT;
@@ -271,14 +255,13 @@
*/
static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
{
- pval ***args = (pval ***) NULL;
+ zval ***args = (zval ***) NULL;
int ac = ZEND_NUM_ARGS();
dba_mode_t modenr;
dba_info *info;
dba_handler *hptr;
char *key = NULL;
int keylen = 0;
- int listid;
int i;
if(ac < 3) {
@@ -286,19 +269,21 @@
}
/* we pass additional args to the respective handler */
- args = emalloc(ac * sizeof(pval *));
- if(zend_get_parameters_array_ex(ac, args) != SUCCESS) {
+ args = emalloc(ac * sizeof(zval *));
+ if (zend_get_parameters_array_ex(ac, args) != SUCCESS) {
FREENOW;
WRONG_PARAM_COUNT;
}
/* we only take string arguments */
- for(i = 0; i < ac; i++) {
+ for (i = 0; i < ac; i++) {
convert_to_string_ex(args[i]);
keylen += Z_STRLEN_PP(args[i]);
}
- if(persistent) {
+ if (persistent) {
+ list_entry *le;
+
/* calculate hash */
key = emalloc(keylen);
keylen = 0;
@@ -307,23 +292,31 @@
memcpy(key+keylen, Z_STRVAL_PP(args[i]), Z_STRLEN_PP(args[i]));
keylen += Z_STRLEN_PP(args[i]);
}
-
- if(zend_hash_find(&ht_keys, key, keylen, (void **) &info) == SUCCESS) {
+
+ /* try to find if we already have this link in our persistent list */
+ if (zend_hash_find(&EG(persistent_list), key, keylen+1, (void **) &le) == SUCCESS) {
FREENOW;
- RETURN_LONG(zend_list_insert(info, GLOBAL(le_pdb)));
+
+ if (Z_TYPE_P(le) != le_pdb) {
+ RETURN_FALSE;
+ }
+
+ info = (dba_info *)le->ptr;
+
+ ZEND_REGISTER_RESOURCE(return_value, info, le_pdb);
+ return;
}
}
- for(hptr = handler; hptr->name &&
- strcasecmp(hptr->name, Z_STRVAL_PP(args[2])); hptr++);
+ for (hptr = handler; hptr->name && strcasecmp(hptr->name, Z_STRVAL_PP(args[2]));
hptr++);
- if(!hptr->name) {
+ if (!hptr->name) {
php_error(E_WARNING, "%s(): no such handler: %s", get_active_function_name(TSRMLS_C),
Z_STRVAL_PP(args[2]));
FREENOW;
RETURN_FALSE;
}
- switch(Z_STRVAL_PP(args[1])[0]) {
+ switch (Z_STRVAL_PP(args[1])[0]) {
case 'c':
modenr = DBA_CREAT;
break;
@@ -350,23 +343,30 @@
info->argv = args + 3;
info->hnd = NULL;
- if(hptr->open(info TSRMLS_CC) != SUCCESS) {
+ if (hptr->open(info TSRMLS_CC) != SUCCESS) {
dba_close(info);
php_error(E_WARNING, "%s(): driver initialization failed",
get_active_function_name(TSRMLS_C));
FREENOW;
RETURN_FALSE;
}
+
info->hnd = hptr;
info->argc = 0;
info->argv = NULL;
- listid = zend_list_insert(info, persistent?GLOBAL(le_pdb):GLOBAL(le_db));
- if(persistent) {
- zend_hash_update(&ht_keys, key, keylen, info, sizeof(*info), NULL);
+ if (persistent) {
+ list_entry new_le;
+
+ Z_TYPE(new_le) = le_pdb;
+ new_le.ptr = info;
+ if (zend_hash_update(&EG(persistent_list), key, keylen+1, &new_le, sizeof(list_entry),
NULL) == FAILURE) {
+ FREENOW;
+ RETURN_FALSE;
+ }
}
-
+
+ ZEND_REGISTER_RESOURCE(return_value, info, (persistent ? le_pdb : le_db));
FREENOW;
- RETURN_LONG(listid);
}
/* }}} */
#undef FREENOW
@@ -387,13 +387,13 @@
}
/* }}} */
-/* {{{ proto void dba_close(int handle)
+/* {{{ proto void dba_close(resource handle)
Closes database */
PHP_FUNCTION(dba_close)
{
- DBA_ID_GET1;
+ DBA_ID_GET1;
- zend_list_delete(Z_LVAL_PP(id));
+ zend_list_delete(Z_RESVAL_PP(id));
}
/* }}} */
Index: php4/ext/dba/php_dba.h
diff -u php4/ext/dba/php_dba.h:1.11 php4/ext/dba/php_dba.h:1.12
--- php4/ext/dba/php_dba.h:1.11 Thu Feb 28 03:26:00 2002
+++ php4/ext/dba/php_dba.h Fri Aug 23 18:24:02 2002
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_dba.h,v 1.11 2002/02/28 08:26:00 sebastian Exp $ */
+/* $Id: php_dba.h,v 1.12 2002/08/23 22:24:02 sniper Exp $ */
#ifndef PHP_DBA_H
#define PHP_DBA_H
@@ -37,7 +37,7 @@
dba_mode_t mode;
/* arg[cv] are only available when the dba_open handler is called! */
int argc;
- pval ***argv;
+ zval ***argv;
/* private */
struct dba_handler *hnd;
} dba_info;