用于更改 C++ 的脚本类名

发布于 2024-08-22 11:52:54 字数 1234 浏览 3 评论 0原文

我已将我的类从全局命名空间移至特定命名空间。我还更改了班级名称。我想将所有使用这些类的源文件转换为新格式。我正在考虑使用 Cygwin 上的 sed 文件的 bash 脚本或执行 perl 脚本。我在使用 bash 脚本时遇到问题。

这是我尝试执行的过程:

  1. 对于每个源文件,*.cpp 和 *.hpp,递归:
  2. 如果文件包含旧类 名称,然后转换文件。
  3. 结束为。

我在 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跳出Bashfor循环:

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:

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫性小仙女 2024-08-29 11:52:55

不要回避 Perl:它使此类任务变得简单!

#! /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;
}

-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!

#! /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.

征棹 2024-08-29 11:52:55

这在我的 Linux 上工作。它管理其他人指出的各种事情:

#!/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 {}

重要的是:

  • find 的 -print0 选项和 xargs 的 -0 管理带空格的文件。它使用“null”ASCII 字符作为分隔符。 xargs 的 -0 选项将“null”字符理解为分隔符:您可以正确管理带空格的文件名。
  • --replace 选项用于用当前处理的文件替换 '{}' 字符串
  • sed 的 -i 选项用 .save 备份
  • 文件作为 if 的快捷方式。仅当第一部分为真时,表达式的第二部分才有效

希望它有帮助,
我的2c

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

孤星 2024-08-29 11:52:55

您可以使用 grep 的正则表达式选项来为您的搜索提供更大的灵活性。根据您的示例:

if [ grep "Field_Blob_Medium" $f -eq 0 || grep "Field_Boolean" ];

可能是

if [ grep -E "(Field_Blob_Medium|Field_Boolean)" ... ];

您可以根据自己的喜好将这些“|”串在一起。

另外,你可以将你的发现合并到你的 grep 中

find . -name "*.hpp" or -name "*.cpp" | xargs grep -E ...

......以防你想简化那里的循环。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文