Back to index

php5  5.3.10
dba_qdbm.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: Marcin Gibula <mg@iceni.pl>                                  |
00016   +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: dba_qdbm.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_QDBM
00028 #include "php_qdbm.h"
00029 
00030 #ifdef QDBM_INCLUDE_FILE
00031 #include QDBM_INCLUDE_FILE
00032 #endif
00033 
00034 #define QDBM_DATA dba_qdbm_data *dba = info->dbf
00035 
00036 typedef struct {
00037        DEPOT *dbf;
00038 } dba_qdbm_data;
00039 
00040 DBA_OPEN_FUNC(qdbm)
00041 {
00042        DEPOT *dbf;
00043 
00044        switch(info->mode) {
00045               case DBA_READER:
00046                      dbf = dpopen(info->path, DP_OREADER, 0);
00047                      break;
00048               case DBA_WRITER:
00049                      dbf = dpopen(info->path, DP_OWRITER, 0);
00050                      break;
00051               case DBA_CREAT:
00052                      dbf = dpopen(info->path, DP_OWRITER | DP_OCREAT, 0);
00053                      break;
00054               case DBA_TRUNC:
00055                      dbf = dpopen(info->path, DP_OWRITER | DP_OCREAT | DP_OTRUNC, 0);
00056                      break;
00057               default:
00058                      return FAILURE;
00059        }
00060 
00061        if (dbf) {
00062               info->dbf = pemalloc(sizeof(dba_qdbm_data), info->flags & DBA_PERSISTENT);
00063               memset(info->dbf, 0, sizeof(dba_qdbm_data));
00064               ((dba_qdbm_data *) info->dbf)->dbf = dbf;
00065               return SUCCESS;
00066        }
00067 
00068        *error = (char *) dperrmsg(dpecode);
00069        return FAILURE;
00070 }
00071 
00072 DBA_CLOSE_FUNC(qdbm)
00073 {
00074        QDBM_DATA;
00075 
00076        dpclose(dba->dbf);
00077        pefree(dba, info->flags & DBA_PERSISTENT);
00078 }
00079 
00080 DBA_FETCH_FUNC(qdbm)
00081 {
00082        QDBM_DATA;
00083        char *value, *new = NULL;
00084        int value_size;
00085 
00086        value = dpget(dba->dbf, key, keylen, 0, -1, &value_size);
00087        if (value) {
00088               if (newlen) *newlen = value_size;
00089               new = estrndup(value, value_size);
00090               free(value);
00091        }
00092 
00093        return new;
00094 }
00095 
00096 DBA_UPDATE_FUNC(qdbm)
00097 {
00098        QDBM_DATA;
00099        int result;
00100 
00101        result = dpput(dba->dbf, key, keylen, val, vallen, mode == 1 ? DP_DKEEP : DP_DOVER);
00102        if (result)
00103               return SUCCESS;
00104 
00105        php_error_docref2(NULL TSRMLS_CC, key, val, E_WARNING, "%s", dperrmsg(dpecode));
00106        return FAILURE;
00107 }
00108 
00109 DBA_EXISTS_FUNC(qdbm)
00110 {
00111        QDBM_DATA;
00112        char *value;
00113 
00114        value = dpget(dba->dbf, key, keylen, 0, -1, NULL);
00115        if (value) {
00116               free(value);
00117               return SUCCESS;
00118        }
00119 
00120        return FAILURE;
00121 }
00122 
00123 DBA_DELETE_FUNC(qdbm)
00124 {
00125        QDBM_DATA;
00126 
00127        return dpout(dba->dbf, key, keylen) ? SUCCESS : FAILURE;
00128 }
00129 
00130 DBA_FIRSTKEY_FUNC(qdbm)
00131 {
00132        QDBM_DATA;
00133        int value_size;
00134        char *value, *new = NULL;
00135 
00136        dpiterinit(dba->dbf);
00137 
00138        value = dpiternext(dba->dbf, &value_size);
00139        if (value) {
00140               if (newlen) *newlen = value_size;
00141               new = estrndup(value, value_size);
00142               free(value);
00143        }
00144 
00145        return new;
00146 }
00147 
00148 DBA_NEXTKEY_FUNC(qdbm)
00149 {
00150        QDBM_DATA;
00151        int value_size;
00152        char *value, *new = NULL;
00153 
00154        value = dpiternext(dba->dbf, &value_size);
00155        if (value) {
00156               if (newlen) *newlen = value_size;
00157               new = estrndup(value, value_size);
00158               free(value);
00159        }
00160 
00161        return new;
00162 }
00163 
00164 DBA_OPTIMIZE_FUNC(qdbm)
00165 {
00166        QDBM_DATA;
00167 
00168        dpoptimize(dba->dbf, 0);
00169        return SUCCESS;
00170 }
00171 
00172 DBA_SYNC_FUNC(qdbm)
00173 {
00174        QDBM_DATA;
00175 
00176        dpsync(dba->dbf);
00177        return SUCCESS;
00178 }
00179 
00180 DBA_INFO_FUNC(qdbm)
00181 {
00182        return estrdup(dpversion);
00183 }
00184 
00185 #endif
00186 
00187 /*
00188  * Local variables:
00189  * tab-width: 4
00190  * c-basic-offset: 4
00191  * End:
00192  * vim600: sw=4 ts=4 fdm=marker
00193  * vim<600: sw=4 ts=4
00194  */