【问题标题】:How to get line of text from source file using Preprocessor?如何使用预处理器从源文件中获取文本行?
【发布时间】:2012-07-30 21:43:35
【问题描述】:

我们有一个文件和一行(__file____line__),我们想打印它的内容。这样的事情可以通过预处理器实现吗?

【问题讨论】:

  • 不使用 C++ 预处理器,不。

标签: c++ boost c-preprocessor boost-preprocessor


【解决方案1】:

也许是这样的?

#include <stdio.h>

#define DBG(X) printf(__FILE__":%d DBG("#X")\n", __LINE__)

int main ()
{
    DBG(this is a test);
}

这会产生输出:

test.cc:7 DBG(this is a test)

编辑:您要么必须访问源文件,要么存储源文件。存储文件后,打印正确的行是微不足道的。一种方式:

#define PROGRAM_PRINT_INIT(X) \
    static ProgramPrint pp = ProgramPrint(X)
#define DBG(X) std::cerr << "[" << pp.name << ":" << X << "]" \
                         << pp.file[X] << std::endl

struct ProgramPrint {
    std::string name;
    std::vector<std::string> file;
    ProgramPrint (const char *filename) : name(filename) {
        std::ifstream in(filename);
        std::string line;
        while (getline(in, line)) file.push_back(line);
    }
};

请注意,vector 索引从零开始,因此请相应地使用DBG()

【讨论】:

  • 不,不能直接将行内容传递给您,只有行号
【解决方案2】:

是的,C/C++ 不是为此而生的。但如果你真的想看看你能把东西弯曲多远,这是一个建议。

假设你想编译 test.cpp 来测试。在编译之前,我们需要将源代码行的 cstring 数组添加到源代码文件中。我们将在单独的文件 test_temp.cpp 中执行此操作,我们将调用数组 TEST_CODE。

将以下内容放入文件中:code.awk(这将使其更易于重复使用)。

BEGIN {
    for (i = 0; i < 128; i++) {
        table = sprintf("%s%c", table, i);
    }
    print("= {\"\"")
}
{
    printf(",\"")
    for(j=1; j<=length($1); j++) {
        c = substr($1,j,1)
        printf("\\x%x", index(table, c) - 1)
    }
    printf("\"\n")
}
END {
    printf("};\n")
}

通过引用 ASCII 字符的“表”,这将使用每个字符的(十六进制)ASCII 值(例如 '\x41' 表示 'A'),以防制表符等可能在您的源代码,但应在 C/C++ 字符串文字中转义。

要编译,请在命令行(或您的 makefile)中键入

echo "char* TEST_CODE[]" > test_temp.cpp
awk -f code.awk test.cpp >> test_temp.cpp
cat test.cpp >> test_temp.cpp
g++ -o test test_temp.cpp
rm test_temp.cpp

如果你想获取源代码文件的第 80 行(从 1 开始),你会写

TEST_CODE[80]

注意这发生在预处理器命令之前。 这可以使用多个文件和头文件来完成,方法是在每种情况下从 TEST_CODE 更改数组名称。

这确实有漏洞,但它会在常见情况下工作。

注意:由于 char* / string 文字转换,g++ 可能会发出很多编译器警告,但您可以使用 -w 标志关闭这些警告。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 2019-03-18
    • 1970-01-01
    • 2016-09-22
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多