www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to embed data within an executable?

reply "Gary Willoughby" <dev nomad.so> writes:
I'm using DMD to create an executable and wondered what is the 
best way to embed data into it.

For example, i want to embed base64 image data in the executable 
then access it during runtime. Is this possible? if so how?
Feb 02 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Gary Willoughby:

 I'm using DMD to create an executable and wondered what is the 
 best way to embed data into it.

 For example, i want to embed base64 image data in the 
 executable then access it during runtime. Is this possible? if 
 so how?
There are various ways to do it. If your data is not too much, one way is to use a "hex string". You can also use mixin(import("...")). Bye, bearophile
Feb 02 2014
next sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
On Sunday, 2 February 2014 at 16:52:38 UTC, bearophile wrote:
 Gary Willoughby:

 I'm using DMD to create an executable and wondered what is the 
 best way to embed data into it.

 For example, i want to embed base64 image data in the 
 executable then access it during runtime. Is this possible? if 
 so how?
There are various ways to do it. If your data is not too much, one way is to use a "hex string". You can also use mixin(import("...")). Bye, bearophile
How do you access that at runtime?
Feb 02 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Gary Willoughby:

 How do you access that at runtime?
In the usual ways. A hex string is just a different kind of literal for a string (so if you need hex data you need to cast it, unfortunately). The mixin(import("...")) can import anything, including your base64 data, that you can convert at compile-time or run-time in what you need. Bye, bearophile
Feb 02 2014
parent reply "Gary Willoughby" <dev nomad.so> writes:
On Sunday, 2 February 2014 at 17:17:14 UTC, bearophile wrote:
 Gary Willoughby:

 How do you access that at runtime?
In the usual ways. A hex string is just a different kind of literal for a string (so if you need hex data you need to cast it, unfortunately). The mixin(import("...")) can import anything, including your base64 data, that you can convert at compile-time or run-time in what you need. Bye, bearophile
Ah right i see what you mean. Something like this: template embed(string file) { private string getData() { return Base64.encode(cast(ubyte[])import(file)); } enum embed = getData(); }
Feb 02 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Sunday, 2 February 2014 at 17:45:37 UTC, Gary Willoughby wrote:
 	enum embed = getData();
 }
I am pretty sure you want immutable variable here, not enum. Latter is placement constant.
Feb 02 2014
parent reply "Gary Willoughby" <dev nomad.so> writes:
On Sunday, 2 February 2014 at 19:57:11 UTC, Dicebot wrote:
 On Sunday, 2 February 2014 at 17:45:37 UTC, Gary Willoughby 
 wrote:
 	enum embed = getData();
 }
I am pretty sure you want immutable variable here, not enum. Latter is placement constant.
I thought this was an idiomatic way to write eponymous templates? Manifest constants by their nature can not be mutated. http://dlang.org/enum.html
Feb 02 2014
parent "Dicebot" <public dicebot.lv> writes:
On Sunday, 2 February 2014 at 20:45:45 UTC, Gary Willoughby wrote:
 On Sunday, 2 February 2014 at 19:57:11 UTC, Dicebot wrote:
 On Sunday, 2 February 2014 at 17:45:37 UTC, Gary Willoughby 
 wrote:
 	enum embed = getData();
 }
I am pretty sure you want immutable variable here, not enum. Latter is placement constant.
I thought this was an idiomatic way to write eponymous templates? Manifest constants by their nature can not be mutated. http://dlang.org/enum.html
At first glance I had impression that you intend to use it as a mixin. As a simple eponymous shortcut it will work, nevermind :)
Feb 02 2014
prev sibling parent reply "Orvid King" <blah38621 gmail.com> writes:
On Sun, 02 Feb 2014 10:52:37 -0600, bearophile <bearophileHUGS lycos.com>  
wrote:

 Gary Willoughby:

 I'm using DMD to create an executable and wondered what is the best way  
 to embed data into it.

 For example, i want to embed base64 image data in the executable then  
 access it during runtime. Is this possible? if so how?
There are various ways to do it. If your data is not too much, one way is to use a "hex string". You can also use mixin(import("...")). Bye, bearophile
What's the purpose of the mixin portion of that? The documentation for file imports say that `import("...")` produces a string, so `__gshared immutable myFile = Base64.encode(import("myFile"));` should work perfectly, provided "myFile" is in a path passed to dmd with the -j switch.
Feb 02 2014
parent "Gary Willoughby" <dev nomad.so> writes:
On Sunday, 2 February 2014 at 17:25:12 UTC, Orvid King wrote:
 Base64.encode(import("myFile"));` should work perfectly, 
 provided "myFile" is in a path passed to dmd with the -j switch.
Yep it does.
Feb 02 2014
prev sibling parent "Rikki Cattermole" <alphaglosined gmail.com> writes:
On Sunday, 2 February 2014 at 16:48:17 UTC, Gary Willoughby wrote:
 I'm using DMD to create an executable and wondered what is the 
 best way to embed data into it.

 For example, i want to embed base64 image data in the 
 executable then access it during runtime. Is this possible? if 
 so how?
Perhaps my Bin2D would help[0]. Basically creates a D array literal in a file you give it. [0] https://github.com/rikkimax/Bin2D
Feb 02 2014