qpkg: moved comp_versions from prereq.c to id.c

- id.c: added section titles
- prereq.c (comp_versions), id.h, id.c: moved comp_versions from prereq.c
  to id.c, to allow sharing with other code
This commit is contained in:
Werner Almesberger 2010-11-21 21:14:04 -03:00
parent a0c0e854cc
commit dd8bb6eecd
3 changed files with 70 additions and 62 deletions

View File

@ -12,6 +12,7 @@
#include <string.h>
#include <ctype.h>
#include "jrb.h"
@ -22,6 +23,9 @@
static struct id *free_id = NULL;
/* ----- ID comparison (regular string compare) ---------------------------- */
static int do_comp_id(const struct id *a, const struct id *b)
{
int len = a->len < b->len ? a->len : b->len;
@ -39,6 +43,71 @@ int comp_id(const void *a, const void *b)
return do_comp_id(a, b);
}
/* ----- Version comparison ------------------------------------------------ */
static int epoch(const char **s, const struct id *id)
{
const char *end = id->s+id->len;
int n = 0;
while (*s != end && isdigit(**s)) {
n = 10*n+**s-'0';
(*s)++;
}
if (*s == id->s || (*s != end && **s == ':'))
return n;
*s = id->s;
return 0;
}
int comp_versions(const struct id *va, const struct id *vb)
{
int epoch_a = 0, epoch_b = 0;
const char *a = va->s;
const char *b = vb->s;
const char *ea = a+va->len;
const char *eb = b+vb->len;
epoch_a = epoch(&a, va);
epoch_b = epoch(&b, vb);
if (epoch_a != epoch_b)
return epoch_a-epoch_b;
while (1) {
if (a == ea || b == eb)
return (a != ea)-(b != eb);
if (isdigit(*a) && isdigit(*b)) {
int na = 0, nb = 0;
while (a != ea && isdigit(*a)) {
na = na*10+*a-'0';
a++;
}
while (b != eb && isdigit(*b)) {
nb = nb*10+*b-'0';
b++;
}
if (na == nb)
continue;
return na-nb;
}
if (*a > *b)
return 1;
if (*a < *b)
return 1;
a++;
b++;
}
}
/* ----- Tree maintenance -------------------------------------------------- */
struct tree *make_tree(int (*comp)(const void *a, const void *b))
{
struct tree *tree;

View File

@ -68,6 +68,7 @@ struct id {
int comp_id(const void *a, const void *b);
int comp_versions(const struct id *va, const struct id *vb);
struct tree *make_tree(int (*comp)(const void *a, const void *b));
struct jrb *make_id(struct tree *tree, const char *s, size_t len);

View File

@ -14,7 +14,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "id.h"
@ -41,67 +40,6 @@ static int n_install = 0;
static int install_max = 0;
/* ----- Version comparison ------------------------------------------------ */
static int epoch(const char **s, const struct id *id)
{
const char *end = id->s+id->len;
int n = 0;
while (*s != end && isdigit(**s)) {
n = 10*n+**s-'0';
(*s)++;
}
if (*s == id->s || (*s != end && **s == ':'))
return n;
*s = id->s;
return 0;
}
static int comp_versions(const struct id *va, const struct id *vb)
{
int epoch_a = 0, epoch_b = 0;
const char *a = va->s;
const char *b = vb->s;
const char *ea = a+va->len;
const char *eb = b+vb->len;
epoch_a = epoch(&a, va);
epoch_b = epoch(&b, vb);
if (epoch_a != epoch_b)
return epoch_a-epoch_b;
while (1) {
if (a == ea || b == eb)
return (a != ea)-(b != eb);
if (isdigit(*a) && isdigit(*b)) {
int na = 0, nb = 0;
while (a != ea && isdigit(*a)) {
na = na*10+*a-'0';
a++;
}
while (b != eb && isdigit(*b)) {
nb = nb*10+*b-'0';
b++;
}
if (na == nb)
continue;
return na-nb;
}
if (*a > *b)
return 1;
if (*a < *b)
return 1;
a++;
b++;
}
}
/* ----- List of packages considered for installation ---------------------- */