I wonder that it is possible to store severl data under one key. for example:
(key) : (data)
fruit = apple,orange,banada
vegetalbe = carot,onion,
...
....
and above source is just for storing data.Therefore if I want to make a program for just reading berkeley db data, how can I make it ? I made following these sources but it doesn't work. That is,i want to make two program Data is stored by input.c and data is read by reading.c
== input.c ===
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <db.h>
#define DATABASE "access.db"
#define SSIZE 256
typedef struct _content{
char RE[SSIZE];
char REURL[SSIZE];
char ACCEL[SSIZE];
} CONTENT;
CONTENT content;
static char* _cpBase64Encoding =
"ABCDEFGHIJKLMNOPQRSTUVWXY
Zabcdefghi
jklmnopqrs
tuvwxyz012
3456789+/=
";
void Base64Encode( char* cpInput, char* cpOutput )
{
int nIdx[ 4 ];
while ( '\0' != *cpInput )
{
nIdx[0] = ((*cpInput) & 0xFC)>>2;
nIdx[1] = ((*cpInput) & 0x03)<<4;
cpInput++;
if ( '\0' != *cpInput )
{
nIdx[1] |= ((*cpInput) & 0xF0)>>4;
nIdx[2] = ((*cpInput) & 0x0F)<<2;
cpInput++;
if ( '\0' != (*cpInput) )
{
nIdx[2] |= ((*cpInput) & 0xC0) >> 6;
nIdx[3] = (*cpInput) & 0x3F;
cpInput++;
}
else
nIdx[3] = 64;
}
else
{
nIdx[2] = 64;
nIdx[3] = 64;
}
*(cpOutput+0) = *(_cpBase64Encoding + nIdx[0]);
*(cpOutput+1) = *(_cpBase64Encoding + nIdx[1]);
*(cpOutput+2) = *(_cpBase64Encoding + nIdx[2]);
*(cpOutput+3) = *(_cpBase64Encoding + nIdx[3]);
cpOutput += 4;
}
*cpOutput = '\0';
return;
}
int main (){
FILE *fp;
DB *dbp;
DBT key,data;
int ret,t_ret;
int size=600;
char buf[size];
char sNonCoded[256];
char ssNonCoded[256];
char sssNonCoded[256];
char ssssNonCoded[256];
char sEncoded[256];
char ssEncoded[256];
char sssEncoded[256];
char ssssEncoded[256];
char regex[6];
char redirect[512];
char redirect_url[512];
char accel[512];
//Read data
if((fp=fopen("2.txt","rt")
) == NULL){
printf("Error: Cannot open data file\n");
return(1);
}
// Create Berkeley DB
if ((ret = db_create(&dbp, NULL, 0)) != 0) {
fprintf(stderr, "db_create: %s\n", db_strerror(ret));
exit (1);
}
if ((ret = dbp->open(
dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
dbp->err(dbp, ret, "%s", DATABASE);
goto err;
}
while(fgets(buf,size-1,fp)
){
sscanf(buf,"%6s %512s %512s %512s",regex,redirect,redi
rect_url,a
ccel);
strcpy(sNonCoded,regex);
strcpy(ssNonCoded,redirect
);
strcpy(sssNonCoded,redirec
t_url);
strcpy(ssssNonCoded,accel)
;
Base64Encode( sNonCoded, sEncoded );
Base64Encode( ssNonCoded, ssEncoded );
Base64Encode( sssNonCoded, sssEncoded );
Base64Encode( ssssNonCoded, ssssEncoded);
memset(&key,0,sizeof(key))
;
memset(&data,0,sizeof(data
));
key.data = sEncoded;
key.size = sizeof(sEncoded);
strncpy(content.RE,ssEncod
ed,SSIZE);
strncpy(content.REURL,sssE
ncoded,SSI
ZE);
strncpy(content.ACCEL,ssss
Encoded,SS
IZE);
data.data = (char *)&content;
data.size = sizeof(CONTENT);
if ((ret = dbp->put(dbp,NULL,&key,&da
ta,0))==0)
{
printf("db: %s: field 1 stored.\n",(char *)key.data);
printf("db: %s: field 2 stored.\n",content.RE);
printf("db: %s: field 3 stored.\n",content.REURL);
printf("db: %s: field 4 stored.\n",content.ACCEL);
}else{
printf("Error");
dbp->err(dbp,ret,"DB->put"
);
goto err;
}
}
err: if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
ret = t_ret;
exit(ret);
}
== reading.c =====
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <db.h>
#define DATABASE "access.db"
#define SSIZE 256
typedef struct _content{
char RE[SSIZE];
char REURL[SSIZE];
char ACCEL[SSIZE];
} CONTENT;
CONTENT content;
int main (){
FILE *fp;
DB *dbp;
DBT key,data;
int ret,t_ret,i=0;
char sNonCoded[256];
char rkey[1000 + 1], *retaddr;
// Create Berkeley DB
if ((ret = db_create(&dbp, NULL, 0)) != 0) {
fprintf(stderr, "db_create: %s\n", db_strerror(ret));
exit (1);
}
if ((ret = dbp->open(
dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
dbp->err(dbp, ret, "%s", DATABASE);
goto err;
}
while((ret = dbp->get(dbp, NULL, &key, &data, 0)) != 0){
printf("%d\n",i);
memset(&key,0,sizeof(key))
;
memset(&data,0,sizeof(data
));
key.data = rkey;
key.size = sizeof(rkey);
data.data = (char *)&content;
data.size = sizeof(CONTENT);
printf( "Decoded(field 1): %s\n", (char *)key.data );
printf( "Decoded(field 2): %s\n",((CONTENT *)(data.data))->RE);
printf( "Decoded(field 2): %s\n", ((CONTENT *)(data.data))->REURL);
printf( "Decoded(field 2): %s\n\n", ((CONTENT *)(data.data))->ACCEL);
i++;
if ( i == 20) {
exit(1);
}
}
err: if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
ret = t_ret;
exit(ret);
}
data are :
AAAA
http://www.zzz.zzz http://myhome http://www.zzz.*aaaAAAi
http://www.bbb.zzz http://myhome http://www.zzz.*aaaAAAA
http://www.zzz.zzz http://myhome http://www.zzz.*aaaAAAA
http://www.ccc.zzz http://myhome http://www.zzz.*aaaAAAi
http://www.zzz.zzz http://myhome http://www.zzz.*aaaAAAA
http://www.ddd.zzz http://myhome http://www.zzz.*aaaI apprecate of your help..!!