www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - including arbitrary files into the binary

reply mandel <mandel foobar.com> writes:
Hi,

I like to include arbitrary files into the D binary.
writefln(import("data.txt"))
let you include a single file as char[], but that is not sufficient.

I like to include all files from some subdirectories ( ~200 small files).
Ideally I like to create an AA with a path/filename as key
and the file content as value.

They are two problems I see with that:
1. manually writing an import for every file.
2. import doesn't seem to allow you to import with path like
import("mydir/data.txt"),
    all files must be in the directory defined by the compiler option -J

Is there a solution to this problem?
Sep 08 2007
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"mandel" <mandel foobar.com> wrote in message 
news:fbuc6n$e8h$1 digitalmars.com...
 Hi,

 I like to include arbitrary files into the D binary.
 writefln(import("data.txt"))
 let you include a single file as char[], but that is not sufficient.

 I like to include all files from some subdirectories ( ~200 small files).
 Ideally I like to create an AA with a path/filename as key
 and the file content as value.

 They are two problems I see with that:
 1. manually writing an import for every file.
 2. import doesn't seem to allow you to import with path like 
 import("mydir/data.txt"),
    all files must be in the directory defined by the compiler option -J

 Is there a solution to this problem?
You could write a D program that does all this machination for you as a build step or so. It could copy all the files to a directory, come up with an AA, write out a .d file with the AA declaration and the imports, and then compile that into an object, which you then link into your program.
Sep 08 2007
parent Walter Bright <newshound1 digitalmars.com> writes:
Jarrett Billingsley wrote:
 You could write a D program that does all this machination for you as a 
 build step or so.  It could copy all the files to a directory, come up with 
 an AA, write out a .d file with the AA declaration and the imports, and then 
 compile that into an object, which you then link into your program. 
For the AA declaration you can use hex string literals for the binary data: x"00 0A FD EE" etc. They're easy for a simple D program to generate.
Sep 08 2007
prev sibling parent mandel <mandel foobar.com> writes:
Using dmd-1.021 I use this to initialize an AA:

ubyte[][char[]] files;

static this()
{
	files = [ cast(char[]) "data.txt" : cast(ubyte[]) import("data.txt") ];
}

Writing a program to generate a D code file is of course possible,
albeit not that elegant.

The only problem left is that I get an error starting my main program:
"object.Exception: Cyclic dependency in module Main"
because I have more that one static this in my application.

Walter Bright Wrote:

 Jarrett Billingsley wrote:
 You could write a D program that does all this machination for you as a 
 build step or so.  It could copy all the files to a directory, come up with 
 an AA, write out a .d file with the AA declaration and the imports, and then 
 compile that into an object, which you then link into your program. 
For the AA declaration you can use hex string literals for the binary data: x"00 0A FD EE" etc. They're easy for a simple D program to generate.
Nice to know, that would spare the process of copying files around.
Sep 08 2007