Search code examples
cgraphvizboost-graph

Avoiding the use of temporary files, when the function wants a FILE * passed


I currently use C++ to do some graph related computation using boost::graph. boost::graph can output its graph as a dot file and I use a std::stringstream to capture the output dot file. Thus the contents of the dot file resides in memory.

I want to use the dot file to visualize the graph (as fast as possible). Thus I want to generate the dot file, generate an svg file and print it onto some canvas. I want to avoid using temporary files for this, as the graphs shall be small and memory is available anyway.

However graphviz libgraph has only the function extern Agraph_t *agread(FILE *); the only way I can imagine to make this working is to hack around in the filehandle struct __FILE which is really not portable.

How would you let a library read you memory contents as a file in Unix/linux?

I just found out that libcgraph from GraphViz allows to enter a overloaded version here, but so far the documentation doesn't point me to some usefull place.


Solution

  • Well, it is arguably a bug in the API, but here's an idea. This is assuming that the agread() function would read the file in as binary data.

    Note that I am not familiar with the API you're using, but I hope this may be useful anyway.

    1. Map a file into memory using mmap().
    2. Use that memory region to do your graph construction.
    3. When it comes time to call agread(), open that file descriptor into a FILE * struct (fopen() or fdopen() if you didn't close the descriptor).
    4. Pass the FILE * struct.

    Edit: Or, ignore my answer and use the fmemopen() call. It probably is exactly what you need. I didn't want to delete my answer though, in case someone is currently writing a response :-).