diff options
-rw-r--r-- | libopkg/pkg.c | 25 | ||||
-rw-r--r-- | libopkg/pkg_depends.c | 93 | ||||
-rw-r--r-- | libopkg/pkg_depends.h | 1 |
3 files changed, 71 insertions, 48 deletions
diff --git a/libopkg/pkg.c b/libopkg/pkg.c index 89f47a9..e37806b 100644 --- a/libopkg/pkg.c +++ b/libopkg/pkg.c @@ -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"); } diff --git a/libopkg/pkg_depends.c b/libopkg/pkg_depends.c index f7f05a1..67924bd 100644 --- a/libopkg/pkg_depends.c +++ b/libopkg/pkg_depends.c @@ -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; } /* diff --git a/libopkg/pkg_depends.h b/libopkg/pkg_depends.h index 32461f6..05ee1c4 100644 --- a/libopkg/pkg_depends.h +++ b/libopkg/pkg_depends.h @@ -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 |