Borland Online And The Cobb Group Present:


May, 1994 - Vol. 1 No. 5

Borland C++ 4.0 feature - Calling GREP from the IDE

In the April 1994 issue of Borland C++ Developer's Journal, we showed you how you can use the GREP.COM utility from a DOS command line to search for specific text strings in different files (Source Code Management - Searching for text in multiple source files). However, once you've seen the names of the files and the line numbers in which the search text appears, you may have difficulty remembering this information when it's time to open the files in the Integrated Development Environment (IDE).

With Borland C++ 4.0, you can use the GREP (Global Regular Expression Print) utility from inside the IDE. In this article, we'll show you how you can use the GREP utility with the IDE to quickly locate and examine strings in source, header, or other types of text files.

Finding text strings

To call the GREP utility from inside the IDE, you'll first choose Grep from the Tool menu. When the Program Arguments dialog box appears, enter the command-line parameters you'd normally add after GREP in a DOS command line, as shown in Figure A.


Figure A - You'll enter the command-line arguments for the GREP utility in the Program Arguments dialog box.

When you click OK, the IDE will call the GREP utility with your parameters. By default, the output from the GREP utility will appear in the IDE's Message window. (For information on routing the output to a text file rather than the Message window, see Extending the 4.0 IDE - Sending GREP's IDE output to a file.)

After GREP's output appears, you can open any of the files it lists by double-clicking on the corresponding line in the Message window. The IDE will then open an editor window for that file and scroll to the line containing the particular text you searched for.

Searching an open file

If the IDE's current front window is an editor window for a file, the IDE will pass that file's name to the GREP utility as the default file argument, as shown in Figure B. If you want to search for a text string in that file only, replace the search text in the Program Arguments dialog box with the text or pattern of characters you want to find.

Figure B - If the front window in the IDE is an editor window for a file, the IDE passes that file's name as the default file argument.

Searching files in the current project

If you've created a project that contains multiple source or header files, you can pass one or more of those files as the file argument(s) for the GREP utility. Simply highlight the first file in the Project window you want to search, then hold down [Shift] and click on the names of any other files you want to search. (If you want to select multiple files, you must list them contiguously­­you can't skip files in between the ones you want.)

If you select too many files or if the paths or filenames are too long, you may see a warning in the Program Arguments dialog box, as shown in Figure C. If you want to search all the CPP or H files in a given directory, supply the directory path and *.CPP or *.H instead.


Figure C - You may see a warning if the command-line arguments become too complex.

Putting GREP to work

To see how the IDE integrates the GREP utility, let's work through a simple example. To begin, launch the Borland C++ 4.0 IDE.

When the IDE's desktop appears, choose Open Project... from the Project menu. When the Open Project File dialog box appears, enter the following path and filename in the File Name entry field:

\BC4\EXAMPLES\WINDOWS\WHELLO\WHELLO.IDE

Then, click OK to open the project.

In the Project window, click on the name WHELLO[.CPP] with the left mouse button. Now, choose Grep from the Tool menu.

When the Program Arguments dialog box appears, double-click on the word void in the entry field and enter WPARAM and a space. When you click the OK button, the Running Tool dialog box appears, as shown in Figure D.


Figure D - The Running Tool dialog box displays the command-line parameters that the IDE passed to GREP.COM.

After GREP searches WHELLO.CPP, the Running Tool dialog box closes, and the IDE brings the Message window to the front. The Message window now contains a message for each line in WHELLO.CPP that contains the text string WPARAM, as shown in Figure E.

Figure E - When you call the GREP utility from the IDE, the output appears in the Message window by default.

To see a particular occurrence of the search string, double-click on any of the Transfer messages. When you do so, an editor window for WHELLO.CPP will appear with the cursor on the line the Message window referenced.

Calling GREP from the 3.1 DOS IDE

You can also call the GREP utility from the Borland C++ version 3.1 DOS IDE by choosing GREP from the Transfer menu. However, the default command-line arguments in the DOS IDE are

void *.c

If you want to use the front editor window's file as the default filename argument, you'll have to change the default settings of the GREP Transfer item.

Conclusion

The GREP utility provides an easy way to search for text strings in a project's source and header files. When you call GREP from the IDE, it's even simpler to use.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.