home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D12 / GREPOWL.ZIP / OWLGREP.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  11.0 KB  |  446 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Grep Demo                      }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. program OwlGrep;
  9.  
  10. {$M 20000,8192}
  11.  
  12. uses Strings, WinCrt, WinDos, WinProcs, WinTypes, Objects,
  13.   OWindows, ODialogs, RegExp, OGConst;
  14.  
  15. {$R OwlGrep}
  16.  
  17. const
  18.   Profile = 'OWLGREP.INI';
  19.  
  20. const
  21.   wm_GetPrivateStrings = wm_User;
  22.  
  23.  
  24.   rgb_Yellow      = $0000FFFF;
  25.   rgb_DarkYellow  = $0000C1FF;
  26.   rgb_Blue        = $00FF0000;
  27.   rgb_Red         = $000000FF;
  28.   rgb_Green       = $0000FF00;
  29.   rgb_DarkGreen   = $00007F00;
  30.  
  31. type
  32.   TRequest = record
  33.     Expression: array[0..80] of Char;
  34.     FileMask: array[0..40] of Char;
  35.     StartDir: array[0..128] of Char;
  36.     CaseSensitive,
  37.     UseSubDirs: Boolean;
  38.   end;
  39.  
  40.   PGrep = ^TGrep;
  41.   TGrep = object(TDlgWindow)
  42.     BoxBrush,
  43.     TheBrush: HBrush;
  44.     FileList: PListBox;
  45.     CaseCheck,
  46.     SubDirCheck: PCheckBox;
  47.     StatusText: PStatic;
  48.     FileMask,
  49.     Directory,
  50.     Expression: PEdit;
  51.     FileColl: PStrCollection;
  52.     constructor Init(P: PWindowsObject; N: PChar);
  53.     procedure InitControls;
  54.     destructor Done; virtual;
  55.     procedure SetupWindow; virtual;
  56.     procedure GetWindowClass(var WndClass: TWndClass); virtual;
  57.     function GetClassName: PChar; virtual;
  58.     function RunSearch: Boolean;
  59.     procedure FillBox;
  60.     procedure GetControlData;
  61.     procedure SetControlData;
  62.     procedure ReadProfile;
  63.     procedure WriteProfile;
  64.     procedure ListClick(var Msg: TMessage);
  65.       virtual id_First + idFileList;
  66.     procedure BeginSearch(var Msg: TMessage);
  67.       virtual id_First + idBeginSearch;
  68.     procedure WMControlColor(var Msg: TMessage);
  69.       virtual wm_First + wm_CtlColor;
  70.   end;
  71.  
  72.   TWhereApp = object(TApplication)
  73.     procedure InitMainWindow; virtual;
  74.   end;
  75.  
  76. var
  77.   Request: TRequest;
  78.  
  79. procedure ACenterDlg(HWindow: HWnd);
  80. var
  81.   R: TRect;
  82.   StartX,
  83.   StartY : Integer;
  84.   Frame,
  85.   Caption: Integer;
  86. begin
  87.   Frame := GetSystemMetrics(sm_CxFrame) * 2;
  88.   Caption := GetSystemMetrics(sm_CyCaption);
  89.   GetClientRect(HWindow, R);
  90.   StartX := ((GetSystemMetrics(sm_CxScreen) - (R.Right - R.Left)) div 2);
  91.   StartY := ((GetSystemMetrics(sm_CyScreen) - (R.Bottom - R.Top)) div 2);
  92.   MoveWindow(HWindow, StartX, StartY - ((Caption + Frame) div 2),
  93.     R.Right + Frame, R.Bottom + Frame + Caption, False);
  94. end;
  95.  
  96. { TGrep }
  97.  
  98. constructor TGrep.Init(P: PWindowsObject; N: PChar);
  99. begin
  100.   inherited Init(P, N);
  101.   InitControls;
  102.   FileColl := nil;
  103. end;
  104.  
  105. procedure TGrep.InitControls;
  106. var
  107.   LogBrush: TLogBrush;
  108. begin
  109.   FileList := New(PListBox, InitResource(@Self, idFileList));
  110.   Directory := New(PEdit, InitResource(@Self, idDirectory, 80));
  111.   FileMask := New(PEdit, InitResource(@Self, idFileMask, 80));
  112.   Expression := New(PEdit, InitResource(@Self, idExpression, 80));
  113.  
  114.   StatusText := New(PStatic, InitResource(@Self, idStatus,80));
  115.  
  116.   CaseCheck := New(PCheckBox, InitResource(@Self, idCaseSensitive));
  117.   SubDirCheck := New(PCheckBox, InitResource(@Self, idRecurseDirs));
  118.  
  119.   LogBrush.lbStyle := bs_Solid;
  120.   LogBrush.lbColor := RGB(128,128,128);
  121.   LogBrush.lbHatch := bs_Solid;
  122.   TheBrush  := CreateBrushIndirect(LogBrush);
  123.  
  124.   LogBrush.lbStyle := bs_Solid;
  125.   LogBrush.lbColor := RGB(0,0,64);
  126.   LogBrush.lbHatch := bs_Solid;
  127.   BoxBrush := CreateBrushIndirect(LogBrush);
  128. end;
  129.  
  130. destructor TGrep.Done;
  131. begin
  132.   WriteProfile;
  133.   DeleteObject(TheBrush);
  134.   DeleteObject(BoxBrush);
  135.   inherited Done;
  136. end;
  137.  
  138. function TGrep.RunSearch: Boolean;
  139. var
  140.   RegExp: HRegExp;
  141.   Error: Integer;
  142.   P: PChar;
  143.  
  144.   function Search(Filename: PChar): Boolean;
  145.   var
  146.     TextFile: Text;
  147.     Line: array[0..255] of Char;
  148.     Match: TRegMatch;
  149.   begin
  150.     Search := False;
  151.     StatusText^.SetText(Filename);
  152.     Assign(TextFile, Filename);
  153.     {$I-}
  154.     Reset(TextFile);
  155.     while not Eof(TextFile) do
  156.     begin
  157.       Readln(TextFile, Line);
  158.       if not Request.CaseSensitive then StrUpper(Line);
  159.       if RegExec(RegExp, Line, Match) = 0 then
  160.       begin
  161.         Search := True;
  162.         Break;
  163.       end;
  164.     end;
  165.     Close(TextFile);
  166.   end;
  167.  
  168.   procedure SearchDir(Dir: PChar);
  169.   var
  170.     SR: TSearchRec;
  171.     NewDir: array[0..255] of Char;
  172.   begin
  173.     with Request do
  174.     begin
  175.       StrCopy(StrECopy(NewDir, Dir), FileMask);
  176.       FindFirst(NewDir, faArchive, SR);
  177.       while DosError = 0 do
  178.       begin
  179.         StrCopy(StrECopy(NewDir, Dir), SR.Name);
  180.         if Search(NewDir) then
  181.           FileColl^.Insert(StrNew(NewDir));
  182.         FindNext(SR);
  183.       end;
  184.  
  185.       if Request.UseSubDirs then
  186.       begin
  187.         StrCopy(StrECopy(NewDir, Dir), '*.*');
  188.         FindFirst(NewDir, faDirectory, SR);
  189.         while DosError = 0 do
  190.         begin
  191.           if (SR.Attr and faDirectory <> 0) and (SR.Name[0] <> '.') then
  192.           begin
  193.             StrCopy(StrECopy(StrECopy(NewDir, Dir), SR.Name), '\'); 
  194.             SearchDir(NewDir);
  195.           end;
  196.           FindNext(SR);
  197.         end;
  198.       end;
  199.     end;
  200.   end;
  201.  
  202.   procedure CompileExp;
  203.   var
  204.     Exp: array[0..SizeOf(Request.Expression)] of Char;
  205.   begin
  206.     StrCopy(Exp, Request.Expression);
  207.     if not Request.CaseSensitive then StrUpper(Exp);
  208.     RegExp := RegComp(Exp, Error);
  209.   end;
  210.     
  211. begin
  212.   CompileExp;
  213.   with Request do
  214.   begin
  215.     P := StrEnd(Request.StartDir);
  216.  
  217.     { Force a trailing back-slash }
  218.     if ((P - StartDir > 2) or (StartDir[1] <> ':')) and
  219.         ((P - 1)^ <> '\') then
  220.       StrCopy(P, '\');
  221.   end;
  222.   SearchDir(Request.StartDir);
  223.   P^ := #0; { Undo the backslash }
  224.  
  225.   RegFree(RegExp);
  226. end;
  227.  
  228. procedure TGrep.ReadProfile;
  229. begin
  230.   GetPrivateProfileString('OwlGrep', 'Expression', '',
  231.     Request.Expression, SizeOf(Request.Expression), Profile);
  232.   GetPrivateProfileString('OwlGrep','FileMask','*.PAS',
  233.     Request.FileMask, SizeOf(Request.FileMask), Profile);
  234.   GetCurDir(Request.StartDir, 0);
  235.  
  236.   Request.CaseSensitive := Bool(GetPrivateProfileInt('OwlGrep',
  237.     'CaseSensitive', 1, Profile));
  238.   Request.UseSubDirs := Bool(GetPrivateProfileInt('OwlGrep',
  239.     'SubDirs', 1, Profile));
  240. end;
  241.  
  242. procedure TGrep.WriteProfile;
  243. var
  244.   S: array[0..10] of Char;
  245. begin
  246.   WritePrivateProfileString('OwlGrep', 'Expression', Request.Expression,
  247.     Profile);
  248.   WritePrivateProfileString('OwlGrep', 'FileMask', Request.FileMask,
  249.     Profile);
  250.  
  251.   Str(Integer(Request.CaseSensitive), S);
  252.   WritePrivateProfileString('OwlGrep', 'CaseSensitive', S, Profile);
  253.   Str(Integer(Request.UseSubDirs), S);
  254.   WritePrivateProfileString('OwlGrep', 'SubDirs', S, Profile);
  255. end;
  256.  
  257. procedure TGrep.SetControlData;
  258. begin
  259.   Expression^.SetText(Request.Expression);
  260.   FileMask^.SetText(Request.FileMask);
  261.   Directory^.SetText(Request.StartDir);
  262.  
  263.   CaseCheck^.SetCheck(Integer(Request.CaseSensitive));
  264.   SubDirCheck^.SetCheck(Integer(Request.UseSubDirs));
  265. end;
  266.  
  267. procedure TGrep.GetControlData;
  268. begin
  269.   Expression^.GetText(Request.Expression, SizeOf(Request.Expression));
  270.   FileMask^.GetText(Request.FileMask, SizeOf(Request.FileMask));
  271.   Directory^.GetText(Request.StartDir, SizeOf(Request.StartDir) - 1);
  272.  
  273.   Request.CaseSensitive := CaseCheck^.GetCheck = 1;
  274.   Request.UseSubDirs := SubDirCheck^.GetCheck = 1;
  275. end;
  276.  
  277. procedure TGrep.SetupWindow;
  278. var
  279.   Msg: TMessage;
  280. begin
  281.   inherited SetUpWindow;
  282.  
  283.   ACenterDlg(HWindow);
  284.   ReadProfile;
  285.   SetControlData;
  286. end;
  287.  
  288. procedure TGrep.WMControlColor(var Msg: TMessage);
  289. begin
  290.   case Msg.LParamHi of
  291.     ctlColor_Btn:
  292.       begin
  293.         SetTextColor(Msg.WParam, Rgb_Blue);
  294.         SetBkMode(Msg.WParam, transparent);
  295.         Msg.Result := TheBrush;
  296.       end;
  297.     ctlColor_Static:
  298.       begin
  299.         SetTextColor(Msg.WParam, Rgb_Blue);
  300.         SetBkMode(Msg.WParam, transparent);
  301.         Msg.Result := TheBrush;
  302.       end;
  303.     ctlColor_ListBox, ctlColor_Edit:
  304.       begin
  305.         SetTextColor(Msg.WParam, Rgb_DarkYellow);
  306.         SetBkMode(Msg.WParam, transparent);
  307.         Msg.Result := BoxBrush;
  308.       end;
  309.     ctlcolor_Dlg:
  310.       begin
  311.         SetBkMode(Msg.WParam, Transparent);
  312.         Msg.Result := TheBrush;
  313.       end;
  314.   else
  315.     DefWndProc(Msg);
  316.   end;
  317. end;
  318.  
  319. procedure TGrep.FillBox;
  320. var
  321.   i: Integer;
  322. begin
  323.   FileList^.ClearList;
  324.   if FileColl = nil then Exit;
  325.   for i := 0 to FileColl^.Count - 1 do
  326.     FileList^.AddString(FileColl^.At(i));
  327. end;
  328.  
  329. procedure TGrep.ListClick(var Msg: TMessage);
  330. var
  331.   Choice: Integer;
  332.   Selection: PChar;
  333.   Cmd: array[0..300] of Char;
  334. begin
  335.   if Msg.LParamHi = lbn_DblClk then
  336.   begin
  337.     Choice := FileList^.GetSelIndex;
  338.     Selection := FileColl^.At(Choice);
  339.     StrCopy(StrECopy(Cmd, 'NotePad.Exe '), Selection);
  340.     WinExec(Cmd, sw_ShowNormal);
  341.   end;
  342. end;
  343.  
  344. procedure TGrep.BeginSearch(var Msg: TMessage);
  345. var
  346.   S: array[0..255] of Char;
  347.  
  348.   function DirOk(StartDir: PChar): Boolean;
  349.   var
  350.     P: PChar;
  351.   begin
  352.     StrUpper(StartDir);
  353.     { Strip trailing backslash }
  354.     SetCurDir(StartDir);
  355.     if (DosError <> 0) or (StrLen(StartDir) = 0) then
  356.     begin
  357.       StrCopy(StrECopy(S, 'Could not find directory: '), StartDir);
  358.       MessageBox(HWindow, S, 'Notice',mb_Ok);
  359.       SetFocus(Directory^.HWindow);
  360.       DirOk := False;
  361.     end;
  362.   end;
  363.  
  364.   function FileMaskOk(Mask: PChar): Boolean;
  365.   begin
  366.     if Mask[0] = #0 then
  367.     begin
  368.       FileMaskOk := False;
  369.       MessageBox(HWindow, 'You must provide a file mask. For Instance: *.*',
  370.         'Notice', mb_Ok);
  371.       SetFocus(FileMask^.HWindow);
  372.     end
  373.     else FileMaskOk := True;
  374.   end;
  375.  
  376.   function KeyOk(SrchStr: PChar): Boolean;
  377.   var
  378.     RegExp: HRegExp;
  379.     Error: Integer;
  380.     P: PChar;
  381.   begin
  382.     P := nil;
  383.     if SrchStr[0] = #0 then
  384.       P := 'Search Key can''t be empty'
  385.     else
  386.     begin
  387.       RegExp := RegComp(SrchStr, Error);
  388.       if RegExp = 0 then
  389.         P := 'Invalid regular expression'
  390.       else
  391.         RegFree(RegExp);
  392.     end;
  393.  
  394.     if P <> nil then
  395.     begin
  396.       KeyOk := False;
  397.       MessageBox(HWindow, P, 'Notice', mb_Ok);
  398.       SetFocus(Expression^.HWindow);
  399.     end
  400.     else
  401.       KeyOk := True;
  402.   end;
  403.  
  404. begin
  405.   if FileColl <> nil then Dispose(FileColl, Done);
  406.   FileColl := New(PStrCollection, Init(100, 50));
  407.   GetControlData;
  408.   if not DirOk(Request.StartDir) then Exit;
  409.   if not FileMaskOk(Request.FileMask) then Exit;
  410.   if not KeyOk(Request.Expression) then Exit;
  411.   SetControlData;
  412.  
  413.   StatusText^.SetText('Searching...');
  414.   RunSearch;
  415.   FillBox;
  416.   WVSPrintF(S, 'Number found: %d', FileColl^.Count);
  417.   StatusText^.SetText(S);
  418. end;
  419.  
  420. procedure TGrep.GetWindowClass(var WndClass: TWndClass);
  421. begin
  422.   inherited GetWindowClass(WndClass);
  423.   WndClass.hIcon := LoadIcon(hInstance,'WhereIcon');
  424. end;
  425.  
  426. function TGrep.GetClassName : PChar;
  427. begin
  428.   GetClassName := 'TGrep';
  429. end;
  430.  
  431. { TWhereApp }
  432.  
  433. procedure TWhereApp.InitMainWindow;
  434. begin
  435.   MainWindow := New(PGrep, Init(nil, 'GrepDlg'));
  436. end;
  437.  
  438. var
  439.   WhereApp: TWhereApp;
  440.  
  441. begin
  442.   WhereApp.Init('Search');
  443.   WhereApp.Run;
  444.   WhereApp.Done;
  445. end.
  446.