Debian Codegraph @

src/isocodes.c

raw symbols SCIP
1 /**
2 * SPDX-FileCopyrightText: 2016-2022 Dr. Tobias Quathamer <[email protected]>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7 #include <stdlib.h>
8 #include <glib.h>
9 #include <glib/gi18n.h>
10 #include <glib/gprintf.h>
11 #include <locale.h>
12 #include "isocodes.h"
13 #include "options.h"
14 #include "search.h"
15
16 /**
17 * Validate the given JSON data.
18 */
19 gboolean isocodes_validate(JsonParser *parser, GError **error)
20 {
21 // Get the root node
22 JsonNode *root = json_parser_get_root(parser);
23 // Ensure that there is a root element
24 if (root == NULL) {
25 isocodes_set_validation_error(error);
26 return FALSE;
27 }
28 // Ensure that the root element is an object
29 if (g_strcmp0(json_node_type_name(root), "JsonObject")) {
30 isocodes_set_validation_error(error);
31 return FALSE;
32 }
33 // Get the root object
34 JsonObject *root_object = json_node_get_object(root);
35 // Ensure that the root object has only one member
36 if (json_object_get_size(root_object) != 1) {
37 isocodes_set_validation_error(error);
38 return FALSE;
39 }
40 // The root object must have the standard as only member
41 if (!json_object_has_member(root_object, option_standard)) {
42 isocodes_set_validation_error(error);
43 return FALSE;
44 }
45 // Get the standard object
46 JsonNode *standard = json_object_get_member(root_object, option_standard);
47 // The standard must be an array of entries
48 if (!JSON_NODE_HOLDS_ARRAY(standard)) {
49 isocodes_set_validation_error(error);
50 return FALSE;
51 }
52 return TRUE;
53 }
54
55 /**
56 * Helper function to set the GError
57 */
58 void isocodes_set_validation_error(GError **error)
59 {
60 g_set_error(error, g_quark_from_string(GETTEXT_PACKAGE), 0,
61 // TRANSLATORS:
62 // The first placeholder is a filename, including the directory path.
63 // The second placeholder is an ISO standard, e.g. 3166-1 or 639-3.
64 _("The file \"%1$s\" does not contain valid ISO %2$s data."), options_get_filename(), option_standard);
65 }
66
67 /**
68 * Show codes from ISO standard
69 */
70 void isocodes_show_codes(JsonParser *parser, gchar **codes)
71 {
72 // Get the root node, object, and entries array
73 JsonNode *root = json_parser_get_root(parser);
74 JsonObject *root_object = json_node_get_object(root);
75 JsonNode *standard = json_object_get_member(root_object, option_standard);
76 JsonArray *entries_array = json_node_get_array(standard);
77 GList *entries_list = json_array_get_elements(entries_array);
78 GList *list_entry = g_list_first(entries_list);
79
80 // If there are codes given on the command line,
81 // only display entries matching those.
82 if (codes[0]) {
83 int i = 0;
84 GError *error;
85 while (codes[i]) {
86 error = NULL;
87 if (!search_entry(codes[i], entries_list, &error)) {
88 // TRANSLATORS: This is an error message.
89 g_printerr(_("isoquery: %s\n"), error->message);
90 g_error_free(error);
91 }
92 i++;
93 }
94 } else {
95 // Set up a JSON object for an entry
96 JsonObject *entry;
97 // Show all entries
98 while (list_entry) {
99 // Ensure that the list entry is an object
100 if (JSON_NODE_HOLDS_OBJECT(list_entry->data)) {
101 entry = json_node_get_object(list_entry->data);
102 isocodes_show_entry(entry);
103 }
104 list_entry = g_list_next(list_entry);
105 }
106 }
107
108 g_list_free(entries_list);
109 }
110
111 /**
112 * Print the given entry to stdout
113 */
114 void isocodes_show_entry(JsonObject *entry)
115 {
116 gchar **fields = isocodes_get_fields();
117 gchar separator = '\n';
118
119 // Ensure that we've got fields to display
120 if (!fields) {
121 return;
122 }
123 // Cycle through all fields
124 int i = 0;
125 while (fields[i]) {
126 // Handle optional fields gracefully,
127 // always add the tab as separator
128 g_printf("%s\t", isocodes_get_string_member(entry, fields[i]));
129 i++;
130 }
131
132 // Special case for the name:
133 // switching which field to display,
134 // translate the name
135 isocodes_show_name(entry);
136
137 // Separate entries, either with NULL byte or newline
138 if (option_null_separator) {
139 separator = 0;
140 }
141 g_printf("%c", separator);
142 g_strfreev(fields);
143 }
144
145 /**
146 * Returns a list of the fields in the current ISO standard
147 */
148 gchar **isocodes_get_fields(void)
149 {
150 gchar **fields = NULL;
151 if (!g_strcmp0(option_standard, "639-2")) {
152 gchar *f[] = {
153 "alpha_3", "bibliographic", "alpha_2", NULL
154 };
155 fields = g_strdupv(f);
156 } else if (!g_strcmp0(option_standard, "639-3")) {
157 gchar *f[] = {
158 "alpha_3", "scope", "type", "alpha_2", "bibliographic", NULL
159 };
160 fields = g_strdupv(f);
161 } else if (!g_strcmp0(option_standard, "639-5")) {
162 gchar *f[] = {
163 "alpha_3", NULL
164 };
165 fields = g_strdupv(f);
166 } else if (!g_strcmp0(option_standard, "3166-1")) {
167 if (option_flag_output) {
168 gchar *f[] = {
169 "alpha_2", "alpha_3", "numeric", "flag", NULL
170 };
171 fields = g_strdupv(f);
172 } else {
173 gchar *f[] = {
174 "alpha_2", "alpha_3", "numeric", NULL
175 };
176 fields = g_strdupv(f);
177 }
178 } else if (!g_strcmp0(option_standard, "3166-2")) {
179 gchar *f[] = {
180 "code", "type", "parent", NULL
181 };
182 fields = g_strdupv(f);
183 } else if (!g_strcmp0(option_standard, "3166-3")) {
184 gchar *f[] = {
185 "alpha_3", "alpha_4", "numeric", "comment", "withdrawal_date", NULL
186 };
187 fields = g_strdupv(f);
188 } else if (!g_strcmp0(option_standard, "4217")) {
189 gchar *f[] = {
190 "alpha_3", "numeric", NULL
191 };
192 fields = g_strdupv(f);
193 } else if (!g_strcmp0(option_standard, "15924")) {
194 gchar *f[] = {
195 "alpha_4", "numeric", NULL
196 };
197 fields = g_strdupv(f);
198 }
199 return fields;
200 }
201
202 /**
203 * Returns the string member of a JSON node.
204 * Defaults to an empty string.
205 */
206 const gchar *isocodes_get_string_member(JsonObject *entry, const gchar *member)
207 {
208 if (!json_object_has_member(entry, member)) {
209 return "";
210 }
211 // Ensure that the given member holds a value, not
212 // an array or another object.
213 JsonNode *node = json_object_get_member(entry, member);
214 if (!JSON_NODE_HOLDS_VALUE(node)) {
215 return "";
216 }
217 // Get the string, this might be NULL as well.
218 const gchar *value = json_node_get_string(node);
219 return value ? value : "";
220 }
221
222 /**
223 * Special handling for the name fields
224 */
225 void isocodes_show_name(JsonObject *entry)
226 {
227 const gchar *output;
228 // Use the correct name field
229 if (json_object_has_member(entry, option_namefield)) {
230 output = isocodes_get_string_member(entry, option_namefield);
231 } else {
232 // Fallback: use the "name" field, which is mandatory
233 output = isocodes_get_string_member(entry, "name");
234 }
235 // Try to translate, if there's a locale given
236 if (strlen(option_locale) > 0) {
237 // Save the current locale and environment variable LANGUAGE.
238 gchar *locale_backup = g_strdup(setlocale(LC_ALL, NULL));
239 gchar *env_backup = g_strdup(getenv("LANGUAGE"));
240
241 // Use the wanted locale to look for a translation
242 setenv("LANGUAGE", option_locale, TRUE);
243 // Use the default locale, based on the environment variable above
244 setlocale(LC_ALL, "");
245
246 // Determine the gettext domain from the standard
247 gchar *domain = g_strdup_printf("iso_%s", option_standard);
248 // Do the actual translation
249 output = dgettext(domain, output);
250
251 // Restore the environment variable LANGUAGE, either
252 // to the previous value or unset the variable.
253 if (env_backup == NULL) {
254 unsetenv("LANGUAGE");
255 } else {
256 setenv("LANGUAGE", env_backup, TRUE);
257 }
258 // Restore the locale from backup
259 setlocale(LC_ALL, locale_backup);
260
261 g_free(locale_backup);
262 g_free(env_backup);
263 g_free(domain);
264 }
265 g_printf("%s", output);
266 }