1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Author: Marcus Boerger <helly@php.net> |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : /* $Id: dba_inifile.c 272374 2008-12-31 11:17:49Z sebastian $ */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : #include "config.h"
23 : #endif
24 :
25 : #include "php.h"
26 :
27 : #if DBA_INIFILE
28 : #include "php_inifile.h"
29 :
30 : #include "libinifile/inifile.h"
31 :
32 : #ifdef HAVE_UNISTD_H
33 : #include <unistd.h>
34 : #endif
35 : #include <sys/types.h>
36 : #include <sys/stat.h>
37 : #include <fcntl.h>
38 :
39 : #define INIFILE_DATA \
40 : inifile *dba = info->dbf
41 :
42 : #define INIFILE_GKEY \
43 : key_type ini_key; \
44 : if (!key) { \
45 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "No key specified"); \
46 : return 0; \
47 : } \
48 : ini_key = inifile_key_split((char*)key) /* keylen not needed here */
49 :
50 : #define INIFILE_DONE \
51 : inifile_key_free(&ini_key)
52 :
53 : DBA_OPEN_FUNC(inifile)
54 8 : {
55 8 : info->dbf = inifile_alloc(info->fp, info->mode == DBA_READER, info->flags&DBA_PERSISTENT TSRMLS_CC);
56 :
57 8 : return info->dbf ? SUCCESS : FAILURE;
58 : }
59 :
60 : DBA_CLOSE_FUNC(inifile)
61 8 : {
62 8 : INIFILE_DATA;
63 :
64 8 : inifile_free(dba, info->flags&DBA_PERSISTENT);
65 8 : }
66 :
67 : DBA_FETCH_FUNC(inifile)
68 16 : {
69 : val_type ini_val;
70 :
71 16 : INIFILE_DATA;
72 16 : INIFILE_GKEY;
73 :
74 16 : ini_val = inifile_fetch(dba, &ini_key, skip TSRMLS_CC);
75 16 : *newlen = ini_val.value ? strlen(ini_val.value) : 0;
76 16 : INIFILE_DONE;
77 16 : return ini_val.value;
78 : }
79 :
80 : DBA_UPDATE_FUNC(inifile)
81 18 : {
82 : val_type ini_val;
83 : int res;
84 :
85 18 : INIFILE_DATA;
86 18 : INIFILE_GKEY;
87 :
88 18 : ini_val.value = val;
89 :
90 18 : if (mode == 1) {
91 14 : res = inifile_append(dba, &ini_key, &ini_val TSRMLS_CC);
92 : } else {
93 4 : res = inifile_replace(dba, &ini_key, &ini_val TSRMLS_CC);
94 : }
95 18 : INIFILE_DONE;
96 18 : switch(res) {
97 : case -1:
98 0 : php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Operation not possible");
99 0 : return FAILURE;
100 : default:
101 : case 0:
102 18 : return SUCCESS;
103 : case 1:
104 0 : php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Key already exists");
105 0 : return SUCCESS;
106 : }
107 : }
108 :
109 : DBA_EXISTS_FUNC(inifile)
110 10 : {
111 : val_type ini_val;
112 :
113 10 : INIFILE_DATA;
114 10 : INIFILE_GKEY;
115 :
116 10 : ini_val = inifile_fetch(dba, &ini_key, 0 TSRMLS_CC);
117 10 : INIFILE_DONE;
118 10 : if (ini_val.value) {
119 6 : inifile_val_free(&ini_val);
120 6 : return SUCCESS;
121 : }
122 4 : return FAILURE;
123 : }
124 :
125 : DBA_DELETE_FUNC(inifile)
126 6 : {
127 : int res;
128 :
129 6 : INIFILE_DATA;
130 6 : INIFILE_GKEY;
131 :
132 6 : res = inifile_delete(dba, &ini_key TSRMLS_CC);
133 :
134 6 : INIFILE_DONE;
135 6 : return (res == -1 ? FAILURE : SUCCESS);
136 : }
137 :
138 : DBA_FIRSTKEY_FUNC(inifile)
139 4 : {
140 4 : INIFILE_DATA;
141 :
142 4 : if (inifile_firstkey(dba TSRMLS_CC)) {
143 4 : char *result = inifile_key_string(&dba->curr.key);
144 4 : *newlen = strlen(result);
145 4 : return result;
146 : } else {
147 0 : return NULL;
148 : }
149 : }
150 :
151 : DBA_NEXTKEY_FUNC(inifile)
152 14 : {
153 14 : INIFILE_DATA;
154 :
155 14 : if (!dba->curr.key.group && !dba->curr.key.name) {
156 0 : return NULL;
157 : }
158 :
159 14 : if (inifile_nextkey(dba TSRMLS_CC)) {
160 10 : char *result = inifile_key_string(&dba->curr.key);
161 10 : *newlen = strlen(result);
162 10 : return result;
163 : } else {
164 4 : return NULL;
165 : }
166 : }
167 :
168 : DBA_OPTIMIZE_FUNC(inifile)
169 0 : {
170 : /* dummy */
171 0 : return SUCCESS;
172 : }
173 :
174 : DBA_SYNC_FUNC(inifile)
175 0 : {
176 : /* dummy */
177 0 : return SUCCESS;
178 : }
179 :
180 : DBA_INFO_FUNC(inifile)
181 0 : {
182 0 : return estrdup(inifile_version());
183 : }
184 :
185 : #endif
186 :
187 : /*
188 : * Local variables:
189 : * tab-width: 4
190 : * c-basic-offset: 4
191 : * End:
192 : * vim600: sw=4 ts=4 fdm=marker
193 : * vim<600: sw=4 ts=4
194 : */
|