Prev: fft.html Up Next: terminals.html
Pipe streams, remote files, opening of input/output files/pipes

Practical Guide:

Pipes are implemented in blop in a form of standard C++ streams:

ipstream inputcmd("cat somefile");
string s;
while(inputcmd>>s) cerr<<s<<endl;

opstream outputcmd("cat");
outputcmd<<"Hello World"<<endl;

You can use remote files:

iscpstream file1("user@remote.machine:dir1/dir2/filename");  // read remote file via scp
oscpstream file2("user@remote.machine:dir1/dir2/filename"); // write a remote file via scp
ihttpstream file3("http://www.machine.com/url");            // read a file downloaded from the web

Pipe streams
Pipe streams can be used (hopefully) exactly as any other streams in C++ (ifstream, ofstream, istringstream, etc). I feel that no more comments are needed here. Maybe one: the buffersize can be set easily:

void ipstream::ibufsize(int size);
void opstream::obufsize(int size);

A related topic: the output of commands (pipes) can be plotted in blop with the usual plot or mplot commands: if the last letter of the filename is |, the preceding part of the filename is interpreted as a command, the output of which will be plotted.

Remote file I/O via scp

To access remote files for read or write via scp as a c++-stream, you can use the following classes: iscpstream or oscpstream. Both have a constructor accepting a filename (which should be of the form user@machine.address.com:dir1/dir2/filename, or, alternatively, their open(const char *) member function can be used.

The iscpstream first downloads the remote file, and then opens it for reading locally. The oscpstream first opens a local temporary file for writing, and upon closing (which is also called from its destructor) it uploads it to the remote location via scp.

Read remote file via http

Similarly to iscpstream above, the ihttpstream class downloads the file via wget from the given URL (provided as the filename to the constructor or the open(const char *url) member function).

The openin and openout functions
istream *openin(const var &name)
  • If name begins with <<, the remaining part of name is taken to be a here-document, and the returned input stream is an istringstream, which will read from this string
  • If name ends with a |, then it is interpreted as a shell command, and the returned stream is an ipstream, which will read from the standard output of this command
  • If name starts with scp://, the remaining part of it is taken to be a remote file location (via scp), which should be of the form user@remote.machine.com:dir1/dir2/filename The opened c++ stream is an iscpstream (see above)
  • If name starts with http://, it is taken to be a URL, which is downloaded via the wget function. The opened c++ stream is an ihttpstream (see above)
  • Otherwise name is interpreted as a filename, and an ifstream is returned
ostream *openout(const var &name)
  • If name begins with |, the rest of it is interpreted as a shell command, an opstream is returned, which will write to the standard input of this command
  • If name begins with >>, then the rest of it is interpreted as a file, which will be opened in append mode, and an ofstream is returned
  • If name begins with scp://, the remaining part of it is interpreted as a remote file (via scp), which should be of the form user@remote.machine.com:dir1/dir2/filename The opened c++ stream is an oscpstream (see above).
  • Otherwise name is interpreted as a filename, and will be opened in overwrite mode

Source files:
   pstream.h
   pstream.cc

Prev: fft.html Up Next: terminals.html