Back to index

php5  5.3.10
dba_ndbm.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Sascha Schumann <sascha@schumann.cx>                         |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: dba_ndbm.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include "php.h"
00026 
00027 #if DBA_NDBM
00028 #include "php_ndbm.h"
00029 
00030 #include <fcntl.h>
00031 #ifdef NDBM_INCLUDE_FILE
00032 #include NDBM_INCLUDE_FILE
00033 #endif
00034 
00035 #define NDBM_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
00036 
00037 DBA_OPEN_FUNC(ndbm)
00038 {
00039        DBM *dbf;
00040        int gmode = 0;
00041        int filemode = 0644;
00042        dba_info *pinfo = (dba_info *) info;
00043 
00044        switch(info->mode) {
00045               case DBA_READER:
00046                      gmode = O_RDONLY;
00047                      break;
00048               case DBA_WRITER:
00049                      gmode = O_RDWR;
00050                      break;
00051               case DBA_CREAT:
00052                      gmode = O_RDWR | O_CREAT;
00053                      break;
00054               case DBA_TRUNC:
00055                      gmode = O_RDWR | O_CREAT | O_TRUNC;
00056                      break;
00057               default:
00058                      return FAILURE; /* not possible */
00059        }
00060 
00061        if(info->argc > 0) {
00062               convert_to_long_ex(info->argv[0]);
00063               filemode = Z_LVAL_PP(info->argv[0]);
00064        }
00065 
00066        dbf = dbm_open(info->path, gmode, filemode);
00067        
00068        pinfo->dbf = dbf;
00069        return SUCCESS;
00070 }
00071 
00072 DBA_CLOSE_FUNC(ndbm)
00073 {
00074        dbm_close(info->dbf);
00075 }
00076 
00077 DBA_FETCH_FUNC(ndbm)
00078 {
00079        datum gval;
00080        char *new = NULL;
00081 
00082        NDBM_GKEY;
00083        gval = dbm_fetch(info->dbf, gkey);
00084        if(gval.dptr) {
00085               if(newlen) *newlen = gval.dsize;
00086               new = estrndup(gval.dptr, gval.dsize);
00087        }
00088        return new;
00089 }
00090 
00091 DBA_UPDATE_FUNC(ndbm)
00092 {
00093        datum gval;
00094 
00095        NDBM_GKEY;
00096        gval.dptr = (char *) val;
00097        gval.dsize = vallen;
00098 
00099        if(!dbm_store(info->dbf, gkey, gval, mode == 1 ? DBM_INSERT : DBM_REPLACE))
00100               return SUCCESS;
00101        return FAILURE;
00102 }
00103 
00104 DBA_EXISTS_FUNC(ndbm)
00105 {
00106        datum gval;
00107        NDBM_GKEY;
00108        gval = dbm_fetch(info->dbf, gkey);
00109        if(gval.dptr) {
00110               return SUCCESS;
00111        }
00112        return FAILURE;
00113 }
00114 
00115 DBA_DELETE_FUNC(ndbm)
00116 {
00117        NDBM_GKEY;
00118        return(dbm_delete(info->dbf, gkey) == -1 ? FAILURE : SUCCESS);
00119 }
00120 
00121 DBA_FIRSTKEY_FUNC(ndbm)
00122 {
00123        datum gkey;
00124        char *key = NULL;
00125 
00126        gkey = dbm_firstkey(info->dbf);
00127        if(gkey.dptr) {
00128               if(newlen) *newlen = gkey.dsize;
00129               key = estrndup(gkey.dptr, gkey.dsize);
00130        }
00131        return key;
00132 }
00133 
00134 DBA_NEXTKEY_FUNC(ndbm)
00135 {
00136        datum gkey;
00137        char *nkey = NULL;
00138 
00139        gkey = dbm_nextkey(info->dbf);
00140        if(gkey.dptr) {
00141               if(newlen) *newlen = gkey.dsize;
00142               nkey = estrndup(gkey.dptr, gkey.dsize);
00143        }
00144        return nkey;
00145 }
00146 
00147 DBA_OPTIMIZE_FUNC(ndbm)
00148 {
00149        return SUCCESS;
00150 }
00151 
00152 DBA_SYNC_FUNC(ndbm)
00153 {
00154        return SUCCESS;
00155 }
00156 
00157 DBA_INFO_FUNC(ndbm)
00158 {
00159        return estrdup("NDBM");
00160 }
00161 
00162 #endif
00163 
00164 /*
00165  * Local variables:
00166  * tab-width: 4
00167  * c-basic-offset: 4
00168  * End:
00169  * vim600: sw=4 ts=4 fdm=marker
00170  * vim<600: sw=4 ts=4
00171  */