CBuild
C++ build system with scripts written in c++
Loading...
Searching...
No Matches
system.cpp
Go to the documentation of this file.
1
21// C++ libraries
22#include "map"
23#include "stdlib.h"
24#include "string"
25#include "vector"
26// Project headers
30/* namespace CBuild */
31namespace CBuild {
32std::vector<std::string> log;
33std::map<std::string, std::string> env_vars;
34bool enabled = true;
35std::string get_env_vars() {
36 std::string ret = "";
37 for (auto elem : CBuild::env_vars) {
38 ret += elem.first;
39 ret += std::string("=");
40 ret += elem.second;
41 ret += std::string("; ");
42 }
43 return ret;
44}
45} // namespace CBuild
46/* system.hpp */
47int CBuild::system(std::string cmd) {
48 // CBuild::log.push_back(cmd);
49 int ret = -1;
50 if (CBuild::enabled) {
52 ret = std::system((CBuild::get_env_vars() + cmd).c_str());
53 } else {
54 ret = 0;
55 }
56 return ret;
57}
58std::string CBuild::system_piped(std::string cmd, unsigned int buffsize) {
59 CBuild::log.push_back(cmd);
60 if (CBuild::enabled) {
62 std::string ret = "";
63 char* buffer = (char*)malloc(buffsize);
64 FILE* pipe = popen((CBuild::get_env_vars() + cmd).c_str(), "r");
65 size_t bytesread;
66 while ((bytesread = fread(buffer, sizeof(buffer[0]), sizeof(buffer), pipe)) != 0) {
67 ret += std::string(buffer, bytesread);
68 }
69 pclose(pipe);
70 return ret;
71 }
72 return "";
73}
74std::vector<std::string>* CBuild::get_log() {
75 return &CBuild::log;
76}
78 CBuild::enabled = false;
79}
81 CBuild::enabled = true;
82}
86/* environment.hpp */
87void CBuild::add_env_var(std::string var, std::string value) {
88 CBuild::env_vars.insert(std::make_pair(var, value));
89}
90std::string CBuild::remove_env_var(std::string var) {
91 std::string ret = CBuild::env_vars.at(var);
92 CBuild::env_vars.erase(var);
93 return ret;
94}
95void CBuild::change_env_var(std::string var, std::string new_value) {
96 CBuild::env_vars.at(var) = new_value;
97}
Set environment variables.
Filebuffer for CBuild ecosystem.
Definition Build.hpp:34
std::string remove_env_var(std::string var)
Remove registered environment var.
Definition system.cpp:90
bool is_system_enabled()
Check if CBuild::system enabled.
Definition system.cpp:83
void change_env_var(std::string var, std::string new_value)
Change value for environment var.
Definition system.cpp:95
void print(std::string msg, color fg=CBuild::WHITE)
Print colored text to STDOUT.
Definition print.cpp:70
@ BLUE
Definition print.hpp:37
std::vector< std::string > * get_log()
Get the internall command log.
Definition system.cpp:74
bool enabled
Definition system.cpp:34
void enable_system()
Reanable system commands execution, used only in pkg-config.
Definition system.cpp:80
std::string get_env_vars()
Definition system.cpp:35
void disable_system()
Disable system commands execution.
Definition system.cpp:77
void add_env_var(std::string var, std::string value)
Add environment variable to CBuild::system and CBuild::system_piped calls.
Definition system.cpp:87
int system(std::string cmd)
Call stdlib system() and print cmd to shell.
Definition system.cpp:47
std::string system_piped(std::string cmd, unsigned int buffsize)
Execute command and return it's output.
Definition system.cpp:58
std::vector< std::string > log
Definition system.cpp:32
std::map< std::string, std::string > env_vars
Definition system.cpp:33
Custom print that support color codes.
Command for compile_commands.json.
Custom system() wraper.