Lodestar
An integrated real-time control package in C++
ExecuteCommand.hpp
1 //
2 // Created by Hamza El-Kebir on 2/15/22.
3 //
4 
5 #ifndef LODESTAR_EXECUTECOMMAND_HPP
6 #define LODESTAR_EXECUTECOMMAND_HPP
7 
8 #include <cstdio>
9 #include <iostream>
10 #include <memory>
11 #include <stdexcept>
12 #include <string>
13 #include <array>
14 
15 namespace ls {
16  namespace cli {
18  public:
19  static bool commandExists(const ::std::string &cmd);
20 
21  template<unsigned int NBufferSize = 512>
22  static ::std::string getStdout(const ::std::string &cmd)
23  {
24  static std::array<char, NBufferSize> buffer{};
25  std::string result;
26  std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
27  if (!pipe) {
28  throw std::runtime_error("popen() failed!");
29  }
30  while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
31  result += buffer.data();
32  }
33  return result;
34  }
35  };
36  }
37 }
38 
39 
40 #endif //LODESTAR_EXECUTECOMMAND_HPP
ls::cli::ExecuteCommand
Definition: ExecuteCommand.hpp:17
ls
Main Lodestar code.
Definition: BilinearTransformation.hpp:12