xref: /PHP-5.4/ext/dba/dba_inifile.c (revision c0d060f5)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2014 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 		php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Key already exists");
105 		return FAILURE;
106 	}
107 }
108 
DBA_EXISTS_FUNC(inifile)109 DBA_EXISTS_FUNC(inifile)
110 {
111 	val_type ini_val;
112 
113 	INIFILE_DATA;
114 	INIFILE_GKEY;
115 
116 	ini_val = inifile_fetch(dba, &ini_key, 0 TSRMLS_CC);
117 	INIFILE_DONE;
118 	if (ini_val.value) {
119 		inifile_val_free(&ini_val);
120 		return SUCCESS;
121 	}
122 	return FAILURE;
123 }
124 
DBA_DELETE_FUNC(inifile)125 DBA_DELETE_FUNC(inifile)
126 {
127 	int res;
128 
129 	INIFILE_DATA;
130 	INIFILE_GKEY;
131 
132 	res =  inifile_delete(dba, &ini_key TSRMLS_CC);
133 
134 	INIFILE_DONE;
135 	return (res == -1 ? FAILURE : SUCCESS);
136 }
137 
DBA_FIRSTKEY_FUNC(inifile)138 DBA_FIRSTKEY_FUNC(inifile)
139 {
140 	INIFILE_DATA;
141 
142 	if (inifile_firstkey(dba TSRMLS_CC)) {
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 TSRMLS_CC)) {
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