This is done as a preparation to better handle optdepends.
This commit should not change pacman's behaviour, as it simply adds new
functions and data structures and doesn't yet hook them up anywhere.

Signed-off-by: Benedikt Morbach <[email protected]>
---
 lib/libalpm/alpm.h |   12 +++++++++
 lib/libalpm/deps.c |   68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/libalpm/deps.h |    3 ++
 3 files changed, 83 insertions(+), 0 deletions(-)

diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index cd124e5..4587148 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -147,6 +147,12 @@ typedef struct _alpm_depend_t {
        alpm_depmod_t mod;
 } alpm_depend_t;
 
+/** Optional dependency */
+typedef struct _alpm_optdepend_t {
+       alpm_depend_t *depend;
+       char *description;
+} alpm_optdepend_t;
+
 /** Missing dependency */
 typedef struct _alpm_depmissing_t {
        char *target;
@@ -1090,6 +1096,12 @@ alpm_list_t *alpm_checkconflicts(alpm_handle_t *handle, 
alpm_list_t *pkglist);
  */
 char *alpm_dep_compute_string(const alpm_depend_t *dep);
 
+/** Returns a newly allocated string representing the optional dependency 
information.
+ * @param dep a optional dependency info structure
+ * @return a formatted string, e.g. "sqlite: for Database support"
+ */
+char *alpm_optdep_compute_string(const alpm_optdepend_t *optdep);
+
 /** @} */
 
 /** @} */
diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c
index 0da20c1..b6aba20 100644
--- a/lib/libalpm/deps.c
+++ b/lib/libalpm/deps.c
@@ -44,6 +44,13 @@ void _alpm_dep_free(alpm_depend_t *dep)
        FREE(dep);
 }
 
+void _alpm_optdep_free(alpm_optdepend_t *optdep)
+{
+       _alpm_dep_free(optdep->depend);
+       FREE(optdep->description);
+       FREE(optdep);
+}
+
 static alpm_depmissing_t *depmiss_new(const char *target, alpm_depend_t *dep,
                const char *causingpkg)
 {
@@ -473,6 +480,44 @@ alpm_depend_t *_alpm_dep_dup(const alpm_depend_t *dep)
        return newdep;
 }
 
+alpm_optdepend_t *_alpm_splitoptdep(const char *optstring)
+{
+       alpm_optdepend_t *optdep;
+       char *depstring;
+       const char *ptr;
+
+       ASSERT(optstring != NULL, return NULL);
+
+       CALLOC(optdep, 1, sizeof(alpm_optdepend_t), return NULL);
+
+       /* Note the extra space in ": " to avoid matching the epoch */
+       if((ptr = strstr(optstring, ": ")) == NULL) {
+               ptr = optstring + strlen(optstring);
+       }
+
+       STRNDUP(depstring, optstring, ptr - optstring, return NULL);
+       optdep->depend = _alpm_splitdep(depstring);
+       FREE(depstring);
+
+       if(*ptr != '\0') {
+               STRDUP(optdep->description, ptr + 2, return NULL);
+               optdep->description = _alpm_strtrim(optdep->description);
+       }
+
+       return optdep;
+}
+
+alpm_optdepend_t *_alpm_optdep_dup(const alpm_optdepend_t *optdep)
+{
+       alpm_optdepend_t *newdep;
+       CALLOC(newdep, 1, sizeof(alpm_optdepend_t), return NULL);
+
+       newdep->depend = _alpm_dep_dup(optdep->depend);
+       STRDUP(newdep->description, optdep->description, return NULL);
+
+       return newdep;
+}
+
 /* These parameters are messy. We check if this package, given a list of
  * targets and a db is safe to remove. We do NOT remove it if it is in the
  * target list, or if if the package was explictly installed and
@@ -868,4 +913,27 @@ char SYMEXPORT *alpm_dep_compute_string(const 
alpm_depend_t *dep)
 
        return str;
 }
+
+/** Reverse of splitoptdep; make a optdep string from a alpm_optdepend_t 
struct.
+ * The string must be freed!
+ * @param optdep the optdepend to turn into a string
+ * @return a string-formatted optional dependency with description
+ */
+char SYMEXPORT *alpm_optdep_compute_string(const alpm_optdepend_t *optdep)
+{
+       ASSERT(optdep != NULL, return NULL);
+
+       char *depstring = alpm_dep_compute_string(optdep->depend);
+
+       if(optdep->description != NULL) {
+               char *str;
+               size_t len = strlen(depstring) + strlen(optdep->description) + 
3;
+               MALLOC(str, len, return NULL);
+               snprintf(str, len, "%s: %s", depstring, optdep->description);
+               free(depstring);
+               return str;
+       }
+
+       return depstring;
+}
 /* vim: set ts=2 sw=2 noet: */
diff --git a/lib/libalpm/deps.h b/lib/libalpm/deps.h
index ce25bda..69b65df 100644
--- a/lib/libalpm/deps.h
+++ b/lib/libalpm/deps.h
@@ -28,7 +28,9 @@
 #include "alpm.h"
 
 void _alpm_dep_free(alpm_depend_t *dep);
+void _alpm_optdep_free(alpm_optdepend_t *optdep);
 alpm_depend_t *_alpm_dep_dup(const alpm_depend_t *dep);
+alpm_optdepend_t *_alpm_optdep_dup(const alpm_optdepend_t *optdep);
 void _alpm_depmiss_free(alpm_depmissing_t *miss);
 alpm_list_t *_alpm_sortbydeps(alpm_handle_t *handle, alpm_list_t *targets, int 
reverse);
 int _alpm_recursedeps(alpm_db_t *db, alpm_list_t *targs, int include_explicit);
@@ -36,6 +38,7 @@ int _alpm_resolvedeps(alpm_handle_t *handle, alpm_list_t 
*localpkgs, alpm_pkg_t
                alpm_list_t *preferred, alpm_list_t **packages, alpm_list_t 
*remove,
                alpm_list_t **data);
 alpm_depend_t *_alpm_splitdep(const char *depstring);
+alpm_optdepend_t *_alpm_splitoptdep(const char *optstring);
 int _alpm_depcmp_literal(alpm_pkg_t *pkg, alpm_depend_t *dep);
 int _alpm_depcmp(alpm_pkg_t *pkg, alpm_depend_t *dep);
 
-- 
1.7.6.1


Reply via email to