| 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 "options.h" |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
gchar *option_standard; |
| 17 |
gchar *option_pathname; |
| 18 |
gchar *option_namefield; |
| 19 |
gchar *option_locale; |
| 20 |
gboolean option_null_separator; |
| 21 |
gboolean option_flag_output; |
| 22 |
gboolean option_version; |
| 23 |
|
| 24 |
|
| 25 |
gboolean option_name; |
| 26 |
gboolean option_officialname; |
| 27 |
gboolean option_commonname; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
static GOptionEntry entries[] = { |
| 33 |
{ "iso", 'i', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &option_standard, |
| 34 |
N_ |
| 35 |
("The ISO standard to use. Possible values: 639-2, 639-3, 639-5, 3166-1, 3166-2, 3166-3, 4217, 15924 (default: 3166-1)"), |
| 36 |
N_("STANDARD") }, |
| 37 |
{ "pathname", 'p', G_OPTION_FLAG_NONE, G_OPTION_ARG_FILENAME, &option_pathname, |
| 38 |
N_("Use pathname as prefix for the data files (default: /usr/share/iso-codes/json)"), |
| 39 |
N_("PATHNAME") }, |
| 40 |
{ "locale", 'l', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &option_locale, |
| 41 |
N_("Use this locale for output"), |
| 42 |
N_("LOCALE") }, |
| 43 |
{ "name", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &option_name, |
| 44 |
N_("Name for the supplied codes (default)"), NULL }, |
| 45 |
{ "official_name", 'o', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &option_officialname, |
| 46 |
N_("Official name for the supplied codes. This may be the same as --name (only applies to ISO 3166-1)"), |
| 47 |
NULL }, |
| 48 |
{ "common_name", 'c', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &option_commonname, |
| 49 |
N_ |
| 50 |
("Common name for the supplied codes. This may be the same as --name (only applies to ISO 639-2, 639-3, and 3166-1)"), |
| 51 |
NULL }, |
| 52 |
{ "null", '0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &option_null_separator, |
| 53 |
N_("Separate entries with a NULL character instead of newline"), NULL }, |
| 54 |
{ "flag", 'f', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &option_flag_output, |
| 55 |
N_("Show country flags with regional indicator symbol letters (only applies to ISO 3166-1)"), NULL }, |
| 56 |
{ "version", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &option_version, |
| 57 |
N_("Show program version and copyright"), NULL }, |
| 58 |
{ NULL } |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
gboolean options_parse_command_line(gchar **arguments, GError **error) |
| 67 |
{ |
| 68 |
int argc = 0; |
| 69 |
GOptionContext *context; |
| 70 |
|
| 71 |
|
| 72 |
options_set_default_values(); |
| 73 |
|
| 74 |
context = g_option_context_new(_("[ISO codes]")); |
| 75 |
g_option_context_add_main_entries(context, entries, GETTEXT_PACKAGE); |
| 76 |
|
| 77 |
|
| 78 |
while (arguments[argc] != NULL) { |
| 79 |
argc++; |
| 80 |
} |
| 81 |
|
| 82 |
if (!g_option_context_parse(context, &argc, &arguments, error)) { |
| 83 |
return FALSE; |
| 84 |
} |
| 85 |
|
| 86 |
if (!options_validate(error)) { |
| 87 |
return FALSE; |
| 88 |
} |
| 89 |
return TRUE; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
void options_set_default_values(void) |
| 96 |
{ |
| 97 |
option_standard = "3166-1"; |
| 98 |
option_pathname = "/usr/share/iso-codes/json"; |
| 99 |
option_namefield = "name"; |
| 100 |
option_locale = ""; |
| 101 |
option_null_separator = FALSE; |
| 102 |
option_flag_output = FALSE; |
| 103 |
option_version = FALSE; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
gboolean options_validate(GError **error) |
| 110 |
{ |
| 111 |
|
| 112 |
if (!g_strcmp0("3166", option_standard)) { |
| 113 |
|
| 114 |
g_free(option_standard); |
| 115 |
option_standard = g_strdup("3166-1"); |
| 116 |
|
| 117 |
g_printerr("isoquery: The standard 3166 is deprecated, please use 3166-1 instead.\n"); |
| 118 |
} |
| 119 |
if (!g_strcmp0("639", option_standard)) { |
| 120 |
|
| 121 |
g_free(option_standard); |
| 122 |
option_standard = g_strdup("639-2"); |
| 123 |
|
| 124 |
g_printerr("isoquery: The standard 639 is deprecated, please use 639-2 instead.\n"); |
| 125 |
} |
| 126 |
|
| 127 |
int i = 0; |
| 128 |
gboolean supported = FALSE; |
| 129 |
gchar *supported_standards[] = { "639-2", "639-3", "639-5", "3166-1", "3166-2", "3166-3", "4217", "15924", NULL }; |
| 130 |
while (supported_standards[i]) { |
| 131 |
if (!g_strcmp0(supported_standards[i], option_standard)) { |
| 132 |
supported = TRUE; |
| 133 |
break; |
| 134 |
} |
| 135 |
i++; |
| 136 |
} |
| 137 |
if (!supported) { |
| 138 |
|
| 139 |
g_set_error(error, g_quark_from_string(GETTEXT_PACKAGE), 0, _("ISO standard \"%s\" is not supported."), |
| 140 |
option_standard); |
| 141 |
return FALSE; |
| 142 |
} |
| 143 |
|
| 144 |
if (option_officialname) { |
| 145 |
option_namefield = "official_name"; |
| 146 |
} |
| 147 |
if (option_commonname) { |
| 148 |
option_namefield = "common_name"; |
| 149 |
} |
| 150 |
return TRUE; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
gchar *options_get_filename(void) |
| 159 |
{ |
| 160 |
gchar *filename, *complete_path; |
| 161 |
filename = g_strdup_printf("iso_%s.json", option_standard); |
| 162 |
complete_path = g_build_filename(option_pathname, filename, NULL); |
| 163 |
g_free(filename); |
| 164 |
return complete_path; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
void options_show_version(void) |
| 171 |
{ |
| 172 |
|
| 173 |
g_printf(_("isoquery %s\n"), VERSION); |
| 174 |
g_printf(_("Copyright © 2007-2026 Dr. Tobias Quathamer\n")); |
| 175 |
|
| 176 |
|
| 177 |
gchar *translation = _("Translation to LANGUAGE Copyright © YEAR YOUR-NAME\n"); |
| 178 |
if (!g_strrstr(translation, "LANGUAGE")) { |
| 179 |
g_printf("%s", translation); |
| 180 |
} |
| 181 |
g_print("\n"); |
| 182 |
g_print("This program is free software: you can redistribute it and/or modify\n"); |
| 183 |
g_print("it under the terms of the GNU General Public License as published by\n"); |
| 184 |
g_print("the Free Software Foundation, either version 3 of the License, or\n"); |
| 185 |
g_print("(at your option) any later version.\n"); |
| 186 |
g_print("\n"); |
| 187 |
g_print("This program is distributed in the hope that it will be useful,\n"); |
| 188 |
g_print("but WITHOUT ANY WARRANTY; without even the implied warranty of\n"); |
| 189 |
g_print("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"); |
| 190 |
g_print("GNU General Public License for more details.\n"); |
| 191 |
g_print("\n"); |
| 192 |
g_print("You should have received a copy of the GNU General Public License\n"); |
| 193 |
g_print("along with this program. If not, see <http://www.gnu.org/licenses/>.\n"); |
| 194 |
} |