2 * Copyright 2004 Timo Hirvonen
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
26 struct keyval
*comments_dup(const struct keyval
*comments
)
31 for (i
= 0; comments
[i
].key
; i
++)
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
);
43 void comments_free(struct keyval
*comments
)
47 for (i
= 0; comments
[i
].key
; i
++) {
48 free(comments
[i
].key
);
49 free(comments
[i
].val
);
54 const char *comments_get_val(const struct keyval
*comments
, const char *key
)
58 for (i
= 0; comments
[i
].key
; i
++) {
59 if (strcasecmp(comments
[i
].key
, key
) == 0)
60 return comments
[i
].val
;
65 int comments_get_int(const struct keyval
*comments
, const char *key
)
70 val
= comments_get_val(comments
, key
);
73 if (str_to_int(val
, &ival
) == -1)
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
)
87 val
= comments_get_val(comments
, key
);
91 year
= strtol(val
, &endptr
, 10);
92 /* Looking for a four-digit number */
93 if (year
< 1000 || year
> 9999)
97 if (*endptr
== '-' || *endptr
== ' ' || *endptr
== '/') {
98 month
= strtol(endptr
+1, &endptr
, 10);
99 if (month
< 1 || month
> 12)
104 if (*endptr
== '-' || *endptr
== ' ' || *endptr
== '/') {
105 day
= strtol(endptr
+1, &endptr
, 10);
106 if (day
< 1 || day
> 31)
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",
125 int is_interesting_key(const char *key
)
129 for (i
= 0; interesting
[i
]; i
++) {
130 if (strcasecmp(key
, interesting
[i
]) == 0)
136 void fix_track_or_disc(char *str
)
138 char *slash
= strchr(str
, '/');