Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script for changing C++ class names

I have moved my classes from a global namespace into a specific namespace. I have also changed the class names. I want to convert all my source files that use these classes to the new format. I was thinking either a bash script using a sed file on Cygwin or executing a perl script. I'm having troubles with the bash script.

Here's the process I'm trying to perform:

  1. For each source file, *.cpp and *.hpp, recursively:
  2. If the file contains an old class name, then convert the file.
  3. End for.

My problem in Bash script is detecting if the file contains an old class name. I was thinking of one grep statement for each class name:

for f in `find . -iname \*.[ch]pp`;
do
  if [ grep "Field_Blob_Medium" $f -eq 0 || grep "Field_Boolean" ]; then
      sed -f conversion.sed $f
  fi
done

An issue is that only one command can be in Bash if statement, using this syntax:

if grep "Field_Unsigned_Short" $f;

so I can't do a logical OR of greps.

I could perform a nested loop, but I don't know how to break out of a Bash for loop:

OLD_CLASS_NAMES="Field_Blob_Medium Field_Boolean Field_Unsigned_Int"
for f in `find . -iname \*.[ch]pp`;
do
  for c_name in $OLD_CLASS_NAMES;
  do
     if grep $c_name $f then
        sed -f convert.sed $f # <-- Need to break out of inner loop after this execution.
     fi
  done
done

So I'm looking for suggestions on how to process every source file that contains old class names and convert them to new ones. A Cygwin Bash script example is preferred, although a Perl script would also be acceptable. Also, the script should make a backup copy of the original source file before writing the converted contents out to the new file.

I'm running Cygwin on Windows XP and Windows Vista.

like image 690
Thomas Matthews Avatar asked Feb 22 '10 21:02

Thomas Matthews


2 Answers

Don't shy away from Perl: it makes this sort of task easy!

#! /usr/bin/perl -i.bak

use warnings;
use strict;

my $old = join "|" => qw(
  Field_Blob_Medium
  Field_Boolean
  Field_Unsigned_Int
);

chomp(@ARGV = `find . -iname \*.[ch]pp -print0 |
               xargs -0 grep -lE '($old)'`);

unless (@ARGV) {
  warn "$0: nothing to do!\n";
  exit 0;
}

while (<>) {
  s[Field_Blob_Medium]  [my::ns::MediumBlob]g ||
  s[Field_Boolean]      [my::ns::Bool]g       ||
  s[Field_Unsigned_Int] [my::ns::UInt]g;

  print;
}

The -i switch is for in-place editing. It automatically creates backups and writes the transformed results to the original locations.

Setting @ARGV makes it as though you had invoked the program with only those *.cpp and *.hpp files that contain the old class names. The -E switch to grep enables extended regular expressions so unescaped (, ), and | are metacharacters.

If there were no hits (i.e., if @ARGV is empty), then there's nothing to do.

Otherwise, for each line of each file that has the old names (the mechanics of which Perl handles for you!), try to rename Field_Blob_Medium to my::ns::MediumBlob and so on. Note that these substitution attempts cease after the first success, so if a line contains two different old names, one will be replaced and the other will remain the same.

Usually the substitution operator is written s///, but you may use bracketing delimiters as above. I did so to left-justify the new names.

Of course this is a stand-in for your actual substitution.

Finally, print the possibly-modified line, which because of -i writes it to the updated source file.

like image 91
Greg Bacon Avatar answered Oct 12 '22 05:10

Greg Bacon


This work on my linux. It manages the various things pointed by others :

#!/bin/bash
OLD_NAMES="(EField_Blob_Medium|Field_Boolean|Field_Unsigned_Int)"

find "$1" -name "*.hpp" -o -name "*.cpp" -print0 | \
   xargs -0 --replace grep -E "${OLD_NAMES}" {} && sed -i.save -f convert.sed {}

What is important :

  • the -print0 option of find with the -0 of xargs manages files with spaces. It uses the 'null' ASCII char as a separator. The -0 option of xargs understands the 'null' char as a separator : you correctly manage filenames with spaces.
  • the --replace option is used to replace the '{}' string by the current processed file
  • the -i option of sed backs up the file with a .save
  • The && work as a shortcut of if. The second part of the expression works only if the first part is true

Hope it helsp, my2c

like image 28
neuro Avatar answered Oct 12 '22 07:10

neuro