Jump to page: 1 2
Thread overview
How to embed data within an executable?
Feb 02, 2014
Gary Willoughby
Feb 02, 2014
bearophile
Feb 02, 2014
Gary Willoughby
Feb 02, 2014
bearophile
Feb 02, 2014
Gary Willoughby
Feb 02, 2014
Dicebot
Feb 02, 2014
Gary Willoughby
Feb 02, 2014
Dicebot
Feb 02, 2014
Orvid King
Feb 02, 2014
Gary Willoughby
Feb 03, 2014
Rikki Cattermole
February 02, 2014
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?
February 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
February 02, 2014
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?
February 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
February 02, 2014
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.
February 02, 2014
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();
}
February 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.
February 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.
February 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:
>> 	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
February 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:
>> 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 :)
« First   ‹ Prev
1 2