dirvec.c 3.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "dirvec.h"
21
#include "directory.h"
22

23
#include <glib.h>
24

Max Kellermann's avatar
Max Kellermann committed
25 26
#include <assert.h>
#include <string.h>
27
#include <stdlib.h>
Max Kellermann's avatar
Max Kellermann committed
28

29
static GMutex *nr_lock = NULL;
30

31
static size_t dv_size(const struct dirvec *dv)
32
{
33
	return dv->nr * sizeof(struct directory *);
34 35 36 37 38
}

/* Only used for sorting/searching a dirvec, not general purpose compares */
static int dirvec_cmp(const void *d1, const void *d2)
{
39 40
	const struct directory *a = ((const struct directory * const *)d1)[0];
	const struct directory *b = ((const struct directory * const *)d2)[0];
41
	return g_utf8_collate(a->path, b->path);
42 43
}

44 45 46 47 48 49 50 51 52 53 54 55 56
void dirvec_init(void)
{
	g_assert(nr_lock == NULL);
	nr_lock = g_mutex_new();
}

void dirvec_deinit(void)
{
	g_assert(nr_lock != NULL);
	g_mutex_free(nr_lock);
	nr_lock = NULL;
}

57 58
void dirvec_sort(struct dirvec *dv)
{
59
	g_mutex_lock(nr_lock);
60
	qsort(dv->base, dv->nr, sizeof(struct directory *), dirvec_cmp);
61
	g_mutex_unlock(nr_lock);
62 63
}

64
struct directory *dirvec_find(const struct dirvec *dv, const char *path)
65
{
66
	char *base;
67
	int i;
68
	struct directory *ret = NULL;
69

70
	base = g_path_get_basename(path);
71

72
	g_mutex_lock(nr_lock);
73
	for (i = dv->nr; --i >= 0; )
74
		if (!strcmp(directory_get_name(dv->base[i]), base)) {
75 76
			ret = dv->base[i];
			break;
77
		}
78
	g_mutex_unlock(nr_lock);
79

80
	g_free(base);
81
	return ret;
82 83
}

84
int dirvec_delete(struct dirvec *dv, struct directory *del)
85
{
86
	size_t i;
87

88
	g_mutex_lock(nr_lock);
89
	for (i = 0; i < dv->nr; ++i) {
90 91
		if (dv->base[i] != del)
			continue;
92
		/* we _don't_ call directory_free() here */
93
		if (!--dv->nr) {
94
			g_mutex_unlock(nr_lock);
95
			g_free(dv->base);
96
			dv->base = NULL;
97
			return i;
98 99
		} else {
			memmove(&dv->base[i], &dv->base[i + 1],
100
				(dv->nr - i) * sizeof(struct directory *));
101
			dv->base = g_realloc(dv->base, dv_size(dv));
102
		}
103
		break;
104
	}
105
	g_mutex_unlock(nr_lock);
106

107
	return i;
108 109
}

110
void dirvec_add(struct dirvec *dv, struct directory *add)
111
{
112
	g_mutex_lock(nr_lock);
113
	++dv->nr;
114
	dv->base = g_realloc(dv->base, dv_size(dv));
115
	dv->base[dv->nr - 1] = add;
116
	g_mutex_unlock(nr_lock);
117 118 119 120
}

void dirvec_destroy(struct dirvec *dv)
{
121
	g_mutex_lock(nr_lock);
122
	dv->nr = 0;
123
	g_mutex_unlock(nr_lock);
124
	if (dv->base) {
125
		g_free(dv->base);
126 127 128
		dv->base = NULL;
	}
}
129 130 131 132 133

int dirvec_for_each(const struct dirvec *dv,
                    int (*fn)(struct directory *, void *), void *arg)
{
	size_t i;
134
	size_t prev_nr;
135

136
	g_mutex_lock(nr_lock);
137
	for (i = 0; i < dv->nr; ) {
138 139 140
		struct directory *dir = dv->base[i];

		assert(dir);
141
		prev_nr = dv->nr;
142
		g_mutex_unlock(nr_lock);
143 144
		if (fn(dir, arg) < 0)
			return -1;
145
		g_mutex_lock(nr_lock); /* dv->nr may change in fn() */
146 147
		if (prev_nr == dv->nr)
			++i;
148
	}
149
	g_mutex_unlock(nr_lock);
150 151 152

	return 0;
}