Back to index

php5  5.3.10
dba_gdbm.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_gdbm.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_GDBM
00028 #include "php_gdbm.h"
00029 
00030 #ifdef GDBM_INCLUDE_FILE
00031 #include GDBM_INCLUDE_FILE
00032 #endif
00033 
00034 #define GDBM_DATA dba_gdbm_data *dba = info->dbf
00035 #define GDBM_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
00036 
00037 typedef struct {
00038        GDBM_FILE dbf;
00039        datum nextkey;
00040 } dba_gdbm_data;
00041 
00042 DBA_OPEN_FUNC(gdbm)
00043 {
00044        GDBM_FILE dbf;
00045        int gmode = 0;
00046        int filemode = 0644;
00047 
00048        gmode = info->mode == DBA_READER ? GDBM_READER :
00049               info->mode == DBA_WRITER ? GDBM_WRITER : 
00050               info->mode == DBA_CREAT  ? GDBM_WRCREAT : 
00051               info->mode == DBA_TRUNC ? GDBM_NEWDB : -1;
00052               
00053        if(gmode == -1) 
00054               return FAILURE; /* not possible */
00055 
00056        if(info->argc > 0) {
00057               convert_to_long_ex(info->argv[0]);
00058               filemode = Z_LVAL_PP(info->argv[0]);
00059        }
00060 
00061        dbf = gdbm_open(info->path, 0, gmode, filemode, NULL);
00062        
00063        if(dbf) {
00064               info->dbf = pemalloc(sizeof(dba_gdbm_data), info->flags&DBA_PERSISTENT);
00065               memset(info->dbf, 0, sizeof(dba_gdbm_data));
00066               ((dba_gdbm_data *) info->dbf)->dbf = dbf;
00067               return SUCCESS;
00068        }
00069        *error = gdbm_strerror(gdbm_errno);
00070        return FAILURE;
00071 }
00072 
00073 DBA_CLOSE_FUNC(gdbm)
00074 {
00075        GDBM_DATA;
00076        
00077        if(dba->nextkey.dptr) free(dba->nextkey.dptr);
00078        gdbm_close(dba->dbf);
00079        pefree(dba, info->flags&DBA_PERSISTENT);
00080 }
00081 
00082 DBA_FETCH_FUNC(gdbm)
00083 {
00084        GDBM_DATA;
00085        datum gval;
00086        char *new = NULL;
00087 
00088        GDBM_GKEY;
00089        gval = gdbm_fetch(dba->dbf, gkey);
00090        if(gval.dptr) {
00091               if(newlen) *newlen = gval.dsize;
00092               new = estrndup(gval.dptr, gval.dsize);
00093               free(gval.dptr);
00094        }
00095        return new;
00096 }
00097 
00098 DBA_UPDATE_FUNC(gdbm)
00099 {
00100        datum gval;
00101        GDBM_DATA;
00102 
00103        GDBM_GKEY;
00104        gval.dptr = (char *) val;
00105        gval.dsize = vallen;
00106 
00107        if(gdbm_store(dba->dbf, gkey, gval, 
00108                             mode == 1 ? GDBM_INSERT : GDBM_REPLACE) == 0)
00109               return SUCCESS;
00110        php_error_docref2(NULL TSRMLS_CC, key, val, E_WARNING, "%s", gdbm_strerror(gdbm_errno));
00111        return FAILURE;
00112 }
00113 
00114 DBA_EXISTS_FUNC(gdbm)
00115 {
00116        GDBM_DATA;
00117        GDBM_GKEY;
00118 
00119        return gdbm_exists(dba->dbf, gkey) ? SUCCESS : FAILURE;
00120 }
00121 
00122 DBA_DELETE_FUNC(gdbm)
00123 {
00124        GDBM_DATA;
00125        GDBM_GKEY;
00126        
00127        return gdbm_delete(dba->dbf, gkey) == -1 ? FAILURE : SUCCESS;
00128 }
00129 
00130 DBA_FIRSTKEY_FUNC(gdbm)
00131 {
00132        GDBM_DATA;
00133        datum gkey;
00134        char *key = NULL;
00135 
00136        if(dba->nextkey.dptr) {
00137               free(dba->nextkey.dptr);
00138        }
00139        
00140        gkey = gdbm_firstkey(dba->dbf);
00141        if(gkey.dptr) {
00142               key = estrndup(gkey.dptr, gkey.dsize);
00143               if(newlen) *newlen = gkey.dsize;
00144               dba->nextkey = gkey;
00145        } else {
00146               dba->nextkey.dptr = NULL;
00147        }
00148        return key;
00149 }
00150 
00151 DBA_NEXTKEY_FUNC(gdbm)
00152 {
00153        GDBM_DATA;
00154        char *nkey = NULL;
00155        datum gkey;
00156 
00157        if(!dba->nextkey.dptr) return NULL;
00158        
00159        gkey = gdbm_nextkey(dba->dbf, dba->nextkey);
00160        free(dba->nextkey.dptr);
00161        if(gkey.dptr) {
00162               nkey = estrndup(gkey.dptr, gkey.dsize);
00163               if(newlen) *newlen = gkey.dsize;
00164               dba->nextkey = gkey;
00165        } else {
00166               dba->nextkey.dptr = NULL;
00167        }
00168        return nkey;
00169 }
00170 
00171 DBA_OPTIMIZE_FUNC(gdbm)
00172 {
00173        GDBM_DATA;
00174        gdbm_reorganize(dba->dbf);
00175        return SUCCESS;
00176 }
00177 
00178 DBA_SYNC_FUNC(gdbm) 
00179 {
00180        GDBM_DATA;
00181 
00182        gdbm_sync(dba->dbf);
00183        return SUCCESS;
00184 }
00185 
00186 DBA_INFO_FUNC(gdbm)
00187 {
00188        return estrdup(gdbm_version);
00189 }
00190 
00191 #endif
00192 
00193 /*
00194  * Local variables:
00195  * tab-width: 4
00196  * c-basic-offset: 4
00197  * End:
00198  * vim600: sw=4 ts=4 fdm=marker
00199  * vim<600: sw=4 ts=4
00200  */