#ifdef HAVE_CONFIG_H
#include<config.h>
#endif
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include"HashFile.h"
#include"jtRecord.h"
#define KEYOFFSET 0
#define KEYLEN sizeof(int)
#define FileNAME "jing.hash"
void showHashFile();
int main(int argc,char *argv[])
{
struct jtRecord rec[6] = {
{1,"jing"},{2,"wang"},{3,"li"},
{4,"zhang"},{5,"qing"},{6,"yuan"}
};
int j=0;
for(j=0;j<6;j++)
{
printf("<%d,%d> ",rec[j].key,hash(KEYOFFSET,KEYLEN,&rec[j],6));
}
int fd = hashfile_creat(FileNAME,O_RDWR|O_CREAT,RECORDLEN,6);
int i=0;
printf("
Open and Save Record...
");
fd = hashfile_open(FileNAME,O_RDWR,0);
for(i=0;i<6;i++)
{
hashfile_saverec(fd,KEYOFFSET,KEYLEN,&rec[i]);
}
hashfile_close(fd);
showHashFile();
printf("
Find Record...");
fd = hashfile_open(FileNAME,O_RDWR,0);
int offset = hashfile_findrec(fd,KEYOFFSET,KEYLEN,&rec[4]);
printf("
offset is %d
",offset);
hashfile_close(fd);
struct jtRecord jt;
struct CFTag tag;
fd = open(FileNAME,O_RDWR);
lseek(fd,offset,SEEK_SET);
read(fd,&tag,sizeof(struct CFTag));
printf("Tag is <%d,%d> ",tag.collision,tag.free);
read(fd,&jt,sizeof(struct jtRecord));
printf("Record is {%d,%s}
",jt.key,jt.other);
printf("
Delete Record...");
fd = hashfile_open(FileNAME,O_RDWR,0);
hashfile_delrec(fd,KEYOFFSET,KEYLEN,&rec[2]);
hashfile_close(fd);
showHashFile();
fd = hashfile_open(FileNAME,O_RDWR,0);
char buf[32];
memcpy(buf,&rec[1],KEYLEN);
hashfile_read(fd,KEYOFFSET,KEYLEN,buf);
printf("
Read Record is {%d,%s}
",
((struct jtRecord *)buf)->key,((struct jtRecord *)buf)->other);
hashfile_close(fd);
printf("
Write Record...");
fd = hashfile_open(FileNAME,O_RDWR,0);
hashfile_write(fd,KEYOFFSET,KEYLEN,&rec[3]);
hashfile_close(fd);
showHashFile();
return 0;
}
void showHashFile()
{
int fd;
printf("
");
fd = open(FileNAME,O_RDWR);
lseek(fd,sizeof(struct HashFileHeader),SEEK_SET);
struct jtRecord jt;
struct CFTag tag;
while(1)
{
if(read(fd,&tag,sizeof(struct CFTag))<=0)
break;
printf("Tag is <%d,%d> ",tag.collision,tag.free);
if(read(fd,&jt,sizeof(struct jtRecord))<=0)
break;
printf("Record is {%d,%s}
",jt.key,jt.other);
}
close(fd);
}