Debian Codegraph @

src/isoquery.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 <locale.h>
11 #include <json-glib/json-glib.h>
12 #include "options.h"
13 #include "isocodes.h"
14
15 int main(int argument_count, gchar **arguments)
16 {
17 // The first parameter is not used, but cannot be omitted.
18 // This cast is to suppress the compiler warning about
19 // unused parameters.
20 (void) argument_count;
21
22 GError *error = NULL;
23 JsonParser *parser;
24 gchar *filename;
25 gchar **codes;
26
27 // Set up I18N infrastructure
28 // @TODO: Use LOCALEDIR
29 bindtextdomain(GETTEXT_PACKAGE, "/usr/share/locale");
30 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
31 textdomain(GETTEXT_PACKAGE);
32 setlocale(LC_ALL, "");
33
34 // Parse command line and report possible errors
35 if (!options_parse_command_line(arguments, &error)) {
36 // TRANSLATORS: This is an error message.
37 g_printerr(_("isoquery: %s\n"), error->message);
38 if (error->domain == g_quark_from_string("g-option-context-error-quark")) {
39 g_printerr(_("Run \"isoquery --help\" to see a full list of available command line options.\n"));
40 }
41 g_error_free(error);
42 return EXIT_FAILURE;
43 }
44 // Show program version and copyright
45 if (option_version) {
46 options_show_version();
47 return EXIT_SUCCESS;
48 }
49 // Try opening and parsing the given file
50 parser = json_parser_new();
51 filename = options_get_filename();
52 if (!json_parser_load_from_file(parser, filename, &error)) {
53 // TRANSLATORS: This is an error message.
54 g_printerr(_("isoquery: %s\n"), error->message);
55 g_error_free(error);
56 g_free(filename);
57 g_object_unref(parser);
58 return EXIT_FAILURE;
59 }
60 // The file could be parsed, now see if there's
61 // valid iso-codes data in it.
62 if (!isocodes_validate(parser, &error)) {
63 // TRANSLATORS: This is an error message.
64 g_printerr(_("isoquery: %s\n"), error->message);
65 g_error_free(error);
66 g_free(filename);
67 g_object_unref(parser);
68 return EXIT_FAILURE;
69 }
70 // Remove the program name from the arguments and collect
71 // remaining arguments as codes to search for.
72 codes = &arguments[1];
73
74 // Finally, show the codes
75 isocodes_show_codes(parser, codes);
76
77 // Cleanup and exit
78 g_free(filename);
79 g_object_unref(parser);
80 return EXIT_SUCCESS;
81 }