From 139fb3b758dd0f212df03f612f7860a0d87c7683 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 26 Jul 2019 22:26:21 +0200 Subject: [PATCH] [DEV] add external exec component --- etk-core/system.cpp | 30 ++++++++++++++++++++++++++++++ etk-core/system.hpp | 14 ++++++++++++++ lutin_etk-core.py | 4 +++- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 etk-core/system.cpp create mode 100644 etk-core/system.hpp diff --git a/etk-core/system.cpp b/etk-core/system.cpp new file mode 100644 index 0000000..346b69f --- /dev/null +++ b/etk-core/system.cpp @@ -0,0 +1,30 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license MPL v2.0 (see license file) + */ +#include +#include +#include + +etk::String etk::exec(const etk::String& _cmd) { + FILE* pipe = popen(_cmd.c_str(), "r"); + if (!pipe) { + ETK_THROW_EXCEPTION(etk::exception::RuntimeError("popen() failed!")); + return ""; + } + etk::String out; + try { + char buffer[128]; + while (fgets(buffer, 128, pipe) != NULL) { + out += buffer; + } + } catch (...) { + pclose(pipe); + ETK_THROW_EXCEPTION(etk::exception::RuntimeError("Broken pipe")); + } + pclose(pipe); + return out; +} + + diff --git a/etk-core/system.hpp b/etk-core/system.hpp new file mode 100644 index 0000000..0fd1d93 --- /dev/null +++ b/etk-core/system.hpp @@ -0,0 +1,14 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license MPL v2.0 (see license file) + */ + +#include + +#pragma once + +namespace etk { + etk::String exec(const etk::String& _cmd); +} + diff --git a/lutin_etk-core.py b/lutin_etk-core.py index b391815..9e57dae 100644 --- a/lutin_etk-core.py +++ b/lutin_etk-core.py @@ -37,6 +37,7 @@ def configure(target, my_module): 'etk-core/Allocator.cpp', 'etk-core/typeInfo.cpp', 'etk-core/Exception.cpp', + 'etk-core/system.cpp', ]) my_module.add_header_file([ @@ -58,7 +59,8 @@ def configure(target, my_module): 'etk-core/Function.hpp', 'etk-core/NullPtr.hpp', 'etk-core/typeInfo.hpp', - 'etk-core/Exception.hpp' + 'etk-core/Exception.hpp', + 'etk-core/system.hpp', ], 'etk') # build in C++ mode