From ee3d7613f2f519373a64c89899905da195918330 Mon Sep 17 00:00:00 2001
From: Nagy Gabor <ngaba@bibl.u-szeged.hu>
Date: Mon, 19 Nov 2007 15:37:10 +0100
Subject: [PATCH] Remove alpm_dep_get_string usage from libalpm

We use pmdepmodstr char*-vector instead which can be indexed with pmdepmod_t

Signed-off-by: Nagy Gabor <ngaba@bibl.u-szeged.hu>
---
 lib/libalpm/be_files.c |    5 ++---
 lib/libalpm/deps.c     |   38 +++++++++-----------------------------
 lib/libalpm/deps.h     |    1 +
 lib/libalpm/sync.c     |    7 +++----
 4 files changed, 15 insertions(+), 36 deletions(-)

diff --git a/lib/libalpm/be_files.c b/lib/libalpm/be_files.c
index 61c0c0e..b3c6c6c 100644
--- a/lib/libalpm/be_files.c
+++ b/lib/libalpm/be_files.c
@@ -672,9 +672,8 @@ int _alpm_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
 		if(info->depends) {
 			fputs("%DEPENDS%\n", fp);
 			for(lp = info->depends; lp; lp = lp->next) {
-				char *depstring = alpm_dep_get_string(lp->data);
-				fprintf(fp, "%s\n", depstring);
-				free(depstring);
+				pmdepend_t *dep = lp->data;
+				fprintf(fp, "%s%s%s\n", dep->name, pmdepmodstr[dep->mod], dep->version);
 			}
 			fprintf(fp, "\n");
 		}
diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c
index 5d88c37..ed15ae1 100644
--- a/lib/libalpm/deps.c
+++ b/lib/libalpm/deps.c
@@ -38,6 +38,8 @@
 #include "cache.h"
 #include "handle.h"
 
+const char *pmdepmodstr[] = {"", "", "=", ">=", "<="};
+
 static pmgraph_t *_alpm_graph_new(void)
 {
 	pmgraph_t *graph = NULL;
@@ -662,10 +664,8 @@ int _alpm_resolvedeps(pmdb_t *local, alpm_list_t *dbs_sync, pmpkg_t *syncpkg,
 		for(j = *list; j && !found; j = j->next) {
 			pmpkg_t *sp = j->data;
 			if(alpm_depcmp(sp, missdep)) {
-				char *missdepstring = alpm_dep_get_string(missdep);
-				_alpm_log(PM_LOG_DEBUG, "%s satisfies dependency %s -- skipping\n",
-				          alpm_pkg_get_name(sp), missdepstring);
-				free(missdepstring);
+				_alpm_log(PM_LOG_DEBUG, "%s satisfies dependency %s%s%s -- skipping\n",
+				          alpm_pkg_get_name(sp), missdep->name, pmdepmodstr[missdep->mod], missdep->version);
 				found = 1;
 			}
 		}
@@ -713,10 +713,8 @@ int _alpm_resolvedeps(pmdb_t *local, alpm_list_t *dbs_sync, pmpkg_t *syncpkg,
 		}
 
 		if(!found) {
-			char *missdepstring = alpm_dep_get_string(missdep);
-			_alpm_log(PM_LOG_ERROR, _("cannot resolve \"%s\", a dependency of \"%s\"\n"),
-			          missdepstring, miss->target);
-			free(missdepstring);
+			_alpm_log(PM_LOG_ERROR, _("cannot resolve \"%s%s%s\", a dependency of \"%s\"\n"),
+			          missdep->name, pmdepmodstr[missdep->mod], missdep->version, miss->target);
 			if(data) {
 				if((miss = malloc(sizeof(pmdepmissing_t))) == NULL) {
 					_alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %zd bytes\n"), sizeof(pmdepmissing_t));
@@ -807,7 +805,7 @@ const char SYMEXPORT *alpm_dep_get_version(const pmdepend_t *dep)
  */
 char SYMEXPORT *alpm_dep_get_string(const pmdepend_t *dep)
 {
-	char *opr, *str = NULL;
+	char *str = NULL;
 	size_t len;
 
 	ALPM_LOG_FUNC;
@@ -815,29 +813,11 @@ char SYMEXPORT *alpm_dep_get_string(const pmdepend_t *dep)
 	/* Sanity checks */
 	ASSERT(dep != NULL, return(NULL));
 
-	switch(dep->mod) {
-		case PM_DEP_MOD_ANY:
-			opr = "";
-			break;
-		case PM_DEP_MOD_GE:
-			opr = ">=";
-			break;
-		case PM_DEP_MOD_LE:
-			opr = "<=";
-			break;
-		case PM_DEP_MOD_EQ:
-			opr = "=";
-			break;
-		default:
-			opr = "";
-			break;
-	}
-
 	/* we can always compute len and print the string like this because opr
 	 * and ver will be empty when PM_DEP_MOD_ANY is the depend type */
-	len = strlen(dep->name) + strlen(opr) + strlen(dep->version) + 1;
+	len = strlen(dep->name) + strlen(dep->version) + 3;
 	MALLOC(str, len, RET_ERR(PM_ERR_MEMORY, NULL));
-	snprintf(str, len, "%s%s%s", dep->name, opr, dep->version);
+	snprintf(str, len, "%s%s%s", dep->name, pmdepmodstr[dep->mod], dep->version);
 
 	return(str);
 }
diff --git a/lib/libalpm/deps.h b/lib/libalpm/deps.h
index a736f70..ff8c3a7 100644
--- a/lib/libalpm/deps.h
+++ b/lib/libalpm/deps.h
@@ -34,6 +34,7 @@ struct __pmdepend_t {
 	char name[PKG_NAME_LEN];
 	char version[PKG_VERSION_LEN];
 };
+extern const char *pmdepmodstr[];
 
 /* Missing dependency */
 struct __pmdepmissing_t {
diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c
index e4a7799..9b8282b 100644
--- a/lib/libalpm/sync.c
+++ b/lib/libalpm/sync.c
@@ -661,10 +661,9 @@ int _alpm_sync_prepare(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync
 						pmsyncpkg_t *sp = l->data;
 						pmpkg_t *sppkg = sp->pkg;
 						if(alpm_depcmp(sppkg, &(miss->depend))) {
-							char *missdepstring = alpm_dep_get_string(&(miss->depend));
-							_alpm_log(PM_LOG_DEBUG, "sync: dependency '%s' satisfied by package '%s'\n",
-									missdepstring, alpm_pkg_get_name(sppkg));
-							free(missdepstring);
+							pmdepend_t *missdep = &(miss->depend);
+							_alpm_log(PM_LOG_DEBUG, "sync: dependency '%s%s%s' satisfied by package '%s'\n",
+									missdep->name, pmdepmodstr[missdep->mod], missdep->version, alpm_pkg_get_name(sppkg));
 							satisfied = 1;
 						}
 					}
-- 
1.5.2.5

