home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / ECO30603.ZIP / ECO30603.LZH / ECOLIBCS / DEMOS / QF.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-03-08  |  13.2 KB  |  417 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.   function  __backrem(s: string) : string;
  86.   begin
  87.     if s[length(s)] = '\' then __backrem := copy(s, 1, length(s)-1) else
  88.       __backrem := s;
  89.   end;
  90.  
  91.  
  92.   function  __backapp(s: string) : string;
  93.   begin
  94.     if s[length(s)]<>'\' then __backapp := s + '\' else __backapp := s;
  95.   end;
  96.  
  97.  
  98.  
  99.   function __extractname(s : string): string;
  100.   var i : byte;
  101.   begin
  102.     i := length(s); while s[i] <> '\' do dec(i);
  103.     __extractname := copy(s, i+1, length(s)-i);
  104.   end;
  105.  
  106.  
  107.   function __extractpath(s : string): string; { alleen ROOT eindigt op \ }
  108.   var i : byte;
  109.   begin
  110.     i := length(s); while (s[i] <> '\') and (i > 1) do dec(i);
  111.     if i > 3 then dec(i);
  112.     __extractpath := copy(s, 1, i);
  113.   end;
  114.  
  115.  
  116.  
  117.   procedure help;
  118.   begin
  119.     textcolor(yellow);
  120.     writeln('QF - QuickFind String Find Utility -- Version 1.0');
  121.     writeln('(C) MCMXCII by UltiHouse Software / The ECO Group.');
  122.     writeln('Part of the UltiGREP package: GSR, USR, QF, UGREP.');
  123.     textcolor(lightgray); writeln;
  124.     writeln('Usage: QF [wildfile] [searchtext] [/c] [/s] [/d]');
  125.     writeln('  /c search case sensitive');
  126.     writeln('  /s search entire subtree');
  127.     writeln('  /d disable show on screen');
  128.     writeln;
  129.     writeln('Examples:');
  130.     writeln('  QF *.PAS helpproc /c /s');
  131.     writeln('  QF C:\PROG\BORLANDC\INCLUDE\*.H _WinExitErr /c /s >d:\find');
  132.     halt(0);
  133.   end;
  134.  
  135.  
  136.   function __comp(s1, s2: string): boolean;
  137.   begin
  138.     __comp := (
  139.       __cvtstr(s1, _rem_white_str + _to_upcase_str) =
  140.       __cvtstr(s2, _rem_white_str + _to_upcase_str)
  141.     )
  142.   end;
  143.  
  144.  
  145.  
  146.   function __inparams(s: string): boolean;
  147.   var
  148.     i :    word;
  149.     b : boolean;
  150.  
  151.   begin
  152.     b := false;
  153.     for i := 1 to paramcount do b := b or __comp(s, paramstr(i));
  154.     __inparams := b;
  155.   end;
  156.  
  157.  
  158.  
  159.   function trace_path(temphead: integer): pathstr;
  160.   var
  161.     off      :                    word;
  162.     st       :                  string;
  163.     i        :                    word;
  164.     tmp      : array[1..50] of integer;
  165.  
  166.   begin
  167.     off := 0;
  168.     while temphead>0 do begin
  169.       inc(off); tmp[off] := temphead;
  170.       temphead := nodearray^[temphead].parent;
  171.     end; st := '\';
  172.     if off>0 then for i := off downto 1 do
  173.       st := st + nodearray^[tmp[i]].name +'\';
  174.     off := 0; fillchar(tmp, sizeof(tmp), chr(48));
  175.     trace_path := st;
  176.   end;
  177.  
  178.  
  179. {$I-}
  180.   procedure search_tree(level: word);
  181.   var
  182.     i         :  integer;
  183.     s         :   string;
  184.     srec_root,
  185.     srec_link : srec_ptr;
  186.  
  187.     { not much subdirs in one dir, so no efficiency taken into account }
  188.     procedure sort(srec_root: srec_ptr);
  189.     var
  190.       srec1, srec2, srec3 :  srec_ptr;
  191.       srec                : searchrec;
  192.  
  193.     begin
  194.       srec1 := srec_root;
  195.       while srec1^.link <> nil do begin
  196.         srec2 := srec1^.link; srec3 := srec1;
  197.         repeat { assume we are already sorted properly }
  198.           if srec2^.srec.name < srec3^.srec.name then srec3 := srec2;
  199.           srec2 := srec2^.link;
  200.         until srec2 = nil;
  201.         if srec3 <> srec1 then begin
  202.           srec := srec1^.srec; srec1^.srec := srec3^.srec; srec3^.srec := srec;
  203.         end; srec1 := srec1^.link;
  204.       end;
  205.     end;
  206.  
  207.   begin
  208.     srec_root := nil; findfirst('*.*', anyfile, srec);
  209.     nodearray^[nodenumber].bid := 0;
  210.     while doserror=0 do begin
  211.       if (((srec.attr and directory)>0) and (srec.name[1]<>'.')) then begin
  212.         if srec_root = nil then begin
  213.           new(srec_root); srec_link := srec_root;
  214.         end else begin
  215.           new(srec_link^.link); srec_link := srec_link^.link;
  216.         end; srec_link^.srec := srec; srec_link^.link := nil;
  217.         srec_link^.prev := prevparent;
  218.       end;
  219.       findnext(srec);
  220.     end; inc(prevparent);
  221.     if srec_root <> nil then begin
  222.       if sort_items then sort(srec_root);
  223.       {
  224.       if (srec_root^.link = nil) and (level = 0) and showscan then __betwscn(
  225.         x3, x4, y3, popup_f, popup_b, '\'
  226.       );
  227.       }
  228.       repeat
  229.         s := srec_root^.srec.name;
  230.         with nodearray^[nodenumber] do begin
  231.           name := s; parent := srec_root^.prev;
  232.         end;
  233.         {
  234.         if showscan then __betwscn(
  235.           x3, x4, y3, popup_f, popup_b, __rep(12, ' ')
  236.         );
  237.         }
  238.         inc(nodenumber);
  239.         {
  240.         if showscan then begin
  241.           __betwscn(x3, x4, y3, popup_f, popup_b,
  242.             __juststr(__num(nodenumber), ' ', 3, _left_just_str) + ' : ' +
  243.             __juststr(s, ' ', 12, _right_just_str)
  244.           );
  245.         end;
  246.         }
  247.         chdir(s); search_tree(level+1); chdir('..');
  248.         srec_link := srec_root; srec_root := srec_root^.link;
  249.         dispose(srec_link);
  250.       until (srec_root = nil); {@}
  251.     end { else if (level = 0) and showscan then __betwscn(
  252.       x3, x4, y3, popup_f, popup_b, 'No <DIR>''s'
  253.     );}
  254.   end; { search_tree }
  255.  
  256.  
  257.  
  258.   procedure trace_tree;
  259.   var oldpath: string;
  260.   begin
  261.     nodenumber := 1; prevparent := 0; new(nodearray);
  262.     getdir(0, oldpath); search_tree(0); chdir(oldpath);
  263.   end;
  264.   {██subtree functions███████████████████████████████}
  265. {$I+}
  266.  
  267.  
  268.   procedure dosearch(st: string);
  269.   var searchfn : string;
  270.   begin
  271.     if show then begin
  272.       textcolor(yellow); writeln(st + searchfilename); textcolor(lightgray);
  273.     end;
  274.     findfirst(st + searchfilename, anyfile, dirinfo);
  275.     while doserror = 0 do begin
  276.       nf := false;
  277.       if (
  278.         not(dirinfo.attr in [directory, volumeid]) and (dirinfo.size > 0)
  279.       ) then begin
  280.         searchfn := st + dir + dirinfo.name;
  281.         if show then writeln('  Searching ', dirinfo.name);
  282.         assign(searchfile, searchfn);
  283.         getfattr(searchfile, oldattr); setfattr(searchfile, archive);
  284. {$I-}
  285.         reset(searchfile,1);
  286. {$I+}
  287.         if ioresult<>0 then begin
  288.           writeln('File niet gevonden. Foutje, bedankt!'); 
  289.           chdir(olddir); halt;
  290.         end;
  291.  
  292.         longbufpos := 0;
  293.         repeat
  294.           seek(searchfile, longbufpos);
  295.           blockread(searchfile, buffer, maxbuffer, sizeread);
  296.           maxpos := sizeread - ord(target[0]); bufpointer := 0;
  297.  
  298.           repeat
  299.             i := boyer_moore_search(
  300.               buffer, bufpointer, sizeread,
  301.               target, table1, table2, casesensitive
  302.             );
  303.             if (i > 0) then begin
  304.               bufpointer := i+length(target);
  305.               if redirect then begin
  306.                 writeln;
  307.                 writeln('▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀');
  308.               end;
  309.               clrscr;
  310.               writeln;
  311.               if i < 80 then b4 := 1 else b4 := i - 80;
  312.               textcolor(cyan); j := b4;
  313.               while (j <= i) and (j < sizeread) do begin
  314.                 write(chr(buffer[j])); inc(j);
  315.               end;
  316.               textcolor(white); during := j;
  317.               while (j < sizeread) and (j <= i+length(target)) do begin
  318.                 write(chr(buffer[j])); inc(j);
  319.               end;
  320.               textcolor(cyan); after := j;
  321.               while (j < sizeread) and (j < b4+319) do begin
  322.                 write(chr(buffer[j])); inc(j);
  323.               end;
  324.               textcolor(white); gotoxy(1, 1);
  325.               write('File: ', searchfn, ' Position: ', longbufpos+i);
  326.               gotoxy(1, 22);
  327.               if not redirect then begin
  328.                 writeln; writeln; writeln('Press <RETURN> for next match...');
  329.                 write('<Esc> to quit, <N> to skip to next file');
  330.                 ch := readkey; if ch=#27 then begin
  331.                   chdir(olddir);  halt(0);
  332.                 end;
  333.                 if upcase(ch) = 'N' then nf := true;
  334.               end else if keypressed then if readkey = #27 then nf := true;
  335.             end
  336.           until (i = 0) or (bufpointer > maxpos) or (sizeread=0) or nf;
  337.           longbufpos := longbufpos + maxpos;
  338.         until nf or (sizeread < maxbuffer);
  339.         setfattr(searchfile, oldattr); close(searchfile);
  340.       end;
  341.       textcolor(lightgray);
  342.       if keypressed then if readkey = #27 then begin chdir(olddir); halt end;
  343.       findnext(dirinfo);
  344.     end;
  345.   end; { dosearch }
  346.  
  347.  
  348.  
  349.   function __handlfil(var filevar) : word;
  350.   begin
  351.     if (filerec(filevar).mode = fmclosed) then __handlfil := $ffff else
  352.       __handlfil := filerec(filevar).handle
  353.   end;
  354.  
  355.  
  356.   function  __isconfil(handle : word) : boolean;
  357.   var reg : registers;
  358.   begin
  359.     with reg do begin
  360.      ah := $44; al := 0; bx := handle; intr($21, reg);
  361.      __isconfil := ((dl and $80) <> 0) and ((dl and $03) <> 0)
  362.     end
  363.   end;
  364.  
  365.  
  366. {main}begin
  367.   getdir(0, olddir); 
  368.   {commandstr := string(ptr(prefixseg, $80)^);}
  369.   redirect := not __isconfil(__handlfil(output));
  370.   if redirect then begin assign(output, ''); rewrite(output) end;
  371.   textcolor(lightgray);
  372.   if (paramstr(1)='?') or (paramstr(1)='/?') or (paramstr(1)='-?') then help;
  373.  
  374.   clrscr; 
  375.   if paramstr(1) <> '' then begin
  376.     if pos('\', paramstr(1)) > 0 then begin
  377.       searchfilename := __extractname(paramstr(1));
  378.       chdir(__extractpath(fexpand(paramstr(1))));
  379.     end else searchfilename := paramstr(1)
  380.   end else begin
  381.     write('Enter File to search: '); readln(searchfilename); writeln;
  382.   end;
  383.   curdir := fexpand('');
  384.   if paramstr(2) <> '' then target := paramstr(2) else begin
  385.     write('Enter Search text: '); readln(target); writeln;
  386.   end;
  387.   if __inparams('/c') then casesensitive := true else casesensitive := false;
  388.   if __inparams('/d') then show := false;
  389.  
  390.   make_boyer_moore_table(target, table1, table2, casesensitive);
  391.   writeln; fsplit(searchfilename, dir, name, ext);
  392.  
  393.   if __inparams('/s') then begin
  394.     textcolor(lightgray); textbackground(black);
  395.     if redirect then begin
  396.       for i := 0 to paramcount do writeln(paramstr(i));
  397.       writeln(searchfilename, ', ', dir, ', ', name, ', ', ext);
  398.       writeln;
  399.     end else begin writeln; write('Tree...') end;
  400.     trace_tree; gotoxy(1, wherey); clreol;
  401.     for nod := 0 to nodenumber-1 do dosearch(
  402.       __backrem(curdir) + __backapp(trace_path(nod))
  403.     );
  404.   end else dosearch('');
  405.  
  406.   writeln;
  407.   textcolor(black); textbackground(lightgray);
  408.   write('Searches complete...');
  409.   textcolor(lightgray); textbackground(black);
  410.   writeln; chdir(olddir);
  411. {happy}end.
  412.  
  413. {
  414.   best regards, mark ouellet.
  415.   stuck with a virus, anyone heard of windows ??? (fidonet 1:240/1.4)
  416. }
  417.