From cf8107eb2a93ec91d36cdb6efbb6195a77616b41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Valverde?= Date: Mon, 6 Feb 2023 22:50:32 +0000 Subject: Move ui/clopts_common.[ch] to wsutil --- wsutil/clopts_common.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 wsutil/clopts_common.c (limited to 'wsutil/clopts_common.c') diff --git a/wsutil/clopts_common.c b/wsutil/clopts_common.c new file mode 100644 index 0000000000..8145bfb8b5 --- /dev/null +++ b/wsutil/clopts_common.c @@ -0,0 +1,108 @@ +/* clopts_common.c + * Handle command-line arguments common to various programs + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "config.h" + +#include +#include + +#include +#include + +#include "clopts_common.h" + +int +get_natural_int(const char *string, const char *name) +{ + gint32 number; + + if (!ws_strtoi32(string, NULL, &number)) { + if (errno == EINVAL) { + cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string); + exit(1); + } + if (number < 0) { + cmdarg_err("The specified %s \"%s\" is a negative number", name, string); + exit(1); + } + cmdarg_err("The specified %s \"%s\" is too large (greater than %d)", + name, string, number); + exit(1); + } + if (number < 0) { + cmdarg_err("The specified %s \"%s\" is a negative number", name, string); + exit(1); + } + return (int)number; +} + +int +get_positive_int(const char *string, const char *name) +{ + int number; + + number = get_natural_int(string, name); + + if (number == 0) { + cmdarg_err("The specified %s is zero", name); + exit(1); + } + + return number; +} + +guint32 +get_guint32(const char *string, const char *name) +{ + guint32 number; + + if (!ws_strtou32(string, NULL, &number)) { + if (errno == EINVAL) { + cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string); + exit(1); + } + cmdarg_err("The specified %s \"%s\" is too large (greater than %d)", + name, string, number); + exit(1); + } + return number; +} + +guint32 +get_nonzero_guint32(const char *string, const char *name) +{ + guint32 number; + + number = get_guint32(string, name); + + if (number == 0) { + cmdarg_err("The specified %s is zero", name); + exit(1); + } + + return number; +} + +double +get_positive_double(const char *string, const char *name) +{ + double number = g_ascii_strtod(string, NULL); + + if (errno == EINVAL) { + cmdarg_err("The specified %s \"%s\" isn't a floating point number", name, string); + exit(1); + } + if (number < 0.0) { + cmdarg_err("The specified %s \"%s\" is a negative number", name, string); + exit(1); + } + + return number; +} -- cgit v1.2.3