1 /*
2  * Copyright 2003-2021 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include "Table.hxx"
21 #include "util/ASCII.hxx"
22 #include "util/StringView.hxx"
23 
24 #include <string.h>
25 
26 /**
27  * Looks up a string in a tag translation table (case sensitive).
28  * Returns TAG_NUM_OF_ITEM_TYPES if the specified name was not found
29  * in the table.
30  */
31 TagType
tag_table_lookup(const struct tag_table * table,const char * name)32 tag_table_lookup(const struct tag_table *table, const char *name) noexcept
33 {
34 	for (; table->name != nullptr; ++table)
35 		if (strcmp(name, table->name) == 0)
36 			return table->type;
37 
38 	return TAG_NUM_OF_ITEM_TYPES;
39 }
40 
41 TagType
tag_table_lookup(const struct tag_table * table,StringView name)42 tag_table_lookup(const struct tag_table *table, StringView name) noexcept
43 {
44 	for (; table->name != nullptr; ++table)
45 		if (name.Equals(table->name))
46 			return table->type;
47 
48 	return TAG_NUM_OF_ITEM_TYPES;
49 }
50 
51 /**
52  * Looks up a string in a tag translation table (case insensitive).
53  * Returns TAG_NUM_OF_ITEM_TYPES if the specified name was not found
54  * in the table.
55  */
56 TagType
tag_table_lookup_i(const struct tag_table * table,const char * name)57 tag_table_lookup_i(const struct tag_table *table, const char *name) noexcept
58 {
59 	for (; table->name != nullptr; ++table)
60 		if (StringEqualsCaseASCII(name, table->name))
61 			return table->type;
62 
63 	return TAG_NUM_OF_ITEM_TYPES;
64 }
65 
66 TagType
tag_table_lookup_i(const struct tag_table * table,StringView name)67 tag_table_lookup_i(const struct tag_table *table, StringView name) noexcept
68 {
69 	for (; table->name != nullptr; ++table)
70 		if (name.EqualsIgnoreCase(table->name))
71 			return table->type;
72 
73 	return TAG_NUM_OF_ITEM_TYPES;
74 }
75 
76 const char *
tag_table_lookup(const tag_table * table,TagType type)77 tag_table_lookup(const tag_table *table, TagType type) noexcept
78 {
79 	for (; table->name != nullptr; ++table)
80 		if (table->type == type)
81 			return table->name;
82 
83 	return nullptr;
84 }
85