digitalmars.D.learn - How to embed data within an executable?
- Gary Willoughby (4/4) Feb 02 2014 I'm using DMD to create an executable and wondered what is the
- bearophile (6/11) Feb 02 2014 There are various ways to do it. If your data is not too much,
- Gary Willoughby (2/14) Feb 02 2014 How do you access that at runtime?
- bearophile (8/9) Feb 02 2014 In the usual ways. A hex string is just a different kind of
- Gary Willoughby (10/19) Feb 02 2014 Ah right i see what you mean. Something like this:
- Dicebot (3/5) Feb 02 2014 I am pretty sure you want immutable variable here, not enum.
- Gary Willoughby (4/10) Feb 02 2014 I thought this was an idiomatic way to write eponymous templates?
- Dicebot (3/15) Feb 02 2014 At first glance I had impression that you intend to use it as a
- Orvid King (6/16) Feb 02 2014 What's the purpose of the mixin portion of that? The documentation for
- Gary Willoughby (2/4) Feb 02 2014 Yep it does.
- Rikki Cattermole (4/9) Feb 02 2014 Perhaps my Bin2D would help[0]. Basically creates a D array
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
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
On Sunday, 2 February 2014 at 16:52:38 UTC, bearophile wrote:Gary Willoughby:How do you access that at runtime?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
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
On Sunday, 2 February 2014 at 17:17:14 UTC, bearophile wrote:Gary Willoughby: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(); }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
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
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:I thought this was an idiomatic way to write eponymous templates? Manifest constants by their nature can not be mutated. http://dlang.org/enum.htmlenum embed = getData(); }I am pretty sure you want immutable variable here, not enum. Latter is placement constant.
Feb 02 2014
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:At first glance I had impression that you intend to use it as a mixin. As a simple eponymous shortcut it will work, nevermind :)On Sunday, 2 February 2014 at 17:45:37 UTC, Gary Willoughby wrote:I thought this was an idiomatic way to write eponymous templates? Manifest constants by their nature can not be mutated. http://dlang.org/enum.htmlenum embed = getData(); }I am pretty sure you want immutable variable here, not enum. Latter is placement constant.
Feb 02 2014
On Sun, 02 Feb 2014 10:52:37 -0600, bearophile <bearophileHUGS lycos.com> wrote:Gary Willoughby: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.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
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
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