Back to index

php5  5.3.10
dba_inifile.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_inifile.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_INIFILE
00028 #include "php_inifile.h"
00029 
00030 #include "libinifile/inifile.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 INIFILE_DATA \
00040        inifile *dba = info->dbf
00041 
00042 #define INIFILE_GKEY \
00043        key_type ini_key; \
00044        if (!key) { \
00045               php_error_docref(NULL TSRMLS_CC, E_WARNING, "No key specified"); \
00046               return 0; \
00047        } \
00048        ini_key = inifile_key_split((char*)key) /* keylen not needed here */
00049 
00050 #define INIFILE_DONE \
00051        inifile_key_free(&ini_key)
00052 
00053 DBA_OPEN_FUNC(inifile)
00054 {
00055        info->dbf = inifile_alloc(info->fp, info->mode == DBA_READER, info->flags&DBA_PERSISTENT TSRMLS_CC);
00056 
00057        return info->dbf ? SUCCESS : FAILURE;
00058 }
00059 
00060 DBA_CLOSE_FUNC(inifile)
00061 {
00062        INIFILE_DATA;
00063 
00064        inifile_free(dba, info->flags&DBA_PERSISTENT);
00065 }
00066 
00067 DBA_FETCH_FUNC(inifile)
00068 {
00069        val_type ini_val;
00070 
00071        INIFILE_DATA;
00072        INIFILE_GKEY;
00073 
00074        ini_val = inifile_fetch(dba, &ini_key, skip TSRMLS_CC);
00075        *newlen = ini_val.value ? strlen(ini_val.value) : 0;
00076        INIFILE_DONE;
00077        return ini_val.value;
00078 }
00079 
00080 DBA_UPDATE_FUNC(inifile)
00081 {
00082        val_type ini_val;
00083        int res;
00084 
00085        INIFILE_DATA;
00086        INIFILE_GKEY;
00087        
00088        ini_val.value = val;
00089        
00090        if (mode == 1) {
00091               res = inifile_append(dba, &ini_key, &ini_val TSRMLS_CC);
00092        } else {
00093               res = inifile_replace(dba, &ini_key, &ini_val TSRMLS_CC);
00094        }
00095        INIFILE_DONE;
00096        switch(res) {
00097        case -1:
00098               php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Operation not possible");
00099               return FAILURE;
00100        default:
00101        case 0:
00102               return SUCCESS;
00103        case 1:
00104               php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Key already exists");
00105               return FAILURE;
00106        }
00107 }
00108 
00109 DBA_EXISTS_FUNC(inifile)
00110 {
00111        val_type ini_val;
00112 
00113        INIFILE_DATA;
00114        INIFILE_GKEY;
00115        
00116        ini_val = inifile_fetch(dba, &ini_key, 0 TSRMLS_CC);
00117        INIFILE_DONE;
00118        if (ini_val.value) {
00119               inifile_val_free(&ini_val);
00120               return SUCCESS;
00121        }
00122        return FAILURE;
00123 }
00124 
00125 DBA_DELETE_FUNC(inifile)
00126 {
00127        int res;
00128 
00129        INIFILE_DATA;
00130        INIFILE_GKEY;
00131 
00132        res =  inifile_delete(dba, &ini_key TSRMLS_CC);
00133 
00134        INIFILE_DONE;
00135        return (res == -1 ? FAILURE : SUCCESS);
00136 }
00137 
00138 DBA_FIRSTKEY_FUNC(inifile)
00139 {
00140        INIFILE_DATA;
00141 
00142        if (inifile_firstkey(dba TSRMLS_CC)) {
00143               char *result = inifile_key_string(&dba->curr.key);
00144               *newlen = strlen(result);
00145               return result;
00146        } else {
00147               return NULL;
00148        }
00149 }
00150 
00151 DBA_NEXTKEY_FUNC(inifile)
00152 {
00153        INIFILE_DATA;
00154        
00155        if (!dba->curr.key.group && !dba->curr.key.name) {
00156               return NULL;
00157        }
00158 
00159        if (inifile_nextkey(dba TSRMLS_CC)) {
00160               char *result = inifile_key_string(&dba->curr.key);
00161               *newlen = strlen(result);
00162               return result;
00163        } else {
00164               return NULL;
00165        }
00166 }
00167 
00168 DBA_OPTIMIZE_FUNC(inifile)
00169 {
00170        /* dummy */
00171        return SUCCESS;
00172 }
00173 
00174 DBA_SYNC_FUNC(inifile)
00175 {
00176        /* dummy */
00177        return SUCCESS;
00178 }
00179 
00180 DBA_INFO_FUNC(inifile)
00181 {
00182        return estrdup(inifile_version());
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  */