Commit d4d2d23f1eb87e55340448e269d7308b47d4a101

Authored by Jordi Inglada
1 parent 05146a94
Exists in master

Implement system call

Showing 1 changed file with 13 additions and 0 deletions   Show diff stats
include/cbutils.h
... ... @@ -16,6 +16,7 @@
16 16 #include <algorithm>
17 17 #include <boost/algorithm/string.hpp>
18 18 #include <cstddef>
  19 +#include <cstdlib>
19 20 #include <fstream>
20 21 #include <stdexcept>
21 22 #include <string>
... ... @@ -40,6 +41,18 @@ std::vector&lt;std::string&gt; split(const std::string&amp; str,
40 41 return tokens;
41 42 }
42 43 } //end ns string
  44 +// ----------------- System calls ----------------------------------------------
  45 +namespace system{
  46 +std::string call(const std::string& command)
  47 +{
  48 + std::string tmp_filename{std::tmpnam(nullptr)};
  49 + std::stringstream command_redirect{command+" > "+tmp_filename};
  50 + std::system(command_redirect.str().c_str());
  51 + std::stringstream command_output;
  52 + command_output << std::ifstream(tmp_filename).rdbuf();
  53 + return command_output.str();
  54 +}
  55 +} //end ns system
43 56 // ----------------- File IO ---------------------------------------------------
44 57 namespace file
45 58 {
... ...