C语言读写伯克利DB 3

gcc -o channeldb channel.c -db -Wall
# -Wall参数等价于执行lint,即:进行代码的静态分析,它可以指出未初始化的变量,未使用的变量
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <db.h>
#include <sys/types.h>
#include <getopt.h> //define DATABASE "/work/stat/read/newstat/data/userlist/historydb/channel.db" #define DATABASE "/mnt/disk1/ucshuqi/touch/userlist/historydb/channel.db"
#define YES 1
#define NO 0 /* ViewData Module */ struct ViewData {
int date;
}; void setDate(struct ViewData *data,char *s)
{
assert(data!=NULL && s!= NULL && strlen(s) == );
data->date = atoi(s);
} // the function return YES when query.date >= stored.date
int isHistoryViewInfo(struct ViewData *query , struct ViewData *stored)
{
assert(query != NULL && stored != NULL);
printf("query date is %d , stored date is %d \n",query->date, stored->date); if(query->date >= stored->date)
{
return YES;
}
else
{
return NO;
}
} void printViewData(struct ViewData *data)
{
printf("print view date : %d\n",data->date);
} /* string helper module */ char *trim(char *s)
{
int i = strlen(s);
for(;i>;i--)
{
if(s[i]==' ' || s[i]=='\n' || s[i]=='\0' || s[i]=='\t')
{
s[i] = '\0';
}
else
{
break;
}
}
return s;
} /* database helper module */ DB *openDb()
{
int ret;
DB *dbp = NULL; ret = db_create(&dbp, NULL, );
if(ret != )
{
fprintf(stderr,"create Db error!\n");
exit();
} ret = dbp->open(dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE,); if(ret != )
{
fprintf(stderr,"open Db error!\n");
exit();
} return dbp;
} /* business module */ int saveViewInfo(DB *dbp, char *user, struct ViewData *data)
{
DBT key,value; memset(&key, , sizeof(key));
key.data = user;
key.size = strlen(user) + ; memset(&value, , sizeof(value));
value.data = data;
value.size = sizeof(*data);
if(dbp->put(dbp, NULL, &key, &value, ) == )
{
//printf("save ---> %s, %d\n", key.data, key.size);
printViewData(value.data);
return YES;
}
else
{
return NO;
}
} int findViewInfo(DB *dbp, char *user, struct ViewData *data)
{
DBT key,value; memset(&key, , sizeof(key));
key.data = user;
key.size = strlen(user) + ; memset(&value,,sizeof(value)); // must !!! if(dbp->get(dbp, NULL, &key, &value, ) == )
{
memcpy((char*)data,(char*)value.data,value.size);
//printViewData(data);
return YES; //view in database
}
else
{
return NO; //view not in database
}
} void recordNewView(FILE *fp, char *user)
{
char buffer[] = {};
assert(fp != NULL && user != NULL); printf("%s is new view\n", user);
sprintf(buffer, "%s found\n", user);
fwrite(buffer, sizeof(char), strlen(buffer), fp);
} void saveViewFile(char *from, char *to, struct ViewData *writeData)
{
DB *dbp = openDb();
struct ViewData stored;
char buffer[ * ];
FILE *fp = fopen(from,"r");
FILE *fpResult = fopen(to,"w"); assert(fp != NULL && fpResult != NULL && writeData != NULL); while(fgets((char*)buffer, *, fp)!=NULL)
{
char *user = trim((char*)buffer);
memset(&stored, , sizeof(stored)); if(findViewInfo(dbp, user, &stored) == YES)
{
if(isHistoryViewInfo(writeData,&stored) == NO)
{
recordNewView(fpResult, user);
}
continue;
} recordNewView(fpResult, user);
if(saveViewInfo(dbp, user, writeData) == NO)
{
printf("save %s faild\n", user);
}
memset(&buffer, , * );
}
dbp->close(dbp, );
fclose(fp);
fclose(fpResult);
} void hasViewInfo(char *user, struct ViewData *query)
{
DB *dbp = NULL;
struct ViewData stored; dbp = openDb();
assert(user != NULL);
memset(&stored, ,sizeof(stored));
user = trim(user); if(findViewInfo(dbp, user, &stored) == YES)
{
if(isHistoryViewInfo(query, &stored) == YES)
{
printf("found %s\n",user);
}
else
{
//printf("%s in db\n",user);
printf("not found %s\n",user);
}
}
else
{
printf("%s not in db\n",user);
printf("not found %s\n",user);
} dbp->close(dbp, );
} int main (int argc, char *argv[])
{
int oc;
extern char *optarg;
extern int optind, opterr, optopt; char *from = NULL;
char *to = NULL; struct ViewData viewData;
memset(&viewData, , sizeof(viewData)); while((oc=getopt(argc,argv,"f:s:t:")) != -)
{
switch(oc)
{
case 's':
setDate(&viewData,argv[optind]);
hasViewInfo(optarg, &viewData);
break; case 'f':
from = optarg;
to = argv[optind++];
setDate(&viewData,argv[optind]);
saveViewFile(from, to, &viewData);
break;
}
} return ;
}

我开始把ViewData写成了这样:

struct ViewData{
char date[];
}; void setDate(char *date, char *s)
{
assert(date != NULL && s!= NULL);
strncpy(&date,s,);
} //call setDate
struct ViewData viewData;
memset(viewData,,sizeof(viewData));
setDate(&viewData.date,"");

后来还是觉得viewData直接存int更好:

struct ViewData{
int date;
}; void setDate(int *date, char *s)
{
assert(date != NULL && s!= NULL);
*date = atoi(s);
} //call setDate
struct ViewData viewData;
memset(viewData,,sizeof(viewData));
setDate(&viewData.date,"");

看似没啥问题,但其实我已经暴露了ViewData的实现!

//call setDate
struct ViewData viewData;
memset(viewData,,sizeof(viewData));
setDate(&viewData.date,"");
//断言设置之后的日期等于20130808,用户这么写是因为已经暴露了ViewData的date是字符串这个事情!
assert(strcmp(viewData.date,"")==);

那你说,不怎么写我想知道是不是设置成功了怎么办?

//call setDate
struct ViewData viewData;
memset(viewData,,sizeof(viewData));
setDate(&viewData.date,"");
//断言设置之后的日期等于20130808,这个函数由ViewData模块提供
verifyDate(&viewDate,"");
上一篇:Java中的位运算符、移位运算


下一篇:有关获取session属性时报nullPointException(空指针异常)的解决方案