3

I have an html source file which I use to edit and test with an html editor. For my purposes I would like to include this file in my C++11 source code by instantiating a std::string containing the html file content as it is.

For now, I am using the following way, which is not ideal as it require an unwanted modification to the html source code, as shown below:

std::string my_html = 
#include "my.html"
;

where my.html contains something like:

R"(
<!DOCTYPE html>
<html>
</html>
)"

Note the R"( and )" tokens. They are there for the only purpose of allowing the C+11 std::string to be instantiated by the compiler.

Of course, those tokens make my html source code non-html compliant. I'd like to have them removed from the html source, yet be able to instantiate the string in some other way.

Do you know any solution? I prefer standard solutions to pre-processor tricks. However, a pre-processor help is okay in the only case the are no standard solutions.

Martin
  • 9,089
  • 11
  • 52
  • 87
  • 2
    That's somewhat crazy. Why not just read the file off disk dynamically when the program is running? – user229044 Mar 07 '15 at 17:17
  • @meagar, not crazy at all, why should i need to load it from a disk dynamically? – Martin Mar 07 '15 at 17:18
  • The only problem with your resulting string is the initial newline. Put the DOCTYPE on the same line as the R. – Cheers and hth. - Alf Mar 07 '15 at 17:19
  • Have your make/buikd system takr raw html, and output a .cpphtml wrapped in `R"`$guid`(` and `)`$guid`"`. – Yakk - Adam Nevraumont Mar 07 '15 at 17:19
  • @tux3: do you know c++11? it compiles as it should – Martin Mar 07 '15 at 17:19
  • @Martin Yes, I'm an idiot. I deleted that comment 5s after posting it. – tux3 Mar 07 '15 at 17:20
  • Possible duplicate of [this question](http://stackoverflow.com/questions/410980/include-a-text-file-in-a-c-program-as-a-char) – Bert Mar 07 '15 at 17:21
  • 1
    @Bert that's a totally different question – Martin Mar 07 '15 at 17:21
  • @Martin: is it okay if I close this question, or do you have additional concerns (apart from the invalid claim about the HTML)? – Cheers and hth. - Alf Mar 07 '15 at 17:23
  • @Alf wait.. I am considering your answer, but I don't get it. Why should the HTML be valid when putting DOCTYPE on the same line as the R? – Martin Mar 07 '15 at 17:26
  • @Martin: sorry, i haven't drunk enough coffee. one needs to also use a macro for generating the `R`, I think. checking... – Cheers and hth. - Alf Mar 07 '15 at 17:28
  • @alf , yes, probably a pre-processor trick (not standard macro) is the only possible way. I am using g++, for anyone who is willing to give a solution – Martin Mar 07 '15 at 17:29
  • 2
    After checking, no I can't see any way to make the C++ compiler (even a specific one such as g++) do this on its own. Some manual or automated preprocessing is needed. Instead of the Unix-land specific translation recommended in the suggested duplicate, I think I would just let the build generate a file with the `char const s[] = R"(` followed by contents of HTML file followed by `")`. Easy to do both in Windows and Unix-land. – Cheers and hth. - Alf Mar 07 '15 at 17:49

0 Answers0