Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members

stream_wrap.h

00001 // Copyright (C) 2001 Jean-Marc Valin
00002 
00003 #ifndef STREAM_WRAP_H
00004 #define STREAM_WRAP_H
00005 
00006 //#include <streambuf>
00007 #include <iostream>
00008 #include <stdio.h>
00009 
00010 #include <sys/types.h>
00011 #include <sys/stat.h>
00012 #include <fcntl.h>
00013 
00014 #ifndef WIN32
00015 #include <unistd.h>
00016 #endif
00017 #include <string>
00018 
00019 #include "Object.h"
00020 
00021 
00022 namespace FD {
00023 
00024 class EOFObject : public Object {
00025   public:
00026    void printOn(std::ostream &out = std::cout) const {out << "<EOFObject >\n";}
00027 };
00028 
00029 
00030 class fileptr_streambuf : public std::streambuf {
00031   protected:
00032    virtual int overflow(int = EOF);
00033    virtual std::streamsize xsputn(const char *s, std::streamsize n);
00034 
00035    virtual int uflow();
00036    virtual int underflow();
00037    virtual std::streamsize xsgetn(char *s, std::streamsize n);
00038    virtual int pbackfail(int c);
00039   public:
00040    fileptr_streambuf(FILE *_file, bool _owner=true, bool _isPipe=false);
00041    ~fileptr_streambuf() {if (owner) { if (isPipe) pclose(file); else fclose (file);}}
00042   protected:
00043    FILE *file;
00044    bool owner;
00045    bool takeFromBuf;
00046    char charBuf;
00047    bool isPipe;
00048 };
00049 
00050 
00051 
00052 class fileptr_ostream : public std::ostream {
00053    fileptr_streambuf _streambuffer;
00054   public:
00055    fileptr_ostream(FILE *_file, bool _owner=true, bool _isPipe=false)
00056       : std::ostream(&_streambuffer)
00057       , _streambuffer (_file, _owner, _isPipe)
00058       {clear();}
00059 };
00060 
00061 class fileptr_istream : public std::istream {
00062    fileptr_streambuf _streambuffer;
00063   public:
00064    fileptr_istream(FILE *_file, bool _owner=true, bool _isPipe=false)
00065       : std::istream(&_streambuffer)
00066       , _streambuffer (_file, _owner, _isPipe)
00067       {clear();}
00068 };
00069 
00070 class fileptr_iostream : public std::iostream {
00071    fileptr_streambuf _streambuffer;
00072   public:
00073    fileptr_iostream(FILE *_file, bool _owner=true, bool _isPipe=false)
00074       : std::iostream(&_streambuffer)
00075       , _streambuffer (_file, _owner, _isPipe)
00076       {clear();}
00077 };
00078 
00079 
00080 
00081 
00082 #ifndef WIN32
00083 
00084 class fd_streambuf : public std::streambuf {
00085   protected:
00086    virtual int overflow(int = EOF);
00087    virtual std::streamsize xsputn(const char *s, std::streamsize n);
00088 
00089    virtual int uflow();
00090    virtual int underflow();
00091    virtual std::streamsize xsgetn(char *s, std::streamsize n);
00092    virtual int pbackfail(int c);
00093   public:
00094    fd_streambuf(int _fd, bool _owner=true);
00095    ~fd_streambuf() {if (owner) close (fd);}
00096   protected:
00097    int fd;
00098    bool owner;
00099    bool takeFromBuf;
00100    char charBuf;
00101 };
00102 
00103 
00104 
00105 class fd_ostream : public std::ostream {
00106    fd_streambuf _streambuffer;
00107   public:
00108    fd_ostream(int _fd, bool _owner=true)
00109       : std::ostream(&_streambuffer)
00110       , _streambuffer (_fd, _owner)
00111       {clear();}
00112 };
00113 
00114 
00115 class fd_istream : public std::istream {
00116    fd_streambuf _streambuffer;
00117   public:
00118    fd_istream(int _fd, bool _owner=true)
00119       : std::istream(&_streambuffer)
00120       , _streambuffer (_fd, _owner)
00121       {clear();}
00122 };
00123 
00124 class fd_iostream : public std::iostream {
00125    fd_streambuf _streambuffer;
00126   public:
00127    fd_iostream(int _fd, bool _owner=true)
00128       : std::iostream(&_streambuffer)
00129       , _streambuffer (_fd, _owner)
00130       {clear();}
00131 };
00132 
00133 
00134 
00135 
00136 
00137 
00138 class pipe_streambuf : public std::streambuf {
00139   protected:
00140    virtual int overflow(int = EOF);
00141    virtual std::streamsize xsputn(const char *s, std::streamsize n);
00142 
00143    virtual int uflow();
00144    virtual int underflow();
00145    virtual std::streamsize xsgetn(char *s, std::streamsize n);
00146    virtual int pbackfail(int c);
00147    int ll_read(void *buf, size_t count);
00148    int ll_write(const void *buf, size_t count);
00149   public:
00150    //pipe_streambuf(int _ifd, int _ofd, pid_t _pid, bool _waitOnClose=false);
00151    pipe_streambuf(const std::string &command, bool _waitOnClose=false);
00152    ~pipe_streambuf();
00153    void pipeString(const std::string &in, std::string &out);
00154   protected:
00155    int ifd;
00156    int ofd;
00157    pid_t pid;
00158    bool waitOnClose;
00159    bool takeFromBuf;
00160    char charBuf;
00161 };
00162 
00163 
00164 
00165 class pipe_istream : public std::istream {
00166    pipe_streambuf _streambuffer;
00167   public:
00168    pipe_istream(const std::string &command, bool waitOnClose=false)
00169       : std::istream(&_streambuffer)
00170       , _streambuffer (command, waitOnClose)
00171       {clear();}
00172 };
00173 
00174 class pipe_iostream : public std::iostream {
00175    pipe_streambuf _streambuffer;
00176   public:
00177    pipe_iostream(const std::string &command, bool waitOnClose=false)
00178       : std::iostream(&_streambuffer)
00179       , _streambuffer (command, waitOnClose)
00180       {clear();}
00181    void pipeString(const std::string &in, std::string &out) {_streambuffer.pipeString(in,out);}
00182 };
00183 
00184 #endif
00185 
00186 }//namespace FD
00187 #endif

Generated on Wed Oct 5 14:28:56 2005 for FlowDesigner by  doxygen 1.4.4