aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2010-06-30 21:31:45 +0200
committerMax Kellermann <max@duempel.org>2010-06-30 21:31:45 +0200
commit9550c87327cdb0c97ca855bc5d77d28cbbb723d5 (patch)
tree6f68e282b3f67a3ef29e493af570afde77f16680 /src
parente223e8a5b58509287098cb1deffb9655c7249a0c (diff)
downloadmpd-9550c87327cdb0c97ca855bc5d77d28cbbb723d5.tar.gz
mpd-9550c87327cdb0c97ca855bc5d77d28cbbb723d5.tar.xz
mpd-9550c87327cdb0c97ca855bc5d77d28cbbb723d5.zip
tag: added function tag_name_parse()
Convert a string into a tag_type enum.
Diffstat (limited to 'src')
-rw-r--r--src/locate.c6
-rw-r--r--src/tag.c50
-rw-r--r--src/tag.h16
3 files changed, 60 insertions, 12 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/tag.c b/src/tag.c
index c34256b78..b228480c8 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -64,6 +64,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 *);
@@ -76,7 +106,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 */
@@ -98,16 +128,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 75a86b387..8d968c254 100644
--- a/src/tag.h
+++ b/src/tag.h
@@ -94,6 +94,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);