File for web page

I would like to create a file to be stored on the RP2040 the contains a web page. Right now, I generate the page by a sequence of c string constants.
Each constant is enclosed in quotes. When the html is to contain quotes, those quotes are escaped. It's kind of messy. Just loading a file from the program and delivering it to the web client would be much cooler. But how?

Because the source files are compiled into a binary, the underlying file system "vanishes", so there is nowhere to load from (unless you also have an SD card or other actual file system). Some languages like Go with //go:embed and even very recently, plain C with #embed allow (you guessed it) embedding a file. But not C++ yet.

If the main complaint is escaping quotes, you can use a raw string, perhaps with a well-chosen arbitrary delimiter that doesn't appear in your HTML/JS/CSS

const char home_page[] = R"~~~(
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- etc -->
)~~~";

Otherwise, being able to use actual files is handy; e.g. to edit them as HTML. In that case, you can look at using xxd to convert them to C source as a bunch of bytes.

Thanks. Raw string is great. Seems like xxd would have wider use, such as sound files.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.