-
Notifications
You must be signed in to change notification settings - Fork 12
/
pstream_compat.h
170 lines (133 loc) · 4.13 KB
/
pstream_compat.h
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
// PStreams - POSIX Process I/O for C++
// Copyright (C) 2001 - 2020 Jonathan Wakely
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// SPDX-License-Identifier: BSL-1.0
/**
* @file pstream_compat.h
* Declares non-standard implementations of the PStreams classes for older
* compilers.
*/
#ifndef REDI_PSTREAM_COMPAT_H_SEEN
#define REDI_PSTREAM_COMPAT_H_SEEN
#if ! ( BACK_COMPAT == 1 || GCC_BACK_COMPAT == 1 )
#error \
You must define either BACK_COMPAT or GCC_BACK_COMPAT to be 1 \
to use this file. For gcc versions 2.7 to 2.9x (including egcs) \
define GCC_BACK_COMPAT = 1. For other compilers define BACK_COMPAT = 1 \
For ISO C++ conforming compilers use pstream.h instead.
#elif GCC_BACK_COMPAT == 1
#warning "PStreams needs an ISO C++ compliant compiler."
#warning "These classes are unsupported and largely untested."
// turn on non-standard extensions
#define _STREAM_COMPAT 1
#include <fstream>
#include <stdio.h>
#include <errno.h>
#include <string.h>
namespace redi
{
class pstreambase : public fstreambase
{
public:
void open(const char* command)
{
if ((fp_ = popen(command, mode_)))
{
attach(fileno(fp_));
// errno is set to zero because fstreambase tries to lseek()
// to the end of the stream which is not allowed for pipes
// and sets errno to ESPIPE, so we clear it.
errno=0;
}
}
int close() { return fp_ ? rdbuf()->sync(), pclose(fp_) : 0; }
bool is_open() { return fp_; }
protected:
pstreambase(const char* mode) : fstreambase(), mode_(mode), fp_(0) { }
pstreambase(const char* command, const char* mode)
: fstreambase(), mode_(mode), fp_(0)
{
open(command);
}
~pstreambase() { close(); }
private:
const char* const mode_; // Points to static mode member of derived class.
FILE* fp_; // Preserves return value of popen().
};
class ipstream : public pstreambase, public istream
{
static const char * const mode = "r";
public:
ipstream() : pstreambase(mode) { }
ipstream(const char* command) : pstreambase(command, mode) { }
};
class opstream : public pstreambase, public ostream
{
static const char * const mode = "w";
public:
opstream() : pstreambase(mode) { }
opstream(const char* command) : pstreambase(command, mode) { }
};
#if REDI_PSTREAMS_POPEN_USES_BIDIRECTIONAL_PIPE
class pstream : protected pstreambase, public iostream {
static const char * const mode = "r+";
public:
pstream() : pstreambase(mode) { }
pstream(const char* command) : pstreambase(command, mode) { }
};
#endif
} // namespace redi
#elif BACK_COMPAT == 1
#warning "PStreams needs an ISO C++ compliant compiler!"
#warning "These classes are unsupported and untested."
#include <fstream>
#include <cstdio>
#include <cerrno>
// very basic implementations
// incomplete interfaces: no open(const char*)
// use non-standard extensions
// use at your own risk!
namespace redi
{
struct FILE_wrapper {
explicit FILE_wrapper(FILE* p) : fp(p) {}
FILE* fp;
};
class opstream : private FILE_wrapper, public std::ofstream {
static const char * const mode = "w";
public:
opstream(const char* command)
: FILE_wrapper(popen(command, mode)), std::ofstream(fileno(fp))
{
errno=0;
}
~opstream() { pclose(fp); }
};
class ipstream : private FILE_wrapper, public std::ifstream {
static const char * const mode = "r";
public:
ipstream(const char* command)
: FILE_wrapper(popen(command, mode)), std::ifstream(fileno(fp))
{
errno=0;
}
~ipstream() { pclose(fp); }
};
#if REDI_PSTREAMS_POPEN_USES_BIDIRECTIONAL_PIPE
class pstream : private FILE_wrapper, public std::fstream {
static const char * const mode = "r+";
public:
pstream(const char* command)
: FILE_wrapper(popen(command, mode)), std::fstream(fileno(fp))
{
errno=0;
}
~pstream() { pclose(fp); }
};
#endif
} // namespace redi
#endif // __GNUC__
#endif // REDI_PSTREAM_COMPAT_H_SEEN