Panda3D

pfstreamBuf.h

00001 // Filename: pfstreamBuf.h
00002 // Created by:  cary (12Dec00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
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 // By default, we'll use the Windows flavor of pipe functions if we're
00023 // compiling under Windows.  Turn this off to use popen(), even on
00024 // Windows.  (popen() doesn't seem to work on Win9x, although it does
00025 // work on NT-based variants.)
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 /* __PFSTREAMBUF_H__ */
 All Classes Functions Variables Enumerations