xref: /PHP-5.5/ext/dba/dba_inifile.c (revision 73c1be26)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2015 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$ */
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 
DBA_OPEN_FUNC(inifile)53 DBA_OPEN_FUNC(inifile)
54 {
55 	info->dbf = inifile_alloc(info->fp, info->mode == DBA_READER, info->flags&DBA_PERSISTENT TSRMLS_CC);
56 
57 	return info->dbf ? SUCCESS : FAILURE;
58 }
59 
DBA_CLOSE_FUNC(inifile)60 DBA_CLOSE_FUNC(inifile)
61 {
62 	INIFILE_DATA;
63 
64 	inifile_free(dba, info->flags&DBA_PERSISTENT);
65 }
66 
DBA_FETCH_FUNC(inifile)67 DBA_FETCH_FUNC(inifile)
68 {
69 	val_type ini_val;
70 
71 	INIFILE_DATA;
72 	INIFILE_GKEY;
73 
74 	ini_val = inifile_fetch(dba, &ini_key, skip TSRMLS_CC);
75 	*newlen = ini_val.value ? strlen(ini_val.value) : 0;
76 	INIFILE_DONE;
77 	return ini_val.value;
78 }
79 
DBA_UPDATE_FUNC(inifile)80 DBA_UPDATE_FUNC(inifile)
81 {
82 	val_type ini_val;
83 	int res;
84 
85 	INIFILE_DATA;
86 	INIFILE_GKEY;
87 
88 	ini_val.value = val;
89 
90 	if (mode == 1) {
91 		res = inifile_append(dba, &ini_key, &ini_val TSRMLS_CC);
92 	} else {
93 		res = inifile_replace(dba, &ini_key, &ini_val TSRMLS_CC);
94 	}
95 	INIFILE_DONE;
96 	switch(res) {
97 	case -1:
98 		php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Operation not possible");
99 		return FAILURE;
100 	default:
101 	case 0:
102 		return SUCCESS;
103 	case 1:
104 		return FAILURE;
105 	}
106 }
107 
DBA_EXISTS_FUNC(inifile)108 DBA_EXISTS_FUNC(inifile)
109 {
110 	val_type ini_val;
111 
112 	INIFILE_DATA;
113 	INIFILE_GKEY;
114 
115 	ini_val = inifile_fetch(dba, &ini_key, 0 TSRMLS_CC);
116 	INIFILE_DONE;
117 	if (ini_val.value) {
118 		inifile_val_free(&ini_val);
119 		return SUCCESS;
120 	}
121 	return FAILURE;
122 }
123 
DBA_DELETE_FUNC(inifile)124 DBA_DELETE_FUNC(inifile)
125 {
126 	int res;
127 
128 	INIFILE_DATA;
129 	INIFILE_GKEY;
130 
131 	res =  inifile_delete(dba, &ini_key TSRMLS_CC);
132 
133 	INIFILE_DONE;
134 	return (res == -1 ? FAILURE : SUCCESS);
135 }
136 
DBA_FIRSTKEY_FUNC(inifile)137 DBA_FIRSTKEY_FUNC(inifile)
138 {
139 	INIFILE_DATA;
140 
141 	if (inifile_firstkey(dba TSRMLS_CC)) {
142 		char *result = inifile_key_string(&dba->curr.key);
143 		*newlen = strlen(result);
144 		return result;
145 	} else {
146 		return NULL;
147 	}
148 }
149 
DBA_NEXTKEY_FUNC(inifile)150 DBA_NEXTKEY_FUNC(inifile)
151 {
152 	INIFILE_DATA;
153 
154 	if (!dba->curr.key.group && !dba->curr.key.name) {
155 		return NULL;
156 	}
157 
158 	if (inifile_nextkey(dba TSRMLS_CC)) {
159 		char *result = inifile_key_string(&dba->curr.key);
160 		*newlen = strlen(result);
161 		return result;
162 	} else {
163 		return NULL;
164 	}
165 }
166 
DBA_OPTIMIZE_FUNC(inifile)167 DBA_OPTIMIZE_FUNC(inifile)
168 {
169 	/* dummy */
170 	return SUCCESS;
171 }
172 
DBA_SYNC_FUNC(inifile)173 DBA_SYNC_FUNC(inifile)
174 {
175 	/* dummy */
176 	return SUCCESS;
177 }
178 
DBA_INFO_FUNC(inifile)179 DBA_INFO_FUNC(inifile)
180 {
181 	return estrdup(inifile_version());
182 }
183 
184 #endif
185 
186 /*
187  * Local variables:
188  * tab-width: 4
189  * c-basic-offset: 4
190  * End:
191  * vim600: sw=4 ts=4 fdm=marker
192  * vim<600: sw=4 ts=4
193  */
194