-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocext.hpp
More file actions
77 lines (67 loc) · 2.78 KB
/
Copy pathprocext.hpp
File metadata and controls
77 lines (67 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Process library extentions.
//
// Copyright (C) 2026, Martin Young <martin_young@live.cn>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//------------------------------------------------------------------------
#pragma once
#include <string>
#include <string_view>
#include <concepts>
#include <type_traits>
#include "strext.hpp"
//------------------------------------------------------------------------
// 统一的进程执行状态返回体
struct cmd_status {
int exitcode{0};
std::string errmsg;
// 提供便捷的状态检验接口
[[nodiscard]] bool ok() const noexcept {
return exitcode == 0 && errmsg.empty();
}
};
//------------------------------------------------------------------------
namespace ns_execute_cmd {
// 底层统一接口:类型擦除以隔离所有 OS 宏和文件流扩展
int execute_cmd(const std::string& cmd, void* ctx, void(*cb)(void*, std::string_view), std::string& errmsg);
}
//------------------------------------------------------------------------
class cmdoutput {
public:
int exitcode{}; // cmd exit code
std::string errmsg{}; // Not empty if error occurs or exitcode!=0
TStrVec lines;
cmdoutput() = default;
explicit cmdoutput(const std::string& cmd) { setcmd(cmd); }
// move construction
cmdoutput(cmdoutput&&) noexcept = default;
cmdoutput& operator=(cmdoutput&&) noexcept = default;
// copy construction
cmdoutput(const cmdoutput&) = default;
cmdoutput& operator=(const cmdoutput&) = default;
void setcmd(const std::string& cmd);
};
//------------------------------------------------------------------------
// Process every output line with 'func' and return the exit code
// C++20: 利用 std::invocable 进行严格的泛型约束
template <std::invocable<std::string_view> F>
[[nodiscard]] cmd_status cmdoutputline(const std::string& cmd, F&& func) {
auto thunk = [](void* ctx, std::string_view line) {
(*static_cast<std::remove_reference_t<F>*>(ctx))(line);
};
cmd_status status;
status.exitcode = ns_execute_cmd::execute_cmd(cmd, &func, thunk, status.errmsg);
return status;
}
//------------------------------------------------------------------------