xref: /PHP-5.3/ext/dba/dba_qdbm.c (revision a2045ff3)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2013 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 	int result;
100 
101 	result = dpput(dba->dbf, key, keylen, val, vallen, mode == 1 ? DP_DKEEP : DP_DOVER);
102 	if (result)
103 		return SUCCESS;
104 
105 	php_error_docref2(NULL TSRMLS_CC, key, val, E_WARNING, "%s", dperrmsg(dpecode));
106 	return FAILURE;
107 }
108 
DBA_EXISTS_FUNC(qdbm)109 DBA_EXISTS_FUNC(qdbm)
110 {
111 	QDBM_DATA;
112 	char *value;
113 
114 	value = dpget(dba->dbf, key, keylen, 0, -1, NULL);
115 	if (value) {
116 		free(value);
117 		return SUCCESS;
118 	}
119 
120 	return FAILURE;
121 }
122 
DBA_DELETE_FUNC(qdbm)123 DBA_DELETE_FUNC(qdbm)
124 {
125 	QDBM_DATA;
126 
127 	return dpout(dba->dbf, key, keylen) ? SUCCESS : FAILURE;
128 }
129 
DBA_FIRSTKEY_FUNC(qdbm)130 DBA_FIRSTKEY_FUNC(qdbm)
131 {
132 	QDBM_DATA;
133 	int value_size;
134 	char *value, *new = NULL;
135 
136 	dpiterinit(dba->dbf);
137 
138 	value = dpiternext(dba->dbf, &value_size);
139 	if (value) {
140 		if (newlen) *newlen = value_size;
141 		new = estrndup(value, value_size);
142 		free(value);
143 	}
144 
145 	return new;
146 }
147 
DBA_NEXTKEY_FUNC(qdbm)148 DBA_NEXTKEY_FUNC(qdbm)
149 {
150 	QDBM_DATA;
151 	int value_size;
152 	char *value, *new = NULL;
153 
154 	value = dpiternext(dba->dbf, &value_size);
155 	if (value) {
156 		if (newlen) *newlen = value_size;
157 		new = estrndup(value, value_size);
158 		free(value);
159 	}
160 
161 	return new;
162 }
163 
DBA_OPTIMIZE_FUNC(qdbm)164 DBA_OPTIMIZE_FUNC(qdbm)
165 {
166 	QDBM_DATA;
167 
168 	dpoptimize(dba->dbf, 0);
169 	return SUCCESS;
170 }
171 
DBA_SYNC_FUNC(qdbm)172 DBA_SYNC_FUNC(qdbm)
173 {
174 	QDBM_DATA;
175 
176 	dpsync(dba->dbf);
177 	return SUCCESS;
178 }
179 
DBA_INFO_FUNC(qdbm)180 DBA_INFO_FUNC(qdbm)
181 {
182 	return estrdup(dpversion);
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