home *** CD-ROM | disk | FTP | other *** search
/ PC Media 2 / PC MEDIA CD02.iso / share / udos / fgrep103 / execute.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-27  |  3.2 KB  |  88 lines

  1. #include <stdio.h>
  2. #include <stdlib.h> /* N2 03-17-91 */
  3. #include <dos.h>    /* N2 03-17-91 */
  4. #include "bm.h"
  5. #include "extern.h"
  6. #include "proto.h" /* N2 04-05-91 */
  7.  
  8.  Execute(struct PattDesc *DescVec[], int NPats, int TextFile, char Buffer[])
  9. /* ---------
  10.  struct PattDesc *DescVec[]; // pointers to status vectors for the different
  11.           patterns, including skip tables, position in buffer, etc.
  12.         int NPats;     // number of patterns
  13.         char Buffer[]; // holds text from file
  14.         int TextFile;  // file to search
  15. ------------ */
  16.        {
  17.         int NRead,     /* number of chars read from file */
  18.             BuffSize,  /* number of chars in buffer */
  19.             BuffPos,   /* offset of first char in Buffer in TextFile */
  20.             BuffEx,    /* flag to indicate that buffer has been searched */
  21.             ResSize,    /* number of characters in the last, incomplete line */
  22.             Found;     /* flag indicates whether pattern found
  23.                           * completely and all matches printed */
  24.         char *BuffEnd; /* pointer to last char of last complete line */
  25.  
  26.         /* misc working variables */
  27.         int i;
  28.  
  29.         /* initialize */
  30.         ResSize = 0;
  31.         Found = 0;
  32.         BuffPos = 0;
  33.         for (i=0; i < NPats; i++)
  34.         {
  35.           DescVec[i] -> Success = 0;
  36.           DescVec[i] -> Start = Buffer;
  37.         } /* for */
  38.         /* now do the searching */
  39.         do
  40.         {
  41.           /* first, read a bufferfull and set up the variables */
  42.           NRead = read(TextFile,Buffer + ResSize, MAXBUFF-ResSize);
  43.           BuffEx = 0;
  44.           BuffSize = ResSize + NRead;
  45.           BuffEnd = Buffer + BuffSize - 1;
  46.           /* locate the end of the last complete line */
  47.           while (*BuffEnd != '\n' && BuffEnd >= Buffer)
  48.           {
  49.             --BuffEnd;
  50.           }
  51.           if (BuffEnd < Buffer)
  52.           {
  53.             BuffEnd = Buffer + BuffSize - 1;
  54.           }
  55.           while (!BuffEx)   /* work through one buffer full */
  56.           {
  57.             BuffEx = 1; /* set it provisionally, then clear
  58.             * it if we find the buffer non-empty */
  59.             for (i=0; i< NPats; i++)
  60.             {
  61.               if (!DescVec[i]->Success)
  62.               /* if the pattern  has not been found */
  63.                       DescVec[i]-> Success =
  64.                       Search(DescVec[i]->Pattern,
  65.                       DescVec[i]->PatLen, BuffEnd,
  66.                       DescVec[i]->Skip1, DescVec[i]->Skip2,
  67.                       DescVec[i]);
  68.               if (DescVec[i]->Success)
  69.               {
  70.                 /* if a match occurred */
  71.                 BuffEx = 0;
  72.                 Found = 1;
  73.                 if (sFlag) return(!Found);
  74.                 MatchFound(DescVec[i],BuffPos,
  75.                 Buffer, BuffEnd);
  76.                 if (lFlag) return(!Found);
  77.               } /* if */
  78.             } /* for */
  79.           } /* while */
  80.           if(NRead)
  81.           {
  82.             ResSize = MoveResidue(DescVec,NPats,Buffer,Buffer + BuffSize - 1);
  83.             BuffPos += BuffSize - ResSize;
  84.           } /* if */
  85.         } while (NRead);
  86.         return(!Found);
  87.        } /* Execute */
  88.