Console Library
8.0.0
A header-only library that makes C++ simple
Toggle main menu visibility
Loading...
Searching...
No Matches
syscmd.h
Go to the documentation of this file.
1
10
11
/*
12
Copyright (c) 2026 MrXie1109
13
14
Permission is hereby granted, free of charge, to any person obtaining a copy
15
of this software and associated documentation files (the "Software"), to deal
16
in the Software without restriction, including without limitation the rights
17
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
copies of the Software, and to permit persons to whom the Software is
19
furnished to do so, subject to the following conditions:
20
21
The above copyright notice and this permission notice shall be included in all
22
copies or substantial portions of the Software.
23
24
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
SOFTWARE.
31
*/
32
33
#pragma once
34
#include <array>
35
#include <cstdlib>
36
#include <map>
37
#include <memory>
38
#include <string>
39
#include <vector>
40
41
#ifdef _WIN32
42
#define POPEN _popen
43
#define PCLOSE _pclose
44
#else
45
#define POPEN popen
46
#define PCLOSE pclose
47
#endif
48
49
namespace
console
{
55
class
CmdResult
{
56
int
status_
;
57
std::unique_ptr<std::string>
ptr_
;
58
62
CmdResult
() {}
63
67
friend
CmdResult
syscmd
(
const
std::string &,
68
const
std::vector<std::string> &,
69
const
std::map<std::string, std::string> &);
70
71
public
:
77
bool
ok
()
const
{
return
ptr_
&&
status_
== 0; }
78
84
bool
error
()
const
{
return
ptr_
&&
status_
!= 0; }
85
91
bool
fail
()
const
{
return
!
ptr_
; }
92
97
int
status
()
const
{
return
status_
; }
98
105
const
std::string &
output
()
const
{
return
*
ptr_
; }
106
};
107
123
inline
CmdResult
syscmd
(
const
std::string &exe,
124
const
std::vector<std::string> &args = {},
125
const
std::map<std::string, std::string> &kwargs = {}) {
126
std::string cmd = exe;
127
for
(
const
std::string &arg : args) cmd +=
" "
+ arg;
128
for
(
const
auto
&kwarg : kwargs)
129
cmd +=
" "
+ kwarg.first +
" "
+ kwarg.second;
130
cmd +=
" 2>&1"
;
131
FILE *pipe =
POPEN
(cmd.c_str(),
"r"
);
132
if
(!pipe) {
133
CmdResult cr;
134
cr.status_ = -1;
135
cr.ptr_ =
nullptr
;
136
return
cr;
137
}
138
std::array<char, 256> buffer;
139
std::unique_ptr<std::string> output(
new
std::string);
140
while
(fgets(buffer.data(), buffer.size(), pipe) !=
nullptr
)
141
*output += buffer.data();
142
int
status =
PCLOSE
(pipe);
143
CmdResult
cr;
144
cr.
status_
= status;
145
cr.ptr_ = std::move(output);
146
return
cr;
147
}
148
}
149
150
#undef POPEN
151
#undef PCLOSE
console::CmdResult
命令执行结果封装类。
Definition
syscmd.h:55
console::CmdResult::ok
bool ok() const
检查命令是否成功执行。
Definition
syscmd.h:77
console::CmdResult::output
const std::string & output() const
获取命令的标准输出内容。
Definition
syscmd.h:105
console::CmdResult::syscmd
friend CmdResult syscmd(const std::string &, const std::vector< std::string > &, const std::map< std::string, std::string > &)
友元声明,仅允许 syscmd 创建实例。
Definition
syscmd.h:123
console::CmdResult::CmdResult
CmdResult()
私有默认构造函数。
Definition
syscmd.h:62
console::CmdResult::ptr_
std::unique_ptr< std::string > ptr_
命令输出内容(含 stderr)
Definition
syscmd.h:57
console::CmdResult::status_
int status_
命令退出状态码(0 成功,-1 管道打开失败)
Definition
syscmd.h:56
console::CmdResult::fail
bool fail() const
检查命令执行是否完全失败。
Definition
syscmd.h:91
console::CmdResult::status
int status() const
获取命令退出状态码。
Definition
syscmd.h:97
console::CmdResult::error
bool error() const
检查命令是否执行但返回错误码。
Definition
syscmd.h:84
console
本库所有组件所在的顶层命名空间。
console::syscmd
CmdResult syscmd(const std::string &exe, const std::vector< std::string > &args={}, const std::map< std::string, std::string > &kwargs={})
执行系统命令并捕获输出。
Definition
syscmd.h:123
PCLOSE
#define PCLOSE
Definition
syscmd.h:46
POPEN
#define POPEN
Definition
syscmd.h:45
include
util
syscmd.h
Generated on
for Console Library by
1.18.0