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