2002-11-06 12:16:18 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP Version 4 |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Copyright (c) 1997-2002 The PHP Group |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.02 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
|
|
|
| http://www.php.net/license/2_02.txt. |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: Marcus Boerger <helly@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
|
|
|
|
#if DBA_FLATFILE
|
|
|
|
#include "php_flatfile.h"
|
|
|
|
|
|
|
|
#include "libflatfile/flatfile.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2002-11-08 07:03:16 +08:00
|
|
|
#define FLATFILE_DATA flatfile *dba = info->dbf
|
|
|
|
#define FLATFILE_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
|
2002-11-06 12:16:18 +08:00
|
|
|
|
2002-11-06 17:17:51 +08:00
|
|
|
DBA_OPEN_FUNC(flatfile)
|
2002-11-06 12:16:18 +08:00
|
|
|
{
|
|
|
|
char *fmode;
|
|
|
|
php_stream *fp;
|
|
|
|
|
2002-11-07 01:59:03 +08:00
|
|
|
info->dbf = emalloc(sizeof(flatfile));
|
|
|
|
memset(info->dbf, 0, sizeof(flatfile));
|
2002-11-06 12:16:18 +08:00
|
|
|
|
|
|
|
switch(info->mode) {
|
|
|
|
case DBA_READER:
|
|
|
|
fmode = "r";
|
|
|
|
break;
|
|
|
|
case DBA_WRITER:
|
|
|
|
fmode = "r+b";
|
|
|
|
break;
|
|
|
|
case DBA_CREAT:
|
|
|
|
fmode = "a+b";
|
|
|
|
break;
|
|
|
|
case DBA_TRUNC:
|
|
|
|
fmode = "w+b";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
efree(info->dbf);
|
|
|
|
return FAILURE; /* not possible */
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = php_stream_open_wrapper(info->path, fmode, STREAM_MUST_SEEK|IGNORE_PATH|ENFORCE_SAFE_MODE, NULL);
|
|
|
|
if (!fp) {
|
|
|
|
*error = "Unable to open file";
|
|
|
|
efree(info->dbf);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-11-06 17:17:51 +08:00
|
|
|
((flatfile*)info->dbf)->fp = fp;
|
2002-11-06 12:16:18 +08:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2002-11-06 17:17:51 +08:00
|
|
|
DBA_CLOSE_FUNC(flatfile)
|
2002-11-06 12:16:18 +08:00
|
|
|
{
|
2002-11-08 07:03:16 +08:00
|
|
|
FLATFILE_DATA;
|
2002-11-06 12:16:18 +08:00
|
|
|
|
|
|
|
php_stream_close(dba->fp);
|
|
|
|
if (dba->nextkey.dptr)
|
|
|
|
efree(dba->nextkey.dptr);
|
|
|
|
efree(dba);
|
|
|
|
}
|
|
|
|
|
2002-11-06 17:17:51 +08:00
|
|
|
DBA_FETCH_FUNC(flatfile)
|
2002-11-06 12:16:18 +08:00
|
|
|
{
|
|
|
|
datum gval;
|
|
|
|
char *new = NULL;
|
|
|
|
|
2002-11-08 07:03:16 +08:00
|
|
|
FLATFILE_DATA;
|
|
|
|
FLATFILE_GKEY;
|
|
|
|
|
|
|
|
gval = flatfile_fetch(dba, gkey TSRMLS_CC);
|
2002-11-06 12:16:18 +08:00
|
|
|
if(gval.dptr) {
|
|
|
|
if(newlen) *newlen = gval.dsize;
|
|
|
|
new = estrndup(gval.dptr, gval.dsize);
|
|
|
|
efree(gval.dptr);
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2002-11-06 17:17:51 +08:00
|
|
|
DBA_UPDATE_FUNC(flatfile)
|
2002-11-06 12:16:18 +08:00
|
|
|
{
|
|
|
|
datum gval;
|
|
|
|
|
2002-11-08 07:03:16 +08:00
|
|
|
FLATFILE_DATA;
|
|
|
|
FLATFILE_GKEY;
|
2002-11-06 12:16:18 +08:00
|
|
|
gval.dptr = (char *) val;
|
|
|
|
gval.dsize = vallen;
|
|
|
|
|
2002-11-08 07:03:16 +08:00
|
|
|
switch(flatfile_store(dba, gkey, gval, mode==1 ? FLATFILE_INSERT : FLATFILE_REPLACE TSRMLS_CC)) {
|
|
|
|
case -1:
|
|
|
|
php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Operation not possible");
|
|
|
|
return FAILURE;
|
|
|
|
default:
|
|
|
|
case 0:
|
|
|
|
return SUCCESS;
|
|
|
|
case 1:
|
|
|
|
php_error_docref1(NULL TSRMLS_CC, key, E_WARNING, "Key already exists");
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2002-11-06 12:16:18 +08:00
|
|
|
}
|
|
|
|
|
2002-11-06 17:17:51 +08:00
|
|
|
DBA_EXISTS_FUNC(flatfile)
|
2002-11-06 12:16:18 +08:00
|
|
|
{
|
|
|
|
datum gval;
|
2002-11-08 07:03:16 +08:00
|
|
|
FLATFILE_DATA;
|
|
|
|
FLATFILE_GKEY;
|
2002-11-06 12:16:18 +08:00
|
|
|
|
2002-11-08 07:03:16 +08:00
|
|
|
gval = flatfile_fetch(dba, gkey TSRMLS_CC);
|
2002-11-06 12:16:18 +08:00
|
|
|
if(gval.dptr) {
|
|
|
|
efree(gval.dptr);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-11-06 17:17:51 +08:00
|
|
|
DBA_DELETE_FUNC(flatfile)
|
2002-11-06 12:16:18 +08:00
|
|
|
{
|
2002-11-08 07:03:16 +08:00
|
|
|
FLATFILE_DATA;
|
|
|
|
FLATFILE_GKEY;
|
|
|
|
return(flatfile_delete(dba, gkey TSRMLS_CC) == -1 ? FAILURE : SUCCESS);
|
2002-11-06 12:16:18 +08:00
|
|
|
}
|
|
|
|
|
2002-11-06 17:17:51 +08:00
|
|
|
DBA_FIRSTKEY_FUNC(flatfile)
|
2002-11-06 12:16:18 +08:00
|
|
|
{
|
2002-11-08 07:03:16 +08:00
|
|
|
FLATFILE_DATA;
|
2002-11-06 12:16:18 +08:00
|
|
|
|
|
|
|
if (dba->nextkey.dptr)
|
|
|
|
efree(dba->nextkey.dptr);
|
2002-11-08 07:03:16 +08:00
|
|
|
dba->nextkey = flatfile_firstkey(dba TSRMLS_CC);
|
2002-11-06 12:16:18 +08:00
|
|
|
if(dba->nextkey.dptr) {
|
|
|
|
if(newlen)
|
|
|
|
*newlen = dba->nextkey.dsize;
|
|
|
|
return estrndup(dba->nextkey.dptr, dba->nextkey.dsize);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-11-06 17:17:51 +08:00
|
|
|
DBA_NEXTKEY_FUNC(flatfile)
|
2002-11-06 12:16:18 +08:00
|
|
|
{
|
2002-11-08 07:03:16 +08:00
|
|
|
FLATFILE_DATA;
|
2002-11-06 12:16:18 +08:00
|
|
|
|
|
|
|
if(!dba->nextkey.dptr)
|
|
|
|
return NULL;
|
|
|
|
|
2002-11-08 07:03:16 +08:00
|
|
|
if (dba->nextkey.dptr)
|
|
|
|
efree(dba->nextkey.dptr);
|
|
|
|
dba->nextkey = flatfile_nextkey(dba TSRMLS_CC);
|
2002-11-06 12:16:18 +08:00
|
|
|
if(dba->nextkey.dptr) {
|
|
|
|
if(newlen)
|
|
|
|
*newlen = dba->nextkey.dsize;
|
|
|
|
return estrndup(dba->nextkey.dptr, dba->nextkey.dsize);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-11-06 17:17:51 +08:00
|
|
|
DBA_OPTIMIZE_FUNC(flatfile)
|
2002-11-06 12:16:18 +08:00
|
|
|
{
|
|
|
|
/* dummy */
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2002-11-06 17:17:51 +08:00
|
|
|
DBA_SYNC_FUNC(flatfile)
|
2002-11-06 12:16:18 +08:00
|
|
|
{
|
|
|
|
/* dummy */
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
|
|
|
*/
|