Domanda

OK, so this is basically what I need :

  • I'm trying to integrate Warp (https://github.com/facebook/warp) in my D project
  • I need to get the output (of the pre-processing) into a string, and not into a file (as it currently is)

I've had a good look into the sources, but not being a D guru, I thought I might be missing something obvious.

The key probably is in main.d :

            auto foutr = fout.lockingTextWriter();      // has destructor

            context.localStart(sf, &foutr);
            context.preprocess();
            context.localFinish();

With context.localStart() expecting a alias typeof(File.lockingTextWriter()) R; as a second param (the output stream?).

However, I simple cannot spot that anywhere in the documentation.

Any ideas?


UPDATE

I think I'm very close; I'll post a complete solution once I'm 100% sure. But this is what I spotted in context.d (unittests are a great place to find useful code, for sure! lol)

version (unittest)
{
    void testPreprocess(const Params params, string src, string result)
    {

        uchar[100] tmpbuf = void;
        auto outbuf = Textbuf!uchar(tmpbuf);

        auto context = Context!(Textbuf!uchar)(params);

        // Create a fake source file with contents
        auto sf = SrcFile.lookup("test.c");
        sf.contents = cast(ustring)src;

        context.localStart(sf, &outbuf);

        context.preprocess();

        context.expanded.finish();
        if (outbuf[] != result)
            writefln("output = |%s|", outbuf[]);
        assert(outbuf[] == result);
    }
}

version (all)
{
unittest
{
    const Params params;
    testPreprocess(params,
"asdf\r
asd\\\r
ff\r
",

`# 2 "test.c"
asdf
# 3 "test.c"
asdff
`);
}
È stato utile?

Soluzione

I haven't actually looked at this code, but lockingTextWriter is what D calls an output range.

It's simply a struct with a method called put that accepts a string as an argument. So, you might be able to get the info as a string by doing this:

struct StringSink {
    string result;
    void put(in char[] s) { result ~= s; }
}


StringSink sink;
context.localStart(sf, &sink);
.....

string result = sink.result;

or something along those lines.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top