aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-03-15 12:01:06 +0100
committerChristian Grothoff <christian@grothoff.org>2017-03-15 12:01:06 +0100
commitae3e850c946947cead6b9330f7c8e9764bd5f934 (patch)
treec031aeb264149454869f1ace7bf8db0a04d3002b /src/util
parent296f919ce4dcd9123c402d52d18afae7e353b11a (diff)
update getopt config parsing style to match 'getopt' GNUnet branch API change
Diffstat (limited to 'src/util')
-rw-r--r--src/util/amount.c21
-rw-r--r--src/util/util.c66
2 files changed, 76 insertions, 11 deletions
diff --git a/src/util/amount.c b/src/util/amount.c
index 7684ddf32..44eefe6a6 100644
--- a/src/util/amount.c
+++ b/src/util/amount.c
@@ -252,16 +252,15 @@ invalidate (struct TALER_Amount *a)
/**
- * Test if @a a is valid
+ * Test if the given amount is valid.
*
- * @param a amount to test
- * @return #GNUNET_YES if valid,
- * #GNUNET_NO if invalid
+ * @param amount amount to check
+ * @return #GNUNET_OK if @a amount is valid
*/
-static int
-test_valid (const struct TALER_Amount *a)
+int
+TALER_amount_is_valid (const struct TALER_Amount *amount)
{
- return ('\0' != a->currency[0]);
+ return ('\0' != amount->currency[0]);
}
@@ -292,8 +291,8 @@ int
TALER_amount_cmp_currency (const struct TALER_Amount *a1,
const struct TALER_Amount *a2)
{
- if ( (GNUNET_NO == test_valid (a1)) ||
- (GNUNET_NO == test_valid (a2)) )
+ if ( (GNUNET_NO == TALER_amount_is_valid (a1)) ||
+ (GNUNET_NO == TALER_amount_is_valid (a2)) )
return GNUNET_SYSERR;
if (0 == strcasecmp (a1->currency,
a2->currency))
@@ -499,7 +498,7 @@ TALER_amount_normalize (struct TALER_Amount *amount)
{
int ret;
- if (GNUNET_YES != test_valid (amount))
+ if (GNUNET_YES != TALER_amount_is_valid (amount))
return GNUNET_SYSERR;
ret = GNUNET_NO;
while ( (amount->value != UINT64_MAX) &&
@@ -535,7 +534,7 @@ TALER_amount_to_string (const struct TALER_Amount *amount)
unsigned int i;
struct TALER_Amount norm;
- if (GNUNET_YES != test_valid (amount))
+ if (GNUNET_YES != TALER_amount_is_valid (amount))
return NULL;
norm = *amount;
GNUNET_break (GNUNET_SYSERR !=
diff --git a/src/util/util.c b/src/util/util.c
index e01a2cc02..8976b0a3d 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -93,4 +93,70 @@ TALER_config_get_denom (const struct GNUNET_CONFIGURATION_Handle *cfg,
}
+
+
+/**
+ * Set an option with an amount from the command line. A pointer to
+ * this function should be passed as part of the 'struct
+ * GNUNET_GETOPT_CommandLineOption' array to initialize options of
+ * this type.
+ *
+ * @param ctx command line processing context
+ * @param scls additional closure (will point to the `struct TALER_Amount`)
+ * @param option name of the option
+ * @param value actual value of the option as a string.
+ * @return #GNUNET_OK if parsing the value worked
+ */
+static int
+set_amount (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
+ void *scls,
+ const char *option,
+ const char *value)
+{
+ struct TALER_Amount *amount = scls;
+
+ if (GNUNET_OK !=
+ TALER_string_to_amount (value,
+ amount))
+ {
+ FPRINTF (stderr,
+ _("Failed to parse amount in option `%s'\n"),
+ option);
+ return GNUNET_SYSERR;
+ }
+
+ return GNUNET_OK;
+}
+
+
+/**
+ * Allow user to specify an amount on the command line.
+ *
+ * @param shortName short name of the option
+ * @param name long name of the option
+ * @param argumentHelp help text for the option argument
+ * @param description long help text for the option
+ * @param[out] amount set to the amount specified at the command line
+ */
+struct GNUNET_GETOPT_CommandLineOption
+TALER_getopt_get_amount (char shortName,
+ const char *name,
+ const char *argumentHelp,
+ const char *description,
+ struct TALER_Amount *amount)
+{
+ struct GNUNET_GETOPT_CommandLineOption clo = {
+ .shortName = shortName,
+ .name = name,
+ .argumentHelp = argumentHelp,
+ .description = description,
+ .require_argument = 1,
+ .processor = &set_amount,
+ .scls = (void *) amount
+ };
+
+ return clo;
+}
+
+
/* end of util.c */