Home

 

Examples

 

Technical

 TextMaestro"

 

2003 TextMaestro Technologies

 

  Examples

  Example 1

  Example 2

  Example 3

  Example 4

  Example 5

  Example 6

  Example 7

  Example 8

  Example 9

  Example 10

  Example 11

  Example 12

  Example 13

  Example 14

  Example 15

  Example 16

  Example 17

  Example 18

  Example 19

  Example 20

  Example 21

 

 

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:

 

In text format, the macro is:

Find:

/*@*/

Replace:

<@1?@=//@>

Wild-card is @

 

Enter above Find and Replace in the Interactive window as below.

Enter the following text in the Input window as shown below:

Input window

/*

=====================================================================

TextMaestro Technologies TM

All rights reserved.

=====================================================================

*/

/*

* This module determines the insert location of text

* for right-append feature.

*

*/

int  CTextCvt::M_GetInsertLoc(CString &temp)

{

    int len = temp.GetLength();

    int n=0;

    n = temp.ReverseFind(' ');   /* "23423423423234234" case, no blank */

    if (n<0) return 0;

    if (n==len-1){      /* "123213  23423    " case, trailing blanks */

        while(1){

            if (temp.GetAt(n)==' ') n--;  /* clip tail blanks */

            else break;

        }

        temp = temp.Mid(0, n);

    }

    n = temp.ReverseFind(' ');   /* "231234  23234" case, get pre blank. */

    if (n<0) n = 0;           /* needed for "12312312   " case. */

    return n;

}

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
?@=//@
qualifier grabs each line of the comment text and inserts // in the beginning of the line. Once again, this qualifier is cleansing the argument which is holding the comment text at the moment, and it has no effect outside of the comment block. More about qualifiers is here.

 

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:

  • Replace tabs with blanks. (Nobody should use tabs in source code. It hurts source code portability the most.)

  • Handle inline C-style comment properly. That is, if you have C-style comment embedded in a line of code, it needs to be 'stripped-off' and placed after the code on the right hand side.

  • Align all side comments.

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:

Find

Replace

Per line

Comment

\t

Uses wild-card

<a=-4>

Uses wild-card

Does not matter

Replaces tabs. It assumes that the input text has tab-width of 4 characters. If the tabs were of 8 characters, we needed to have <a=-8> instead.

/*@*/@/*@*/@/*@*/

Uses wild-card

<@2> <@4>// <@1>|<@3>|<@5>

Uses wild-card

Yes

Handles the instance when a line contains three inline C-style comments.

/*@*/@/*@*/

Uses wild-card

<@2>// <@1>|<@3>

Uses wild-card

Yes

Handles the instance when a line contains two inline C-style comments.

/*@*/@

Uses wild-card

<@2>// <@1?*=->

Uses wild-card

Yes

Handles the instance when a line contains one inline C-style comment.

/*@*/

Uses wild-card

<@1?@=//@?*=->

Uses wild-card

No

This is the most crucial macro here. See Comment above.

^a@//

Uses wild-card

^a@<a=77>//

Uses wild-card

Yes

Aligns side-comment preceded by alpha character (A-Z, a-z) at column 77.

^d@//

Uses wild-card

^d@<a=77>//

Uses wild-card

Yes

Aligns side-comment preceded by digits (0-9) at column 77.

^m@//

Uses wild-card

^m@<a=77>//

Uses wild-card

Yes

Aligns side-comment preceded by math operators

(+ - * / < > { } [ ] ( ) =) at column 77.

^p@//

Uses wild-card

^p@<a=77>//

Uses wild-card

Yes

Aligns side-comment preceded by punctuation

(, . : ; " ') at column 77.

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.

 

2. Select all the items in the left list, and hit

Batch name output files button. The dialog on the right will appear.

 

Hit on Same as input names, and you will notice that the 2nd column in above dialog named Output is populated, as shown below.

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.

On Output Filenames dialog box, click on the 3rd option called,

 Choose Output Location button and enter C:\Test__output\ as shown below. Then hit Modify.

This will populate the changes on the 2nd column labeled Output, as shown below:

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:

  • Number of files in the list.

  • Length of the library.

  • Use of wild-card in each macro. Use of wild-card enters extensive code execution.

  • Not using Match case in a macro. Match case will consume less time than the converse.

  • Finally, Preview mode will take much longer time than the non-preview mode. If desired, simply un-check the Preview button.

 

Here are some examples:

Example 1: Input file. Output file.

Example 2: Input file. Output file.

Top