xref: /PHP-7.1/ext/dba/dba_inifile.c (revision ccd4716e)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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, 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);
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);
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);
92 	} else {
93 		res = inifile_replace(dba, &ini_key, &ini_val);
94 	}
95 	INIFILE_DONE;
96 	switch(res) {
97 	case -1:
98 		php_error_docref1(NULL, 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);
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 	zend_bool found = 0;
128 
129 	INIFILE_DATA;
130 	INIFILE_GKEY;
131 
132 	res =  inifile_delete_ex(dba, &ini_key, &found);
133 
134 	INIFILE_DONE;
135 	return (res == -1 || !found ? FAILURE : SUCCESS);
136 }
137 
DBA_FIRSTKEY_FUNC(inifile)138 DBA_FIRSTKEY_FUNC(inifile)
139 {
140 	INIFILE_DATA;
141 
142 	if (inifile_firstkey(dba)) {
143 		char *result = inifile_key_string(&dba->curr.key);
144 		*newlen = strlen(result);
145 		return result;
146 	} else {
147 		return NULL;
148 	}
149 }
150 
DBA_NEXTKEY_FUNC(inifile)151 DBA_NEXTKEY_FUNC(inifile)
152 {
153 	INIFILE_DATA;
154 
155 	if (!dba->curr.key.group && !dba->curr.key.name) {
156 		return NULL;
157 	}
158 
159 	if (inifile_nextkey(dba)) {
160 		char *result = inifile_key_string(&dba->curr.key);
161 		*newlen = strlen(result);
162 		return result;
163 	} else {
164 		return NULL;
165 	}
166 }
167 
DBA_OPTIMIZE_FUNC(inifile)168 DBA_OPTIMIZE_FUNC(inifile)
169 {
170 	/* dummy */
171 	return SUCCESS;
172 }
173 
DBA_SYNC_FUNC(inifile)174 DBA_SYNC_FUNC(inifile)
175 {
176 	/* dummy */
177 	return SUCCESS;
178 }
179 
DBA_INFO_FUNC(inifile)180 DBA_INFO_FUNC(inifile)
181 {
182 	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  */
195