此节重点分析的是nrgrep中与搜索相关的函数(其他与pattern函数往后再说),从Shell.c的234行/* get the pattern */开始:
1、 searchData *searchPreproc (byte *pat)
该函数是搜索前的预处理函数,根据pat初始化searchData(此数据结构在nrgrep中十分重要)
typedef struct
{ int searchType; /* type of pattern */
void *preprocData; /* data from preprocessing */
bool (*search)(); /* search function */
} searchData;
初始化工作有:给数据类型赋予相应的值
searchType | preprocData(返回Data) | search |
SIMPLE | simplePreproc(simpleData) | simpleSearch |
ESIMPLE | esimplePreproc(esimpleData) | esimpleSearch |
EXTENDED | extendedPreproc(extendedData) | extendedSearch |
EEXTENDED | eextendedPreproc(eextendedData) | eextendedSearch |
REGULAR | regularPreproc(regulardData) | regularSearch |
EREGULAR | eregularPreproc(eregulardData) | eregularSearch |
2、 void recPreproc (void) 记录的预处理 当中也执行了simplePreproc().
/* search the files */接下来是两类不同选项时的搜索函数,由main调用
3、 int recSearchFile (char *fname, Buffer B, searchData *P)
/* searches the file handled by B for P using R as record
separator, reports matches as appropriate and returns number
of matches */
while循环
l searchScan (&pbeg,&pend,P) return P->search (beg,end,P->preprocData);
执行预处理是初始化的search函数(e.g. simpleSearch,extekdedSearch etc.)
l /* report the matching record */ 反馈相应匹配record
4、 int recSearchRecFile (char *fname, Buffer B, searchData *P)
/* searches the file handled by B for P using R as record
separator, but scans record by record. this is our way to
handle OptRecNumber or !OptRecPositive */
while循环
l simpleSearch (&rbeg,&rend,RecPatt)
l searchScan (&pbeg,&pend,P) return P->search (beg,end,P->preprocData);
执行预处理是初始化的search函数(e.g. simpleSearch,extekdedSearch etc.)
l /* report the matching record */ 反馈相应匹配record