Algorithm 版 (精华区)
发信人: Lerry (想不开·撞树), 信区: Algorithm
标 题: pathconvert.c
发信站: 哈工大紫丁香 (2002年06月09日21:28:41 星期天), 站内信件
/*
* 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
*/
#define PATHCONVERT_VERSION "1.1"
#include <malloc.h>
#include "parker.h"
#include <string.h>
struct PathItem { char *path; long offset; struct PathItem *next;};
struct PathItem *head=NULL;
struct PathItem **ShortCut;
long cnt;
void LoadPaths(void)
{
FILE *pdb;
struct PathItem **last, *PIptr;
long offset;
char tmppath[MAX];
char *ptr;
if(!(pdb=fopen(SEEDPATHDB,"r"))) {perror("Error opening SEEDPATHDB");exit(
-1);}
last=&head;
cnt=0;
while(1)
{
offset=ftell(pdb);
fgets(tmppath,MAX,pdb);
if(feof(pdb)) break;
if((ptr=strchr(tmppath,CR))!=NULL) *ptr='\0';
if((ptr=strchr(tmppath,LF))!=NULL) *ptr='\0';
PIptr=malloc(sizeof(struct PathItem));
PIptr->path=strdup(tmppath);
PIptr->offset=offset;
PIptr->next=NULL;
*last=PIptr;
last=&(PIptr->next);
cnt++;
}
fclose(pdb);
}
void InitTable(void)
{
long i;
struct PathItem *ptr;
ShortCut=malloc(cnt*sizeof(struct PathItem *));
ptr=head;
for(i=0L;ptr;i++) {ShortCut[i]=ptr; ptr=ptr->next;}
}
void main(void)
{
char oldpath[MAX];
long oldoff;
char *ptr, *sptr;
char tmppath[MAX], buffer[MAX];
long top, mid, bot;
struct PathItem *PIptr;
int i;
LoadPaths();
InitTable();
oldoff=0L;
*oldpath='\0';
if(!feof(stdin)) while(1) {
fgets(buffer,MAX,stdin);
if(feof(stdin)) break;
if((ptr=strchr(buffer,CR))!=NULL) *ptr='\0';
if((ptr=strchr(buffer,LF))!=NULL) *ptr='\0';
ptr=buffer;
sptr=NULL;
while((ptr=strchr(ptr,'/'))!=NULL) if(*(ptr+1)==' '||*(ptr+1)==0) break;
else sptr=ptr++;
if(!sptr) {
printf("%s -1\n",buffer);
continue;
}
if(sptr==buffer) {
strcpy(tmppath,buffer);
sptr=tmppath+1;
strcpy(buffer,"/");
}
else *sptr++='\0';
if(!strcmp(buffer,oldpath)) {printf("%s %ld\n",sptr,oldoff); continue;}
top=0L;bot=cnt-1;
while((top+1)<bot)
{
mid=(top+bot)/2;
i=strcmp(buffer,ShortCut[mid]->path);
if(i<=0) bot=mid;
else top=mid;
}
PIptr=ShortCut[top];
if(strcmp(PIptr->path,buffer)) {
PIptr=PIptr->next;
if(strcmp(PIptr->path,buffer)) {
fprintf(stderr,"\007Error finding `%s' -- found `%s'\n",
buffer,PIptr->path);
exit(1);
}
}
printf("%s %ld\n",sptr,PIptr->offset);
strcpy(oldpath,tmppath);
oldoff=PIptr->offset;
}
}
--
当一个女孩儿觉得她不太容易了解那个男人的时候,她会爱他。
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 天外飞仙]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.365毫秒