Commit 93ec25ad authored by Bastien Nocera's avatar Bastien Nocera Committed by Bastien Nocera
Browse files

add documentation for G_OPTION_ARG_INT64

2006-05-11  Bastien Nocera  <hadess@hadess.net>

	* glib/tmpl/option.sgml: add documentation for G_OPTION_ARG_INT64

2006-05-11  Bastien Nocera  <hadess@hadess.net>

	* 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)
parent 268084ca
2006-05-11 Bastien Nocera <hadess@hadess.net>
* 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 <wilhelmi@google.com>
* glib/gthread.h, gthread/gthread-impl.c: Make the magic and
......
2006-05-11 Bastien Nocera <hadess@hadess.net>
* 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 <wilhelmi@google.com>
* glib/gthread.h, gthread/gthread-impl.c: Make the magic and
......
2006-05-11 Bastien Nocera <hadess@hadess.net>
* glib/tmpl/option.sgml: add documentation for G_OPTION_ARG_INT64
2006-05-10 Matthias Clasen <mclasen@redhat.com>
* gobject/tmpl/gtype.sgml: Updates
......
......@@ -259,6 +259,10 @@ or combined in a single argument: <option>--name=arg</option>.
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 <literal>0x</literal>, for example, <literal>0xffffffff</literal>).
Since 2.12
<!-- ##### ENUM GOptionFlags ##### -->
<para>
......
......@@ -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 ();
}
......
......@@ -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,
......
......@@ -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 ();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment