www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Treat memory as a file with a name.

reply "Steve Teale" <steve.teale britseyeview.com> writes:
This is probably not exactly a D question.

The library librsvg expects to get an SVG file filename. I have 
the data of such a file in memory.

How do I dress up that memory as a file with a name so that I can 
pass the name to the librsvg open function.

I've looked at std.mmfile, and played with it, giving a name to 
the constructor, along with the address and size of my memory 
block.

import std.mmfile;
import std.stdio;
import std.stream;

void main()
{
    char[] ca;
    ca.length = 100000;
    ca[] = 'X';
    MmFile mmf = new MmFile("glob", 0, 100000UL, ca.ptr);
    std.stream.File sf = new std.stream.File("glob", FileMode.In);
    char[] b;
    b.length = 20;
    sf.readExact(b.ptr, 20);
    writefln("%s", b);
}

But then I just get 'No such file or directory', which is what 
I'd expect after I'd taken the trouble to read mmfile.d.

I had expected it to create a stub entry in the file system or 
something, and the somehow use fmemopen(), but it just looks for 
an existing file.

At the moment I'm having to write the data to a temporary file, 
and then pass the name of the temp to rsvg.

There must be some elegant way to do this!
Feb 21 2014
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Saturday, 22 February 2014 at 07:26:26 UTC, Steve Teale wrote:
 This is probably not exactly a D question.

 The library librsvg expects to get an SVG file filename. I have 
 the data of such a file in memory.
If it's the same librsvg as below, then it looks like it has an API function which can also load a SVG from memory: https://developer.gnome.org/rsvg/2.40/RsvgHandle.html#rsvg-handle-new-from-data To answer your original question, I think the best way to do this is to create a pipe on the filesystem. Once created, you can feed data through it without touching the disk.
Feb 21 2014
parent "Steve Teale" <steve.teale britseyeview.com> writes:
 If it's the same librsvg as below, then it looks like it has an 
 API function which can also load a SVG from memory:

 https://developer.gnome.org/rsvg/2.40/RsvgHandle.html#rsvg-handle-new-from-data

 To answer your original question, I think the best way to do 
 this is to create a pipe on the filesystem. Once created, you 
 can feed data through it without touching the disk.
Thanks Vladimir, My librsvg is 2.36, but rsvg-handle-new-from-data pre-dates that, so it should be good. Thanks for the link - I had clearly been looking at some crap documentation. Thanks for the pipe idea too - it will come in handy one day, and I'd completely forgotten about pipes. Steve
Feb 22 2014