diff --git a/code/nel/include/nel/misc/common.h b/code/nel/include/nel/misc/common.h index c2a40cc0f..10f6a97e6 100644 --- a/code/nel/include/nel/misc/common.h +++ b/code/nel/include/nel/misc/common.h @@ -349,6 +349,9 @@ std::string formatThousands(const std::string& s); /// The program will be launched in the current directory bool launchProgram (const std::string &programName, const std::string &arguments, bool log = true); +/// This function executes a program and returns output as a string +std::string getCommandOutput(const std::string &command); + /// This function kills a program using his pid (on unix, it uses the kill() POSIX function) bool killProgram(uint32 pid); diff --git a/code/nel/src/misc/common.cpp b/code/nel/src/misc/common.cpp index 8eca1d566..d2d2e5cb0 100644 --- a/code/nel/src/misc/common.cpp +++ b/code/nel/src/misc/common.cpp @@ -23,6 +23,10 @@ # include # include # include + +#define popen _popen +#define pclose _pclose + #elif defined NL_OS_MAC # include #elif defined NL_OS_UNIX @@ -32,6 +36,8 @@ # include #endif +#define MAX_LINE_WIDTH 256 + #include "nel/misc/command.h" #include "nel/misc/path.h" #include "nel/misc/i18n.h" @@ -840,6 +846,25 @@ bool launchProgram(const std::string &programName, const std::string &arguments, } +std::string getCommandOutput(const std::string &command) +{ + FILE *pipe = popen(command.c_str(), "r"); + + if (!pipe) return ""; + + char buffer[MAX_LINE_WIDTH]; + std::string result; + + while (!feof(pipe)) + { + if (fgets(buffer, MAX_LINE_WIDTH, pipe) != NULL) result += buffer; + } + + pclose(pipe); + + return result; +} + /* * Display the bits (with 0 and 1) composing a byte (from right to left) */