CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Popen.hh
Go to the documentation of this file.
1 #ifndef Popen_HH
2 #define Popen_HH
3 
4 #include <limits.h>
5 
6 #include <functional>
7 #include <iostream>
8 #include <string>
9 
10 namespace CASM {
11 
13 class Popen {
14  public:
16  Popen(
17  std::function<void(FILE *)> _popen_handler = Popen::default_popen_handler,
18  std::function<void(int)> _pclose_handler = Popen::default_pclose_handler,
19  bool _combine_stdout_stderr = true);
20 
22  void popen(std::string _command);
23 
25  std::string gets() const;
26 
28  void print(std::ostream &sout) const;
29 
31  int exit_code() const;
32 
35  static void default_popen_handler(FILE *fp);
36 
39  static void default_pclose_handler(int status);
40 
41  private:
42  std::string m_command;
43  std::string m_stdout;
45  std::function<void(FILE *)> m_popen_handler;
46  std::function<void(int)> m_pclose_handler;
48 };
49 
50 } // namespace CASM
51 
52 #endif
Remember how to use popen.
Definition: Popen.hh:13
std::string gets() const
Returns the stdout resulting from the last popen call.
Definition: Popen.cc:43
int m_pclose_result
Definition: Popen.hh:44
Popen(std::function< void(FILE *)> _popen_handler=Popen::default_popen_handler, std::function< void(int)> _pclose_handler=Popen::default_pclose_handler, bool _combine_stdout_stderr=true)
Construct a Popen object.
Definition: Popen.cc:8
static void default_pclose_handler(int status)
Default pclose error handler throws std::runtime_error if pclose failed.
Definition: Popen.cc:63
void popen(std::string _command)
Execute popen for a given command.
Definition: Popen.cc:19
void print(std::ostream &sout) const
Print the last command executed and the resulting stdout.
Definition: Popen.cc:46
std::function< void(int)> m_pclose_handler
Definition: Popen.hh:46
std::string m_command
Definition: Popen.hh:42
int exit_code() const
Returns pclose(fp)/256.
Definition: Popen.cc:52
static void default_popen_handler(FILE *fp)
Default popen error handler throws std::runtime_error if popen failed.
Definition: Popen.cc:55
std::function< void(FILE *)> m_popen_handler
Definition: Popen.hh:45
std::string m_stdout
Definition: Popen.hh:43
bool m_combine_stdout_stderr
Definition: Popen.hh:47
Main CASM namespace.
Definition: APICommand.hh:8