Algorithm 版 (精华区)
发信人: Lerry (想不开·撞树), 信区: Algorithm
标 题: btreehosts.c
发信站: 哈工大紫丁香 (2002年06月09日21:23:30 星期天), 站内信件
/*
* NoseyParker, the search engine for FTP archives
* Copyright (C) 1993-96 by Jiri A. Randus
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* The author can be reached as follows:
* Internet: <Jiri.Randus@vslib.cz>
* Phone: ++42 48 5227374
* SnailMail: Jiri Randus
* KIN HF TU v Liberci
* Halkova 6
* 46117 Liberec
* Czech Republic
*/
/*
* Uses procedures in btree.c
*/
#define BTREEHOSTS_VERSION "1.02"
#include "parker.h"
#include <dirent.h>
void IndexFile(BTcnts fileno, char *filename)
{
FILE *in;
char buffer[MAX];
long offset;
char *ptr;
char triple[KEYSIZE+1];
struct BTreeItem *MyTriple;
union DiskTripleRef ref;
long triplecnt;
int j,len;
printf("Indexing %3d %30s... ",fileno,filename);
fflush(stdout);
triplecnt=0L;
if(!(in=fopen(filename,"r"))) {
perror("Cannot open file");
return;
}
triple[KEYSIZE]='\0';
while(1)
{
offset=ftell(in);
fgets(buffer,MAX,in);
if(feof(in)) break;
for(ptr=buffer;*ptr;ptr++) if(*ptr==' ') {*ptr='\0';break;}
len=strlen(buffer)-KEYSIZE;
for(j=0;j<=len;j++)
{
strncpy(triple,&buffer[j],KEYSIZE);
MyTriple=LocateBItem(triple,Bhead);
if(!MyTriple) {
printf("Error: Btree does not contain `%s' as found in\n",triple);
printf("`%s'\n",buffer);
continue;
}
if((MyTriple->lastfileno==fileno)&&(MyTriple->lastoffset==offset))
continue;
if(MyTriple->lastfileno!=fileno) {
ref.file.mark=BTREEENDMARK;
MyTriple->lastfileno=ref.file.fileno=fileno;
AddRef(MyTriple,&ref);
triplecnt++;
}
ref.offset=offset;
AddRef(MyTriple,&ref);
triplecnt++;
}
}
fclose(in);
printf("%8ld\n",triplecnt);
}
void IndexData(void)
{
DIR *dir;
struct dirent *ptr;
BTcnts fileno;
FILE *filedb;
fileno=0;
if(!(dir=opendir("."))) {
perror("Cannot open dir");
exit(101);
}
if(!(filedb=fopen(SEEDFILEDB,"w"))) {
perror("Cannot open SEEDFILEDB");
exit(102);
}
while((ptr=readdir(dir))!=NULL)
{
if(*ptr->d_name=='.') continue;
if(!strncmp(ptr->d_name,EXCLUDEFILES,strlen(EXCLUDEFILES))) continue;
if(!strcmp(ptr->d_name,CORE)) continue;
fprintf(filedb,"%s\n",ptr->d_name);
IndexFile(fileno++,ptr->d_name);
}
fclose(filedb);
closedir(dir);
}
int main(void)
{
long lo;
printf("This is NoseyParker %s, BtreeHosts Version %s\n",
VERSION, BTREEHOSTS_VERSION);
AnnounceMethod();
/* Load triples from stdin */
LoadTriples();
printf("Found %ld unique keys\n",triplecount);
/* Ok, create the tree */
CreateBtree(0L,triplecount);
for(lo=0L;lo<triplecount;lo++)
addB(TriIndex[lo],Bhead,&Bhead,&BTreeMax);
printf("Created %ld Btree items\n",nodecount);
/* Free the linked triple list as it is no longer needed */
Tptr=Thead;
while(Tptr) {Tptr2=Tptr->next; free(Tptr); Tptr=Tptr2;}
/* Read the databases, index them with Btree... */
IndexData();
/* ... count the disk offsets... */
diskoffset=0L;
printf("Counting the database size\n");
IndexOffset(Bhead,&diskoffset);
printf("Expected size of the index: %ld KB\n",
(long)(diskoffset/1024));
/* ... and save the index */
SaveIndex(SEEDBTREE);
return(0);
}
--
当一个女孩儿觉得她不太容易了解那个男人的时候,她会爱他。
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 天外飞仙]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.746毫秒