Changed: Added new function getCommandOutput

This commit is contained in:
kervala 2015-12-25 15:29:23 +01:00
parent 36fc5a3839
commit ba706b46f7
2 changed files with 28 additions and 0 deletions

View file

@ -349,6 +349,9 @@ std::string formatThousands(const std::string& s);
/// The program will be launched in the current directory /// The program will be launched in the current directory
bool launchProgram (const std::string &programName, const std::string &arguments, bool log = true); 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) /// This function kills a program using his pid (on unix, it uses the kill() POSIX function)
bool killProgram(uint32 pid); bool killProgram(uint32 pid);

View file

@ -23,6 +23,10 @@
# include <ShellAPI.h> # include <ShellAPI.h>
# include <io.h> # include <io.h>
# include <tchar.h> # include <tchar.h>
#define popen _popen
#define pclose _pclose
#elif defined NL_OS_MAC #elif defined NL_OS_MAC
# include <ApplicationServices/ApplicationServices.h> # include <ApplicationServices/ApplicationServices.h>
#elif defined NL_OS_UNIX #elif defined NL_OS_UNIX
@ -32,6 +36,8 @@
# include <sched.h> # include <sched.h>
#endif #endif
#define MAX_LINE_WIDTH 256
#include "nel/misc/command.h" #include "nel/misc/command.h"
#include "nel/misc/path.h" #include "nel/misc/path.h"
#include "nel/misc/i18n.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) * Display the bits (with 0 and 1) composing a byte (from right to left)
*/ */