mad: Fix id3_key enumeration
[cmus.git] / tabexp.c
blobc7a5a18af3ad690499a52b7260def6276bd52f7a
1 /*
2 * Copyright 2004-2005 Timo Hirvonen
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
20 #include "tabexp.h"
21 #include "xmalloc.h"
22 #include "xstrjoin.h"
23 #include "debug.h"
25 #include <stdlib.h>
27 struct tabexp tabexp = {
28 .head = NULL,
29 .tails = NULL,
30 .nr_tails = 0,
33 /* index to tabexp.tails */
34 static int tabexp_index = 0;
36 char *tabexp_expand(const char *src, void (*load_matches)(const char *src))
38 if (tabexp.tails == NULL) {
39 char *expanded;
41 load_matches(src);
43 if (tabexp.tails == NULL) {
44 BUG_ON(tabexp.head != NULL);
45 BUG_ON(tabexp.nr_tails != 0);
46 return NULL;
49 BUG_ON(tabexp.head == NULL);
50 BUG_ON(tabexp.nr_tails < 1);
52 expanded = xstrjoin(tabexp.head, tabexp.tails[0]);
53 if (tabexp.nr_tails == 1)
54 tabexp_reset();
55 return expanded;
56 } else {
57 tabexp_index++;
58 tabexp_index %= tabexp.nr_tails;
59 return xstrjoin(tabexp.head, tabexp.tails[tabexp_index]);
63 void tabexp_reset(void)
65 int i;
67 for (i = 0; i < tabexp.nr_tails; i++)
68 free(tabexp.tails[i]);
69 free(tabexp.tails);
70 free(tabexp.head);
71 tabexp.tails = NULL;
72 tabexp.head = NULL;
73 tabexp.nr_tails = 0;
75 tabexp_index = 0;