configure.ac: Move OggVorbis Encoder to Encoder Plugins.
[mpd-mk.git] / src / dirvec.c
blob89b32a4f4a282869be8bcc378663a97cbd6872ac
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "dirvec.h"
22 #include "directory.h"
24 #include <glib.h>
26 #include <assert.h>
27 #include <string.h>
28 #include <stdlib.h>
30 static GMutex *nr_lock = NULL;
32 static size_t dv_size(const struct dirvec *dv)
34 return dv->nr * sizeof(struct directory *);
37 /* Only used for sorting/searching a dirvec, not general purpose compares */
38 static int dirvec_cmp(const void *d1, const void *d2)
40 const struct directory *a = ((const struct directory * const *)d1)[0];
41 const struct directory *b = ((const struct directory * const *)d2)[0];
42 return g_utf8_collate(a->path, b->path);
45 void dirvec_init(void)
47 g_assert(nr_lock == NULL);
48 nr_lock = g_mutex_new();
51 void dirvec_deinit(void)
53 g_assert(nr_lock != NULL);
54 g_mutex_free(nr_lock);
55 nr_lock = NULL;
58 void dirvec_sort(struct dirvec *dv)
60 g_mutex_lock(nr_lock);
61 qsort(dv->base, dv->nr, sizeof(struct directory *), dirvec_cmp);
62 g_mutex_unlock(nr_lock);
65 struct directory *dirvec_find(const struct dirvec *dv, const char *path)
67 char *base;
68 int i;
69 struct directory *ret = NULL;
71 base = g_path_get_basename(path);
73 g_mutex_lock(nr_lock);
74 for (i = dv->nr; --i >= 0; )
75 if (!strcmp(directory_get_name(dv->base[i]), base)) {
76 ret = dv->base[i];
77 break;
79 g_mutex_unlock(nr_lock);
81 g_free(base);
82 return ret;
85 int dirvec_delete(struct dirvec *dv, struct directory *del)
87 size_t i;
89 g_mutex_lock(nr_lock);
90 for (i = 0; i < dv->nr; ++i) {
91 if (dv->base[i] != del)
92 continue;
93 /* we _don't_ call directory_free() here */
94 if (!--dv->nr) {
95 g_mutex_unlock(nr_lock);
96 g_free(dv->base);
97 dv->base = NULL;
98 return i;
99 } else {
100 memmove(&dv->base[i], &dv->base[i + 1],
101 (dv->nr - i) * sizeof(struct directory *));
102 dv->base = g_realloc(dv->base, dv_size(dv));
104 break;
106 g_mutex_unlock(nr_lock);
108 return i;
111 void dirvec_add(struct dirvec *dv, struct directory *add)
113 g_mutex_lock(nr_lock);
114 ++dv->nr;
115 dv->base = g_realloc(dv->base, dv_size(dv));
116 dv->base[dv->nr - 1] = add;
117 g_mutex_unlock(nr_lock);
120 void dirvec_destroy(struct dirvec *dv)
122 g_mutex_lock(nr_lock);
123 dv->nr = 0;
124 g_mutex_unlock(nr_lock);
125 if (dv->base) {
126 g_free(dv->base);
127 dv->base = NULL;
131 int dirvec_for_each(const struct dirvec *dv,
132 int (*fn)(struct directory *, void *), void *arg)
134 size_t i;
135 size_t prev_nr;
137 g_mutex_lock(nr_lock);
138 for (i = 0; i < dv->nr; ) {
139 struct directory *dir = dv->base[i];
141 assert(dir);
142 prev_nr = dv->nr;
143 g_mutex_unlock(nr_lock);
144 if (fn(dir, arg) < 0)
145 return -1;
146 g_mutex_lock(nr_lock); /* dv->nr may change in fn() */
147 if (prev_nr == dv->nr)
148 ++i;
150 g_mutex_unlock(nr_lock);
152 return 0;