www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Read file on compiler time.

reply "Remo" <remo4d gmail.com> writes:
Is there a way to read a text file into a sting at compile time 
in D2 ?
It would be great to read for example some JSON file and then 
parse it using CTFU and create some D code based on it.
May 29 2014
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
string a = import("file.txt");

dmd yourprogram.d -Jlocation_of_file


so for example

dmd yourprogram.d -J.


if file.txt is in the same directory as the .d file.
May 29 2014
parent reply "Remo" <remo4d gmail.com> writes:
On Thursday, 29 May 2014 at 20:21:32 UTC, Adam D. Ruppe wrote:
 string a = import("file.txt");

 dmd yourprogram.d -Jlocation_of_file


 so for example

 dmd yourprogram.d -J.


 if file.txt is in the same directory as the .d file.
Excellent, thank you Adam! Now another question is it also possible to save/write string at compile time? Probably no... Any way I like CTFE !
May 29 2014
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 29 May 2014 at 20:38:30 UTC, Remo wrote:
 Now another question is it also possible to save/write string 
 at compile time?
Sort of, use pragma(msg, "some string"); and it will be printed out when that code is compiled. Important that it is when the code is compiled, NOT when the code is executed at compile time. So void foo() { pragma(msg, "here"); } will say "here" even if you never call foo. So you can't use it to print stuff out inside CTFE loops, and you might need to shield it with version {} or static if() {} to suppress printing. But you can use it to print a completed string or something and then redirect it to a file in your makefile to do something: string myfunction() { string value; foreach(a; ["a", "b"]) value ~= a; return value; } pragma(msg, myfunction()); // would say "ab" foo.txt If you find yourself wanting to print a lot, I say you should just use a regualr program that runs normally instead of CTFE. Since most the language works the same way, transitioning to and from regular execution and compile time execution is easy and generally doesn't need you to change most the code.
May 29 2014
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Remo:

 is it also possible to save/write string at compile time?
There is pragma(msg, "...") but it's a little crappy. There are plans and a pull request for a good _ctWrite, but it's stalled for reasons unknown to me. Bye, bearophile
May 29 2014
parent "Remo" <remo4d gmail.com> writes:
On Thursday, 29 May 2014 at 20:44:09 UTC, bearophile wrote:
 Remo:

 is it also possible to save/write string at compile time?
There is pragma(msg, "...") but it's a little crappy. There are plans and a pull request for a good _ctWrite, but it's stalled for reasons unknown to me. Bye, bearophile
Thanks! Yes I know already about pragma(msg, "...") and use it all the time to debug. Something like _ctWrite would be great even if the name is not that great.
May 29 2014