Collect REPLAYGAIN_* tags
[cmus.git] / comment.c
blob10c74c08581c6a4fc2a7ab497785b2bfec886474
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 /* Return date as an integer in the form YYYYMMDD, for sorting purposes.
79 * This function is not year 10000 compliant. */
80 int comments_get_date(const struct keyval *comments, const char *key)
82 const char *val;
83 char *endptr;
84 int year, month, day;
85 long int ival;
87 val = comments_get_val(comments, key);
88 if (val == NULL)
89 return -1;
91 year = strtol(val, &endptr, 10);
92 /* Looking for a four-digit number */
93 if (year < 1000 || year > 9999)
94 return -1;
95 ival = year * 10000;
97 if (*endptr == '-' || *endptr == ' ' || *endptr == '/') {
98 month = strtol(endptr+1, &endptr, 10);
99 if (month < 1 || month > 12)
100 return ival;
101 ival += month * 100;
104 if (*endptr == '-' || *endptr == ' ' || *endptr == '/') {
105 day = strtol(endptr+1, &endptr, 10);
106 if (day < 1 || day > 31)
107 return ival;
108 ival += day;
112 return ival;
115 static const char *interesting[] = {
116 "artist", "album", "title", "tracknumber", "discnumber", "genre",
117 "date", "compilation", "albumartist",
118 "replaygain_track_gain",
119 "replaygain_track_peak",
120 "replaygain_album_gain",
121 "replaygain_album_peak",
122 NULL
125 int is_interesting_key(const char *key)
127 int i;
129 for (i = 0; interesting[i]; i++) {
130 if (strcasecmp(key, interesting[i]) == 0)
131 return 1;
133 return 0;
136 void fix_track_or_disc(char *str)
138 char *slash = strchr(str, '/');
140 if (slash)
141 *slash = 0;