Embed files into binary.

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 5 21:11:13 PDT 2016


On Monday, 6 June 2016 at 03:11:32 UTC, Pie? wrote:
> e.g., how could I do this easily with your read in your png 
> module? It takes a file..
>
> /// Easily reads a png file into a MemoryImage
> MemoryImage readPng(string filename) {
> 	import std.file;
> 	return imageFromPng(readPng(cast(ubyte[]) read(filename)));
>
> recognize the code?


The string version just forwards to the byte array version. So 
you can use it directly with the array by just casting to ubyte[].

rikki cattermole got it right, except I *think* the cast(ubyte[]) 
needs to be explicit on the import line, so

ubyte[] theArray = cast(ubyte[]) import("...");

(Actually, the string one is an afterthought, I originally wrote 
it to only work with arrays (well, technically, input ranges of 
bytes), leaving the file reading to an outside function, but 
since loading a file from a filename is so common, I added the 
overload for it.)




More information about the Digitalmars-d-learn mailing list