aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2009-11-04 18:47:42 +0100
committerMax Kellermann <max@duempel.org>2009-11-04 18:47:42 +0100
commita434c35eb46ff71e2b0441595dcc7985e1b292c2 (patch)
tree073d3e51ab3a11090afecda68d6530415908ef79 /src
parent7af8c88e37610fe7ebb167fadc28656385705d18 (diff)
downloadmpd-a434c35eb46ff71e2b0441595dcc7985e1b292c2.tar.gz
mpd-a434c35eb46ff71e2b0441595dcc7985e1b292c2.tar.xz
mpd-a434c35eb46ff71e2b0441595dcc7985e1b292c2.zip
tag: added function tag_name_parse()
Convert a string into a tag_type enum.
Diffstat (limited to '')
-rw-r--r--src/locate.c6
-rw-r--r--src/song_save.c15
-rw-r--r--src/tag.c50
-rw-r--r--src/tag.h16
4 files changed, 61 insertions, 26 deletions
diff --git a/src/locate.c b/src/locate.c
index 175bca35a..7b4721fa9 100644
--- a/src/locate.c
+++ b/src/locate.c
@@ -42,9 +42,9 @@ locate_parse_type(const char *str)
if (0 == g_ascii_strcasecmp(str, LOCATE_TAG_ANY_KEY))
return LOCATE_TAG_ANY_TYPE;
- for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++)
- if (0 == g_ascii_strcasecmp(str, tag_item_names[i]))
- return i;
+ i = tag_name_parse_i(str);
+ if (i != TAG_NUM_OF_ITEM_TYPES)
+ return i;
return -1;
}
diff --git a/src/song_save.c b/src/song_save.c
index d2c5175b3..075fb6e7a 100644
--- a/src/song_save.c
+++ b/src/song_save.c
@@ -61,19 +61,6 @@ void songvec_save(FILE *fp, struct songvec *sv)
songvec_for_each(sv, song_save, fp);
}
-static enum tag_type
-parse_tag_name(const char *name)
-{
- int i;
-
- for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) {
- if (strcmp(name, tag_item_names[i]) == 0)
- return i;
- }
-
- return TAG_NUM_OF_ITEM_TYPES;
-}
-
struct song *
song_load(FILE *fp, struct directory *parent, const char *uri,
GString *buffer, GError **error_r)
@@ -99,7 +86,7 @@ song_load(FILE *fp, struct directory *parent, const char *uri,
*colon++ = 0;
value = g_strchug(colon);
- if ((type = parse_tag_name(line)) != TAG_NUM_OF_ITEM_TYPES) {
+ if ((type = tag_name_parse(line)) != TAG_NUM_OF_ITEM_TYPES) {
if (!song->tag) {
song->tag = tag_new();
tag_begin_add(song->tag);
diff --git a/src/tag.c b/src/tag.c
index 874fdaef2..3e0bfc559 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -66,6 +66,36 @@ const char *tag_item_names[TAG_NUM_OF_ITEM_TYPES] = {
bool ignore_tag_items[TAG_NUM_OF_ITEM_TYPES];
+enum tag_type
+tag_name_parse(const char *name)
+{
+ assert(name != NULL);
+
+ for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) {
+ assert(tag_item_names[i] != NULL);
+
+ if (strcmp(name, tag_item_names[i]) == 0)
+ return (enum tag_type)i;
+ }
+
+ return TAG_NUM_OF_ITEM_TYPES;
+}
+
+enum tag_type
+tag_name_parse_i(const char *name)
+{
+ assert(name != NULL);
+
+ for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) {
+ assert(tag_item_names[i] != NULL);
+
+ if (g_ascii_strcasecmp(name, tag_item_names[i]) == 0)
+ return (enum tag_type)i;
+ }
+
+ return TAG_NUM_OF_ITEM_TYPES;
+}
+
static size_t items_size(const struct tag *tag)
{
return tag->num_items * sizeof(struct tag_item *);
@@ -78,7 +108,7 @@ void tag_lib_init(void)
char *temp;
char *s;
char *c;
- int i;
+ enum tag_type type;
/* parse the "metadata_to_use" config parameter below */
@@ -100,16 +130,18 @@ void tag_lib_init(void)
if (*s == '\0')
quit = 1;
*s = '\0';
- for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) {
- if (g_ascii_strcasecmp(c, tag_item_names[i]) == 0) {
- ignore_tag_items[i] = false;
- break;
- }
- }
- if (strlen(c) && i == TAG_NUM_OF_ITEM_TYPES) {
+
+ c = g_strstrip(c);
+ if (*c == 0)
+ continue;
+
+ type = tag_name_parse_i(c);
+ if (type == TAG_NUM_OF_ITEM_TYPES)
g_error("error parsing metadata item \"%s\"",
c);
- }
+
+ ignore_tag_items[type] = false;
+
s++;
c = s;
}
diff --git a/src/tag.h b/src/tag.h
index 8b1dd9b30..8cd09003e 100644
--- a/src/tag.h
+++ b/src/tag.h
@@ -96,6 +96,22 @@ struct tag {
};
/**
+ * Parse the string, and convert it into a #tag_type. Returns
+ * #TAG_NUM_OF_ITEM_TYPES if the string could not be recognized.
+ */
+enum tag_type
+tag_name_parse(const char *name);
+
+/**
+ * Parse the string, and convert it into a #tag_type. Returns
+ * #TAG_NUM_OF_ITEM_TYPES if the string could not be recognized.
+ *
+ * Case does not matter.
+ */
+enum tag_type
+tag_name_parse_i(const char *name);
+
+/**
* Creates an empty #tag.
*/
struct tag *tag_new(void);