Thread overview
How to embed static strings to a D program?
Oct 16, 2017
Ky-Anh Huynh
Oct 16, 2017
evilrat
Oct 16, 2017
Adam D. Ruppe
October 16, 2017
Hello,

I want to use some static contents in my program, e.g, a CSS file, a long listing. To help deployment process I'd like to have them embedded in the final binary file.

Is there any convenient way to support this? Maybe I need a tool/library to load file contents and generate D-code at run-time ?

Thanks for your reading.
October 16, 2017
On Monday, 16 October 2017 at 05:34:13 UTC, Ky-Anh Huynh wrote:
> Hello,
>
> I want to use some static contents in my program, e.g, a CSS file, a long listing. To help deployment process I'd like to have them embedded in the final binary file.
>
> Is there any convenient way to support this? Maybe I need a tool/library to load file contents and generate D-code at run-time ?
>
> Thanks for your reading.


import can do this, basically it gives you an array that you can cast to ubyte[] for binary too, be careful with enums though, because enum arrays will allocate every time you access it

string data = import("strings.txt");

more
https://dlang.org/spec/expression.html#import_expressions


However for security reasons it only looks in folders which you pass with -J flag
or with dub using "stringImportPaths"
http://code.dlang.org/package-format?lang=json
October 16, 2017
On Monday, 16 October 2017 at 06:03:40 UTC, evilrat wrote:
> can cast to ubyte[] for binary too, be careful with enums though, because enum arrays will allocate every time you access it

Arrays yes, but not strings. So you can do `enum data = import("strings.txt");`.