00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __PFSTREAMBUF_H__
00020 #define __PFSTREAMBUF_H__
00021
00022 #include <dtoolbase.h>
00023 #include <string>
00024 #include <stdio.h>
00025
00026
00027
00028
00029
00030 #ifdef WIN32_VC
00031 #define WIN_PIPE_CALLS 1
00032 #endif
00033
00034 #ifdef WIN_PIPE_CALLS
00035 #include <windows.h>
00036
00037 #else // WIN_PIPE_CALLS
00038
00039 #ifdef WIN32_VC
00040 #define popen _popen
00041 #define pclose _pclose
00042 #endif
00043
00044 #endif // WIN_PIPE_CALLS
00045
00046 class EXPCL_DTOOL PipeStreamBuf : public streambuf {
00047 public:
00048 enum Direction { Input, Output };
00049
00050 PipeStreamBuf(Direction);
00051 virtual ~PipeStreamBuf(void);
00052
00053 void flush();
00054 void command(const string);
00055
00056 protected:
00057 virtual int overflow(int c);
00058 virtual int sync(void);
00059 virtual int underflow(void);
00060 private:
00061 void init_pipe();
00062 bool is_open() const;
00063 bool eof_pipe() const;
00064 bool open_pipe(const string &cmd);
00065 void close_pipe();
00066 size_t write_pipe(const char *data, size_t len);
00067 size_t read_pipe(char *data, size_t len);
00068
00069 Direction _dir;
00070 string _line_buffer;
00071
00072 #ifndef WIN_PIPE_CALLS
00073 FILE *_pipe;
00074
00075 #else // WIN_PIPE_CALLS
00076 HANDLE _child_out;
00077 #endif // WIN_PIPE_CALLS
00078
00079 void write_chars(const char*, int, bool);
00080 };
00081
00082 #endif