Small clean up to http_open()
[cmus.git] / comment.c
blob39240ff781f92ceac25a1da5be97252c0a47ae79
1 /*
2 * Copyright 2004-2007 Timo Hirvonen
3 */
5 #include "comment.h"
6 #include "xmalloc.h"
7 #include "utils.h"
9 #include <string.h>
11 const char *comments_get_albumartist(const struct keyval *comments)
13 const char *val = keyvals_get_val(comments, "albumartist");
14 if (!val)
15 val = keyvals_get_val(comments, "artist");
16 return val;
19 int comments_get_int(const struct keyval *comments, const char *key)
21 const char *val;
22 long int ival;
24 val = keyvals_get_val(comments, key);
25 if (val == NULL)
26 return -1;
27 if (str_to_int(val, &ival) == -1)
28 return -1;
29 return ival;
32 /* Return date as an integer in the form YYYYMMDD, for sorting purposes.
33 * This function is not year 10000 compliant. */
34 int comments_get_date(const struct keyval *comments, const char *key)
36 const char *val;
37 char *endptr;
38 int year, month, day;
39 long int ival;
41 val = keyvals_get_val(comments, key);
42 if (val == NULL)
43 return -1;
45 year = strtol(val, &endptr, 10);
46 /* Looking for a four-digit number */
47 if (year < 1000 || year > 9999)
48 return -1;
49 ival = year * 10000;
51 if (*endptr == '-' || *endptr == ' ' || *endptr == '/') {
52 month = strtol(endptr+1, &endptr, 10);
53 if (month < 1 || month > 12)
54 return ival;
55 ival += month * 100;
58 if (*endptr == '-' || *endptr == ' ' || *endptr == '/') {
59 day = strtol(endptr+1, &endptr, 10);
60 if (day < 1 || day > 31)
61 return ival;
62 ival += day;
66 return ival;
69 static const char *interesting[] = {
70 "artist", "album", "title", "tracknumber", "discnumber", "genre",
71 "date", "compilation", "albumartist", "artistsort", "albumartistsort",
72 "replaygain_track_gain",
73 "replaygain_track_peak",
74 "replaygain_album_gain",
75 "replaygain_album_peak",
76 NULL
79 static struct {
80 const char *old;
81 const char *new;
82 } key_map[] = {
83 { "album_artist", "albumartist" },
84 { "album artist", "albumartist" },
85 { "disc", "discnumber" },
86 { "track", "tracknumber" },
87 { NULL, NULL }
90 static const char *fix_key(const char *key)
92 int i;
94 for (i = 0; interesting[i]; i++) {
95 if (!strcasecmp(key, interesting[i]))
96 return interesting[i];
98 for (i = 0; key_map[i].old; i++) {
99 if (!strcasecmp(key, key_map[i].old))
100 return key_map[i].new;
102 return NULL;
105 int comments_add(struct growing_keyvals *c, const char *key, char *val)
107 int i;
109 key = fix_key(key);
110 if (!key) {
111 free(val);
112 return 0;
115 if (!strcmp(key, "tracknumber") || !strcmp(key, "discnumber")) {
116 char *slash = strchr(val, '/');
117 if (slash)
118 *slash = 0;
121 /* don't add duplicates. can't use keyvals_get_val() */
122 for (i = 0; i < c->count; i++) {
123 if (!strcasecmp(key, c->keyvals[i].key) && !strcmp(val, c->keyvals[i].val)) {
124 free(val);
125 return 0;
129 keyvals_add(c, key, val);
130 return 1;
133 int comments_add_const(struct growing_keyvals *c, const char *key, const char *val)
135 return comments_add(c, key, xstrdup(val));