From 93ec25ad676b33e0cdf61649156d0b028f6f9aab Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Thu, 11 May 2006 18:37:15 +0000 Subject: [PATCH] add documentation for G_OPTION_ARG_INT64 2006-05-11 Bastien Nocera * glib/tmpl/option.sgml: add documentation for G_OPTION_ARG_INT64 2006-05-11 Bastien Nocera * glib/goption.c: (parse_int64), (parse_arg), (free_changes_list): * glib/goption.h: * tests/option-test.c: (arg_test6), (main): add an int64 type for GOption (G_OPTION_ARG_INT64) (#341237) --- ChangeLog | 7 ++++ ChangeLog.pre-2-12 | 7 ++++ docs/reference/ChangeLog | 4 ++ docs/reference/glib/tmpl/option.sgml | 4 ++ glib/goption.c | 56 ++++++++++++++++++++++++++++ glib/goption.h | 3 +- tests/option-test.c | 33 ++++++++++++++++ 7 files changed, 113 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index b5dd9501f..266644352 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2006-05-11 Bastien Nocera + + * glib/goption.c: (parse_int64), (parse_arg), (free_changes_list): + * glib/goption.h: + * tests/option-test.c: (arg_test6), (main): add an int64 type for + GOption (G_OPTION_ARG_INT64) (#341237) + 2006-05-10 Sebastian Wilhelmi * glib/gthread.h, gthread/gthread-impl.c: Make the magic and diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index b5dd9501f..266644352 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,10 @@ +2006-05-11 Bastien Nocera + + * glib/goption.c: (parse_int64), (parse_arg), (free_changes_list): + * glib/goption.h: + * tests/option-test.c: (arg_test6), (main): add an int64 type for + GOption (G_OPTION_ARG_INT64) (#341237) + 2006-05-10 Sebastian Wilhelmi * glib/gthread.h, gthread/gthread-impl.c: Make the magic and diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index 711145cdd..010a24dfc 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,7 @@ +2006-05-11 Bastien Nocera + + * glib/tmpl/option.sgml: add documentation for G_OPTION_ARG_INT64 + 2006-05-10 Matthias Clasen * gobject/tmpl/gtype.sgml: Updates diff --git a/docs/reference/glib/tmpl/option.sgml b/docs/reference/glib/tmpl/option.sgml index 7e317f70f..c1060799e 100644 --- a/docs/reference/glib/tmpl/option.sgml +++ b/docs/reference/glib/tmpl/option.sgml @@ -259,6 +259,10 @@ or combined in a single argument: . multiple uses of the option are collected into an array of strings. @G_OPTION_ARG_DOUBLE: The option takes a double argument. The argument can be formatted either for the user's locale or for the "C" locale. Since 2.12 +@G_OPTION_ARG_INT64: The option takes a 64-bit integer. Like %G_OPTION_ARG_INT + but for larger numbers. The number can be in decimal base, or in hexadecimal + (when prefixed with 0x, for example, 0xffffffff). + Since 2.12 diff --git a/glib/goption.c b/glib/goption.c index a5026ec2a..10d22b3da 100644 --- a/glib/goption.c +++ b/glib/goption.c @@ -51,6 +51,7 @@ typedef struct gchar *str; gchar **array; gdouble dbl; + gint64 int64; } prev; union { @@ -716,6 +717,41 @@ parse_double (const gchar *arg_name, } +static gboolean +parse_int64 (const gchar *arg_name, + const gchar *arg, + gint64 *result, + GError **error) +{ + gchar *end; + gint64 tmp; + + errno = 0; + tmp = strtoll (arg, &end, 0); + + if (*arg == '\0' || *end != '\0') + { + g_set_error (error, + G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, + _("Cannot parse integer value '%s' for %s"), + arg, arg_name); + return FALSE; + } + if (errno == ERANGE) + { + g_set_error (error, + G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, + _("Integer value '%s' for %s out of range"), + arg, arg_name); + return FALSE; + } + + *result = tmp; + + return TRUE; +} + + static Change * get_change (GOptionContext *context, GOptionArg arg_type, @@ -953,6 +989,23 @@ parse_arg (GOptionContext *context, *(gdouble *)entry->arg_data = data; break; } + case G_OPTION_ARG_INT64: + { + gint64 data; + + if (!parse_int64 (option_name, value, + &data, + error)) + { + return FALSE; + } + + change = get_change (context, G_OPTION_ARG_INT64, + entry->arg_data); + change->prev.int64 = *(gint64 *)entry->arg_data; + *(gint64 *)entry->arg_data = data; + break; + } default: g_assert_not_reached (); } @@ -1220,6 +1273,9 @@ free_changes_list (GOptionContext *context, case G_OPTION_ARG_DOUBLE: *(gdouble *)change->arg_data = change->prev.dbl; break; + case G_OPTION_ARG_INT64: + *(gint64 *)change->arg_data = change->prev.int64; + break; default: g_assert_not_reached (); } diff --git a/glib/goption.h b/glib/goption.h index 6a5bba224..478ed0d4c 100644 --- a/glib/goption.h +++ b/glib/goption.h @@ -50,7 +50,8 @@ typedef enum G_OPTION_ARG_FILENAME, G_OPTION_ARG_STRING_ARRAY, G_OPTION_ARG_FILENAME_ARRAY, - G_OPTION_ARG_DOUBLE + G_OPTION_ARG_DOUBLE, + G_OPTION_ARG_INT64 } GOptionArg; typedef gboolean (*GOptionArgFunc) (const gchar *option_name, diff --git a/tests/option-test.c b/tests/option-test.c index 48dd2531f..65e1f35e5 100644 --- a/tests/option-test.c +++ b/tests/option-test.c @@ -12,6 +12,8 @@ gchar *arg_test2_string; gchar *arg_test3_filename; gdouble arg_test4_double; gdouble arg_test5_double; +gint64 arg_test6_int64; +gint64 arg_test6_int64_2; gchar *callback_test1_string; int callback_test2_int; @@ -407,6 +409,36 @@ arg_test5 (void) g_option_context_free (context); } +void +arg_test6 (void) +{ + GOptionContext *context; + gboolean retval; + GError *error = NULL; + gchar **argv; + int argc; + GOptionEntry entries [] = + { { "test", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64, NULL, NULL }, + { "test2", 0, 0, G_OPTION_ARG_INT64, &arg_test6_int64_2, NULL, NULL }, + { NULL } }; + + context = g_option_context_new (NULL); + g_option_context_add_main_entries (context, entries, NULL); + + /* Now try parsing */ + argv = split_string ("program --test 4294967297 --test 4294967296 --test2 0xfffffffff", &argc); + + retval = g_option_context_parse (context, &argc, &argv, &error); + g_assert (retval); + + /* Last arg specified is the one that should be stored */ + g_assert (arg_test6_int64 == 4294967296LL); + g_assert (arg_test6_int64_2 == 0xfffffffffLL); + + g_strfreev (argv); + g_option_context_free (context); +} + static gboolean callback_parse1 (const gchar *option_name, const gchar *value, gpointer data, GError **error) @@ -1370,6 +1402,7 @@ main (int argc, char **argv) arg_test3 (); arg_test4 (); arg_test5 (); + arg_test6 (); /* Test string arrays */ array_test1 ();