-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_pipe.cpp
More file actions
50 lines (43 loc) · 1.19 KB
/
cpp_pipe.cpp
File metadata and controls
50 lines (43 loc) · 1.19 KB
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
#include <cerrno>
#include <cstring>
#include <iostream>
using namespace std;
#include "auxlib.h"
#include "cpp_pipe.h"
cpp_pipe::cpp_pipe (const char* filename, bool debug, string defines):
cpp_command (string (cpp_name) + " " + defines + " " + filename),
cpp_debug (debug) {
cpp_file = popen (cpp_command.c_str(), "r");
if (cpp_file == nullptr) {
cpp_errno = errno;
}else {
if (cpp_debug) {
cerr << "--cpp_pipe: popen (\"" << cpp_command
<< "\"): fileno = " << fileno (cpp_file) << endl;
}
}
}
cpp_pipe::~cpp_pipe() {
if (cpp_file) close_pipe();
}
cpp_pipe::operator bool() const {
return cpp_file != nullptr;
}
FILE* cpp_pipe::get_pipe() {
if (cpp_file == nullptr) {
throw fatal_error (cpp_command + ": " + strerror (cpp_errno));
}
return cpp_file;
}
void cpp_pipe::close_pipe() {
if (cpp_debug) {
cerr << "--cpp_pipe: pclose (\"" << cpp_command
<< "\"): fileno = " << fileno (cpp_file) << endl;
}
int status = pclose (cpp_file);
cpp_file = nullptr;
if (status != 0) {
exec::error() << "pclose failed: " << cpp_command << ": "
<< wait_status {status} << endl;
}
}