findurl.cpp

来自「小型搜索引擎,用C/C++编写,属于全文搜索引擎」· C++ 代码 · 共 74 行

CPP
74
字号
/* * If content is NULL, find out wheather INDEX_FILE includes the URL. * Otherwise, read the corresponding page of the URL. * */#include "Tse.h"#include "Url.h"int FindUrl(const char *url, char **content){	FILE *fpfile,*fpindex;	char *buffer,file[URL_LEN+1];	int offsett, offset2;	if(content){		fpfile = fopen(DATA_FILE_NAME.c_str(),"r");		if(fpfile == NULL) return -1;	}	fpindex = fopen(INDEX_FILE_NAME.c_str(),"r");	if(fpindex == NULL) return -1;	while(!feof(fpindex)){		fscanf(fpindex, "%d %s", &offsett, file);		//cout << "offset: " << offsett << endl;		//cout << "url: " << file << endl;				if( strlen(url) > strlen(file) ){			if( strncmp(url, file, strlen(url)) ){				continue;			}		}else{			if( strncmp(url, file, strlen(file)) ){				continue;			}		}				if(content == NULL){			fclose(fpindex);			return 0;		}		fseek( fpfile, offsett, 0 );		fscanf( fpindex, "%d", &offset2 );		offset2 = offset2 - offsett;		buffer = ( char* )malloc( offset2+1 );		if(buffer == NULL){			fclose(fpfile);			fclose(fpindex);			return -1;		}		memset(buffer,0,offset2+1);		fread(buffer, 1, offset2, fpfile);		if(content == NULL)			free(buffer);		else			*content = buffer;		if(content) fclose(fpfile);		fclose(fpindex);		return 0;	}		if(content) fclose(fpfile);	fclose(fpindex);	return -1;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?