MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Process.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace MayaFlux::Platform {
4
5#ifdef MAYAFLUX_PLATFORM_WINDOWS
6constexpr char PathSeparator = '\\';
7#else
8constexpr char PathSeparator = '/';
9#endif
10
11inline std::string safe_getenv(const char* var)
12{
13#ifdef MAYAFLUX_PLATFORM_WINDOWS
14 size_t len = 0;
15 if (getenv_s(&len, nullptr, 0, var) != 0 || len <= 1) {
16 return "";
17 }
18 std::string result(len, '\0');
19 if (getenv_s(&len, result.data(), result.size(), var) != 0 || len == 0) {
20 return "";
21 }
22 result.resize(len - 1);
23 return result;
24#else
25 const char* value = std::getenv(var);
26 return value ? std::string(value) : "";
27#endif
28}
29
30// NOLINTNEXTLINE(cert-env33-c)
31inline std::string exec_command(const char* cmd)
32{
33 std::array<char, 128> buffer {};
34 std::string result;
35
36 using PipeCloser = int (*)(FILE*);
37#ifdef MAYAFLUX_PLATFORM_WINDOWS
38 std::unique_ptr<FILE, PipeCloser> pipe(_popen(cmd, "r"), _pclose);
39#else
40 std::unique_ptr<FILE, PipeCloser> pipe(popen(cmd, "r"), pclose);
41#endif
42
43 if (!pipe)
44 return "";
45
46 while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get())) {
47 result += buffer.data();
48 }
49 return result;
50}
51
52inline void trim_output(std::string& str)
53{
54 str.erase(std::remove(str.begin(), str.end(), '\r'), str.end());
55 str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
56 str.erase(str.find_last_not_of(" \t") + 1);
57}
58
59} // namespace MayaFlux::Platform
void trim_output(std::string &str)
Definition Process.hpp:52
std::string safe_getenv(const char *var)
Definition Process.hpp:11
constexpr char PathSeparator
Definition Process.hpp:8
std::string exec_command(const char *cmd)
Definition Process.hpp:31