在编译时将文本文件转换为字符串 [英] Turn a Text File into a String at Compile Time

查看:457
本文介绍了在编译时将文本文件转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件与一些C类似的纯文本。我想在编译时(理想情况下使用 cmake )获取此文件中的文本,并使用它来生成一个简单的C ++头文件 - 包括名称空间声明和std :: string,其值是我的文本文件的内容,新行和包括的所有内容。



这是最好的方法是什么?

解决方案

所以我去了一个不同于这里建议的解决方案。这是我做的。



在我的源代码中,我定义一个处理程序类,我将使用我需要的字符串字面量。这里有两件很重要的:




  • 头文件实际上是一个 .hpp.in 而版本控制。实现文件是一个正常的 .cpp

  • 在这个头文件中,我选择一个变量,#define它: #define MY_STRING @MY_STRING @



现在,在CMakeLists.txt文件,我这样做:




  • 将原始文本文件读入cmake变量

  • // 注释,将结果保存在临时变量中

  • 全部替换 \\\
    字符与字符串newline,将结果保存到一个新的临时变量

  • 定义我使用的同一个变量 c),并从该最新的临时变量中分配它

  • 将标题从$通过指定 @ONLY .hpp >。这确保cmake将复制头文件,但也会更新 MY_STRING #define MY_STRING @MY_STRING @

    最后,在我的处理程序的实现文件中,我定义了以下宏:

      #define STRINGIFY(x)#x 
    #define TOSTRING(x)STRINGIFY(x)

    ,以便我可以调用 TOSTRING(MY_STRING) code> std :: string my_string 。一旦这完成,我需要做的是用\\\
    替换newline - 我完成了! my_string 现在有一个与原始文本文件中的值相同的值(无注释)。



    我希望我有更多的时间来玩这个,更少的其他任务分心我,但事情是这样,现在必须做的。



    谢谢全部为您提供帮助!


    I have a text file with some C-like plain text. I would like to, at compile time (and ideally using cmake), take the text in this file and use it to generate a simple C++ header file - one that has a couple of includes, namespace declaration, and an std::string whose value is the contents of my text file, new lines and everything included.

    What's the best way to do this?

    解决方案

    Okay, so I went with a solution different from the ones suggested here. Here's what I did.

    Inside my source code, I define a handler class where I will make use of the string literal I need. Two things are important here:

    • The header file is actually an .hpp.in while under version control. The implementation file is a normal .cpp
    • Inside this header file, I choose a variable and #define it so: #define MY_STRING @MY_STRING@

    Now, inside the CMakeLists.txt responsible for these two files, I do this:

    • Read the raw text file into a cmake variable
    • Remove all // comments, save the result in a temporary variable
    • Replace all \n characters with the string "newline", save the result into a new temporary variable
    • Define the same variable I used in my header above above (i.e. MY_STRING), and assign it from that latest temporary variable
    • Copy the header from the its location under ${CMAKE_SOURCE_DIR} into the appropriate location under ${CMAKE_BINARY_DIR} (and renaming it to a .hpp) by specifying @ONLY. This ensures that cmake will copy the header file, but will also update the #define MY_STRING @MY_STRING@ with the value of MY_STRING I set a little earlier.

    Finally, inside the implementation file for my handler I define the following macros:

    #define STRINGIFY(x) #x
    #define TOSTRING(x) STRINGIFY(x)
    

    so that I can call TOSTRING(MY_STRING) and assign the output to an std::string my_string. Once this is done, all I need to do is replace the "newline" placeholder strings with "\n" - and I am done! my_string now has a value equivalent to the the one in the raw text file (sans the comments there).

    I wish I had more time to play with this, and fewer other tasks to distract me, but the way things are, this will have to do for now.

    Thank you all for you help!

    这篇关于在编译时将文本文件转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆