commit 52c25ba37e4dfa1ca7bad9c25a419eb9364f1603
parent a00c467cb8d170ce49ffb9011ff79d8f817147c5
Author: Christian Grothoff <christian@grothoff.org>
Date: Mon, 11 Nov 2019 23:13:48 +0100
slight logic clean up
Diffstat:
| M | src/main.c | | | 124 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------- |
| M | taler.conf | | | 29 | +++++++++++++++++++++++++++++ |
2 files changed, 128 insertions(+), 25 deletions(-)
diff --git a/src/main.c b/src/main.c
@@ -19,7 +19,7 @@ along with
/**
* @file main.c
* @brief main functionality of the application
-* @author BOSS Marco
+* @author Boss Marco
* @author Christian Grothoff
*/
#include "config.h"
@@ -94,10 +94,12 @@ static const uint8_t get_data[] = { 0x00, 0xCA, 0x01, 0x00, 0x00, 0x00 };
struct Product
{
struct TALER_Amount price;
- const char *description;
+ char *description;
+ char *number;
char key;
};
+
struct PaymentActivity
{
@@ -143,28 +145,9 @@ static char *authorization;
static struct PaymentActivity *payment_activity;
+static struct Product *products;
-/**
- * FIXME: read from configuration file instead!
- * GNUNET_CONFIGURATION_* iteration over values.
- */
-static struct Product products[] = {
- {
- // .price = "0.1",
- .description = "Snickers",
- .key = 's'
- },
- {
- // .price = "0.1",
- .description = "Twix",
- .key = 't'
- },
- {
- // .price = NULL,
- .description = NULL,
- .key = '\0'
- }
-};
+static unsigned int products_length;
#if HAVE_QRENCODE_H
@@ -320,6 +303,17 @@ shutdown_task (void *cls)
GNUNET_CURL_gnunet_rc_destroy (rc);
rc = NULL;
}
+ if (NULL != products)
+ {
+ for (unsigned int i = 0; i < products_length; i++)
+ {
+ GNUNET_free (products[i].description);
+ GNUNET_free (products[i].number);
+ }
+ GNUNET_array_grow (products,
+ products_length,
+ 0);
+ }
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Shutdown complete\n");
}
@@ -758,7 +752,7 @@ read_keyboard_command (void *cls)
start_read_keyboard ();
return;
}
- for (unsigned int i = 0; NULL != products[i].description; i++)
+ for (unsigned int i = 0; i < products_length; i++)
if (((char) input) == products[i].key)
{
payment_activity = launch_payment (&products[i]);
@@ -778,7 +772,7 @@ start_read_keyboard ()
struct GNUNET_DISK_FileHandle fh = { STDIN_FILENO };
GNUNET_assert (NULL == keyboard_task);
- for (unsigned int i = 0; NULL != products[i].description; i++)
+ for (unsigned int i = 0; i < products_length; i++)
printf ("'%c' to buy %s\n",
products[i].key,
products[i].description);
@@ -792,6 +786,82 @@ start_read_keyboard ()
static void
+read_products (void *cls,
+ const char *section)
+{
+ struct Product tmpProduct;
+ char *tmpKey;
+
+ if (0 != strncmp (section,
+ "product-",
+ strlen ("product-")))
+ return;
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_string (cls,
+ section,
+ "description",
+ &tmpProduct.description))
+ {
+ GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
+ section,
+ "description");
+ return;
+ }
+ if (GNUNET_OK !=
+ TALER_config_get_denom (cls,
+ section,
+ "price",
+ &tmpProduct.price))
+ {
+ GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
+ section,
+ "price");
+ GNUNET_free (tmpProduct.description);
+ return;
+ }
+ if (0 != strcasecmp (currency,
+ tmpProduct.price.currency))
+ {
+ GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
+ section,
+ "price",
+ "currency missmatch");
+ GNUNET_free (tmpProduct.description);
+ return;
+ }
+ if (GNUNET_OK ==
+ GNUNET_CONFIGURATION_get_value_string (cls,
+ section,
+ "key",
+ &tmpKey))
+ {
+ tmpProduct.key = tmpKey[0];
+ GNUNET_free (tmpKey);
+ }
+ else
+ {
+ /* no key */
+ tmpProduct.key = '\0';
+ }
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_string (cls,
+ section,
+ "number",
+ &tmpProduct.number))
+ {
+ GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
+ section,
+ "number");
+ GNUNET_free (tmpProduct.description);
+ return;
+ }
+ GNUNET_array_append (products,
+ products_length,
+ tmpProduct);
+}
+
+
+static void
run (void *cls,
char *const *args,
const char *cfgfile,
@@ -869,6 +939,10 @@ run (void *cls,
global_ret = EXIT_FAILURE;
return;
}
+ GNUNET_CONFIGURATION_iterate_sections (cfg,
+ &read_products,
+ (void *) cfg);
+ GNUNET_assert (NULL != products);
GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
NULL);
diff --git a/taler.conf b/taler.conf
@@ -12,3 +12,32 @@ fulfillment-url = taler://fulfillment-success
# will be concatenated with product
# must be url - utf8 encoded
fulfillment-msg = /Enjoy+your+
+
+#Products
+#end declaration
+
+[product-3]
+description = KitKat
+price = TESTKUDOS:0.1
+key = k
+number = 4
+
+[product-2]
+description = Mars
+price = TESTKUDOS:0.1
+key = m
+number = 3
+#twix
+
+[product-1]
+description = Twix
+price = TESTKDUOS:0.1
+key = t
+number = 2
+#snickers
+
+[product-0]
+description = Snickers
+price = TESTKUDOS:0.1
+key = s
+number = 1