add :refresh command
[cmus.git] / comment.c
blob3fa49924b7c47841528019fc289cf68eed0523be
1 /*
2 * Copyright 2004 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 <comment.h>
21 #include <xmalloc.h>
22 #include <utils.h>
24 #include <string.h>
26 struct keyval *comments_dup(const struct keyval *comments)
28 struct keyval *c;
29 int i;
31 for (i = 0; comments[i].key; i++)
32 ; /* nothing */
33 c = xnew(struct keyval, i + 1);
34 for (i = 0; comments[i].key; i++) {
35 c[i].key = xstrdup(comments[i].key);
36 c[i].val = xstrdup(comments[i].val);
38 c[i].key = NULL;
39 c[i].val = NULL;
40 return c;
43 void comments_free(struct keyval *comments)
45 int i;
47 for (i = 0; comments[i].key; i++) {
48 free(comments[i].key);
49 free(comments[i].val);
51 free(comments);
54 const char *comments_get_val(const struct keyval *comments, const char *key)
56 int i;
58 for (i = 0; comments[i].key; i++) {
59 if (strcasecmp(comments[i].key, key) == 0)
60 return comments[i].val;
62 return NULL;
65 int comments_get_int(const struct keyval *comments, const char *key)
67 const char *val;
68 long int ival;
70 val = comments_get_val(comments, key);
71 if (val == NULL)
72 return -1;
73 if (str_to_int(val, &ival) == -1)
74 return -1;
75 return ival;
78 static const char *interesting[] = {
79 "artist", "album", "title", "tracknumber", "discnumber", "genre", "date", NULL
82 int is_interesting_key(const char *key)
84 int i;
86 for (i = 0; interesting[i]; i++) {
87 if (strcasecmp(key, interesting[i]) == 0)
88 return 1;
90 return 0;
93 void fix_track_or_disc(char *str)
95 char *slash = strchr(str, '/');
97 if (slash)
98 *slash = 0;