CVS update: php3/functions

From: Date: Sat, 10 Apr 1999 10:06:38 +0000
Subject: CVS update: php3/functions
Groups: php.dev 
Request: Send a blank email to php-dev+get-5008@lists.php.net to get a copy of this message
Date: Saturday April 10, 1999 @ 6:06 Author: sas Update of /repository/php3/functions In directory asf:/u/temp/cvs-serv4085/functions Modified Files: dba.c dba_cdb.c php3_dba.h Log Message: Testing persistent framework Index: php3/functions/dba.c diff -c php3/functions/dba.c:1.3 php3/functions/dba.c:1.4 *** php3/functions/dba.c:1.3 Fri Apr 9 05:50:52 1999 --- php3/functions/dba.c Sat Apr 10 06:06:38 1999 *************** *** 27,33 **** +----------------------------------------------------------------------+ */ ! /* $Id: dba.c,v 1.3 1999/04/09 09:50:52 sas Exp $ */ #include "php.h" #include "php3_list.h" --- 27,33 ---- +----------------------------------------------------------------------+ */ ! /* $Id: dba.c,v 1.4 1999/04/10 10:06:38 sas Exp $ */ #include "php.h" #include "php3_list.h" *************** *** 40,45 **** --- 40,46 ---- function_entry dba_functions[] = { PHP_FE(db_open, NULL) + PHP_FE(db_popen, NULL) PHP_FE(db_close, NULL) PHP_FE(db_delete, NULL) PHP_FE(db_exists, NULL) *************** *** 53,64 **** }; static int php3_minit_dba(INIT_FUNC_ARGS); static void php3_info_dba(void); php3_module_entry dba_module_entry = { "DataBase API", dba_functions, php3_minit_dba, ! NULL, NULL, NULL, php3_info_dba, STANDARD_MODULE_PROPERTIES }; --- 54,67 ---- }; static int php3_minit_dba(INIT_FUNC_ARGS); + static int php3_mshutdown_dba(void); static void php3_info_dba(void); php3_module_entry dba_module_entry = { "DataBase API", dba_functions, php3_minit_dba, ! php3_mshutdown_dba, ! NULL, NULL, php3_info_dba, STANDARD_MODULE_PROPERTIES }; *************** *** 94,104 **** } \ convert_to_string(key) #define DBA_ID_GET \ convert_to_long(id); \ ! info = php3_list_find(id->value.lval, &type); \ ! if(!info || type != GLOBAL(le_db)) { \ ! php3_error(E_WARNING, "Unable to find db identifier %d", id->value.lval); \ RETURN_FALSE; \ } --- 97,110 ---- } \ convert_to_string(key) + #define DBA_IF_NOT_CORRECT_TYPE(link_id) \ + info = php3_list_find(link_id, &type); \ + if(!info || (type != GLOBAL(le_db) && type != GLOBAL(le_pdb))) + #define DBA_ID_GET \ convert_to_long(id); \ ! DBA_IF_NOT_CORRECT_TYPE(id->value.lval) { \ ! php3_error(E_WARNING, "Unable to find dba identifier %d", id->value.lval); \ RETURN_FALSE; \ } *************** *** 134,144 **** }; static int le_db; /* }}} */ /* {{{ helper routines */ - /* {{{ dba_close */ static void dba_close(dba_info *info) { if(info->hnd) info->hnd->close(info); --- 140,152 ---- }; static int le_db; + static int le_pdb; + static HashTable ht_keys; /* }}} */ /* {{{ helper routines */ /* {{{ dba_close */ + static void dba_close(dba_info *info) { if(info->hnd) info->hnd->close(info); *************** *** 150,156 **** --- 158,174 ---- static int php3_minit_dba(INIT_FUNC_ARGS) { + _php3_hash_init(&ht_keys, 0, NULL, NULL, 1); GLOBAL(le_db) = register_list_destructors(dba_close, NULL); + GLOBAL(le_pdb) = register_list_destructors(NULL, dba_close); + return SUCCESS; + } + /* }}} */ + /* {{{ php3_mshutdown_dba */ + + static int php3_mshutdown_dba(void) + { + _php3_hash_destroy(&ht_keys); return SUCCESS; } /* }}} */ *************** *** 160,166 **** { dba_handler *hptr; ! PUTS("V1 ($Id: dba.c,v 1.3 1999/04/09 09:50:52 sas Exp $)"); for(hptr = handler; hptr->name; hptr++) { PUTS(" "); PUTS(hptr->name); --- 178,184 ---- { dba_handler *hptr; ! PUTS("V1 ($Id: dba.c,v 1.4 1999/04/10 10:06:38 sas Exp $)"); for(hptr = handler; hptr->name; hptr++) { PUTS(" "); PUTS(hptr->name); *************** *** 185,220 **** RETURN_FALSE; } /* }}} */ ! /* }}} */ ! /* {{{ proto int db_open(string path, string mode, string handlername) ! opens path using the specified handler in mode*/ ! PHP_FUNCTION(db_open) { ! pval *path, *mode, *hndname; int ac = ARG_COUNT(ht); dba_mode_t modenr; dba_info *info; dba_handler *hptr; ! if(ac != 3 || getParameters(ht, ac, &path, &mode, &hndname) != SUCCESS) { WRONG_PARAM_COUNT; } ! ! convert_to_string(path); ! convert_to_string(mode); ! convert_to_string(hndname); /* this is O(n) and could be improved */ for(hptr = handler; hptr->name && ! strcmp(hptr->name, hndname->value.str.val); hptr++); if(!hptr->name) { ! php3_error(E_WARNING, "no such handler: %s", hndname->value.str.val); RETURN_FALSE; } ! switch(mode->value.str.val[0]) { case 'w': modenr = DBA_WRITER; break; --- 203,268 ---- RETURN_FALSE; } /* }}} */ ! /* {{{ _php3_dba_open */ ! static void _php3_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent) { ! pval **args; int ac = ARG_COUNT(ht); dba_mode_t modenr; dba_info *info; dba_handler *hptr; + char *key = NULL; + int keylen = 0; + int listid; + int i; ! if(ac < 3) { WRONG_PARAM_COUNT; } ! ! /* we pass additional args to the respective handler */ ! args = emalloc(ac * sizeof(pval *)); ! if(getParametersArray(ht, ac, args) != SUCCESS) { ! efree(args); ! WRONG_PARAM_COUNT; ! } ! ! /* we only take string arguments */ ! for(i = 0; i < ac; i++) { ! convert_to_string(args[i]); ! keylen += args[i]->value.str.len; ! } + if(persistent) { + /* calculate hash */ + key = emalloc(keylen); + keylen = 0; + + for(i = 0; i < ac; i++) { + memcpy(key+keylen,args[i]->value.str.val,args[i]->value.str.len); + keylen += args[i]->value.str.len; + } + + if(_php3_hash_find(&ht_keys, key, keylen, (void **) &info) == SUCCESS) { + efree(args); + efree(key); + RETURN_LONG(php3_list_insert(info, GLOBAL(le_pdb))); + } + } + /* this is O(n) and could be improved */ for(hptr = handler; hptr->name && ! strcmp(hptr->name, args[2]->value.str.val); hptr++); if(!hptr->name) { ! efree(args); ! if(key) efree(key); ! php3_error(E_WARNING, "no such handler: %s", args[2]->value.str.val); RETURN_FALSE; } ! switch(args[1]->value.str.val[0]) { case 'w': modenr = DBA_WRITER; break; *************** *** 222,244 **** modenr = DBA_READER; break; default: ! php3_error(E_WARNING, "Illegal DBA mode: %s", mode->value.str.val); RETURN_FALSE; } info = emalloc(sizeof(*info)); memset(info, 0, sizeof(info)); ! info->path = estrndup(path->value.str.val, path->value.str.len); info->mode = modenr; if(hptr->open(info) != SUCCESS) { dba_close(info); php3_error(E_WARNING, "driver initialization failed"); RETURN_FALSE; } info->hnd = hptr; ! RETURN_LONG(php3_list_insert(info, GLOBAL(le_db))); } /* }}} */ --- 270,324 ---- modenr = DBA_READER; break; default: ! php3_error(E_WARNING,"illegal DBA mode: %s",args[1]->value.str.val); ! efree(args); ! if(key) efree(key); RETURN_FALSE; } info = emalloc(sizeof(*info)); memset(info, 0, sizeof(info)); ! info->path = estrndup(args[0]->value.str.val, args[0]->value.str.len); info->mode = modenr; + info->argc = ac - 3; + info->argv = args + 3; if(hptr->open(info) != SUCCESS) { dba_close(info); php3_error(E_WARNING, "driver initialization failed"); + if(key) efree(key); + efree(args); RETURN_FALSE; } info->hnd = hptr; + info->argc = 0; + info->argv = NULL; + + listid = php3_list_insert(info, persistent?GLOBAL(le_pdb):GLOBAL(le_db)); + if(persistent) { + _php3_hash_update(&ht_keys, key, keylen, info, sizeof(*info), NULL); + } ! efree(args); ! if(key) efree(key); ! RETURN_LONG(listid); ! } ! /* }}} */ ! /* }}} */ ! ! /* {{{ proto int db_popen(string path, string mode, string handlername, ...) ! opens path using the specified handler in mode persistently */ ! PHP_FUNCTION(db_popen) ! { ! _php3_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); ! } ! /* }}} */ ! ! /* {{{ proto int db_open(string path, string mode, string handlername, ...) ! opens path using the specified handler in mode*/ ! PHP_FUNCTION(db_open) ! { ! _php3_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); } /* }}} */ Index: php3/functions/dba_cdb.c diff -c php3/functions/dba_cdb.c:1.4 php3/functions/dba_cdb.c:1.5 *** php3/functions/dba_cdb.c:1.4 Fri Apr 9 05:50:52 1999 --- php3/functions/dba_cdb.c Sat Apr 10 06:06:38 1999 *************** *** 27,33 **** +----------------------------------------------------------------------+ */ ! /* $Id: dba_cdb.c,v 1.4 1999/04/09 09:50:52 sas Exp $ */ #include "php.h" --- 27,33 ---- +----------------------------------------------------------------------+ */ ! /* $Id: dba_cdb.c,v 1.5 1999/04/10 10:06:38 sas Exp $ */ #include "php.h" *************** *** 41,48 **** #include <cdb.h> #include <cdbmake.h> - #define CDB_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = strlen(key) - #define CDB_INFO \ dba_cdb *cdb = (dba_cdb *) info->dbf --- 41,46 ---- *************** *** 64,75 **** case DBA_WRITER: gmode = O_RDWR | O_CREAT; break; } ! cdb = emalloc(sizeof *cdb); memset(cdb, 0, sizeof *cdb); cdb->fd = open(info->path, gmode); if(cdb->fd < 0) { ! efree(cdb); return FAILURE; } --- 62,73 ---- case DBA_WRITER: gmode = O_RDWR | O_CREAT; break; } ! cdb = malloc(sizeof *cdb); memset(cdb, 0, sizeof *cdb); cdb->fd = open(info->path, gmode); if(cdb->fd < 0) { ! free(cdb); return FAILURE; } *************** *** 82,88 **** CDB_INFO; close(cdb->fd); ! efree(cdb); } char *CDB_FNCT(fetch,, const char *key, int *newlen) --- 80,86 ---- CDB_INFO; close(cdb->fd); ! free(cdb); } char *CDB_FNCT(fetch,, const char *key, int *newlen) Index: php3/functions/php3_dba.h diff -c php3/functions/php3_dba.h:1.2 php3/functions/php3_dba.h:1.3 *** php3/functions/php3_dba.h:1.2 Fri Apr 9 05:50:52 1999 --- php3/functions/php3_dba.h Sat Apr 10 06:06:38 1999 *************** *** 27,33 **** +----------------------------------------------------------------------+ */ ! /* $Id: php3_dba.h,v 1.2 1999/04/09 09:50:52 sas Exp $ */ #ifndef _DBA_H #define _DBA_H --- 27,33 ---- +----------------------------------------------------------------------+ */ ! /* $Id: php3_dba.h,v 1.3 1999/04/10 10:06:38 sas Exp $ */ #ifndef _DBA_H #define _DBA_H *************** *** 44,51 **** void *dbf; /* ptr to private data or whatever */ char *path; dba_mode_t mode; ! char *user; ! char *password; /* private */ struct dba_handler *hnd; } dba_info; --- 44,52 ---- void *dbf; /* ptr to private data or whatever */ char *path; dba_mode_t mode; ! /* arg[cv] are only available when the open handler is called! */ ! int argc; ! pval **argv; /* private */ struct dba_handler *hnd; } dba_info; *************** *** 54,59 **** --- 55,61 ---- #define dba_module_ptr &dba_module_entry extern PHP_FUNCTION(db_open); + extern PHP_FUNCTION(db_popen); extern PHP_FUNCTION(db_close); extern PHP_FUNCTION(db_firstkey); extern PHP_FUNCTION(db_nextkey); -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#5008) next »