我一直在编写一个程序,该程序需要2.6或更高版本的wine。我想把它作为布尔值,所以我一直在尝试使用下面的代码:
#include <string>
#include <iostream>
#include "WineCheck.h"
#include <cstdlib>
using namespace std;
bool checkForWine()
{
// Create variables for checking wine state
bool wineIsThere = false;
bool wineIsVersion = false;
// Check dpkg if wine is there
if (string(system("dpkg -l | cut -c 4-9 | grep \ wine\ ")) == " wine ")
{
wineIsThere = true;
// Check version
if (double(system("wine --version | cut -c 6-8")) >= 2.6)
wineIsVersion = true;
}
// Return
if (wineIsThere && wineIsVersion)
return true;
else
return false;
}
首先,我认为你不应该为此而烦恼。Wine 2.6应该只是作为一个依赖项包含在您的配置脚本和/或包文件中。如果你想保持对其他不使用该软件包的GNU/Linux发行版的可移植性,那么在程序源代码中针对特定的软件包管理系统不是一个好主意。
回答你的问题。我发现有两种方法可以做到这一点。您可以检查/var/lib/dpkg/status
。逐行通读文件,直到读到这一节为止。如果您找不到该部分,或者Status: ...
行没有显示installed
,则不会安装wine。Version: ...
行将告诉您已安装的版本。我通过在Debian Wheezy上安装和卸载Wine验证了这种方法的有效性。你没有说你使用的是什么发行版,但很明显你使用的Debian打包系统,所以这应该也适用于Ubuntu和其他基于Debian的发行版。
$cat /var/lib/dpkg/status
...
Package: wine
Status: install ok installed
Priority: optional
Section: otherosf
Installed-Size: 80
Maintainer: Debian Wine Party <pkg-wine-party@lists.alioth.debian.org>
Architecture: amd64
Version: 1.4.1-4
...
另一个选项是使用libdpkg
。我发现一个示例列出了所有已安装的软件包。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/pkg-array.h>
#include "filesdb.h"
const char thisname[] = "example1";
int
main(int argc, const char *const *argv)
{
struct pkg_array array;
struct pkginfo *pkg;
int i;
enum modstatdb_rw msdb_status;
standard_startup();
filesdbinit();
msdb_status = modstatdb_open(msdbrw_readonly);
pkg_infodb_init(msdb_status);
pkg_array_init_from_db(&array);
pkg_array_sort(&array, pkg_sorter_by_name);
for (i = 0; i < array.n_pkgs; i++) {
pkg = array.pkgs[i];
if (pkg->status == stat_notinstalled)
continue;
printf("Package --> %sn", pkg->set->name);
}
pkg_array_destroy(&array);
modstatdb_shutdown();
standard_shutdown();
}