From 074344fbf9b380b9dab3692832ad25e5d7c589f3 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Thu, 1 Oct 2015 15:16:12 +0200 Subject: [PATCH] Multi level json pointer lookups + example --- spec/example/synchronous-json-pointer.c | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 spec/example/synchronous-json-pointer.c diff --git a/spec/example/synchronous-json-pointer.c b/spec/example/synchronous-json-pointer.c new file mode 100644 index 00000000..9f0f9465 --- /dev/null +++ b/spec/example/synchronous-json-pointer.c @@ -0,0 +1,53 @@ +#include +#include + +int main() +{ + getdns_return_t r; + getdns_context *context = NULL; + getdns_dict *response = NULL; + getdns_bindata *address_data; + char *first = NULL, *second = NULL; + + /* Create the DNS context for this call */ + if ((r = getdns_context_create(&context, 1))) + fprintf(stderr, "Trying to create the context failed"); + + else if ((r = getdns_address_sync(context, "example.com", NULL, &response))) + fprintf(stderr, "Error scheduling synchronous request"); + + else if ((r = getdns_dict_get_bindata(response, "/just_address_answers/0/address_data", &address_data))) + fprintf(stderr, "Could not get first address"); + + else if (!(first = getdns_display_ip_address(address_data))) + fprintf(stderr, "Could not convert first address to string\n"); + + else if ((r = getdns_dict_get_bindata(response, "/just_address_answers/1/address_data", &address_data))) + fprintf(stderr, "Could not get second address"); + + else if (!(second = getdns_display_ip_address(address_data))) + fprintf(stderr, "Could not convert second address to string\n"); + + if (first) { + printf("The address is %s\n", first); + free(first); + } + if (second) { + printf("The address is %s\n", second); + free(second); + } + /* Clean up */ + if (response) + getdns_dict_destroy(response); + + if (context) + getdns_context_destroy(context); + + /* Assuming we get here, leave gracefully */ + if (r) { + fprintf(stderr, ": %d\n", r); + exit(EXIT_FAILURE); + } + else + exit(EXIT_SUCCESS); +}