此節重點分析的是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