[SCM] Development for GoFind! branch, master, updated. 94e5470b123fb7bec2fb143dce8cf5a50be36ca0

Miriam Ruiz miriam at debian.org
Wed May 6 01:03:29 UTC 2009


The following commit has been merged in the master branch:
commit 8ebada29467b35c5b9e997531bbc2045c10d23d0
Author: Miriam Ruiz <miriam at debian.org>
Date:   Wed May 6 02:15:08 2009 +0200

    Add function for retrieving package data from apt

diff --git a/apthelper.cpp b/apthelper.cpp
index f21ca45..76fac27 100644
--- a/apthelper.cpp
+++ b/apthelper.cpp
@@ -45,7 +45,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <regex.h>
-#include <stdio.h>
+#include <cstdio>
 
 #include <iomanip>
 
@@ -55,8 +55,12 @@
 
 using namespace std;
 
-AptHelper::AptHelper(int argc,const char *argv[]) : Init(false), GCache(NULL), SrcList(NULL), Map(NULL)
+bool AptHelper::Init = false;
+
+AptHelper::AptHelper(int argc,const char *argv[]) : GCache(NULL), SrcList(NULL), Map(NULL)
 {
+	if (Init) return;
+
 	CommandLine::Args Args[] =
 	{
 		{0, "pkg-cache",        "Dir::Cache::pkgcache",       CommandLine::HasArg},
@@ -121,6 +125,8 @@ AptHelper::AptHelper(int argc,const char *argv[]) : Init(false), GCache(NULL), S
 
 AptHelper::~AptHelper()
 {
+	if (!Init) return;
+
 	if (Map)
 	{
 		delete Map;
@@ -133,10 +139,14 @@ AptHelper::~AptHelper()
 		//bool Errors = _error->PendingError();
 		_error->DumpErrors();
 	}
+
+	Init = false;
 }
 
-bool AptHelper::ShowPackage(const char *package)
+bool AptHelper::GetPackageInfo(const char *package_name, AptHelper::PackageInfo *info)
 {
+	info->Clean();
+
 	if (!IsInit() || !GCache || _error->PendingError() != false)
 		return false;
 
@@ -145,10 +155,10 @@ bool AptHelper::ShowPackage(const char *package)
 
 	unsigned found = 0;
 
-	pkgCache::PkgIterator Pkg = Cache.FindPkg(package);
+	pkgCache::PkgIterator Pkg = Cache.FindPkg(package_name);
 	if (Pkg.end() == true)
 	{
-		_error->Warning(_("Unable to locate package %s"),package);
+		_error->Warning(_("Unable to locate package %s"), package_name);
 		return _error->Error(_("No packages found"));;
 	}
 
@@ -160,12 +170,14 @@ bool AptHelper::ShowPackage(const char *package)
 		pkgCache::VerIterator V;
 		for (V = Pkg.VersionList(); V.end() == false; V++)
 		{
-		// Show the right description
-		pkgRecords Recs(*GCache);
-		pkgCache::DescIterator Desc = V.TranslatedDescription();
-		pkgRecords::Parser &P = Recs.Lookup(Desc.FileList());
-		cout << "Description" << ( (strcmp(Desc.LanguageCode(),"") != 0) ? "-" : "" ) << Desc.LanguageCode() << ": " << P.LongDesc() << std::endl;
-		break;
+			// Show the right description
+			pkgRecords Recs(*GCache);
+			pkgCache::DescIterator Desc = V.TranslatedDescription();
+			pkgRecords::Parser &P = Recs.Lookup(Desc.FileList());
+			info->LongDescription = P.LongDesc();
+			info->LanguageCode = Desc.LanguageCode();
+			info->Name = package_name;
+			break;
 		}
 	}
 	else
@@ -178,7 +190,9 @@ bool AptHelper::ShowPackage(const char *package)
 		pkgRecords Recs(*GCache);
 		pkgCache::DescIterator Desc = V.TranslatedDescription();
 		pkgRecords::Parser &P = Recs.Lookup(Desc.FileList());
-		cout << "Description" << ( (strcmp(Desc.LanguageCode(),"") != 0) ? "-" : "" ) << Desc.LanguageCode() << ": " << P.LongDesc() << std::endl;
+		info->LongDescription = P.LongDesc();
+		info->LanguageCode = Desc.LanguageCode();
+		info->Name = package_name;
 	}
 
 	if (found > 0)
@@ -186,6 +200,17 @@ bool AptHelper::ShowPackage(const char *package)
 	return _error->Error(_("No packages found"));
 }
 
+bool AptHelper::ShowPackage(const char *package)
+{
+	PackageInfo info;
+	if (!GetPackageInfo(package, &info))
+		return false;
+	cout << "Description"
+		<< ( (strcmp(info.LanguageCode.c_str(),"") != 0) ? "-" : "" ) << info.LanguageCode
+		<< ": " << info.LongDescription << std::endl;
+	return true;
+}
+
 int AptHelper::LocalityCompare(const void *a, const void *b)
 {
 	pkgCache::VerFile *A = *(pkgCache::VerFile **)a;
@@ -203,12 +228,6 @@ int AptHelper::LocalityCompare(const void *a, const void *b)
 	return A->File - B->File;
 }
 
-void AptHelper::CacheInitialize()
-{
-	_config->Set("quiet",0);
-	_config->Set("help",false);
-}
-
 #ifdef UNIT_TEST
 TEST_FUNCTION TestCuAptHelper(CuTest* tc)
 {
diff --git a/apthelper.h b/apthelper.h
index 6d635ef..125ec5b 100644
--- a/apthelper.h
+++ b/apthelper.h
@@ -20,6 +20,7 @@
 #define _games_apthelper_h
 
 #include <cstdlib>
+#include <string>
 
 class pkgCache;
 class pkgSourceList;
@@ -28,18 +29,31 @@ class MMap;
 class AptHelper
 {
 public:
+	struct PackageInfo {
+		std::string Name;
+		std::string ShortDescription;
+		std::string LongDescription;
+		std::string LanguageCode;
+		void Clean()
+		{
+			Name="";
+			ShortDescription="";
+			LongDescription="";
+			LanguageCode="";
+		}
+	};
+
 	AptHelper(int argc,const char *argv[]);
 	~AptHelper();
 
 	inline bool IsInit() { return Init; }
+	bool GetPackageInfo(const char *package_name, PackageInfo *info);
 	bool ShowPackage(const char *package);
 
 	static int LocalityCompare(const void *a, const void *b);
 
 private:
-	static void CacheInitialize();
-
-	bool Init;
+	static bool Init;
 
 	pkgCache *GCache;
 	pkgSourceList *SrcList;

-- 
Development for GoFind!



More information about the Pkg-games-commits mailing list