Rose
Command.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <cstdio>
11 #include <cerrno>
12 #include <cstring>
13 #include <iostream>
14 #include <memory>
15 #include <stdexcept>
16 #include <string>
17 #include <array>
18 #include "Utilities.h"
19 
20 namespace rose {
21 
26  class Command {
27  protected:
28  std::string mResult{};
29 
33  struct PipeClose {
34  int exitStatus{};
35 
36  void operator()(FILE *pipe) {
37  exitStatus = pclose(pipe);
38  }
39  };
40 
42  std::unique_ptr<FILE, PipeClose> mPipe;
43 
44  public:
45  Command() = delete;
46 
53  explicit Command(const char* command) : mPipe(popen(command, "r"), mPipeClose){
54 
55  }
56 
61  int wait() {
62  std::array<char, 128> buffer{};
63 
64  while (fgets(buffer.data(), buffer.size(), mPipe.get()) != nullptr) {
65  mResult += buffer.data();
66  }
67 
68  mPipe.release();
69 
70  return statusCode();
71  }
72 
77  int statusCode() {
78  return mPipeClose.exitStatus;
79  }
80 
85  const std::string& result() const { return mResult; }
86  };
87 }
88 
int wait()
Gather the command output and wait for it to exit.
Definition: Command.h:61
Execute a program and gather the output.
Definition: Command.h:26
Command(const char *command)
Constructor.
Definition: Command.h:53
std::string mResult
The output of the command.
Definition: Command.h:28
int statusCode()
Get the command exit status code.
Definition: Command.h:77
Close the pipe and keep the exit status.
Definition: Command.h:33
PipeClose mPipeClose
The pipe closer.
Definition: Command.h:41
std::unique_ptr< FILE, PipeClose > mPipe
The pipe from the command.
Definition: Command.h:42
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
const std::string & result() const
Get the output of the command.
Definition: Command.h:85
int exitStatus
The status code returned by pclose(3)
Definition: Command.h:34