www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to embed static strings to a D program?

reply Ky-Anh Huynh <saigon example.net> writes:
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.
Oct 15
parent reply evilrat <evilrat666 gmail.com> writes:
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
Oct 15
parent Adam D. Ruppe <destructionator gmail.com> writes:
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");`.
Oct 16