Panda3D
pfstreamBuf.h
1 // Filename: pfstreamBuf.h
2 // Created by: cary (12Dec00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef __PFSTREAMBUF_H__
16 #define __PFSTREAMBUF_H__
17 
18 #include "dtoolbase.h"
19 #include <string>
20 #include <stdio.h>
21 
22 // By default, we'll use the Windows flavor of pipe functions if we're
23 // compiling under Windows. Turn this off to use popen(), even on
24 // Windows. (popen() doesn't seem to work on Win9x, although it does
25 // work on NT-based variants.)
26 #ifdef WIN32_VC
27 #define WIN_PIPE_CALLS 1
28 #endif
29 
30 #ifdef WIN_PIPE_CALLS
31 #ifndef WIN32_LEAN_AND_MEAN
32 #define WIN32_LEAN_AND_MEAN 1
33 #endif
34 #include <windows.h>
35 
36 #else // WIN_PIPE_CALLS
37 
38 #ifdef WIN32_VC
39 #define popen _popen
40 #define pclose _pclose
41 #endif
42 
43 #endif // WIN_PIPE_CALLS
44 
45 class EXPCL_DTOOL PipeStreamBuf : public streambuf {
46 public:
47  enum Direction { Input, Output };
48 
49  PipeStreamBuf(Direction);
50  virtual ~PipeStreamBuf(void);
51 
52  void flush();
53  void command(const string);
54 
55 protected:
56  virtual int overflow(int c);
57  virtual int sync(void);
58  virtual int underflow(void);
59 private:
60  void init_pipe();
61  bool is_open() const;
62  bool eof_pipe() const;
63  bool open_pipe(const string &cmd);
64  void close_pipe();
65  size_t write_pipe(const char *data, size_t len);
66  size_t read_pipe(char *data, size_t len);
67 
68  Direction _dir;
69  string _line_buffer;
70 
71 #ifndef WIN_PIPE_CALLS
72  FILE *_pipe;
73 
74 #else // WIN_PIPE_CALLS
75  HANDLE _child_out;
76 #endif // WIN_PIPE_CALLS
77 
78  void write_chars(const char*, int, bool);
79 };
80 
81 #endif /* __PFSTREAMBUF_H__ */