2003 TextMaestro Technologies
Example 16: Change C-style comment to C++-style
One of the common problems we encounter is converting C-style comments to C++-style. The fundamental reason of preferring C++-style comment is that you can comment out a block of code which may have comments in it. If that comment is in C-style, this block-comment-out of code is not straight forward. Therefore, often we decide to convert C-style comments to C++-style and we like to that for a set of files of a project. Here we will demonstrate step-by-step how TextMaestro does it.
Step 1: Interactive mode
Open TextMaestro, and click on Text Conversion tool as shown below:
This will open Attributes window and Interactive Window side by side. Enter the following information in those boxes:
Enter the following text in the Input window as shown below:
Click on Convert button to obtain the following:
Comment: Note that text within /* and */ is to be transformed. Since * needs to be a non-wild token in Find, we could not use it as a wild-card. Thereby, we selected @ as wild-card for Find (you can select some other character of your choice.) If you have not noticed yet, you change the wild-card from default * to something of your choice by clicking on the Wild-Card Manager button.
By defining Find as /*@*/ we are basically getting hold of a block of C-style comment, which may consist of multiple lines. Once that text is assimilated, we clean it by ?@=//@ qualifier.
Recall, a singleton wild-card denotes one
line of the input text. Therefore, the
Step 2: Using a library of macros
The above macro does 90% of the job. However, remaining 10% will need several macros. So we use a library of macros to process a complex case.
To create a library, hit button.
As shown below, create a new library called C-comment-C++ using left Add button. Then using right Add button, add the aforementioned macro to the right list.
When you hit on right Add button, Find and Replace dialog where we define the new macro looks like this:
That is how we add a macro to a library.
For this problem of converting C-style comments to C++-style, we have not addressed the following issues:
Considering above issues, here is an extension of above library:
Download tmt_lib_fr_c_to_cpp.txt that contains above library.
See Example 11: Import Libraries to import above library from this file.
Here is a brief description of above macros:
Note, this library does not address the case when /* or */ is part of a string. Several ways you can handle that situation. One is to add a macro at the beginning of the library which can detect a string containing /* or */ and hide it. Add another macro at the end of the library that will put /* or */ back in the string.
Step 3: Processing multiple files
Now that we have prepared a library to convert C-style comment to C++ style, let us convert some files, all at one shot.
(1) Drag the *.c files into the list. This is how main view will appear.
(2) Click on Text Conversion button while holding down the Shift key.
The following dialog will appear.
Perform the following steps here:
1. Click on Search in files using macros in library button. This will process the selected files and create a list of the instances of find and replace, as shown below:
You can review an instance by clicking on it on the right list. A secondary dialog box will appear.
At this point if you hit Commit Writing button, results will be written into the Output files. But that will replace the original files. If we want to redirect the output to a different directory, by leaving the input files intact, we do the following.
3. Now you are ready to hit Commit Writing button. That will write all the output to the desired location.
Keep in mind that a batch job can take several hours. There are several parameters that determine the execution time.
Here are a few:
Here are some examples: