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:
- For each source file, *.cpp and
*.hpp, recursively: - If the file contains an old class
name, then convert the file. - 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.
Don’t shy away from Perl: it makes this sort of task easy!
The
-iswitch is for in-place editing. It automatically creates backups and writes the transformed results to the original locations.Setting
@ARGVmakes it as though you had invoked the program with only those*.cppand*.hppfiles that contain the old class names. The-Eswitch togrepenables extended regular expressions so unescaped(,), and|are metacharacters.If there were no hits (i.e., if
@ARGVis 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_Mediumtomy::ns::MediumBloband 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,
printthe possibly-modified line, which because of-iwrites it to the updated source file.