Fix pkg_depend_str() to not use pkg->depends_str.
authorgraham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Tue, 24 Nov 2009 02:34:55 +0000 (02:34 +0000)
committergraham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Tue, 24 Nov 2009 02:34:55 +0000 (02:34 +0000)
git-svn-id: http://opkg.googlecode.com/svn/trunk@353 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358

libopkg/pkg.c
libopkg/pkg_depends.c
libopkg/pkg_depends.h

index 89f47a9e46445b0646025f27c554dcb5560ba1da..e37806b711753e98563f50d5b848cf549498f2ed 100644 (file)
@@ -530,27 +530,6 @@ void set_flags_from_control(opkg_conf_t *conf, pkg_t *pkg){
      return;
 }
 
-const char*
-constraint_to_str(enum version_constraint c)
-{
-       switch (c) {
-       case NONE:
-               return "";
-       case EARLIER:
-               return "< ";
-       case EARLIER_EQUAL:
-              return "<= ";
-       case EQUAL:
-              return "= ";
-       case LATER_EQUAL:
-             return ">= ";
-       case LATER:
-            return "> ";
-       }
-
-       return "";
-}
-
 void pkg_formatted_field(FILE *fp, pkg_t *pkg, const char *field)
 {
      int i;
@@ -616,7 +595,9 @@ void pkg_formatted_field(FILE *fp, pkg_t *pkg, const char *field)
               if (pkg->depends_count) {
                     fprintf(fp, "Depends:");
                    for(i = 0; i < pkg->depends_count; i++) {
-                        fprintf(fp, "%s %s", i == 0 ? "" : ",", pkg->depends_str[i]);
+                       char *str = pkg_depend_str(pkg, i);
+                       fprintf(fp, "%s %s", i == 0 ? "" : ",", str);
+                       free(str);
                     }
                    fprintf(fp, "\n");
               }
index f7f05a16bcef2dcde69bc048664997717935332f..67924bd75cb46d6e1453b92504873f389ccf8835 100644 (file)
@@ -550,14 +550,13 @@ char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx)
 {
     int count;
     char ** resized;
-    char *depend_str = pkg_depend_str(pkg, ref_ndx);
 
     count = 0;
     while(the_lost && the_lost[count]) count++;
 
     count++;  /* need one to hold the null */
     resized = xrealloc(the_lost, sizeof(char *) * (count + 1));
-    resized[count - 1] = xstrdup(depend_str);
+    resized[count - 1] = pkg_depend_str(pkg, ref_ndx);
     resized[count] = NULL;
     
     return resized;
@@ -670,37 +669,79 @@ void buildDepends(hash_table_t * hash, pkg_t * pkg)
      }
 }
 
+const char*
+constraint_to_str(enum version_constraint c)
+{
+       switch (c) {
+       case NONE:
+               return "";
+       case EARLIER:
+               return "< ";
+       case EARLIER_EQUAL:
+              return "<= ";
+       case EQUAL:
+              return "= ";
+       case LATER_EQUAL:
+             return ">= ";
+       case LATER:
+            return "> ";
+       }
+
+       return "";
+}
+
 /*
- * pkg_depend_string: returns the depends string specified by index.
- *   All 4 kinds of dependences: dependence, pre-dependence, recommend, and suggest are number starting from 0.
- *   [0,npredepends) -> returns pre_depends_str[index]
- *   [npredepends,npredepends+nrecommends) -> returns recommends_str[index]
- *   [npredepends+nrecommends,npredepends+nrecommends+nsuggests) -> returns recommends_str[index]
- *   [npredepends+nrecommends+nsuggests,npredepends+nrecommends+nsuggests+ndepends) -> returns depends_str[index]
+ * Returns a printable string for pkg's dependency at the specified index. The
+ * resultant string must be passed to free() by the caller.
  */
-char *pkg_depend_str(pkg_t *pkg, int index)
+char *
+pkg_depend_str(pkg_t *pkg, int index)
 {
-     if (index < pkg->pre_depends_count) {
-         return pkg->pre_depends_str[index];
-     }
-     index -= pkg->pre_depends_count;
+       int i, len;
+       char *str;
+       compound_depend_t *cdep;
+       depend_t *dep;
 
-     if (index < pkg->depends_count) {
-         return pkg->depends_str[index];
-     }
-     index -= pkg->depends_count;
+       len = 0;
+       cdep = &pkg->depends[index];
 
-     if (index < pkg->recommends_count) {
-         return pkg->recommends_str[index];
-     }
-     index -= pkg->recommends_count;
+       /* calculate string length */
+       for (i=0; i<cdep->possibility_count; i++) {
+               dep = cdep->possibilities[i];
 
-     if (index < pkg->suggests_count) {
-         return pkg->suggests_str[index];
-     }
+               if (i != 0)
+                       len += 3; /* space, pipe, space */
+
+               len += strlen(dep->pkg->name);
+
+               if (dep->constraint != NONE) {
+                       len += 2; /* space, left parenthesis */
+                       len += 3; /* constraint string (<=, >=, etc), space */
+                       len += strlen(dep->version);
+                       len += 1; /* right parenthesis */
+               }
+       }
+
+       str = xmalloc(len + 1); /* +1 for the NULL terminator */
+       str[0] = '\0';
+
+       for (i=0; i<cdep->possibility_count; i++) {
+               dep = cdep->possibilities[i];
+
+               if (i != 0)
+                       strncat(str, " | ", len);
+
+               strncat(str, dep->pkg->name, len);
+
+               if (dep->constraint != NONE) {
+                       strncat(str, " (", len);
+                       strncat(str, constraint_to_str(dep->constraint), len);
+                       strncat(str, dep->version, len);
+                       strncat(str, ")", len);
+               }
+       }
 
-     fprintf(stderr, "pkg_depend_str: index %d out of range for pkg=%s\n", index, pkg->name);
-     return NULL;
+       return str;
 }
 
 /*
index 32461f6bf89d6ddeb656f2c744d1ae3d809280f0..05ee1c40bcaf2beb9c3cc9146be7363d23b21818 100644 (file)
@@ -88,5 +88,6 @@ int pkg_hash_fetch_unsatisfied_dependencies(opkg_conf_t *conf, pkg_t * pkg, pkg_
 pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg);
 int pkg_dependence_satisfiable(opkg_conf_t *conf, depend_t *depend);
 int pkg_dependence_satisfied(opkg_conf_t *conf, depend_t *depend);
+const char* constraint_to_str(enum version_constraint c);
 
 #endif