Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

istream from file_descriptor_source (boost::iostreams) or file

I need to do something like this for my program's input:

stream input;
if (decompressed)
    input.open(filepath);
else {
    file_descriptor=_popen("decompressor "+filepath,"r");
    input.open(file_descriptor);
}
input.read(...)
...

I can see one solution - to use _popen in both cases and just copy the file to stdout if it's already decompressed, but this doesn't seem very elegant.

Funny how difficult this is compared with C - I guess the standard library missed it. Now I am lost in the cryptic boost::iostreams documentation. Example code would be great if anyone knows how.

like image 450
graw Avatar asked Dec 24 '09 10:12

graw


2 Answers

Is this what you're after:

#include <cstdio>
#include <string>
#include <iostream>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>

namespace io = boost::iostreams;

int main()
{
    bool flag = false;

    FILE* handle = 0; 
    if (flag)
    {
        handle = _popen("dir", "r");
    }
    else
    {
        handle = fopen ("main.cpp", "r");
    }

    io::stream_buffer<io::file_descriptor_source> fpstream (fileno(handle));
    std::istream in (&fpstream);

    std::string line;
    while (in)
    {
        std::getline (in, line);
        std::cout << line << std::endl;
    }

    return 0;
}
like image 145
jon-hanson Avatar answered Sep 21 '22 21:09

jon-hanson


Adding to jon-hanson's answer, here's a simple example demonstrating how to use file_descriptor_source with a pipe.

How to build:

g++ -m32 -DBOOST_IOSTREAMS_NO_LIB -isystem ${BOOST_PATH}/include \
  ${BOOST_SRC_PATH}/libs/iostreams/src/file_descriptor.cpp blah.cc -o blah

The code:

#include <fcntl.h>
#include <stdio.h>

#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>

int main( int argc, char* argv[] ) {
  // if you just do 'using namespace...', there's a
  // namespace collision with the global 'write'
  // function used in the child
  namespace io = boost::iostreams;

  int pipefd[] = {0,0};
  pipe( pipefd, 0 );  // If you use O_NONBLOCK, you'll have to
                      // add some extra checks to the loop so
                      // it will wait until the child is finished.

  if( 0 == fork() ) {
    // child
    close( pipefd[0] ); // read handle
    dup2( pipefd[1], FILENO_STDOUT );
    printf( "This\nis\na\ntest\nto\nmake sure that\nit\nis\working as expected.\n" );
    return 0; // ya ya, shoot me ;p
  }

  // parent

  close( pipefd[1] ); // write handle

  char *buff = new char[1024];
  memset( buff, 0, 1024 );

  io::stream<io::file_descriptor_source> fds(
    io::file_descriptor_source( pipefd[0], io::never_close_handle ) );

  // this should work with std::getline as well
  while(   fds.getline( buff, 1024 )
        && fds.gcount() > 0 // this condition is not enough if you use
                            // O_NONBLOCK; it should only bail if this
                            // is false AND the child has exited
       ) {
    printf( "%s,", buff );
  }

  printf( "\n" );
}
like image 44
Brian Vandenberg Avatar answered Sep 22 '22 21:09

Brian Vandenberg