home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / ECO30603.ZIP / ECO30603.LZH / ECOLIBCS / DEMOS / UGREP / qf.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-04-02  |  11.6 KB  |  346 lines

  1. (*
  2.     ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  3.     ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  4.     ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  5.     ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  6.     ▓▓▓▓▓▓▓▓·──                                              ──·▓▓▓▓▓▓▓▓▓▓▓
  7.     ▓▓▓▓▓▓▓▓│                                                  │░░▓▓▓▓▓▓▓▓▓
  8.     ▓▓▓▓▓▓▓▓   QuickFind was conceived, designed and written    ░░▓▓▓▓▓▓▓▓▓
  9.     ▓▓▓▓▓▓▓▓   by Floor A.C. Naaijkens for                      ░░▓▓▓▓▓▓▓▓▓
  10.     ▓▓▓▓▓▓▓▓   UltiHouse Software / The ECO Group.              ░░▓▓▓▓▓▓▓▓▓
  11.     ▓▓▓▓▓▓▓▓                                                    ░░▓▓▓▓▓▓▓▓▓
  12.     ▓▓▓▓▓▓▓▓   (C) MCMXCII by EUROCON PANATIONAL CORPORATION.   ░░▓▓▓▓▓▓▓▓▓
  13.     ▓▓▓▓▓▓▓▓   All Rights Reserved for The ECO Group.           ░░▓▓▓▓▓▓▓▓▓
  14.     ▓▓▓▓▓▓▓▓                                                    ░░▓▓▓▓▓▓▓▓▓
  15.     ▓▓▓▓▓▓▓▓   QuickFind files with content.                    ░░▓▓▓▓▓▓▓▓▓
  16.     ▓▓▓▓▓▓▓▓│                                                  │░░▓▓▓▓▓▓▓▓▓
  17.     ▓▓▓▓▓▓▓▓·──                                              ──·░░▓▓▓▓▓▓▓▓▓
  18.     ▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▓▓▓▓▓▓▓▓▓
  19.     ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  20.     ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  21.     ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  22. *)
  23. {$A-,B-,D-,E-,F-,I-,L-,N-,O-,R-,S-,V-}
  24.  
  25. uses
  26.   eco_srch, eco_lib,
  27.   crt, dos
  28.  
  29.   ;
  30.  
  31.  
  32. type
  33.   srec_ptr = ^srec_chain;
  34.   srec_chain = record
  35.     srec : searchrec;
  36.     bid  :   longint;
  37.     prev :      word;
  38.     link :  srec_ptr;
  39.   end;
  40.  
  41.   nodes = record
  42.     name    : string[12];
  43.     bid     :    longint; { bytes in directory }
  44.     parent  :       word;
  45.   end;
  46.  
  47.   nodearraytype =  array[0..511] of nodes;
  48.   nodearraytypeptr = ^nodearraytype;
  49.  
  50.  
  51. const
  52.   sort_items : boolean =  true;
  53.   show       : boolean =  true;
  54.   redirect   : boolean = false;
  55.  
  56. var
  57.   node_file              :    file of nodes;
  58.   nodearray              : nodearraytypeptr;
  59.   srec                   :        searchrec;
  60.   startx, starty,
  61.   index1, index2         :             word;
  62.   hex, curdir,
  63.   olddir,
  64.   commandstr             :           string;
  65.   searchfilename         :          pathstr;
  66.   searchfile             :             file;
  67.   nf, resultat,
  68.   casesensitive          :          boolean;
  69.   ch                     :             char;
  70.   b4, during, nod,
  71.   nodenumber,
  72.   prevparent,
  73.   after, oldattr,
  74.   longeur, min, max,
  75.   bufpointer,
  76.   sizeread, i, j         :             word;
  77.   longbufpos             :          longint;
  78.   dirinfo                :        searchrec;
  79.   dir                    :           dirstr;
  80.   name                   :          namestr;
  81.   ext                    :           extstr;
  82.  
  83.  
  84.  
  85.   procedure help;
  86.   begin
  87.     textcolor(yellow);
  88.     writeln('QF - QuickFind String Find Utility -- Version 1.0');
  89.     writeln('(C) MCMXCII by UltiHouse Software / The ECO Group.');
  90.     writeln('Part of the UltiGREP package: GSR, USR, QF, UGREP.');
  91.     textcolor(lightgray); writeln;
  92.     writeln('Usage: QF [wildfile] [searchtext] [/c] [/s] [/d]');
  93.     writeln('  /c search case sensitive');
  94.     writeln('  /s search entire subtree');
  95.     writeln('  /d disable show on screen');
  96.     writeln;
  97.     writeln('Examples:');
  98.     writeln('  QF *.PAS helpproc /c /s');
  99.     writeln('  QF C:\PROG\BORLANDC\INCLUDE\*.H _WinExitErr /c /s >d:\find');
  100.     halt(0);
  101.   end;
  102.  
  103.  
  104.   function trace_path(temphead: integer): pathstr;
  105.   var
  106.     off      :                    word;
  107.     st       :                  string;
  108.     i        :                    word;
  109.     tmp      : array[1..50] of integer;
  110.  
  111.   begin
  112.     off := 0;
  113.     while temphead>0 do begin
  114.       inc(off); tmp[off] := temphead;
  115.       temphead := nodearray^[temphead].parent;
  116.     end; st := '\';
  117.     if off>0 then for i := off downto 1 do
  118.       st := st + nodearray^[tmp[i]].name +'\';
  119.     off := 0; fillchar(tmp, sizeof(tmp), chr(48));
  120.     trace_path := st;
  121.   end;
  122.  
  123.  
  124. {$I-}
  125.   procedure search_tree(level: word);
  126.   var
  127.     i         :  integer;
  128.     s         :   string;
  129.     srec_root,
  130.     srec_link : srec_ptr;
  131.  
  132.     { not much subdirs in one dir, so no efficiency taken into account }
  133.     procedure sort(srec_root: srec_ptr);
  134.     var
  135.       srec1, srec2, srec3 :  srec_ptr;
  136.       srec                : searchrec;
  137.  
  138.     begin
  139.       srec1 := srec_root;
  140.       while srec1^.link <> nil do begin
  141.         srec2 := srec1^.link; srec3 := srec1;
  142.         repeat { assume we are already sorted properly }
  143.           if srec2^.srec.name < srec3^.srec.name then srec3 := srec2;
  144.           srec2 := srec2^.link;
  145.         until srec2 = nil;
  146.         if srec3 <> srec1 then begin
  147.           srec := srec1^.srec; srec1^.srec := srec3^.srec; srec3^.srec := srec;
  148.         end; srec1 := srec1^.link;
  149.       end;
  150.     end;
  151.  
  152.   begin
  153.     srec_root := nil; findfirst('*.*', anyfile, srec);
  154.     nodearray^[nodenumber].bid := 0;
  155.     while doserror=0 do begin
  156.       if (((srec.attr and directory)>0) and (srec.name[1]<>'.')) then begin
  157.         if srec_root = nil then begin
  158.           new(srec_root); srec_link := srec_root;
  159.         end else begin
  160.           new(srec_link^.link); srec_link := srec_link^.link;
  161.         end; srec_link^.srec := srec; srec_link^.link := nil;
  162.         srec_link^.prev := prevparent;
  163.       end;
  164.       findnext(srec);
  165.     end; inc(prevparent);
  166.     if srec_root <> nil then begin
  167.       if sort_items then sort(srec_root);
  168.       {
  169.       if (srec_root^.link = nil) and (level = 0) and showscan then __betwscn(
  170.         x3, x4, y3, popup_f, popup_b, '\'
  171.       );
  172.       }
  173.       repeat
  174.         s := srec_root^.srec.name;
  175.         with nodearray^[nodenumber] do begin
  176.           name := s; parent := srec_root^.prev;
  177.         end;
  178.         {
  179.         if showscan then __betwscn(
  180.           x3, x4, y3, popup_f, popup_b, __rep(12, ' ')
  181.         );
  182.         }
  183.         inc(nodenumber);
  184.         {
  185.         if showscan then begin
  186.           __betwscn(x3, x4, y3, popup_f, popup_b,
  187.             __juststr(__num(nodenumber), ' ', 3, _left_just_str) + ' : ' +
  188.             __juststr(s, ' ', 12, _right_just_str)
  189.           );
  190.         end;
  191.         }
  192.         chdir(s); search_tree(level+1); chdir('..');
  193.         srec_link := srec_root; srec_root := srec_root^.link;
  194.         dispose(srec_link);
  195.       until (srec_root = nil); {@}
  196.     end { else if (level = 0) and showscan then __betwscn(
  197.       x3, x4, y3, popup_f, popup_b, 'No <DIR>''s'
  198.     );}
  199.   end; { search_tree }
  200.  
  201.  
  202.  
  203.   procedure trace_tree;
  204.   var oldpath: string;
  205.   begin
  206.     nodenumber := 1; prevparent := 0; new(nodearray);
  207.     getdir(0, oldpath); search_tree(0); chdir(oldpath);
  208.   end;
  209.   {██subtree functions███████████████████████████████}
  210. {$I+}
  211.  
  212.  
  213.   procedure dosearch(st: string);
  214.   var searchfn : string;
  215.   begin
  216.     if show then begin
  217.       textcolor(yellow); writeln(st + searchfilename); textcolor(lightgray);
  218.     end;
  219.     findfirst(st + searchfilename, anyfile, dirinfo);
  220.     while doserror = 0 do begin
  221.       nf := false;
  222.       if (
  223.         not(dirinfo.attr in [directory, volumeid]) and (dirinfo.size > 0)
  224.       ) then begin
  225.         searchfn := st + dir + dirinfo.name;
  226.         if show then writeln('  Searching ', dirinfo.name);
  227.         assign(searchfile, searchfn);
  228.         getfattr(searchfile, oldattr); setfattr(searchfile, archive);
  229. {$I-}
  230.         reset(searchfile,1);
  231. {$I+}
  232.         if ioresult<>0 then begin
  233.           writeln('File niet gevonden. Foutje, bedankt!'); 
  234.           chdir(olddir); halt;
  235.         end;
  236.  
  237.         longbufpos := 0;
  238.         repeat
  239.           seek(searchfile, longbufpos);
  240.           blockread(searchfile, buffer, maxbuffer, sizeread);
  241.           maxpos := sizeread - ord(target[0]); bufpointer := 0;
  242.  
  243.           repeat
  244.             i := boyer_moore_search(
  245.               buffer, bufpointer, sizeread,
  246.               target, table1, table2, casesensitive
  247.             );
  248.             if (i > 0) then begin
  249.               bufpointer := i+length(target);
  250.               if redirect then begin
  251.                 writeln;
  252.                 writeln('▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀');
  253.               end;
  254.               clrscr;
  255.               writeln;
  256.               if i < 80 then b4 := 1 else b4 := i - 80;
  257.               textcolor(cyan); j := b4;
  258.               while (j <= i) and (j < sizeread) do begin
  259.                 write(chr(buffer[j])); inc(j);
  260.               end;
  261.               textcolor(white); during := j;
  262.               while (j < sizeread) and (j <= i+length(target)) do begin
  263.                 write(chr(buffer[j])); inc(j);
  264.               end;
  265.               textcolor(cyan); after := j;
  266.               while (j < sizeread) and (j < b4+319) do begin
  267.                 write(chr(buffer[j])); inc(j);
  268.               end;
  269.               textcolor(white); gotoxy(1, 1);
  270.               write('File: ', searchfn, ' Position: ', longbufpos+i);
  271.               gotoxy(1, 22);
  272.               if not redirect then begin
  273.                 writeln; writeln; writeln('Press <RETURN> for next match...');
  274.                 write('<Esc> to quit, <N> to skip to next file');
  275.                 ch := readkey; if ch=#27 then begin
  276.                   chdir(olddir);  halt(0);
  277.                 end;
  278.                 if upcase(ch) = 'N' then nf := true;
  279.               end else if keypressed then if readkey = #27 then nf := true;
  280.             end
  281.           until (i = 0) or (bufpointer > maxpos) or (sizeread=0) or nf;
  282.           longbufpos := longbufpos + maxpos;
  283.         until nf or (sizeread < maxbuffer);
  284.         setfattr(searchfile, oldattr); close(searchfile);
  285.       end;
  286.       textcolor(lightgray);
  287.       if keypressed then if readkey = #27 then begin chdir(olddir); halt end;
  288.       findnext(dirinfo);
  289.     end;
  290.   end; { dosearch }
  291.  
  292.  
  293.  
  294.  
  295. {main}begin
  296.   getdir(0, olddir); 
  297.   {commandstr := string(ptr(prefixseg, $80)^);}
  298.   redirect := not __isconfil(__handlfil(output));
  299.   if redirect then begin assign(output, ''); rewrite(output) end;
  300.   textcolor(lightgray);
  301.   if (paramstr(1)='?') or (paramstr(1)='/?') or (paramstr(1)='-?') then help;
  302.  
  303.   clrscr; 
  304.   if paramstr(1) <> '' then begin
  305.     if pos('\', paramstr(1)) > 0 then begin
  306.       searchfilename := __extractname(paramstr(1));
  307.       chdir(__extractpath(fexpand(paramstr(1))));
  308.     end else searchfilename := paramstr(1)
  309.   end else begin
  310.     write('Enter File to search: '); readln(searchfilename); writeln;
  311.   end;
  312.   curdir := fexpand('');
  313.   if paramstr(2) <> '' then target := paramstr(2) else begin
  314.     write('Enter Search text: '); readln(target); writeln;
  315.   end;
  316.   if __inparams('/c', i) then casesensitive := true else casesensitive := false;
  317.   if __inparams('/d', i) then show := false;
  318.  
  319.   make_boyer_moore_table(target, table1, table2, casesensitive);
  320.   writeln; fsplit(searchfilename, dir, name, ext);
  321.  
  322.   if __inparams('/s', i) then begin
  323.     textcolor(lightgray); textbackground(black);
  324.     if redirect then begin
  325.       for i := 0 to paramcount do writeln(paramstr(i));
  326.       writeln(searchfilename, ', ', dir, ', ', name, ', ', ext);
  327.       writeln;
  328.     end else begin writeln; write('Tree...') end;
  329.     trace_tree; gotoxy(1, wherey); clreol;
  330.     for nod := 0 to nodenumber-1 do dosearch(
  331.       __backrem(curdir) + __backapp(trace_path(nod))
  332.     );
  333.   end else dosearch('');
  334.  
  335.   writeln;
  336.   textcolor(black); textbackground(lightgray);
  337.   write('Searches complete...');
  338.   textcolor(lightgray); textbackground(black);
  339.   writeln; chdir(olddir);
  340. {happy}end.
  341.  
  342. {
  343.   best regards, mark ouellet.
  344.   stuck with a virus, anyone heard of windows ??? (fidonet 1:240/1.4)
  345. }
  346.