Back to index

php5  5.3.10
dba_flatfile.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: Marcus Boerger <helly@php.net>                               |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: dba_flatfile.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_FLATFILE
00028 #include "php_flatfile.h"
00029 
00030 #include "libflatfile/flatfile.h"
00031 
00032 #ifdef HAVE_UNISTD_H
00033 #include <unistd.h>
00034 #endif
00035 #include <sys/types.h>
00036 #include <sys/stat.h>
00037 #include <fcntl.h>
00038 
00039 #define FLATFILE_DATA flatfile *dba = info->dbf
00040 #define FLATFILE_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
00041 
00042 DBA_OPEN_FUNC(flatfile)
00043 {
00044        info->dbf = pemalloc(sizeof(flatfile), info->flags&DBA_PERSISTENT);
00045        memset(info->dbf, 0, sizeof(flatfile));
00046 
00047        ((flatfile*)info->dbf)->fp = info->fp;
00048 
00049        return SUCCESS;
00050 }
00051 
00052 DBA_CLOSE_FUNC(flatfile)
00053 {
00054        FLATFILE_DATA;
00055 
00056        if (dba->nextkey.dptr) {
00057               efree(dba->nextkey.dptr);
00058        }
00059        pefree(dba, info->flags&DBA_PERSISTENT);
00060 }
00061 
00062 DBA_FETCH_FUNC(flatfile)
00063 {
00064        datum gval;
00065        char *new = NULL;
00066 
00067        FLATFILE_DATA;
00068        FLATFILE_GKEY;
00069 
00070        gval = flatfile_fetch(dba, gkey TSRMLS_CC);
00071        if (gval.dptr) {
00072               if (newlen) {
00073                      *newlen = gval.dsize;
00074               }
00075               new = estrndup(gval.dptr, gval.dsize);
00076               efree(gval.dptr);
00077        }
00078        return new;
00079 }
00080 
00081 DBA_UPDATE_FUNC(flatfile)
00082 {
00083        datum gval;
00084 
00085        FLATFILE_DATA;
00086        FLATFILE_GKEY;
00087        gval.dptr = (char *) val;
00088        gval.dsize = vallen;
00089        
00090        switch(flatfile_store(dba, gkey, gval, mode==1 ? FLATFILE_INSERT : FLATFILE_REPLACE TSRMLS_CC)) {
00091        case -1:
00092               php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Operation not possible");
00093               return FAILURE;
00094        default:
00095        case 0:
00096               return SUCCESS;
00097        case 1:
00098               php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Key already exists");
00099               return FAILURE;
00100        }
00101 }
00102 
00103 DBA_EXISTS_FUNC(flatfile)
00104 {
00105        datum gval;
00106        FLATFILE_DATA;
00107        FLATFILE_GKEY;
00108        
00109        gval = flatfile_fetch(dba, gkey TSRMLS_CC);
00110        if (gval.dptr) {
00111               efree(gval.dptr);
00112               return SUCCESS;
00113        }
00114        return FAILURE;
00115 }
00116 
00117 DBA_DELETE_FUNC(flatfile)
00118 {
00119        FLATFILE_DATA;
00120        FLATFILE_GKEY;
00121        return(flatfile_delete(dba, gkey TSRMLS_CC) == -1 ? FAILURE : SUCCESS);
00122 }
00123 
00124 DBA_FIRSTKEY_FUNC(flatfile)
00125 {
00126        FLATFILE_DATA;
00127 
00128        if (dba->nextkey.dptr) {
00129               efree(dba->nextkey.dptr);
00130        }
00131        dba->nextkey = flatfile_firstkey(dba TSRMLS_CC);
00132        if (dba->nextkey.dptr) {
00133               if (newlen)  {
00134                      *newlen = dba->nextkey.dsize;
00135               }
00136               return estrndup(dba->nextkey.dptr, dba->nextkey.dsize);
00137        }
00138        return NULL;
00139 }
00140 
00141 DBA_NEXTKEY_FUNC(flatfile)
00142 {
00143        FLATFILE_DATA;
00144        
00145        if (!dba->nextkey.dptr) {
00146               return NULL;
00147        }
00148        
00149        if (dba->nextkey.dptr) {
00150               efree(dba->nextkey.dptr);
00151        }
00152        dba->nextkey = flatfile_nextkey(dba TSRMLS_CC);
00153        if (dba->nextkey.dptr) {
00154               if (newlen) {
00155                      *newlen = dba->nextkey.dsize;
00156               }
00157               return estrndup(dba->nextkey.dptr, dba->nextkey.dsize);
00158        }
00159        return NULL;
00160 }
00161 
00162 DBA_OPTIMIZE_FUNC(flatfile)
00163 {
00164        /* dummy */
00165        return SUCCESS;
00166 }
00167 
00168 DBA_SYNC_FUNC(flatfile)
00169 {
00170        /* dummy */
00171        return SUCCESS;
00172 }
00173 
00174 DBA_INFO_FUNC(flatfile)
00175 {
00176        return estrdup(flatfile_version());
00177 }
00178 
00179 #endif
00180 
00181 /*
00182  * Local variables:
00183  * tab-width: 4
00184  * c-basic-offset: 4
00185  * End:
00186  * vim600: sw=4 ts=4 fdm=marker
00187  * vim<600: sw=4 ts=4
00188  */