2008-10-08 06:55:10 +02:00
|
|
|
#include "dirvec.h"
|
2008-10-08 10:49:05 +02:00
|
|
|
#include "directory.h"
|
2008-10-08 06:55:10 +02:00
|
|
|
#include "utils.h"
|
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-10-08 06:55:10 +02:00
|
|
|
static size_t dv_size(struct dirvec *dv)
|
|
|
|
{
|
2008-10-08 10:49:05 +02:00
|
|
|
return dv->nr * sizeof(struct directory *);
|
2008-10-08 06:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Only used for sorting/searching a dirvec, not general purpose compares */
|
|
|
|
static int dirvec_cmp(const void *d1, const void *d2)
|
|
|
|
{
|
2008-10-08 10:49:05 +02:00
|
|
|
const struct directory *a = ((const struct directory * const *)d1)[0];
|
|
|
|
const struct directory *b = ((const struct directory * const *)d2)[0];
|
2008-10-08 06:55:10 +02:00
|
|
|
return strcmp(a->path, b->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dirvec_sort(struct dirvec *dv)
|
|
|
|
{
|
2008-10-08 10:49:05 +02:00
|
|
|
qsort(dv->base, dv->nr, sizeof(struct directory *), dirvec_cmp);
|
2008-10-08 06:55:10 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
struct directory *dirvec_find(struct dirvec *dv, const char *path)
|
2008-10-08 06:55:10 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = dv->nr; --i >= 0; )
|
|
|
|
if (!strcmp(dv->base[i]->path, path))
|
|
|
|
return dv->base[i];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
int dirvec_delete(struct dirvec *dv, struct directory *del)
|
2008-10-08 06:55:10 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = dv->nr; --i >= 0; ) {
|
|
|
|
if (dv->base[i] != del)
|
|
|
|
continue;
|
|
|
|
/* we _don't_ call freeDirectory() here */
|
|
|
|
if (!--dv->nr) {
|
|
|
|
free(dv->base);
|
|
|
|
dv->base = NULL;
|
|
|
|
} else {
|
|
|
|
memmove(&dv->base[i], &dv->base[i + 1],
|
2008-10-08 10:49:05 +02:00
|
|
|
(dv->nr - i + 1) * sizeof(struct directory *));
|
2008-10-08 06:55:10 +02:00
|
|
|
dv->base = xrealloc(dv->base, dv_size(dv));
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1; /* not found */
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
void dirvec_add(struct dirvec *dv, struct directory *add)
|
2008-10-08 06:55:10 +02:00
|
|
|
{
|
|
|
|
++dv->nr;
|
|
|
|
dv->base = xrealloc(dv->base, dv_size(dv));
|
|
|
|
dv->base[dv->nr - 1] = add;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dirvec_destroy(struct dirvec *dv)
|
|
|
|
{
|
|
|
|
if (dv->base) {
|
|
|
|
free(dv->base);
|
|
|
|
dv->base = NULL;
|
|
|
|
}
|
|
|
|
dv->nr = 0;
|
|
|
|
}
|