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:
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.
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.
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 :
Hope it helsp, my2c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With