CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Popen.hh
Go to the documentation of this file.
1 #ifndef Popen_HH
2 #define Popen_HH
3 
4 #include <iostream>
5 #include <string>
6 #include <functional>
7 #include <limits.h>
8 
9 namespace CASM {
10 
12  class Popen {
13 
14  public:
15 
17  Popen(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 
34  static void default_popen_handler(FILE *fp);
35 
37  static void default_pclose_handler(int status);
38 
39  private:
40 
41  std::string m_command;
42  std::string m_stdout;
44  std::function<void(FILE *)> m_popen_handler;
45  std::function<void(int)> m_pclose_handler;
47  };
48 
49 }
50 
51 #endif
52 
std::function< void(FILE *)> m_popen_handler
Definition: Popen.hh:44
std::string gets() const
Returns the stdout resulting from the last popen call.
Definition: Popen.cc:44
int m_pclose_result
Definition: Popen.hh:43
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:49
Main CASM namespace.
Definition: complete.cpp:8
Remember how to use popen.
Definition: Popen.hh:12
std::string m_command
Definition: Popen.hh:41
static void default_pclose_handler(int status)
Default pclose error handler throws std::runtime_error if pclose failed.
Definition: Popen.cc:67
std::function< void(int)> m_pclose_handler
Definition: Popen.hh:45
int exit_code() const
Returns pclose(fp)/256.
Definition: Popen.cc:55
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:7
std::string m_stdout
Definition: Popen.hh:42
static void default_popen_handler(FILE *fp)
Default popen error handler throws std::runtime_error if popen failed.
Definition: Popen.cc:60
bool m_combine_stdout_stderr
Definition: Popen.hh:46