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-13 16:33:04 +02:00
|
|
|
#include "path.h"
|
2008-10-08 06:55:10 +02:00
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <string.h>
|
2008-10-20 22:18:12 +02:00
|
|
|
#include <glib.h>
|
2008-10-08 10:49:29 +02:00
|
|
|
|
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-09 15:24:34 +02:00
|
|
|
struct directory *dirvec_find(const struct dirvec *dv, const char *path)
|
2008-10-08 06:55:10 +02:00
|
|
|
{
|
2008-10-20 22:18:12 +02:00
|
|
|
char *basename;
|
2008-10-08 06:55:10 +02:00
|
|
|
int i;
|
|
|
|
|
2008-10-20 22:18:12 +02:00
|
|
|
basename = g_path_get_basename(path);
|
2008-10-13 16:33:04 +02:00
|
|
|
|
2008-10-08 06:55:10 +02:00
|
|
|
for (i = dv->nr; --i >= 0; )
|
2008-10-20 22:18:12 +02:00
|
|
|
if (!strcmp(directory_get_name(dv->base[i]), basename)) {
|
|
|
|
g_free(basename);
|
2008-10-08 06:55:10 +02:00
|
|
|
return dv->base[i];
|
2008-10-20 22:18:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free(basename);
|
2008-10-08 06:55:10 +02:00
|
|
|
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
|
|
|
{
|
2008-10-14 11:10:00 +02:00
|
|
|
size_t i;
|
2008-10-08 06:55:10 +02:00
|
|
|
|
2008-10-14 11:10:00 +02:00
|
|
|
for (i = 0; i < dv->nr; ++i) {
|
2008-10-08 06:55:10 +02:00
|
|
|
if (dv->base[i] != del)
|
|
|
|
continue;
|
2008-10-08 11:07:58 +02:00
|
|
|
/* we _don't_ call directory_free() here */
|
2008-10-08 06:55:10 +02:00
|
|
|
if (!--dv->nr) {
|
|
|
|
free(dv->base);
|
|
|
|
dv->base = NULL;
|
|
|
|
} else {
|
|
|
|
memmove(&dv->base[i], &dv->base[i + 1],
|
2008-10-14 11:10:00 +02:00
|
|
|
(dv->nr - i) * 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;
|
|
|
|
}
|
2008-10-21 01:30:30 +02:00
|
|
|
|
|
|
|
int dirvec_for_each(const struct dirvec *dv,
|
|
|
|
int (*fn)(struct directory *, void *), void *arg)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < dv->nr; ++i) {
|
|
|
|
struct directory *dir = dv->base[i];
|
|
|
|
|
|
|
|
assert(dir);
|
|
|
|
if (fn(dir, arg) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|