用于更改 C++ 的脚本类名
我已将我的类从全局命名空间移至特定命名空间。我还更改了班级名称。我想将所有使用这些类的源文件转换为新格式。我正在考虑使用 Cygwin 上的 sed 文件的 bash 脚本或执行 perl 脚本。我在使用 bash
脚本时遇到问题。
这是我尝试执行的过程:
- 对于每个源文件,*.cpp 和 *.hpp,递归:
- 如果文件包含旧类 名称,然后转换文件。
- 结束为。
我在 Bash 脚本中的问题是检测文件是否包含旧的类名。我正在考虑为每个类名使用一个 grep
语句:
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
问题是,使用这种语法,Bash
if 语句中只能有一个命令:
if grep "Field_Unsigned_Short" $f;
所以我不能这样做grep 的逻辑或。
我可以执行嵌套循环,但我不知道如何break
跳出Bash
for循环:
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
所以我正在寻找有关如何处理每个源的建议包含旧类名的文件并将其转换为新类名。尽管 Perl 脚本也可以接受,但首选 Cygwin Bash
脚本示例。 此外,脚本应在将转换后的内容写入新
文件之前制作原始源文件的备份副本。
我在 Windows XP 和 Windows Vista 上运行 Cygwin 。
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 grep
s.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要回避 Perl:它使此类任务变得简单!
-i
开关用于就地编辑。它会自动创建备份并将转换后的结果写入原始位置。设置 @ARGV 会使您仅使用包含旧类名的
*.cpp
和*.hpp
文件调用该程序。-E
切换到grep
启用扩展正则表达式,因此未转义(
,)
和|< /code> 是元字符。
如果没有命中(即,如果
@ARGV
为空),则无需执行任何操作。否则,对于每个文件中具有旧名称的每一行(Perl 为您处理的机制!),尝试将
Field_Blob_Medium
重命名为my::ns::MediumBlob
等等。请注意,这些替换尝试在第一次成功后就会停止,因此如果一行包含两个不同的旧名称,一个将被替换,另一个将保持不变。通常替换运算符写作
s///
,但您可以使用上面的括号分隔符。我这样做是为了使新名称左对齐。当然,这是您实际替换的替代品。
最后,
打印
可能修改的行,因为-i
将其写入更新的源文件。Don't shy away from Perl: it makes this sort of task easy!
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 togrep
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
tomy::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.这在我的 Linux 上工作。它管理其他人指出的各种事情:
重要的是:
希望它有帮助,
我的2c
This work on my linux. It manages the various things pointed by others :
What is important :
Hope it helsp,
my2c
您可以使用 grep 的正则表达式选项来为您的搜索提供更大的灵活性。根据您的示例:
可能是
您可以根据自己的喜好将这些“|”串在一起。
另外,你可以将你的发现合并到你的 grep 中
......以防你想简化那里的循环。
You could use the regex option for grep to give you more flexibility in your search. Per your example:
could be
You could string together those '|'s to your hearts content.
Plus you could merge your finds into your greps
... in case you want to simplify that loop there.