1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2  *
3  * Copyright (C) 2014-2017 Richard Hughes <richard@hughsie.com>
4  *
5  * SPDX-License-Identifier: LGPL-2.1+
6  */
7 
8 /**
9  * SECTION:as-tag
10  * @short_description: Helper functions to convert to and from tag enums
11  * @include: appstream-glib.h
12  * @stability: Stable
13  *
14  * These functions will convert a tag enum such as %AS_TAG_COMPONENT to
15  * it's string form, and also vice-versa.
16  *
17  * These helper functions may be useful if implementing an AppStream parser.
18  */
19 
20 #include "config.h"
21 
22 #include "as-tag.h"
23 
24 #pragma GCC diagnostic push
25 #pragma GCC diagnostic ignored "-Wconversion"
26 #pragma GCC diagnostic ignored "-Wsign-conversion"
27 #include "as-tag-private.h"
28 #pragma GCC diagnostic pop
29 
30 /**
31  * as_tag_from_string:
32  * @tag: the string.
33  *
34  * Converts the text representation to an enumerated value.
35  *
36  * Returns: a %AsTag, or %AS_TAG_UNKNOWN if not known.
37  *
38  * Since: 0.1.0
39  **/
40 AsTag
as_tag_from_string(const gchar * tag)41 as_tag_from_string (const gchar *tag)
42 {
43 	return as_tag_from_string_full (tag, AS_TAG_FLAG_NONE);
44 }
45 
46 /**
47  * as_tag_from_string_full:
48  * @tag: the string.
49  * @flags: the #AsTagFlags e.g. %AS_TAG_FLAG_USE_FALLBACKS
50  *
51  * Converts the text representation to an enumerated value also converting
52  * legacy key names.
53  *
54  * Returns: a %AsTag, or %AS_TAG_UNKNOWN if not known.
55  *
56  * Since: 0.1.2
57  **/
58 AsTag
as_tag_from_string_full(const gchar * tag,AsTagFlags flags)59 as_tag_from_string_full (const gchar *tag, AsTagFlags flags)
60 {
61 	const struct tag_data *ky;
62 	AsTag etag = AS_TAG_UNKNOWN;
63 
64 	/* invalid */
65 	if (tag == NULL)
66 		return AS_TAG_UNKNOWN;
67 
68 	/* use a perfect hash */
69 	ky = _as_tag_from_gperf (tag, strlen (tag));
70 	if (ky != NULL)
71 		etag = ky->etag;
72 
73 	/* deprecated names */
74 	if (etag == AS_TAG_UNKNOWN && (flags & AS_TAG_FLAG_USE_FALLBACKS)) {
75 		if (g_strcmp0 (tag, "appcategories") == 0)
76 			return AS_TAG_CATEGORIES;
77 		if (g_strcmp0 (tag, "appcategory") == 0)
78 			return AS_TAG_CATEGORY;
79 		if (g_strcmp0 (tag, "licence") == 0)
80 			return AS_TAG_PROJECT_LICENSE;
81 		if (g_strcmp0 (tag, "applications") == 0)
82 			return AS_TAG_COMPONENTS;
83 		if (g_strcmp0 (tag, "application") == 0)
84 			return AS_TAG_COMPONENT;
85 		if (g_strcmp0 (tag, "updatecontact") == 0)
86 			return AS_TAG_UPDATE_CONTACT;
87 		/* fix spelling error */
88 		if (g_strcmp0 (tag, "metadata_licence") == 0)
89 			return AS_TAG_METADATA_LICENSE;
90 	}
91 
92 	/* translated versions */
93 	if (etag == AS_TAG_UNKNOWN && (flags & AS_TAG_FLAG_USE_TRANSLATED)) {
94 		if (g_strcmp0 (tag, "_name") == 0)
95 			return AS_TAG_NAME;
96 		if (g_strcmp0 (tag, "_summary") == 0)
97 			return AS_TAG_SUMMARY;
98 		if (g_strcmp0 (tag, "_caption") == 0)
99 			return AS_TAG_CAPTION;
100 	}
101 
102 	return etag;
103 }
104 
105 /**
106  * as_tag_to_string:
107  * @tag: the %AsTag value.
108  *
109  * Converts the enumerated value to an text representation.
110  *
111  * Returns: string version of @tag
112  *
113  * Since: 0.1.0
114  **/
115 const gchar *
as_tag_to_string(AsTag tag)116 as_tag_to_string (AsTag tag)
117 {
118 	const gchar *str[] = {
119 		"unknown",
120 		"components",
121 		"component",
122 		"id",
123 		"pkgname",
124 		"name",
125 		"summary",
126 		"description",
127 		"url",
128 		"icon",
129 		"categories",
130 		"category",
131 		"keywords",
132 		"keyword",
133 		"mimetypes",
134 		"mimetype",
135 		"project_group",
136 		"project_license",
137 		"screenshot",
138 		"screenshots",
139 		"update_contact",
140 		"image",
141 		"compulsory_for_desktop",
142 		"priority",
143 		"caption",
144 		"languages",
145 		"lang",
146 		"metadata",
147 		"value",
148 		"releases",
149 		"release",
150 		"architectures",
151 		"arch",
152 		"metadata_license",
153 		"provides",
154 		"extends",
155 		"developer_name",
156 		"kudos",
157 		"kudo",
158 		"source_pkgname",
159 		"vetos",
160 		"veto",
161 		"bundle",
162 		"permissions",
163 		"permission",
164 		"location",
165 		"checksum",
166 		"size",
167 		"translation",
168 		"content_rating",
169 		"content_attribute",
170 		"version",
171 		"reviews",
172 		"review",
173 		"reviewer_name",
174 		"reviewer_id",
175 		"suggests",
176 		"requires",
177 		"custom",
178 		"launchable",
179 		"agreement",
180 		"agreement_section",
181 		"p",
182 		"li",
183 		"ul",
184 		"ol",
185 		"binary",
186 		"font",
187 		"dbus",
188 		"modalias",
189 		"library",
190 		NULL };
191 	if (tag > AS_TAG_LAST)
192 		tag = AS_TAG_LAST;
193 	return str[tag];
194 }
195