| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 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 |
|
| 18 |
|
| 19 |
gboolean isocodes_validate(JsonParser *parser, GError **error) |
| 20 |
{ |
| 21 |
|
| 22 |
JsonNode *root = json_parser_get_root(parser); |
| 23 |
|
| 24 |
if (root == NULL) { |
| 25 |
isocodes_set_validation_error(error); |
| 26 |
return FALSE; |
| 27 |
} |
| 28 |
|
| 29 |
if (g_strcmp0(json_node_type_name(root), "JsonObject")) { |
| 30 |
isocodes_set_validation_error(error); |
| 31 |
return FALSE; |
| 32 |
} |
| 33 |
|
| 34 |
JsonObject *root_object = json_node_get_object(root); |
| 35 |
|
| 36 |
if (json_object_get_size(root_object) != 1) { |
| 37 |
isocodes_set_validation_error(error); |
| 38 |
return FALSE; |
| 39 |
} |
| 40 |
|
| 41 |
if (!json_object_has_member(root_object, option_standard)) { |
| 42 |
isocodes_set_validation_error(error); |
| 43 |
return FALSE; |
| 44 |
} |
| 45 |
|
| 46 |
JsonNode *standard = json_object_get_member(root_object, option_standard); |
| 47 |
|
| 48 |
if (!JSON_NODE_HOLDS_ARRAY(standard)) { |
| 49 |
isocodes_set_validation_error(error); |
| 50 |
return FALSE; |
| 51 |
} |
| 52 |
return TRUE; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
void isocodes_set_validation_error(GError **error) |
| 59 |
{ |
| 60 |
g_set_error(error, g_quark_from_string(GETTEXT_PACKAGE), 0, |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
_("The file \"%1$s\" does not contain valid ISO %2$s data."), options_get_filename(), option_standard); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
void isocodes_show_codes(JsonParser *parser, gchar **codes) |
| 71 |
{ |
| 72 |
|
| 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 |
|
| 81 |
|
| 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 |
|
| 89 |
g_printerr(_("isoquery: %s\n"), error->message); |
| 90 |
g_error_free(error); |
| 91 |
} |
| 92 |
i++; |
| 93 |
} |
| 94 |
} else { |
| 95 |
|
| 96 |
JsonObject *entry; |
| 97 |
|
| 98 |
while (list_entry) { |
| 99 |
|
| 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 |
|
| 113 |
|
| 114 |
void isocodes_show_entry(JsonObject *entry) |
| 115 |
{ |
| 116 |
gchar **fields = isocodes_get_fields(); |
| 117 |
gchar separator = '\n'; |
| 118 |
|
| 119 |
|
| 120 |
if (!fields) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
int i = 0; |
| 125 |
while (fields[i]) { |
| 126 |
|
| 127 |
|
| 128 |
g_printf("%s\t", isocodes_get_string_member(entry, fields[i])); |
| 129 |
i++; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
isocodes_show_name(entry); |
| 136 |
|
| 137 |
|
| 138 |
if (option_null_separator) { |
| 139 |
separator = 0; |
| 140 |
} |
| 141 |
g_printf("%c", separator); |
| 142 |
g_strfreev(fields); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 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 |
|
| 204 |
|
| 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 |
|
| 212 |
|
| 213 |
JsonNode *node = json_object_get_member(entry, member); |
| 214 |
if (!JSON_NODE_HOLDS_VALUE(node)) { |
| 215 |
return ""; |
| 216 |
} |
| 217 |
|
| 218 |
const gchar *value = json_node_get_string(node); |
| 219 |
return value ? value : ""; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
void isocodes_show_name(JsonObject *entry) |
| 226 |
{ |
| 227 |
const gchar *output; |
| 228 |
|
| 229 |
if (json_object_has_member(entry, option_namefield)) { |
| 230 |
output = isocodes_get_string_member(entry, option_namefield); |
| 231 |
} else { |
| 232 |
|
| 233 |
output = isocodes_get_string_member(entry, "name"); |
| 234 |
} |
| 235 |
|
| 236 |
if (strlen(option_locale) > 0) { |
| 237 |
|
| 238 |
gchar *locale_backup = g_strdup(setlocale(LC_ALL, NULL)); |
| 239 |
gchar *env_backup = g_strdup(getenv("LANGUAGE")); |
| 240 |
|
| 241 |
|
| 242 |
setenv("LANGUAGE", option_locale, TRUE); |
| 243 |
|
| 244 |
setlocale(LC_ALL, ""); |
| 245 |
|
| 246 |
|
| 247 |
gchar *domain = g_strdup_printf("iso_%s", option_standard); |
| 248 |
|
| 249 |
output = dgettext(domain, output); |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
if (env_backup == NULL) { |
| 254 |
unsetenv("LANGUAGE"); |
| 255 |
} else { |
| 256 |
setenv("LANGUAGE", env_backup, TRUE); |
| 257 |
} |
| 258 |
|
| 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 |
} |