Script for changing C++ class names
Don't shy away from Perl: it makes this sort of task easy!
#! /usr/bin/perl -i.bakuse 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/bashOLD_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
You could use the regex option for grep to give you more flexibility in your search. Per your example:
if [ grep "Field_Blob_Medium" $f -eq 0 || grep "Field_Boolean" ];
could be
if [ grep -E "(Field_Blob_Medium|Field_Boolean)" ... ];
You could string together those '|'s to your hearts content.
Plus you could merge your finds into your greps
find . -name "*.hpp" or -name "*.cpp" | xargs grep -E ...
... in case you want to simplify that loop there.