home *** CD-ROM | disk | FTP | other *** search
/ Phoenix CD 2.0 / Phoenix_CD.cdr / 15a / murutil.zip / SEARCH.PAS < prev    next >
Pascal/Delphi Source File  |  1986-12-02  |  6KB  |  222 lines

  1. PROGRAM SEARCH;
  2.  
  3. {  This Turbo Pascal program searches the  input  file,  INP,  for  all
  4.    occurrences of user-specified strings.  Upper/Lower case differences
  5.    are ignored.
  6.  
  7.    The program ends when the user enters a null or all-blank string.
  8.  
  9.    A search string of "P+" or "+P" enables  printing.
  10.    A search string of "P-" or "-P" disables printing.
  11.  
  12.    Program by Harry M. Murphy,  24 May 1986.
  13.  
  14.    Updated for printout control on 27 July 1986.  }
  15.  
  16.   CONST 
  17.         LENSPEC = 65;
  18.         LENSRCH = 40;
  19.         LINELEN = 132;
  20.  
  21.   TYPE 
  22.        FILESPEC = STRING[LENSPEC];
  23.        SRCHLINE = STRING[LENSRCH];
  24.        TEXTLINE = STRING[LINELEN];
  25.  
  26.   VAR 
  27.       FF:      CHAR;
  28.       HARD:    BOOLEAN;
  29.       HARDOUT: BOOLEAN;
  30.       HIT:     BOOLEAN;
  31.       INP:     TEXT[2048];
  32.       INPNAME: FILESPEC;
  33.       LINENO:  INTEGER;
  34.       LSRCH:   1..LENSRCH;
  35.       SRCH:    SRCHLINE;
  36.  
  37.  
  38. PROCEDURE GETINPFIL(VAR INPNAME: FILESPEC);
  39.  
  40. {  This file gets an input file, either as the first parameter on the
  41.    command line or by requesting it from the user.
  42.  
  43.    Procedure by Harry M. Murphy,  22 February 1986.  }
  44.  
  45.   VAR 
  46.       L: 1..LENSPEC;
  47.  
  48.   BEGIN
  49.     IF PARAMCOUNT = 0
  50.       THEN
  51.         BEGIN
  52.           WRITE('Input  file: ');
  53.           READLN(INPNAME)
  54.         END
  55.       ELSE
  56.         INPNAME := PARAMSTR(1);
  57.     FOR L:=1 TO LENGTH(INPNAME) DO INPNAME[L] := UPCASE(INPNAME[L]);
  58.     ASSIGN(INP,INPNAME);
  59.     {$I-} RESET(INP); {$I+}
  60.     IF IORESULT <> 0
  61.       THEN
  62.         BEGIN
  63.           CLOSE(INP);
  64.           WRITELN('ERROR!  Can''t find file ',INPNAME,'!');
  65.           HALT
  66.         END;
  67.   END {Procedure GETINPFIL};
  68.  
  69.   PROCEDURE GETSRCH;
  70.  
  71.   { This procedure asks for and gets the search strings.  If the search
  72.     string is "P+", "+P", "P-" or "-P",  it sets or clears the hardcopy
  73.     flag, HARD, and asks again for the search string. }
  74.  
  75.   VAR 
  76.         L: 1..LENSRCH;
  77.  
  78.     FUNCTION NOKEY: BOOLEAN;
  79.  
  80.     { This function is internal to GETSRCH.   It returns TRUE if the
  81.       search string, SRCH, is NOT a printer keyword  "P+", "+P", "P-"
  82.       or "-P";  otherwise, it sets or clears the hardcopy flag, HARD,
  83.       outputs the current printer status and returns FALSE. }
  84.  
  85.       VAR 
  86.           K:  0..4;
  87.           PX: STRING[2];
  88.  
  89.       BEGIN
  90.         IF LENGTH(SRCH) <> 2
  91.           THEN
  92.             NOKEY := TRUE
  93.           ELSE
  94.             BEGIN
  95.               PX := SRCH;
  96.               K := POS(PX,'P+P-P');
  97.               CASE K OF
  98.                   0: NOKEY := TRUE;
  99.                 1,2: BEGIN
  100.                         NORMVIDEO;
  101.                         WRITELN('Printer enabled.');
  102.                         LOWVIDEO;
  103.                         HARD := TRUE;
  104.                         NOKEY := FALSE
  105.                       END;
  106.                 3,4: BEGIN
  107.                         NORMVIDEO;
  108.                         WRITELN('Printer disabled.');
  109.                         LOWVIDEO;
  110.                         HARD := FALSE;
  111.                         NOKEY := FALSE
  112.                       END
  113.               END { CASE }
  114.             END
  115.       END {Function NOKEY};
  116.  
  117.  
  118.   BEGIN { Procedure GETSRCH }
  119.     REPEAT
  120.       WRITELN;
  121.       WRITE('Search string? >');
  122.       READLN(SRCH);
  123.       LSRCH := LENGTH(SRCH);
  124.       IF LSRCH > 0
  125.         THEN
  126.           BEGIN
  127.             FOR L:=1 TO LSRCH DO SRCH[L] := UPCASE(SRCH[L]);
  128.             WHILE (SRCH[L] = ' ') AND (LSRCH > 0) DO LSRCH := LSRCH-1
  129.           END;
  130.       SRCH[0] := CHR(LSRCH);
  131.     UNTIL NOKEY
  132.   END {Procedure GETSRCH};
  133.  
  134.   PROCEDURE SCANLINE;
  135.  
  136.   { This procedure reads and scans an input line for an occurrence  of
  137.     the search string, SRCH.   If the string is found, it displays the
  138.     line,  highlighting the portion containing the search string,  and
  139.     sets the flag HIT to TRUE.
  140.  
  141.     If the hardcopy flag, HARD, is set, SCANLINE sends the line to the
  142.     printer and sets the hardcopy output flag,  HARDOUT, to true.   If
  143.     this is the first hardcopy output, SCANLINE precedes the line with
  144.     the name of the file being searched.   If this is the  first  hit,
  145.     SCANLINE precedes the line with the search line. }
  146.  
  147.   VAR 
  148.       L,LS,LT,LZ: 1..LINELEN;
  149.       LINE:       TEXTLINE;
  150.       LINES:      TEXTLINE;
  151.  
  152.   BEGIN
  153.     READLN(INP,LINE);
  154.     LINENO := LINENO+1;
  155.     LS := LENGTH(LINE);
  156.     IF LS >= LSRCH
  157.       THEN
  158.         BEGIN
  159.           LINES := LINE;
  160.           FOR L:=1 TO LS DO LINES[L] := UPCASE(LINES[L]);
  161.           LT := POS(SRCH,LINES);
  162.           IF LT > 0
  163.             THEN
  164.               BEGIN
  165.                 WRITE(LINENO:5,' > ');
  166.                 IF LT > 1 THEN FOR L:=1 TO LT-1 DO WRITE(LINE[L]);
  167.                 LZ := LT+LSRCH-1;
  168.                 NORMVIDEO;
  169.                 FOR L:=LT TO LZ DO WRITE(LINE[L]);
  170.                 LOWVIDEO;
  171.                 IF LZ < LS THEN FOR L:=LZ+1 TO LS DO WRITE(LINE[L]);
  172.                 WRITELN;
  173.                 IF HARD
  174.                   THEN
  175.                     BEGIN
  176.                       IF NOT HARDOUT
  177.                         THEN
  178.                           BEGIN
  179.                             WRITELN(LST,'SEARCH of ',INPNAME,':');
  180.                             WRITELN(LST);
  181.                             HARDOUT := TRUE
  182.                           END;
  183.                       IF NOT HIT
  184.                         THEN
  185.                           BEGIN
  186.                             WRITELN(LST,'Records containing "',SRCH,'":');
  187.                             WRITELN(LST)
  188.                           END;
  189.                       WRITE(LST,LINENO:5,' > ');
  190.                       FOR L:=1 TO LS DO WRITE(LST,LINE[L]);
  191.                       WRITELN(LST)
  192.                     END;
  193.                 HIT := TRUE;
  194.               END
  195.         END
  196.   END {Procedure SCANLINE};
  197.  
  198.  
  199.   BEGIN {Program SEARCH};
  200.     FF := CHR(12);
  201.     HARD := FALSE;
  202.     HARDOUT := FALSE;
  203.     LOWVIDEO;
  204.     GETINPFIL(INPNAME);
  205.     GETSRCH;
  206.     WHILE LSRCH > 0 DO
  207.       BEGIN
  208.         RESET(INP);
  209.         HIT := FALSE;
  210.         LINENO := 0;
  211.         WHILE NOT EOF(INP) DO SCANLINE;
  212.         IF NOT HIT
  213.           THEN
  214.             WRITELN('                Not found.')
  215.           ELSE
  216.             IF HARD THEN WRITELN(LST);
  217.         GETSRCH
  218.       END;
  219.     IF HARDOUT THEN WRITELN(LST,FF);
  220.     CLOSE(INP)
  221. END.
  222.