xref: /PHP-5.5/ext/dba/dba_qdbm.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: Marcin Gibula <mg@iceni.pl>                                  |
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_QDBM
28 #include "php_qdbm.h"
29 
30 #ifdef QDBM_INCLUDE_FILE
31 #include QDBM_INCLUDE_FILE
32 #endif
33 
34 #define QDBM_DATA dba_qdbm_data *dba = info->dbf
35 
36 typedef struct {
37 	DEPOT *dbf;
38 } dba_qdbm_data;
39 
DBA_OPEN_FUNC(qdbm)40 DBA_OPEN_FUNC(qdbm)
41 {
42 	DEPOT *dbf;
43 
44 	switch(info->mode) {
45 		case DBA_READER:
46 			dbf = dpopen(info->path, DP_OREADER, 0);
47 			break;
48 		case DBA_WRITER:
49 			dbf = dpopen(info->path, DP_OWRITER, 0);
50 			break;
51 		case DBA_CREAT:
52 			dbf = dpopen(info->path, DP_OWRITER | DP_OCREAT, 0);
53 			break;
54 		case DBA_TRUNC:
55 			dbf = dpopen(info->path, DP_OWRITER | DP_OCREAT | DP_OTRUNC, 0);
56 			break;
57 		default:
58 			return FAILURE;
59 	}
60 
61 	if (dbf) {
62 		info->dbf = pemalloc(sizeof(dba_qdbm_data), info->flags & DBA_PERSISTENT);
63 		memset(info->dbf, 0, sizeof(dba_qdbm_data));
64 		((dba_qdbm_data *) info->dbf)->dbf = dbf;
65 		return SUCCESS;
66 	}
67 
68 	*error = (char *) dperrmsg(dpecode);
69 	return FAILURE;
70 }
71 
DBA_CLOSE_FUNC(qdbm)72 DBA_CLOSE_FUNC(qdbm)
73 {
74 	QDBM_DATA;
75 
76 	dpclose(dba->dbf);
77 	pefree(dba, info->flags & DBA_PERSISTENT);
78 }
79 
DBA_FETCH_FUNC(qdbm)80 DBA_FETCH_FUNC(qdbm)
81 {
82 	QDBM_DATA;
83 	char *value, *new = NULL;
84 	int value_size;
85 
86 	value = dpget(dba->dbf, key, keylen, 0, -1, &value_size);
87 	if (value) {
88 		if (newlen) *newlen = value_size;
89 		new = estrndup(value, value_size);
90 		free(value);
91 	}
92 
93 	return new;
94 }
95 
DBA_UPDATE_FUNC(qdbm)96 DBA_UPDATE_FUNC(qdbm)
97 {
98 	QDBM_DATA;
99 
100 	if (dpput(dba->dbf, key, keylen, val, vallen, mode == 1 ? DP_DKEEP : DP_DOVER)) {
101 		return SUCCESS;
102 	}
103 
104 	if (dpecode != DP_EKEEP) {
105 		php_error_docref2(NULL TSRMLS_CC, key, val, E_WARNING, "%s", dperrmsg(dpecode));
106 	}
107 
108 	return FAILURE;
109 }
110 
DBA_EXISTS_FUNC(qdbm)111 DBA_EXISTS_FUNC(qdbm)
112 {
113 	QDBM_DATA;
114 	char *value;
115 
116 	value = dpget(dba->dbf, key, keylen, 0, -1, NULL);
117 	if (value) {
118 		free(value);
119 		return SUCCESS;
120 	}
121 
122 	return FAILURE;
123 }
124 
DBA_DELETE_FUNC(qdbm)125 DBA_DELETE_FUNC(qdbm)
126 {
127 	QDBM_DATA;
128 
129 	return dpout(dba->dbf, key, keylen) ? SUCCESS : FAILURE;
130 }
131 
DBA_FIRSTKEY_FUNC(qdbm)132 DBA_FIRSTKEY_FUNC(qdbm)
133 {
134 	QDBM_DATA;
135 	int value_size;
136 	char *value, *new = NULL;
137 
138 	dpiterinit(dba->dbf);
139 
140 	value = dpiternext(dba->dbf, &value_size);
141 	if (value) {
142 		if (newlen) *newlen = value_size;
143 		new = estrndup(value, value_size);
144 		free(value);
145 	}
146 
147 	return new;
148 }
149 
DBA_NEXTKEY_FUNC(qdbm)150 DBA_NEXTKEY_FUNC(qdbm)
151 {
152 	QDBM_DATA;
153 	int value_size;
154 	char *value, *new = NULL;
155 
156 	value = dpiternext(dba->dbf, &value_size);
157 	if (value) {
158 		if (newlen) *newlen = value_size;
159 		new = estrndup(value, value_size);
160 		free(value);
161 	}
162 
163 	return new;
164 }
165 
DBA_OPTIMIZE_FUNC(qdbm)166 DBA_OPTIMIZE_FUNC(qdbm)
167 {
168 	QDBM_DATA;
169 
170 	dpoptimize(dba->dbf, 0);
171 	return SUCCESS;
172 }
173 
DBA_SYNC_FUNC(qdbm)174 DBA_SYNC_FUNC(qdbm)
175 {
176 	QDBM_DATA;
177 
178 	dpsync(dba->dbf);
179 	return SUCCESS;
180 }
181 
DBA_INFO_FUNC(qdbm)182 DBA_INFO_FUNC(qdbm)
183 {
184 	return estrdup(dpversion);
185 }
186 
187 #endif
188 
189 /*
190  * Local variables:
191  * tab-width: 4
192  * c-basic-offset: 4
193  * End:
194  * vim600: sw=4 ts=4 fdm=marker
195  * vim<600: sw=4 ts=4
196  */
197