Skip to content

Commit 20f05af

Browse files
author
Daniel Kroening
authored
Merge pull request #2923 from diffblue/run-with-ostream
run() with stdout ostream
2 parents 0fede8f + 46b8495 commit 20f05af

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/util/run.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,25 @@ Date: August 2012
1515
#else
1616

1717
#include <cstring>
18-
#include <unistd.h>
1918
#include <cerrno>
2019
#include <cstdio>
2120
#include <cstdlib>
2221

23-
#include <sys/wait.h>
24-
#include <sys/types.h>
25-
#include <sys/stat.h>
2622
#include <fcntl.h>
2723
#include <signal.h>
24+
#include <sys/stat.h>
25+
#include <sys/types.h>
26+
#include <sys/wait.h>
27+
#include <unistd.h>
2828

2929
#endif
3030

31-
#include <util/invariant.h>
32-
#include <util/unicode.h>
33-
#include <util/signal_catcher.h>
31+
#include <fstream>
32+
33+
#include "invariant.h"
34+
#include "signal_catcher.h"
35+
#include "tempfile.h"
36+
#include "unicode.h"
3437

3538
int run(const std::string &what, const std::vector<std::string> &argv)
3639
{
@@ -228,3 +231,22 @@ int run(
228231
}
229232
#endif
230233
}
234+
235+
int run(
236+
const std::string &what,
237+
const std::vector<std::string> &argv,
238+
const std::string &std_input,
239+
std::ostream &std_output,
240+
const std::string &std_error)
241+
{
242+
temporary_filet tmpi("tmp.stdout", "");
243+
244+
int result = run(what, argv, std_input, tmpi(), std_error);
245+
246+
std::ifstream instream(tmpi());
247+
248+
if(instream)
249+
std_output << instream.rdbuf(); // copy
250+
251+
return result;
252+
}

src/util/run.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Date: August 2012
1212
#ifndef CPROVER_UTIL_RUN_H
1313
#define CPROVER_UTIL_RUN_H
1414

15+
#include <iosfwd>
1516
#include <string>
1617
#include <vector>
1718

@@ -24,4 +25,12 @@ int run(
2425
const std::string &std_output,
2526
const std::string &std_error);
2627

28+
/// A variant that streams the stdout of the child into an ostream
29+
int run(
30+
const std::string &what,
31+
const std::vector<std::string> &argv,
32+
const std::string &std_input,
33+
std::ostream &std_output,
34+
const std::string &std_error);
35+
2736
#endif // CPROVER_UTIL_RUN_H

0 commit comments

Comments
 (0)