File: pstream.hh

package info (click to toggle)
exactimage 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,048 kB
  • sloc: cpp: 35,940; ansic: 1,952; xml: 1,447; makefile: 338; perl: 138; sh: 110; python: 45; php: 37; ruby: 12
file content (210 lines) | stat: -rw-r--r-- 3,785 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

#ifndef UTILITY__PSTREAM_HH__
#define UTILITY__PSTREAM_HH__

#include <unistd.h>
#include <signal.h> // kill

#include <sys/types.h>
#include <sys/wait.h>

#include <stdarg.h>

#include <iostream>

namespace Utility {

class processbuf : public std::streambuf
{
protected:

  int pid;
  int sink[2];
  int source[2];

  /* output */
  virtual int_type overflow (int_type c) {
	if (c != EOF) {
		char cc = c;
		if (write (sink[1], &cc, 1) != 1)
			return EOF;
	}
	else {
		close_sink();
		return EOF;
	}
	return c;
  }
  virtual std::streamsize xsputn (const char* s, std::streamsize num) {
	return write (sink[1], s, num);
  }

  /* input */
  char buffer [2];
  virtual int_type underflow () {
	if (gptr() < egptr())
		return *gptr();

	int numPutback = gptr() - eback();
	if (numPutback > 1)
		numPutback = 1;

	buffer[0] = buffer[1];

	int num = read (source[0], &buffer[1], 1);
	if (num <= 0)
		return EOF;

	setg (buffer + 1 - numPutback, buffer + 1, buffer + 1 + num);
	return *gptr();
  }

public:
  processbuf () {
    setg (buffer+1, buffer+1, buffer+1);
  }

  processbuf (const char* file, char* const argv[]) {
    setg (buffer+1, buffer+1, buffer+1);
    exec (file, argv);
  }

  ~processbuf () {
    close(source[0]); close(sink[1]);
    waitpid(pid, NULL, 0);
  }
  
  void exec (const char* file, const char* arg, va_list ap) {
    const char* args[10];
    // convert va_list into static char* array -sigh
    int i = 0;
    args[i++] = arg;
    for (; i < ((int) sizeof(args))-1; ++i) {
      const char* s = va_arg(ap, const char*);
      if (s != NULL) {
	// std::cout << "Got: " << s << std::endl;
	args[i] = s;
      }
      else
	break;
    }
    args[i] = NULL;
    exec (file, (char* const*)args);
  }

  void exec (const char* file, char* const argv[]) {
    pipe (sink);
    pipe (source);
    if ((pid = fork()) == 0) {
      dup2(sink[0],0); close(sink[0]); close(sink[1]);
      dup2(source[1],1); close(source[0]); close(source[1]);
      execvp(file, argv);
      perror(file);
      _exit(1);
    }
    close(source[1]); close(sink[0]);
  }
  
  void close_sink () {
    close(sink[1]);
  }

  void terminate (int signal) {
    close(source[0]);
    close(sink[1]);
    kill(pid, signal);
  }
  
};

class postream : public std::ostream
{
public:
  postream (const char* file, char* const argv[])
  : std::ostream(&buf), buf(file, argv){
  }

  postream (const char* file, const char* args, ...)
    : std::ostream(&buf) {
    va_list ap, aq;
    va_start (ap, args);
    va_copy (aq, ap);
    
    buf.exec (file, args, ap);
    va_end (aq);
    va_end (ap);
  }

  void terminate (int signal=SIGTERM) {
    buf.terminate (signal);
  }

protected:
  processbuf buf;
};

class pistream : public std::istream
{
public:
  pistream (const char* file, char* const argv[])
  : std::istream(&buf), buf(file, argv) {
  }

  pistream (const char* file, const char* args, ...)
    : std::istream(&buf) {
    va_list ap, aq;
    va_start (ap, args);
    va_copy (aq, ap);
    
    buf.exec (file, args, ap);
    va_end (aq);
    va_end (ap);
  }

  pistream (...)
    : std::istream(&buf) {
    //    buf(", "");
  }

  void terminate (int signal=SIGTERM) {
    buf.terminate (signal);
  }

protected:
  processbuf buf;
};


class pstream : public std::iostream
{
public:
  pstream (const char* file, char* const argv[])
    : std::iostream(&buf), buf(file, argv) {
  }

  pstream (const char* file, const char* args, ...)
    : std::iostream(&buf) {
    va_list ap, aq;
    va_start (ap, args);
    va_copy (aq, ap);
    
    buf.exec (file, args, ap);
    va_end (aq);
    va_end (ap);
  }

  void close_sink () {
    buf.close_sink ();
  }

  void terminate (int signal=SIGTERM) {
    buf.terminate (signal);
  }

protected:
  processbuf buf;
};

}

#endif