这个数据库一共用到两个头文件
#include"dbtype.h"
#include"XperditDB.h"
dbtype.h提供了一个PList双向链表可以对数据库进行精确的操作
XperditDB.h提供了table一个数据库容器来对数据库进行增删改查
下面是方法介绍:
initTable(table t)
对table变量进行初始化
createTable(PList typeName,char *TabName,table &t)
创建一个新的table,其中typeName为一个Plist,它存放了储存的数据名称,TabName为数据库在本地的名称
getTable(char *tableName,table &t)
获取一个已经存在的table,tableName为已经存在的本地数据库名称
clearTable(table &t)
释放table,但不破坏本地数据库
insert2Table(table &t,PList item)
像table中插入数据,item为插入的数据
delTableElem(table &t,int id)
删除ID=id的那一列数据
getElemInTableFromID(table t,int id)
通过id获取那一列数据
getElemIDInTableFromChar(table t,char *type,char *item)
通过类型名和内容返回该列的id 通常搭配getElemInTableFromID使用
updateDB(table t)
将t同步到本地的数据库
readTable(table t)
在命令行中显示t的所有内容
initList(PList &p)
初始化Plist
PList的基本函数就不写了 详见dbtype.h
下面附上源码:
dbtype.h
#ifndef DBTYPE_H
#define DBTYPE_H
#include
#include
#include
#define ERROR -1
typedef struct list *PList;
typedef struct Node *PNode;
typedef char *Elem;
struct list{
int ava;
PNode head;
PNode tail;
};
struct Node{
Elem elem;
PNode next;
};
int initNode(PNode &p){
if(!(p=(PNode)malloc(sizeof(Node)))){
free(p);
return ERROR;
}
p->next=NULL;
return 1;
}
int initList(PList &p){
if(!(p=(PList)malloc(sizeof(list)))){
free(p);
return ERROR;
}
PNode pp;
initNode(pp);
p->head=NULL;
p->tail=NULL;
p->ava=1;
return 1;
}
int isEmpty(PList p){
if(p->ava==1) return 1;
return 0;
}
void append(PList p,Elem elem){
PNode pp;
initNode(pp);
int len=strlen(elem)+1;
pp->elem=(Elem)malloc(sizeof(char)*len);
strcpy(pp->elem,elem);
if(!(p->head)){
p->head=pp;
p->tail=pp;
}else{
p->tail->next=pp;
p->tail=pp;
}
}
int getlen(PList p){
PNode pp=p->head;
int re=0;
while(pp){
++re;
pp=pp->next;
}
return re;
}
void readAllList(PList p){
if(!isEmpty(p)){
return;
}
PNode pp=p->head;
while(pp){
printf("%c",pp->elem);
pp=pp->next;
}
printf("\n");
}
Elem getElem(PList p,int id){
PNode pp=p->head;
int i=1;
while(pp){
if(i==id){
return pp->elem;
}
pp=pp->next;
i++;
}
return 0;
}
void DelElem(PList p,int id){
PNode pp=p->head;
int i=1;
if(id==1){
p->head=pp->next;
free(pp);
}else{
while(pp){
if(i+1==id){
PNode ppp=pp->next;
pp->next=ppp->next;
free(ppp->elem);
free(ppp);
}
pp=pp->next;
i++;
}
}
}
void insert(PList p,Elem e,int id){
PNode p2=p->head;
PNode p3;
initNode(p3);
int i=1;
if(id==1){
p->head=p3;
p3->next=p2;
p3->elem=e;
}else{
while(p2){
if(i+1==id){
p3->next=p2->next;
p2->next=p3;
p3->elem=e;
}
i++;
p2=p2->next;
}
}
}
void clearList(PList p){
PNode pp=p->head;
while(pp){
PNode p3=pp;
pp=pp->next;
free(p3->elem);
free(p3);
}
free(p);
}
void listcat(PList p1,PList p2){
p1->tail->next=p2->head;
p1->tail=p2->tail;
free(p2);
}
int getid(PList p,char *ch){
PNode n=p->head;
int id=1;
while(n){
if(strcmp(n->elem,ch)==0){
return id;
}
id++;
n=n->next;
}
return 0;
}
#endif
XperditDB.h
#ifndef XPERDITDB_H
#define XPERDITDB_H
#include
#include
#include
#include
#include"dbtype.h"
typedef char *pchar;
typedef struct stable *table;
struct stable{
PList type;
PList item;
char *path;
};
char* readFile(char *path){
FILE *fp;
char *ch1;
char ch;
if((fp=fopen(path,"r"))==NULL) {
printf("file cannot be opened/n");
}
int len=0;
while((ch=fgetc(fp))!=EOF)
len++;
ch1=(char*)malloc(sizeof(char)*((int)(len+1)));
strcpy(ch1,"");
fclose(fp);
if((fp=fopen(path,"r"))==NULL) {
printf("file cannot be opened/n");
}
int i=0;
while((ch=fgetc(fp))!=EOF) {
*(ch1+i)=ch;
i++;
}
fclose(fp);
return ch1;
}
void writeFile(char* ch,char* path){
FILE *fp;
if((fp=fopen(path,"w"))==NULL) {
printf("file cannot be opened/n");
}
int len=strlen(ch);
for(int i=0;ihead;
int len=0;
while(pp){
len+=strlen(pp->elem)+1;
pp=pp->next;
}
char *ch=(char*)malloc(sizeof(char)*((len+1)+1));
strcpy(ch,"");
pp=p->head;
while(pp){
strcat(ch,pp->elem);
strcat(ch,"/");
pp=pp->next;
}
strcat(ch,"<");
clearList(p);
return ch;
}
char* mixTAC(PList p1,PList p2){
char *ch1=mix(p1);
char *ch2=mix(p2);
int len=strlen(ch1)+strlen(ch2)+2;
char *ch=(char*)malloc(sizeof(char)*len);
strcpy(ch,"");
strcat(ch,ch1);
strcat(ch,"*");
strcat(ch,ch2);
return ch;
}
char* strsub(char *ch,int start,int end){
char *cho=(char*)malloc(sizeof(char)*(end-start+2));
int i=start;
int j=0;
while(*(ch+i)){
*(cho+j)=*(ch+i);
if(i==end){
break;
}
i++;
j++;
}
*(cho+j+1)=0;
return cho;
}
void decodePart(char *ch,PList p){
int i=0;
while(*(ch+i)!=47) i++;
append(p,strsub(ch,0,i-1));
int len=strlen(ch);
if(*(ch+i+1)==60){
return;
}
decodePart(strsub(ch,i+1,len-1),p);
}
void decodeDB(char *ch,PList &p1,PList &p2){
char *ch1;
char *ch2;
int breakp=0;
int i=0;
while(*(ch+i)){
if(*(ch+i)==42){
break;
}
i++;
}
ch1=(char*)malloc(sizeof(char)*(i+1));
ch2=(char*)malloc(sizeof(char)*(strlen(ch)-i));
ch1=strcpy(ch1,"");
ch2=strcpy(ch2,"");
ch1=strsub(ch,0,i-1);
ch2=strsub(ch,i+1,strlen(ch)-1);
decodePart(ch1,p1);
decodePart(ch2,p2);
}
void readData(PList &p1,PList &p2){
int len1=getlen(p1);
int len2=getlen(p2);
for(int j=1;j<=len2;j++){
if(j%len1==0){
printf("%s :",getElem(p1,len1));
}else{
printf("%s :",getElem(p1,j%len1));
}
printf("%s\n",getElem(p2,j));
}
}
void getdata(char *path,PList &p1,PList &p2){
char *cho=readFile(path);
int k=0;
int kk=0;
int len=strlen(cho);
while(kk!=2){
if(*(cho+k)==60){
kk++;
}
k++;
}
char *ch;
ch=strsub(cho,0,k);
initList(p1);
initList(p2);
decodeDB(ch,p1,p2);
}
void writedata(PList p1,PList p2,char *TabName){
char *ch=mixTAC(p1,p2);
writeFile(ch,TabName);
};
void readDB(char *TabName){
PList p1,p2;
getdata(TabName,p1,p2);
readData(p1,p2);
clearList(p1);
clearList(p2);
}
void initTable(table &t){
t=(table)malloc(sizeof(stable));
}
void createTable(PList typeName,char *TabName,table &t) {
PList Pobj;
initList(Pobj);
int len=getlen(typeName);
for(int i=0;itype=typeName;
t->item=Pobj;
t->path=TabName;
}
void updateDB(table t){
writedata(t->type,t->item,t->path);
};
void getTable(char *tableName,table &t){
PList p1,p2;
initList(p1);
initList(p2);
initTable(t);
getdata(tableName,p1,p2);
t->type=p1;
t->item=p2;
t->path=tableName;
}
void readTable(table t){
readData(t->type,t->item);
}
void insert2Table(table &t,PList item){
listcat(t->item,item);
int i=0;
for(;i<3;i++){
if(strcmp(getElem(t->item,1),"NULL")==0){
DelElem(t->item,1);
}
}
}
void delTableElem(table &t,int id){
int lenType=getlen(t->type);
PList pit=t->item;
for(int i=0;itype);
clearList(t->item);
free(t);
}
PList getElemInTableFromID(table t,int id){
int len=getlen(t->type);
PList out;
initList(out);
for(int i=1;i<=len;i++){
append(out,getElem(t->item,len*(id-1)+i));
}
return out;
}
int getElemIDInTableFromChar(table t,char *type,char *item){
int len1=getlen(t->type);
int id=getid(t->type,type);
int len2=getlen(t->item);
int len =len2/len1;
for(int i=1;i<=len;i++){
if(strcmp(getElem(t->item,len1*(i-1)+id),item)==0){
return i;
}
}
return 0;
}
#endif