2

I am using a third-parter utility named gnuplot-iostream to invoke gnuplot from C++ in the back-end of a web application. That web application is executed under CGI, so anything that goes to STDOUT is the result to browser. In this case, the web request will be for the image itself, so the output will be preceded by some HTTP headers including a Content-Type of image/png.

Instead of having gnuplot write to a temporary file, then read that file back into memory and stream its contents to STDOUT, I'd like to be able to directly pipe GNUPlot's output through STDOUT, but I cannot see a way to ask gnuplot to do anything other than write to an image file on disk, or to open an X11 window.

Is this possible?

2
  • You could in principle invoke gnuplot in a pipe and repeatedly poll the output. However, I'd just save it to a file as you're doing now. If it's a problem reading the file in its entirety into memory I'd look for a way to do that piece by piece, maybe? Commented Jun 18, 2015 at 12:28
  • @Cheersandhth.-Alf: I just wanted to avoid the roundtrip of storage onto the hard disk and back. Reading the file isn't so much of an issue (indeed, I have to read the bytes from somewhere!) Commented Jun 18, 2015 at 12:34

2 Answers 2

3

In a gnuplot script, just don't set an output file to have the image content directed to stdout:

gnuplot -e 'set terminal pngcairo; plot x' > myfile.png

I'm not completely sure, how that must be integrated with gnuplot-iostream.

Sign up to request clarification or add additional context in comments.

4 Comments

Oh, huh, that's probably what I want. I'll test.
I guess the key here is that I can pipe out the result if I like (I thought I couldn't), but I can't simulatneously pipe in the script so it has to be given on the commandline. BTW I'll probably drop the library wrapper at this point.
You can also pipe in the script with gnuplot < myscript.gp > myfile.png or echo 'set terminal pngcairo; plot x' | gnuplot > myfile.png. I think, the wrapper makes sense, if you have some data which you generate in c++ and want to plot this directly.
The data actually come from a big file on disk (irony) though the script will be generated dynamically. I'm happy rawly piping the result from popen; I just wish POSIX had a pleasant way to pipe the config in, too. I really want to avoid all temporary files.
1

Write to a named pipe? The kind you make with mkfifo?

I have used the following silly trick that combines mkfifo and mktemp:

FOO=$(mktemp -d)
mkfifo "$FOO/bar"
# pipe some stuff through "$FOO/bar"
rm -r "$FOO"

And remember that nothing stops you from piping through the same named pipe, once you have it.

7 Comments

Oooh that's a good idea.. I'd run into a few awkwardnesses trying to get a unique filename for the temporary pipe, alas
@LightnessRacesinOrbit I don't know if this is in any way clever, but I have used mktemp -d together with mkfifo to circumvent that problem.
bash and ksh both have the $RANDOM variable. something like fifoname=$RANDOM$RANDOM$RANDOM.proj_name should be pretty unique ;-) Good luck to all.
@shellter: This is a CGI application, not a Bash script.
if you're creating a fifo, aren't you in a shell? Good luck.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.