nrgrep函數分析(3)--搜索實現分析

此節重點分析的是nrgrep中與搜索相關的函數(其他與pattern函數往後再說),從Shell.c234/* 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

simplePreprocsimpleData

simpleSearch

ESIMPLE

esimplePreprocesimpleData

esimpleSearch

EXTENDED

extendedPreprocextendedData

extendedSearch

EEXTENDED

eextendedPreproceextendedData

eextendedSearch

REGULAR

regularPreprocregulardData

regularSearch

EREGULAR

eregularPreproceregulardData

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章