xref: /PHP-5.4/ext/dba/dba_flatfile.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_FLATFILE
28 #include "php_flatfile.h"
29 
30 #include "libflatfile/flatfile.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 FLATFILE_DATA flatfile *dba = info->dbf
40 #define FLATFILE_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
41 
DBA_OPEN_FUNC(flatfile)42 DBA_OPEN_FUNC(flatfile)
43 {
44 	info->dbf = pemalloc(sizeof(flatfile), info->flags&DBA_PERSISTENT);
45 	memset(info->dbf, 0, sizeof(flatfile));
46 
47 	((flatfile*)info->dbf)->fp = info->fp;
48 
49 	return SUCCESS;
50 }
51 
DBA_CLOSE_FUNC(flatfile)52 DBA_CLOSE_FUNC(flatfile)
53 {
54 	FLATFILE_DATA;
55 
56 	if (dba->nextkey.dptr) {
57 		efree(dba->nextkey.dptr);
58 	}
59 	pefree(dba, info->flags&DBA_PERSISTENT);
60 }
61 
DBA_FETCH_FUNC(flatfile)62 DBA_FETCH_FUNC(flatfile)
63 {
64 	datum gval;
65 	char *new = NULL;
66 
67 	FLATFILE_DATA;
68 	FLATFILE_GKEY;
69 
70 	gval = flatfile_fetch(dba, gkey TSRMLS_CC);
71 	if (gval.dptr) {
72 		if (newlen) {
73 			*newlen = gval.dsize;
74 		}
75 		new = estrndup(gval.dptr, gval.dsize);
76 		efree(gval.dptr);
77 	}
78 	return new;
79 }
80 
DBA_UPDATE_FUNC(flatfile)81 DBA_UPDATE_FUNC(flatfile)
82 {
83 	datum gval;
84 
85 	FLATFILE_DATA;
86 	FLATFILE_GKEY;
87 	gval.dptr = (char *) val;
88 	gval.dsize = vallen;
89 
90 	switch(flatfile_store(dba, gkey, gval, mode==1 ? FLATFILE_INSERT : FLATFILE_REPLACE TSRMLS_CC)) {
91 	case -1:
92 		php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Operation not possible");
93 		return FAILURE;
94 	default:
95 	case 0:
96 		return SUCCESS;
97 	case 1:
98 		php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Key already exists");
99 		return FAILURE;
100 	}
101 }
102 
DBA_EXISTS_FUNC(flatfile)103 DBA_EXISTS_FUNC(flatfile)
104 {
105 	datum gval;
106 	FLATFILE_DATA;
107 	FLATFILE_GKEY;
108 
109 	gval = flatfile_fetch(dba, gkey TSRMLS_CC);
110 	if (gval.dptr) {
111 		efree(gval.dptr);
112 		return SUCCESS;
113 	}
114 	return FAILURE;
115 }
116 
DBA_DELETE_FUNC(flatfile)117 DBA_DELETE_FUNC(flatfile)
118 {
119 	FLATFILE_DATA;
120 	FLATFILE_GKEY;
121 	return(flatfile_delete(dba, gkey TSRMLS_CC) == -1 ? FAILURE : SUCCESS);
122 }
123 
DBA_FIRSTKEY_FUNC(flatfile)124 DBA_FIRSTKEY_FUNC(flatfile)
125 {
126 	FLATFILE_DATA;
127 
128 	if (dba->nextkey.dptr) {
129 		efree(dba->nextkey.dptr);
130 	}
131 	dba->nextkey = flatfile_firstkey(dba TSRMLS_CC);
132 	if (dba->nextkey.dptr) {
133 		if (newlen)  {
134 			*newlen = dba->nextkey.dsize;
135 		}
136 		return estrndup(dba->nextkey.dptr, dba->nextkey.dsize);
137 	}
138 	return NULL;
139 }
140 
DBA_NEXTKEY_FUNC(flatfile)141 DBA_NEXTKEY_FUNC(flatfile)
142 {
143 	FLATFILE_DATA;
144 
145 	if (!dba->nextkey.dptr) {
146 		return NULL;
147 	}
148 
149 	if (dba->nextkey.dptr) {
150 		efree(dba->nextkey.dptr);
151 	}
152 	dba->nextkey = flatfile_nextkey(dba TSRMLS_CC);
153 	if (dba->nextkey.dptr) {
154 		if (newlen) {
155 			*newlen = dba->nextkey.dsize;
156 		}
157 		return estrndup(dba->nextkey.dptr, dba->nextkey.dsize);
158 	}
159 	return NULL;
160 }
161 
DBA_OPTIMIZE_FUNC(flatfile)162 DBA_OPTIMIZE_FUNC(flatfile)
163 {
164 	/* dummy */
165 	return SUCCESS;
166 }
167 
DBA_SYNC_FUNC(flatfile)168 DBA_SYNC_FUNC(flatfile)
169 {
170 	/* dummy */
171 	return SUCCESS;
172 }
173 
DBA_INFO_FUNC(flatfile)174 DBA_INFO_FUNC(flatfile)
175 {
176 	return estrdup(flatfile_version());
177 }
178 
179 #endif
180 
181 /*
182  * Local variables:
183  * tab-width: 4
184  * c-basic-offset: 4
185  * End:
186  * vim600: sw=4 ts=4 fdm=marker
187  * vim<600: sw=4 ts=4
188  */
189