从C++中的管道获取 Gnuplot 版本

Get Gnuplot version from pipe in C++

本文关键字:获取 Gnuplot 版本 管道 C++      更新时间:2023-10-16

在我的C++程序中(在Linux中(,我可以打开一个管道进行写入并为Gnuplot程序设置值。

FILE *pipe = NULL;
#ifdef WIN32
    pipe = _popen("pgnuplot -persist", "w");
#else
    pipe = popen("gnuplot", "w");
#endif
if(pipe == NULL)
    error("Could not open pipe for write!");
// set title name
fprintf(pipe, "set title 'Sample Points' n");

现在我需要获得 Gnuplot 版本。show version命令执行此操作,但我如何发送此命令然后读取值。打开管道进行读取似乎对我不起作用,代码卡在 while 循环中而没有获得任何数据。

FILE* pipe = popen(command, "r");
if (!pipe)
{
    std::cout << "failed! (can not open pipe)" << endl;
    return;
}
char buffer[128];
std::string result = "";
while(!feof(pipe))
{
    if(fgets(buffer, 128, pipe) != NULL)
        result += buffer;
}
pclose(pipe);

由于在我的 Debian/Linux/Sid/x86-64 上,命令gnuplot --version输出到stdout以下行:

 gnuplot 5.0 patchlevel 1

我只是推荐

FILE* pipversion = popen("gnuplot --version", "r");
if (!pipversion) { perror("popen gnuplot"); exit(EXIT_FAILURE); };
char lineversion[128];
memset (lineversion, 0, sizeof(lineversion));
if (!fgets(lineversion, sizeof(lineversion), pipversion) { 
    perror("fgets"); exit(EXIT_FAILURE);
}
/// lineversion is like: gnuplot 5.0 patchlevel 1
int majvers=0, minvers=0, pos= -1;
char* restvers = NULL;
if (sscanf(lineversion, "gnuplot %d.%d %n", &majvers, &minvers, &pos) >= 2) {
  assert (pos>=0);
  restvers = lineversion+pos;
};
pclose(pipversion);
pipversion = NULL;

之后,majvers包含gnuplot的主要版本(例如在我的例子中为 5(,minvers包含次要版本(例如 0(,restvers是一个后缀字符串(例如 "patchlevel 1"没有引号(。

在异常且不太可能的情况下,可能存在潜在的争用条件,即在此popen和下一个pipe = popen("gnuplot", "w");之间更新gnuplot。顺便说一下,命名变量pipe是糟糕的,因为 POSIX 和 Linux 都有 pipe(2( 系统调用。但我认为不值得关心这种竞争条件。

顺便说一句,你很可能想用 pipe(2( 的显式双重调用替换你的第二个pipe = popen("gnuplot", "w");(后跟适当的 fork(2( 和 execvp(3( ...(,以便同时gnuplot输入和输出管道,并在你自己的事件循环中管理它们(可能围绕 poll(2( ...看到这个和那个答案(。

(如果你的应用程序有或使用自己的事件循环,特别是如果它是一个高于Qt或GTK的GUI应用程序,你希望对管道使用相同的事件循环;细节特定于提供该事件循环的库:g_spawn_async_with_pipes & g_source_add_unix_fd for GTK, QQ for Qt ...

我没有时间详细解释如何做到这一点(双重管道进入命令+事件循环(,但高级Linux编程书籍(在线提供(有几章关于这一点。请注意,您需要一些事件循环。